aa0660 发表于 2019-1-16 09:04:47

半小时玩转nagios

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
创建时间:2011-12-16
email: old_hoodlum@163.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nagios用了很久了,虽然也研究过另一款锐利的监控工具zabbix,但是感觉不如nagios顺手,翻过头来写一篇安装及使用文档分享一下

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
一、准备工作:

1、更换Yum源163
cd /etc/yum.repos.d/
mv CentOS-Base.repo{,.bak}
wget http://mirrors.163.com/.help/CentOS-Base-163.repo
yum makecache

2、LAMP环境优化编译安装,都加了简单优化选项,可以直接当LAMP环境的网站使用
apache安装
cd /usr/local/src
wget http://mirrors.sohu.com/apache/httpd-2.2.21.tar.bz2
yum -y install openssl openssl-devel
tar jxvf httpd-2.2.21.tar.bz2
cd httpd-2.2.21
./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --enable-rewrite --enable-deflate --enable-expires
make && make install
cd ..


为了方便以后安装其它需要sql的监控服务,装上mysql
wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.57.tar.gz
groupadd mysql
useradd mysql -g mysql -s /sbin/nologin
tar zxvf mysql-5.1.57.tar.gz
yum -y install ncurses ncurses-devel
./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --sysconfdir=/etc --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --with-unix-socket-path=/tmp/mysql.sock --enable-assembler --without-debug--with-plugins=utf8,gbk --with-plugins=innobase
make && make install
cp my-small.cnf /etc/my.cnf
cp mysql.server /etc/init.d/mysql.server
chmod 755 /etc/init.d/mysql.server

vi /etc/init.d/mysql.server
   46行 basedir=/usr/local/mysql
   47行 datadir=/usr/local/mysql/var

cd /usr/local/mysql/
chown -R mysql.mysql .
bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var
chown -R root .
chown -R mysql /usr/local/mysql/var
chkconfig --add mysql.server
chkconfig mysql.server on
/etc/init.d/mysql.server start
echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source /etc/profile

nagios3以后版本需要php的支持
PHP的安装
wget http://mirrors.sohu.com/php/php-5.3.6.tar.bz2
因为需要有图片产生,所以先需要php的gd库支持
wget ftp://217.146.241.3/pub/linux/lib/gd-2.0.33.tar.gz
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel libXpm libXpm-devel
tar zxvf gd-2.0.33.tar.gz
cd gd-2.0.33
./configure --prefix=/usr/local/gd2 --with-png --with-freetype --with-jpeg
make && make install
编译php
tar jxvf php-5.3.6.tar.bz2
cd php-5.3.6
./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --enable-mbstring=cn--enable-force-cgi-redirect --enable-ftp -with-gd -with-jpeg -with-zlib -with-png -with-freetype --disable-debug --enable-inline-optimization--enable-sockets --enable-bcmath
make && make install
增加php与apache的关联
vi /usr/local/apache2/conf/httpd.conf
确认有这样一行LoadModule php5_module      modules/libphp5.so添加:AddType application/x-httpd-php .php
修改apache的运行用户和组为nagios
phpinfo测试下是否成功


nagios的安装
wget http://cdnetworks-kr-2.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.2.3/nagios-3.2.3.tar.gz
wget http://cdnetworks-kr-2.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.15/nagios-plugins-1.4.15.tar.gz
wget http://cdnetworks-kr-1.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.12/nrpe-2.12.tar.gz
groupadd nagios
useradd nagios -g nagios
tar zxvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --prefix=/usr/local/nagios
mkdir -p /etc/httpd/conf.d/
make all   //编译
make install    //安装nagios主要程序,cgi和html文件等
make install-init   //安装nagios启动脚本
make install-config//将配置文件的例子复制到nagios配置文件里
make install-commandmode//配置nagios目录权限。
make install-webconf 因为这个命令执行的时候,会在/etc/httpd/conf.d/创建   nagios.conf文件
cat /etc/httpd/conf.d/nagios.conf >>/usr/local/apache2/conf/httpd.conf

安装nagios插件
tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/usr/local/nagios/
make && make install
安装完成后,会在/usr/local/nagios/ 目录下面生成libexe插件目录,nagios所有的插件都会在这个目录里面。
chown -R nagios.nagios /usr/local/nagios/

安装nrpe
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure && make all
make install-plugin //安装check_nrpe插件
make install-daemon
make install-daemon-config
make install-xinetd //安装xinetd脚本
yum -y install xinetd
vi /etc/xinetd.d/nrpe
only_from       = 127.0.0.1 注释掉   allowshosts =192.168.1.221添加监控主机,一般为nagios服务器的Ip
vi /etc/services
添加两行
nrpe            5666/tcp
nrpe            5666/udp
启动xinetd
/etc/init.d/xinetd start 看看5666端口是否开启
添加apache授权用户 -c选项为创建文件,创建第二个选项不需要
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users itv1
修改cgi.cfg    #vi /usr/local/nagios/etc/cgi.cfg   
找到这几行,后面添加 erbin用户,可以自己定义用户,使用“,”隔开
authorized_for_system_information=nagiosadmin,itv1
authorized_for_configuration_information=nagiosadmin,itv1
authorized_for_system_commands=nagiosadmin,itv1
authorized_for_all_services=nagiosadmin,itv1
authorized_for_all_hosts=nagiosadmin,itv1
authorized_for_all_service_commands=nagiosadmin,itv1
authorized_for_all_host_commands=nagiosadmin,itv1

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg测试下,如果没有eror就可以启动了,
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg启动就行了
重新启动下apache,然后通过http://IP/nagios访问,输入用户名密码如果成功登陆,到此安装完成。

三、使用
现在跟着我的步骤走,不要怀疑,坚决执行吧哈哈
cd /usr/local/nagios/etc/objects/
ls看一下,看下就可以哈,然后
rm -f contacts.cfg printer.cfg switch.cfg templates.cfg windows.cfg
###如果你是头次安装,建议看下这些文件内容
contacts.cfg ------默认联系人配置文件
printer.cfg------默认监控打印机的配置文件
switch.cfg   ------默认监控交换机的配置问及爱你
templates.cfg------默认的模板文件
windows.cfg------默认监控windows的配置文件
commands.cfg ------命令的配置文件
timeperiods.cfg----时间配置文件

进入正题:
首先介绍下nagios的几个重要的项,就这么叫吧,哈哈
host(主机)------hostgroup(主机组)
service(服务)------servicegroup(服务组)
contact(联系人)------contactgroup(联系人组)
address(监控主机的ip)
service_description(定义检查服务的名称,在nagios页面上显示的)
check_command(检查命令)

好了,了解这些足够了,来一点点建立你自己的nagios
1、建立个模板文件,名字随意.cfg,就叫moban.cfg吧通俗易懂
      1 define contact{
      2 name                            generic-contact---模板名称
      3 #联系组名称
      4 service_notification_period   24x7---和下面一项是在timeperiods.cfg定义的,自己看下就明白
      5 #服务报警周期
      6 host_notification_period      24x7
      7 #主机报警周期
      8 service_notification_options    w,u,c,r,s
      9 #服务通知选项
   10 host_notification_options       d,u,r,s
   11 #主机通知选项
   12 service_notification_commands   notify-service-by-sendEmail----command.cfg中定义,下面说
   13 #服务通知方式
   14 host_notification_commands      notify-host-by-sendEmail
   15 #主机通知方式
   16 register                        0
   17               }
   18
   19 define host{
   20 name                            generic-host----足迹模板名称
   21 #服务器名称
   22 notifications_enabled         1
   23 #开启主机通知
   24 event_handler_enabled         1
   25 #开启时间处理
   26 flap_detection_enabled          1
   27 #启用状态抖动检测
   28 failure_prediction_enabled      1
   29 #启用故障预测
   30 process_perf_data               1
   31 #启用进程性能数据记录
   32 retain_status_information       1
   33 #启用状态信息保存功能
   34 retain_nonstatus_information    1
   35 #启用非状态信息保存功能
   36 notification_period             24x7
   37 #报警的时间周期 timeperiods.cfg配置
   38 register                        0
   39 check_interval                  1
   40 #正常检测的时间间隔(单位:分)
   41 retry_interval                  2
   42 #重试检测间隔时间
   43 max_check_attempts            3
   44 #最大尝试次数
   45 check_command                   check-host-alive----在command.cfg中定义的
   46 #检查主机存货(command.cfg中定义)
   47 notification_period             24x7
   48 #监控周期
   49 notification_interval         120
   50 #同一问题的报警间隔
   51 notification_options            d,u,r
   52 #主机通知报警选项 d--down, u--unknown, r--recover
   53 contact_groups                  SMC----自己定义的联系人组,下面定义
   54         }
   55
   56 define service{
   57 name                            generic-service
   58 active_checks_enabled         1
   59 passive_checks_enabled          1
   60 parallelize_check               1
   61 obsess_over_service             1
   62 check_freshness               0
   63 notifications_enabled         1
   64 event_handler_enabled         1
   65 flap_detection_enabled          1
   66 failure_prediction_enabled      1
   67 process_perf_data               1
   68 retain_status_information       1
   69 retain_nonstatus_information    1
   70 is_volatile                     0
   71 check_period                  24x7
   72 max_check_attempts            3
   73 normal_check_interval         2
   74 retry_check_interval            1
   75 notification_options            w,u,c,r
   76 notification_interval         60
   77 notification_period             24x7
   78 register                        0
   79 contact_groups                  SMC
   80               }


2、建立个联系人文件,还叫contact.cfg吧
define contact{
      contact_name                  duty1---联系人名称
      use                           generic-contact---关联generic-contact,在上面定义了
      alias                           duty1---别名
      email                           138xxxxxxxx7@139.com---邮件地址
      }

define contact{
      contact_name                  duty2
      use                           generic-contact
      alias                           duty2
      email                           138xxxxxxxx7@139.com
      }
define contact{
      contact_name                  duty3
      use                           generic-contact
      alias                           duty1
      email                           138xxxxxxxx7@139.com
      }

define contact{
      contact_name                  duty4
      use                           generic-contact
      alias                           duty2
      email                           138xxxxxxxx7@139.com
      }


define contactgroup {---定义联系组
      contactgroup_name       SMC---组名
      alias                   SMC---组别名
      members               duty1,duty2---成员,必须为已经定义的联系人
      }

define contactgroup {
      contactgroup_name       SMC2
      alias                   SMC2
      members               duty3,duty4
      }

两个小问题解释下:
1、use   generic-contact的作用,关联此模板的所有已定义内容到此define contact中,省去很多笔墨
以duty2为例,完整的就是
define contact{
      contact_name                  duty2
      use                           generic-contact
      alias                           duty2
      email                           138xxxxxxxx7@139.com
service_notification_period   24x7
      host_notification_period      24x7
      service_notification_options    w,u,c,r,s
      host_notification_options       d,u,r,s
service_notification_commands   notify-service-by-sendEmail
host_notification_commands      notify-host-by-sendEmail
register                        0
      }
还一点,定义单个{}与模板中相同参数的优先性 :自定义参数 > 模板中相同参数,也就是说模板的参数是缺省值
2、报警方式(139短信)
现在支持的报警方式很多,后面会详细说,这里就介绍下报警的过程。
nagios产生监控状态硬性变化,调用邮件功能,将报警信息发送至mail.139.com,监控人员开通139短信通知,会自动收到139发来的短信

3、command.cfg文件
添加nrpe插件的命令
# 'check_nrpe' command definition
define command{
      command_name    check_nrpe
      command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
      }

介绍下我的报警发邮件sendEmail,一个perl写的命令行发邮件工具。在我的系统应用了一年多,非常出色
下载
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
解开后,将可执行文件sendEmail复制到/usr/local/bin/目录并且修改为nagios可执行
发个邮件测试一下
/usr/local/bin/sendEmail -f old_hoodlum@163.com -t 138xxxxxxx -s smtp.163.com -u"test" -m "hello" -xu old_hoodlum -xp password -l /var/log/sendEmail.log
-f   --from
-t   --to
-s   使用的smtp域名
-u   标题
-m   内容
-xusmtp登陆用户名
-xpsmtp登陆密码

建议,nagios报警尽量不要使用本机sendmail,在大网站注册个免费邮箱发去吧,也减少了很多麻烦,比如sendmail问题导致的报警不能及时到达
以下为command中定义的发邮件命令,根据自己的实际情况修改
command.cfg中加入
define command{
      command_name    notify-host-by-sendEmail
      command_line    /usr/bin/printf "%b" "***** Nagios-BJ *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail -f old_hoodlum@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -xu old_hoodlum -xp xxxxxxxx -l /var/log/sendEmail.log
      }

define command{
      command_name    notify-service-by-sendEmail
      command_line    /usr/bin/printf "%b" "***** Nagios-BJ *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTNAME$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/local/bin/sendEmail -f old_hoodlum@163.com -t $CONTACTEMAIL$ -s smtp.163.com -u"** $NOTIFICATIONTYPE$ Service Alert: $HOSTNAME$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu old_hoodlum -xp xxxxxxxx -l /var/log/sendEmail.log
      }

4、现在万事具备了,说最重点的主机、服务的监控策略
假如,公司内网n个网段,192.168.x.0/24(x=1-8),每个x运行不同的业务,各有不同的系统,需要监控不同的服务,怎么规划呢?
如果只写一个host.cfg和一个service.cfg的话,维护起来是非常困难的,我们可以这样
建立1-x.cfg.....8-x.cfg,添加每个网段的主机,根据不同的业务及不同系统对主机分组,也可根据需要监控的内容将类似的主机分组,总而言之,怎么方便你少写字你就怎么来
这就是开源的魅力,随心随意不必拘泥于形式,你可以给nagios定义100个配置文件,只要这么做方便你管理就好,写完了配置文件,只要在nagios.cfg文件加入你这个文件名就可以了,cfg_file=/usr/local/nagios/etc/objects/x.cfg
定义service(service_group),一样的道理,监控同个服务的组就好了,这样做会省去你很多的时间,举个例子吧
##定义主机
define host{
      use                     generic-host
      host_name               BJ-OUTPUT-sanlihe
      alias                   BJ-OUTPUT-sanlihe
      address               120.72.48.70
         }

define host{
      use                     generic-host
      host_name               BJ-GATEWAY-sanlihe
      alias                   BJ-GATEWAY-sanlihe
      address               120.72.48.1
         }

define host{
      use                     generic-host
      host_name               NA-GATEWAY
      alias                   NA-GATEWAY
      address               218.30.50.1
         }
#定义单个主机服务
define service {
      use                           generic-service
      host_name                     NA-GATEWAY
      max_check_attempts            3
      normal_check_interval         1
      retry_check_interval            2
      contact_groups                  SMC
      service_description             PING
      check_command                   check_ping!850.0,90%!1600.0,95%
      }

define host{
      use                     generic-host
      host_name               GW-dianxintong
      alias                   GW-dianxintong
      address               218.247.13.60
         }

define host{
      use                     generic-host
      host_name               BJ-dianxintong
      alias                   BJ-dianxintong
      address               218.247.13.62
         }
#定义主机组
define hostgroup{
      hostgroup_name          itv
      alias                   itv
      members               BJ-OUTPUT-sanlihe,BJ-GATEWAY-sanlihe,GW-dianxintong,BJ-dianxintong
      }
#定义主机组服务
define service{
      use                           generic-service
      hostgroup_name                  itv
      max_check_attempts            3
      normal_check_interval         1
      retry_check_interval            2
      contact_groups                  SMC
      service_description             PING
      check_command                   check_ping!500.0,90%!1000.0,95%
      }


假如想用多种方式同时报警怎么办?
在moban.cfg的service_notification_commands 和host....中添加多种报警方式就好了,中间用“,”隔开

想让每个人在不同时间以不同方式收到报警?
联系人中定义不同报警方式和报警时间就行了

想让QQ给你报警?
没戏,哈哈,发到QQ邮箱,如果QQ在线会自动弹出来的

nagios有多少种报警方式?
我也不知道有多少种,我用过的有短信猫、email、飞信、qq、msn....

都想用?
当然可以,escalations会帮你完成你的需求

想让nagios帮你处理?
可以,event_hander会做到,但是强烈建议在你不是非常精通你所要操作的服务时,请不要使用

怎么能让nagios检测到硬性变化后立刻报警?
报警时间跟这几个参数有关,一看就能明白
1、interval_length在nagios.cfg文件731行左右,默认为60,单位s
2、check_interval
3、retry_interval
4、max_check_attempts

nagios分布式??
假如你有100个nagios server,你想看看总况,总不能打开100个web页面吧?nagios分布式玩玩可以,个人建议不用

我想监控...
用我们经理的一句话:“你能想到它就能做到”,知道nagios监控插件的0、1、2就行了,http://exchange.nagios.org/ 插件最多的地方

个人一些建议:
(1)、nagios服务器强烈建议放在最安全的地方,在战争年代,它是一个哨兵,也是你的最后一道防线,如果监控挂了,服务器再挂了,你如何知道?
(2)、报警方式,个人意见:短信猫>email(socket方式)>msn || qq || fetion,以稳为主
(3)、每天定点在nagios server上给自己发个消息是个很好的习惯
(4)、对于服务的检查次数,设置为3-4此比较合理,如果太短你可能会经历一次狼来了的故事,太长的话,你可能都不会知道问题的存在
(5)、报警方式,如果有条件最好不要选择单种,自己设置好策略
(6)、nagios本机的系统信息也很重要,可能一些时候的误报是因为主机系统资源紧张造成的,比如ping
(7)、尽量不去监控无所谓的程序或者应用,尽量不去使用snmp收集数据
(8)、总之、不要把nagios只当个辅助来对待,它是运维的尖锐利



页: [1]
查看完整版本: 半小时玩转nagios