lukyjyt 发表于 2016-8-29 09:58:39

自定义nagios插件实现主动被动模式以及nagios基于mail的简单...

nagios插件程序提供两个返回值:一个是插件的退出状态码,另一个是插件在控制台上打印的第一行数据。退出状态码可以被nagios主程序
作为判断被监控系统服务状态的依据,控制台打印的第一行数据可以被nagios主程序作为被监控系统服务状态的补充说明
会显示在管理页面里面。
        为了管理nagios插件,nagios每查询一个服务的状态时,就会产生一个子进程,并且它使用来自该命令的输出和退出状态码来
确定具体的状态。nagios主程序可识别的状态码和说明如下:
OK            退出代码 0--表示服务正常的工作
warning       退出代码 1--表示服务处于告警状态
critical      退出代码 2--表示服务处于紧急,严重状态
unknown       退出代码 3--表示服务处于未知状态

# head -7 /usr/local/nagios/libexec/utils.sh
#! /bin/sh
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

示例一:判断/etc/passwd文件是否变化,利用nrpe的被动模式
原理:利用md5sum进行指纹收集 md5sum /etc/passwd > /etc/passwd.md5
利用md5sum -c /etc/passwd.md5对指纹进行判别,出现OK则没有变化,反之则变化了
监控密码文件是否被更改:
先做指纹库
md5sum /etc/passwd > /etc/passwd.md5
在client上创建脚本vim /usr/local/nagios/libexec/check_passwd
#!/bin/bash
char=`md5sum -c /etc/passwd.md5 2>&1 |grep "OK"|wc -l`

if [ $char -eq 1 ];then
echo "passwd is OK"
exit 0      
else
echo "passwd is changed"
exit 2
fi

######给脚本执行权限
chmod +x /usr/local/nagios/libexec/check_passwd

#####定义check_passwd命令
vim /usr/local/nagios/etc/nrpe.cfg
command=/usr/local/nagios/libexec/check_passwd

#####重启nrpe服务

######在nagios主程序先手动抓取数据
# ./check_nrpe -H 192.168.1.11 -c check_passwd
passwd is OK

######在nagios主程序上定义service配置
vim /usr/local/nagios/etc/objects/services.cfg(主动模式和被动模式各自的services.cfg配置文件,各自分别管理)
define service{
      use                     generic-service
      host_name               client02
      service_description   check_passwd
      check_command         check_nrpe!check_passwd
}

然后在nagios服务端进行手动抓取数据:

/usr/local/nagios/libexec/check_nrpe -H 192.168.1.11 -c check_passwd
出现数据,表明基本已经没有问题,重启服务,观察web平台页面,如下图:


自定义监控web url,用主动模式监控
# curl -I http://192.168.1.11/index.html 2>/dev/null|grep "OK"
HTTP/1.1 200 OK
# curl -I http://192.168.1.11/index.html 2>/dev/null|grep "OK"|wc -l
1
1、编写执行脚本
cd /usr/local/nagios/libexec
vim check_web_url
#!/bin/bash
char=`curl -I http://192.168.1.11/index.html 2>/dev/null|grep "OK"|wc -l`
if [ $char -eq 1 ];then
echo "the url is OK"
exit 0
else
echo "the url is wrong"
exit 2
fi

chmod +x check_web_url

2、添加check_web_url这个命令到commands.cfg配置文件中
############define command check_web_url##########
define command{
      command_name   check_web_url
      command_line   $USER1$/check_web_url
}

3、编辑servers.cfg文件
cd /usr/local/nagios/etc/services
vim web_url.cfg
define service{
      use   generic-service
      host_name       client02      监控的主机192.168.1.11在hosts.cfg有定义
      service_description   web_url
      check_period 24x7
      check_interval 5
      retry_interval 1
      max_check_attempts 3
      check_command      check_web_url    因为是主动模式
      notification_period 24x7
      notification_interval 30
      notification_optionsw,u,c,r
      contact_groups admins
}

4、检测错误,重启服务
# /etc/init.d/nagios checkconfig
Running configuration check...
OK.

# /etc/init.d/nagios reload
Running configuration check...
Reloading nagios configuration...
done

成功截图:

看下整体监控效果:


实现邮件报警功能:
配置告警的步骤:
1、添加联系人和联系组contacts.cfg
define contact{      
      contact_name                  huang            
      use                           generic-contact      ---》这里使用的模板就是模板文件中的contact定义      
      alias                           Nagios Admin         
      email                           13817419446@139.com      
      }
将定义的contact_name添加到一个新组中
新增联系组:
define contactgroup{
      contactgroup_name       mail_users            这里可以定义邮件组,手机短信组,等等
      alias                   Nagios Administrators
      members               huang
      }

2、添加报警的命令commands.cfg,这里使用默认的命令,当然你也可以自己编写shell脚本或者其他语言脚本

3、调整联系人的默认模板
define contact{
      name                            generic-contact         
      service_notification_period   24x7                  
      host_notification_period      24x7                  
      service_notification_options    w,u,c,r,f,s            
      host_notification_options       d,u,r,f,s               
      service_notification_commands   notify-service-by-email
      host_notification_commands      notify-host-by-email    如果定义了手机,这里可以加上notify-host-by-email,notify-host-by-pager,这里使用邮件告警,所以无需设置
      register                        0                     
      }

4、在hosts、services配置文件中添加报警联系人及报警组
然后修改模板中service、host的定义,将
contact_groups                  admins改为
contact_groups                  mail_users
当然也可以不在模板中定义,在hosts、services配置文件中各自定义不同的报警方式和报警组

实验:
将网站目录下面的index.html文件mv到tmp目录下,使他warning并触发告警
mv /var/www/html/index.html /tmp
可以看见web平台出现warning状态,查看nagios日志如图:

然后查看邮件,发现没有收到告警邮件,看日志发现是找不到mail命令,于是
yum -y install mailx

由于定义的services告警参数:
service_notification_options    w,u,c,r,f,s,表示监控恢复正常也会触发邮件于是将index.html重新放到网站目录下
mv /tmp/index.html /var/www/html
稍微过几分钟可以发现监控正常,查看nagios日志

再次查看邮件,如下:

简单基于mail告警功能实现

页: [1]
查看完整版本: 自定义nagios插件实现主动被动模式以及nagios基于mail的简单...