监控服务器Nagios之四 报警及扩展

一、邮件报警

    Nagios有一个很强大的功能就是邮件、短信报警,当监控的服务有故障(根据设定)的时候,就会通知你,这可以极大地提升工作效率。

1、  安装sendmail组件

#yum –y install sendmail* mailx

重启sendmail服务,并开机启动

#service sendmail  restart;chkconfig  sendmail on

发送测试邮件,测试sendmail的可用性

#echo “ok” |mail  -s  “title”  
[email protected]

2、  邮件报警设置

编辑/usr/local/nagios/etc/objects下的contact.cfg文件

define contact{
       contact_name                 nagiosadmin                   
use                                    generic-contact               
alias                         Nagios Admin                   
email                        [email protected],[email protected]     
       }

只需要把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                                       [email protected]
        }
 
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                                      [email protected]
        }
 
#######################################################################################
 #######################################################################################
 ##############   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                                     [email protected]
        }
 
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                                      [email protected]
        }
#######################################################################################
 #######################################################################################
 ##############   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

[[email protected] ~]# 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

[[email protected] ~]# /etc/init.d/httpd stop
停止 httpd:                                              [确定]
[[email protected] ~]#

再看监控端

客户端192.168.1.3启动httpd

[[email protected] ~]# /etc/init.d/httpd start
正在启动 httpd:                                           [确定]
[[email protected] ~]#

再看监控端

注释:我们这里是关闭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  [email protected]‘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

时间: 2024-07-30 10:21:15

监控服务器Nagios之四 报警及扩展的相关文章

监控服务器Nagios之一 概述及安装

一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知. Nagios原名为NetSaint,由Ethan Galstad开发并维护至今.NAGIOS是一个缩写形式: "Nagios Ain't Gonna Insist On Sainthood" Sainthood 翻译为圣徒

Nagios自定义报警时间

Nagios自定义报警时间 遇到需要自定义检查和报警时间的,总结一下.大家集思广益,不断完善. 常调用的模板配置文件: efine service{ name                           generic-service         ; 定义一个服务名称 active_checks_enabled          1                       ; Activeservice checks are enabled passive_checks_enab

nagios邮件报警配置

搭建nagios的目的就是为了他的报警系统,nagios据我所了解可以有三种报警: 一.声音报警 二.电子邮件报警 三.手机短讯报警 下面是我的邮件报警的配置过程: 1.开启系统自带的sendmail邮件系统 service sendmail start 如果没有安装,可以yum一下 yum install -y sendmail* service sendmail restart 测试发送邮件: echo "test" | mail [email protected] 我系统在关掉i

监控服务器Nagios之三 监控案例

前边的两篇仅是Nagios服务端的安装与配置文件的解读,现在终于来到了第三篇,本篇将介绍下Nagios是如下实现监控的. 安装完Nagios之后,登录Web界面访问,在hosts一项中仅有localhost(hosts代表所监控的主机),如下图: 在Service中默认已经监控Nagios服务器本身的以下选项,如下图所示 上图中http处于WARNING状态,提示HTTP WARNING:HTTP/1.1 403 Forbidden - 5237 bytes in 0.003 second re

实战Nagios网络监控(3)——Nagios 微信报警

1. 下载微信公众平台私有接口 # yum install -y git # git clone https://github.com/lealife/WeiXin-Private-API 2. 修改微信公众平台私有接口代码,以配合 nagios 报警 # cp -r WeiXin-Private-API /usr/local/nagios/libexec/weixin # chown -R nagios.nagios /usr/local/nagios/libexec/weixin # cd

nagios邮件报警脚本

目的:主要作用于nagios监控系统邮件报警功能,也可以稍作修改使用于其他地方: 使用方法: perl notify_service_by_mail.pl -t connact -s subject -m message perl notify_host_by_mail.pl -t connact -s subject -m message 服务报警代码: #!/usr/bin/env perl #notify_service_by_mail.pl use strict; use warning

nagios邮件报警设置

1.下载 sendEmail wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz 2.配置 sendEmail tar zxvf sendEmail-v1.56.tar.gz cd sendEmail-v1.56 cp sendEmail /usr/local/bin chmod 777 /usr/local/bin/sendEmail 发送测试邮件 /usr/local/bin/sendE

nagios监控 报警异常状况   二

遇到一个奇葩问题,nagios监控windows下面的cpu服务项出现如下警告信息:(但是其他服务项状态Ok,且此警告信息过一会 自动会恢复Ok状态 再过一段时间 又会变成如下图中的警告信息状态warning) 解决思路: 第一 : 排查造成此情况的原因: Nsclient++版本原因  ( 经排查同样版本的服务器CPU监控项没出现此问题故  排除) Nsclient++服务重启  ( 经观察 不是此处故障) 更换CPU监控方式: ( 由 check_nt命令检测改为snmp检测.最终解决此问题

nagios监控报警一般搭建过程及常见问题

l Nagios是什么 l Nagios安装步骤 l Nagios-plugins插件安装步骤 l Nagios安装完成后启动 l Nagios web访问 l 常见问题 l 参考资料 Nagios是什么 上个星期,自己搭建了下nagios服务器,这里记录下我自己的方法心得,给新手一个指导. Nagios是什么呢? 官网是这样定义的: Nagios is a host/service/network monitoring program written in C and released und