一、邮件报警
Nagios有一个很强大的功能就是邮件、短信报警,当监控的服务有故障(根据设定)的时候,就会通知你,这可以极大地提升工作效率。
1、 安装sendmail组件
#yum –y install sendmail* mailx
重启sendmail服务,并开机启动
#service sendmail restart;chkconfig sendmail on
发送测试邮件,测试sendmail的可用性
#echo “ok” |mail -s “title” 654001593@qq.com
2、 邮件报警设置
编辑/usr/local/nagios/etc/objects下的contact.cfg文件
define contact{
       contact_name                 nagiosadmin                   
use                                    generic-contact               
alias                         Nagios Admin                   
email                        654001593@qq.com,yang9596@126.com     
       }只需要把email项改为自己定义的邮箱即可,可以定义多个。
3、 邮件分组
如果说公司有上百台监控的设备,而你只负责其中的一部分,其他设备有问题了,也不停的给你发短信,是不是很烦躁?那么我们可以使用邮件分组,好处就在于我们可以只接受规定的服务器报警邮件。
我们可以定义多个contact(联系人),然后再用contact group(联系组)对各contact进行分组。
例如管理网络的有两人,管理服务器的有两个人,我们就可以定义两个contactgroup,然后定义四个管理员的contact,如下例是当前我正在使用的contact.cfg,服务器管理员有两名,网络管理员有两名:
contact.cfg的配置
#######################################################################################
 #######################################################################################
 ##############   NETWORK ADMINISTRATOR MEMBERS
 #######################################################################################
 #######################################################################################
define contact{
        contact_name                               net_jacken
        use                                        generic-contact
        alias                                      net_jacken
        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
       email                                       net_jacken@test.com
        }
 
define contact{
        contact_name                               net_tom
        use                                        generic-contact
        alias                                      net_tom
        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
        email                                      net_tom@test.com
        }
 
#######################################################################################
 #######################################################################################
 ##############   SYSTEM ADMINISTRATOR MEMBERS
 #######################################################################################
 #######################################################################################
 
define contact{
        contact_name                              sa_mark
        use                                       generic-contact
        alias                                     sa_mark
        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
        email                                     sa_mark@test.com
        }
 
define contact{
        contact_name                               sa_bob
        use                                        generic-contact
        alias                                      sa_bob
        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
        email                                      sa_bob@test.com
        }
#######################################################################################
 #######################################################################################
 ##############   NETWORK ADMINISTRATOR GROUP
 #######################################################################################
 #######################################################################################
 define contactgroup{
        contactgroup_name                         network
        alias                                     network
        members                                   net_jacken,net_tom
        }
#######################################################################################
 #######################################################################################
 ##############   SYSTEM ADMINISTRATOR GROUP
 #######################################################################################
 #######################################################################################
 define contactgroup{
        contactgroup_name                         system
        alias                                     system
        members                                   sa_mark,sa_bob
        }4、 主机及监控内容的配置以主机192.168.1.4为例
vim  /usr/local/nagios/etc/objects/192.168.1.4.cfg
define host{
        use                                    linux-server
        host_name                              client-02
        alias                                  192.168.1.4
        address                                192.168.1.4
        }
 define service{
        use                                    generic-service         
        host_name                              client-02
        service_description                    PING
        check_command                          check_ping!100.0,20%!500.0,60%
        contact_groups                         network
        }
 define service{
        use                                    generic-service
        host_name                              client-02
        service_description                    Uptime
        check_command                          check_nt!UPTIME
        contact_groups                         system
         }如上面配置所示,当监控主机的ping出现问题的时候,nagios就会查看contact.cfg中定义的联系人组network中的联系人的信息,然后读取各联系人的邮件地址,这样的话,网络中出现故障时就可以直接给net_jacken和net_tom二人发邮件了;同理,当服务器出现问题的时候就会给system组的相关人员发送邮件了
短信通知
其实短信通知就和nagios服务器没太大关系,就是相当于别人给你发一个邮件,你可以用手机查看而已,这里不再详细描述。
注册后登录mail.139.com---右上角设置—手机通知---接收方式……….
不过有时候可能会识别垃圾邮件,设置---邮箱安全----白名单……….
二、监控Web关键字
试问如果有一天网站突然挂了,或者主页被被人攻击了,那么怎么有效地收到通知并及时处理呢?这时可以通过Nagios监控Web关键字来解决。
可以使用默认监控命令check_http命令+相关的参数来实现,如下:
在command.cfg添加如下关键词监控命令:check_http_word,参数解析:-I指定IP或者主机名,-u指定URL,-p指定端口,-s指定关键词。
这里要监控的是192.168.1.2主机上的由Discuz搭建的论坛。
1、 修改commands.cfg
[root@nagios ~]# vim/usr/local/nagios/etc/objects/commands.cfg
找到”check_http”修改如下:
# ‘check_http‘ command definition
define command{
       command_name    check_http_word
       command_line    $USER1$/check_http-I $HOSTADDRESS$   -s $ARG1$
       }2、 创建监控主机配置文件
#cd  /usr/local/nagios/etc/objects
#cp localhost.cfg  192.168.1.3.cfg
#echo > 192.168.1.3.cfg
#vim 192.168.1.3.cfg
#添加以下内容
define host{
       use                    linux-server
       host_name              192.168.1.3
       alias                  192.168.1.3
       address                192.168.1.3
       }
define hostgroup{
       hostgroup_name  192.168.1.3
       alias           Linux Servers
       members         192.168.1.3
       }
define service{
       use                            local-service
       host_name                      192.168.1.3
       service_description            web_words_monitor
       check_command                  check_http_word!192.168.1.3!X3.2
       notifications_enabled           1                   #关键字X3.2(论坛版本)
       }3、 修改nagios.cfg文件
# vim /usr/local/nagios/etc/nagios.cfg #加入下边一行 cfg_file=/usr/local/nagios/etc/objects/192.168.1.3.cfg #service nagios restart
4、 验证
已经ok
客户端192.168.1.3停止httpd
[root@lamp ~]# /etc/init.d/httpd stop 停止 httpd: [确定] [root@lamp ~]#
再看监控端
客户端192.168.1.3启动httpd
[root@lamp ~]# /etc/init.d/httpd start 正在启动 httpd: [确定] [root@lamp ~]#
再看监控端
注释:我们这里是关闭httpd服务,模拟关键字消失,即使服务正常,如果所监控的关键字消失,依旧会报警。
三、监控Mysql主从
环境为 Nagios服务端IP为192.168.1.2
Mysql主服务器IP为192.168.1.3
Mysql从服务器IP为192.168.1.4
现在Mysql为同步状态。
1、 Nagios服务器上定义command.cfg
# vim /usr/local/nagios/etc/objects/ commands.cfg
添加以下内容
define command{
       command_name   check_mysql_slave
       command_line  $USER1$/check_mysql-H $HOSTADDRESS$ -S -uroot -phi -H $ARG1$
 }2、 定义监控项目
# cd  /usr/local/nagios/etc/objects/
# touch mysql.cfg
# chown nagios:nagios mysql.cfg
#vim mysql.cfg
添加以下内容
define host{
       use                    linux-server
       host_name               mysql
       alias                   mysql_master
       address                192.168.1.3
       }
define hostgroup{
       hostgroup_name          mysql
       alias                   Mysql Servers
       members                 mysql
       }
 
 
define service{
       use                            local-service
       host_name                      mysql
       service_description            Monitor_Mysql_Slave
       check_command                  check_mysql_slave!192.168.1.4
       notifications_enabled           1
       }如果没有check_mysql,Nagios安装mysql-devel然后重新编译Nagios-plugins
3、 更新nagios.cfg配置文件
# vim /usr/local/nagios/etc/nagios.cfg 添加一行 cfg_file=/usr/local/nagios/etc/objects/mysql.cfg
4、 Mysql服务器授权
mysql> grant all on *.* to root@‘192.168.1.2‘ identified by ‘hi‘; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>
5、 验证
192.168.1.4从服务上关闭mysql服务
#service mysqld stop
当Mysql主服务挂掉的时候依旧会报警
Done
本文出自 “Welcome To Linux World” 博客,请务必保留此出处http://linuxnote.blog.51cto.com/9876511/1638905
原文:http://linuxnote.blog.51cto.com/9876511/1638905