开源的监控软件

mrtg  很老的监控软件
 
cacti  使用rrdtool来画图

nagios  有较好的报警功能

ntop

 

cacti  仙人掌  

一套PHP程序,使用rrdtool工具来画图,使用snmp协议收集数据

--软件路径
ls /share/soft/cacti/

 

1。安装rpm版lamp,支持包,和字体包
[root@li ~]# yum install httpd* php php-mysql mysql mysql-server  libpng libjpeg freetype fontconfig gd libxml2 fonts-chinese -y

 

2。安装snmp软件包
[root@li ~]# yum install net-snmp* -y

 

3。安装rrdtool
[root@li ~]# cd /share/soft/cacti/
[root@li cacti]# rpm -ivh rrdtool-1.2.30-1.el5.wrl.i386.rpm rrdtool-perl-1.2.30-1.el5.wrl.i386.rpm
--这两个软件包是互相依赖,所以一条命令都装上

 

4。解压cacti的源码包到apache网站家目录,并修改相关权限
[root@li cacti]# tar xf cacti-0.8.7a-cn-utf8.tar.gz.gz -C /share/weekend/
[root@li cacti]# cd /share/weekend/
[root@li weekend]# mv cacti-0.8.7a-cn-utf8/ cacti

 

[root@li weekend]# chown apache.apache /share/weekend/cacti/ -R
--权限对于cacti写数据很重要,因为现在它是由rpm版的httpd来跑,并且rpm版httpd是由apache这个用户来跑,所以要把相关ownership改成apache

 

5.启动mysql,登录进行cacti的授权
/etc/init.d/mysqld restart

mysql> create database cacti default charset utf8; --需要为utf8字符集,因为下载的cacti中文版是utf8

mysql> grant all on cacti.* to 'cacti'@'localhost' identified by '123';

mysql> flush privileges;


mysql> use cacti

mysql> source /share/weekend/cacti/cacti.sql --执行这个脚本,在mysql提示符下source,或者在shell下用mysql直接批量执行

mysql> show tables; --可以看到默认创建了48张表

 

 

6,修改cacti的config.php文件,确认它所连接的mysql的用户名和密码等信息

[root@li weekend]# vim /share/weekend/cacti/include/config.php

$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "123";
$database_port = "3306";

 

7.配置snmp协议

[root@li weekend]# vim /etc/snmp/snmpd.conf
 41 com2sec notConfigUser  default       public  --default默认就表示允许所有人来监控
 42 com2sec notConfigUser  127.0.0.1       public --加上这一句,表示可以监控127.0.0.1,获取snmp数据


 63 access  notConfigGroup ""      any       noauth    exact  systemview none none
 64 access  notConfigGroup ""      any       noauth    exact  all none none --加上这一行,把systemview换成all


 87 view all    included  .1                               80


# /etc/init.d/snmpd restart


# snmpwalk -v 1 localhost -c public  --返回大量信息,无错误则为OK


8。通过web界面来安装cacti

/etc/init.d/httpd restart


http://2.2.2.10/cacti/


保证所有的命令路径OK,net-snmpd版本为5.x,rrdtool版本为1.2.x

默认登录密码: admin
  admin

 

登录成功后,控制面版--设备-- 本机(可以看到默认就已经监控了本机,并且有四张图) --全是本机相关的配置


点击图像 但看不到,因为数据还没有收集


9,收集数据
# ll /share/weekend/cacti/rra --这个为数据目录,现在为空

# /usr/bin/php /share/weekend/cacti/poller.php  --用此命令收集数据
--如果执行了,还是不出下面的数据,就加一个--force参数再来执行


# ls /share/weekend/cacti/rralocalhost_load_1min_5.rrd    --有数据了,就是默认监控本机的四张图有关的数据
 localhost_proc_7.rrd
localhost_mem_buffers_3.rrd  localhost_users_6.rrd
localhost_mem_swap_4.rrd

 

去web界面查看图像,有图了,但图形字体有乱码

解决方法:
控制面板--设置--路径--RRDTool默认字体路径  
/usr/share/fonts/chinese/TrueType/ukai.ttf  --写上这一句,是装fonts-chinese包得到的字体 

保存,再查看图像,rrdtool也支持中文了


为了不断收集数据,把上面收集的命令写到crontab里

# crontab -e
*/5 * * * * /usr/bin/php /share/weekend/cacti/poller.php &> /dev/null

 

--如果想马上能看到图里的数据的话,就手工再去收集一次

 

模版


一个设备模版 包含 多个图像模版
一个图像模版 包含 多个数据模版

 

数据采集

snmpget 采集
返回单个数据


data quries 采集
返回多个数据


脚本采集
可以支持非snmp数据源
支持shell,php,perl等多种脚本

 

 

例子1:监控一台远程机器


--加一个图像树用于专门放远程linux主机的图像,名称我就叫远程linux
控制面板--图像树--右上角添加--名称写上 远程linux --创建

 

控制面板--设备--右上角点添加--
名称: 137   --随意取
设备名: 2.2.2.137 
设备模版: 本地linux计算机
SNMP版本:  1版本

 点创建后,图像上面报了SNMP错误    --表示无法从客户端获取snmp信息


解决:
137这个客户端
# vim /etc/snmp/snmpd.conf
com2sec notConfigUser  2.2.2.10       public --加上这一句,允许10这个cacti监控服务器通过snmp协议来137收集数据

--不加也可以,因为默认就有com2sec notConfigUser default  public这句,default默认代表允许所有

 

# /etc/init.d/snmpd restart

# snmpwalk -v 1 10.1.1.137 -c public --验证有信息


--再回到10的web界面刷新一下,就不会报SNMP错误了


在右上角点为此设备生成图像--把图像全打勾--颜色就默认就OK--点击创建


控制面板--设备(可以看到137了,状态要为在线)--后面打勾--选择一个操作--选择 放入(远程linux)树中--确认

 

例子2:给监控本机加一个ping延迟的图像

控制面板--设备--本机--添加图像模版添加unix-ping 延迟  --添加之后,显示的是未生成图像


控制面板--新建图像--新建 unix-ping 延迟

--然后本机图像就生成了,再去手工收集一次数据,点查看图像就可以看到了

 

=========================================================

实例导入模版监控mysql


软件包的路径:
/share/soft/cacti/teMySQLcacti-20060810.tar.gz

# tar xf /share/soft/cacti/teMySQLcacti-20060810.tar.gz -C /usr/src/

# cd /usr/src/teMySQLcacti/

[root@li teMySQLcacti]# ls --.xml文件为模版文件。.sh和.php的为脚本文件
cacti_host_template_memcached_host-step300-heartbeat600.xml
cacti_host_template_memcached_host.xml
cacti_host_template_temysql_host-step300-heartbeat600.xml
cacti_host_template_temysql_host.xml
CHANGELOG
convertXmlTo5minutePollingInterval.sh
dumpMemcachedStats.php
makeMin1RrdIntoMin0Rrd.sh
makeReleaseFile.sh
mysql_stats.php
README.txt --查看readme,有安装和使用帮助


1。
# ./convertXmlTo5minutePollingInterval.sh --如果是五分钟一次收集数据,先执行此脚本

2。
# cp mysql_stats.php dumpMemcachedStats.php /share/weekend/cacti/scripts/
--拷贝这两个脚本到cacti/scripts/目录下


# chown apache.apache /share/weekend/cacti/scripts/dumpMemcachedStats.php /share/weekend/cacti/scripts/mysql_stats.php

3。
# vim /etc/php.ini
memory_limit = 32M  --内存限制改成32M,readme里写的是大于8M

# /etc/init.d/httpd reload


4。被监控的远程mysql服务器授权
mysql> grant process,super on *.* to 'cacti'@'2.2.2.10' identified by '123';  --2.2.2.10是监控服务器cacti的IP

mysql> flush privileges;

 

5。导入相关模板

控制面板--导入模板
把下面两个模板导入
cacti_host_template_memcached_host-step300-heartbeat600.xml
cacti_host_template_temysql_host-step300-heartbeat600.xml

 

6。
新建立一个图像树

控制面板--图像树--添加--名字随意 我这叫 mysql服务器组 --创建


设备--添加--

描述:137-mysql
设备名:2.2.2.137  --远程被监控的mysql的IP
设备模版: teMysql Host        --这个设备模版包含21个图形模板,可以不选它,自己手工加需要的图形模板也是可以
snmp版本: 1版本


设备--在127-mysql后面打勾 --选择一个操作里 加入 mysql服务器组 --确认


# /usr/bin/php /share/weekend/cacti/poller.php --force
--强制刷新一下数据,有报错不管

 

==================================


整合版CACTI

[root@li cacti]# ls /share/soft/iso/CactiEZ_V9.iso

系统登录:
用户:root
密码:CactiEZ

只要有IP,然后使用浏览器访问

cacti登录:
用户:admin
密码:admin


这个系统默认为静态IP  192.168.0.5,可以手动修改一下,然后使用浏览器访问

 

==============================================================

安装下面这个
/share/soft/cacti/CactiEZ-v0.6.iso --英文整合版

 

Login Information

Some useful info you will need. You can also see this information by pressing F6 at the CD Installation prompt.

To login to the OS (you will want to change this)
user: root
pass: CactiEZ

To login to cacti (it prompts to change this)
user: admin
pass: admin

Get to webmin at (requires the root user / pass)
https://serverip:10000

To configure ntop (you will want to change this)
user: admin
pass: CactiEZ

To configure nagios
user: nagiosadmin
pass: CactiEZ

 

--这个版本安装的插件是非常多的

http://docs.cacti.net/plugins --插件的官网地址


自己可以尝试上网查查资料去尝试用用

 


--参考网站
http://www.cactiez.com

 

==============================================================


nagios

--官档路径
http://nagios.sourceforge.net/docs/nagioscore/3/en/toc.html
--或者安装主程序包完全后,会在下面这个目录找到
# ls /usr/local/nagios/share/docs/


 监控大量机器上运行的服务和负载等,带报警功能。


[root@li ~]# ls /share/soft/soft/monitor2/
nagios-3.2.3.tar.gz            --主程序包 
nagios-plugins-1.4.15.tar.gz  --插件包
nrpe-2.12.tar.gz   --客户端程序包

--注意插件包等和主程序包的版本号不一定要一致


1,搭建rpm版lamp
# yum install httpd* gd gd-devel


2,建立用户

# useradd nagios
# groupadd nagiosgroup
# usermod -G nagiosgroup nagios
# usermod -G nagiosgroup apache


3,安装nagios主程序包

# tar xf /share/soft/soft/monitor2/nagios-3.2.3.tar.gz -C /usr/src/
# cd /usr/src/nagios-3.2.3/

# ./configure --with-nagios-user=nagios --with-nagios-group=nagiosgroup

# make all
# make install
# make install-init
# make install-commandmode
# make install-config
# make install-webconf


 make install
     - This installs the main program, CGIs, and HTML files

  make install-init
     - This installs the init script in /etc/rc.d/init.d

  make install-commandmode
     - This installs and configures permissions on the
       directory for holding the external command file

  make install-config
     - This installs *SAMPLE* config files in /usr/local/nagios/etc
       You'll have to modify these sample files before you can
       use Nagios.  Read the HTML documentation for more info
       on doing this.  Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!

  make install-webconf
     - This installs the Apache config file for the Nagios
       web interface


# ls /usr/local/nagios/
bin  etc  libexec  sbin  share  var

--libexec目录为空,需要安装插件包才会有很多命令与脚本


4,安装nagios插件包  --包含用于收集数据的程序,命令,脚本等
# tar xf /share/soft/soft/monitor2/nagios-plugins-1.4.15.tar.gz -C /usr/src/
# cd /usr/src/nagios-plugins-1.4.15/

# ./configure --with-nagios-user=nagios --with-nagios-group=nagiosgroup

# make ;make install

 

5。创建web界面可访问的验证用户
/etc/httpd/conf.d/nagios.conf  --在这个文件里已经配置了nagios的apache验证,所以我们要把用户给创建出来

# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin --文件路径和用户名都不要改,被规定了就是这个
New password:
Re-type new password:
Adding password for user nagiosadmin


/etc/init.d/nagios restart
/etc/init.d/httpd restart

 

6,nagios配置文件介绍

/usr/local/nagios/etc/nagios.cfg --主配置文件

/usr/local/nagios/etc/objects/  --子配置文件的目录
  localhost.cfg  --一个示例模版,默认定义了监控本机的8个服务
  templates.cfg  --模版定义文件
  commands.cfg  --命令定义文件
  contacts.cfg  --定义通知方式的文件
  timeperiods.cfg  --监控时间段定义文件

 

==================================================

关于nagios配置文件之间的联系讲解示例

# vim /usr/local/nagios/etc/nagios.cfg 
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg


  

# vim /usr/local/nagios/etc/objects/localhost.cfg


define host{
        use                     linux-server   --模版
        host_name               localhost   --主机名 
        alias                   localhost   --主机别名 
        address                 127.0.0.1   --被监控机器的IP
        }


define hostgroup{
        hostgroup_name  linux-servers
        alias           Linux Servers
        members         localhost    --linux Servers组现在只有localhost这一个成员
        }


--下面是8个默认定义的服务,我以监控磁盘利用率的这一段为例
define service{
        use                             local-service    --模版,在templates.cfg 里定义的   
        host_name                       localhost --主机名,调用的是同配置文件里define host里定义的host_name
        service_description             Root Partition --描述,会在web界面显示的一个标题
        check_command                   check_local_disk!20%!10%!/ --检测利用率的命令,free空间小于20%就报警,小于10就critcal警告
        }

 


# vim /usr/local/nagios/etc/objects/templates.cfg


define host{
        name                            linux-server
 use                             generic-host --linux主机模版也使用了一个叫generic-host的模版,也在templates.cfg里
 check_period                    24x7   --在timeperiods.cfg 里定义的时间段
 check_interval                  5
 retry_interval                  1
 max_check_attempts              10
 check_command                   check-host-alive  --在commands.cfg 里定义的命令
 notification_period             workhours --通知时间在timeperiods.cfg里定义的
 notification_interval           120  --通知间隔
 notification_options            d,u,r   --通知选项
 contact_groups                  admins  --通知组,在contacts.cfg 里定义
 register                        0    --不注册,表示这只是一个模版,被调用,不会被nagios进程认为就是一台主机
 }

 

# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
        command_name    check-host-alive
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
        }

--命令都在libexec下,用--help去查
# /usr/local/nagios/libexec/check_ping --help

 


================================

--配置文件默认什么都不要改,检查配置文件正确性
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


# /etc/init.d/nagios restart
# /etc/init.d/httpd restart


访问路径:http://172.19.1.17/nagios
 用户名:nagioadmin
 密码: 前面htpasswd -c 创建时定义的密码

 

==================================================


现在查看web界面,默认只监控了localhost,并监控了其8个服务

一些小操作:
1,如果http服务为×××,是警告,则需要把网站家目录里加一个主页进去(家目录为空,他就会警告)。
但需要等它下一次check才会OK。如果要手动check,可以点http,再右边点Re-schedule the next check of this service去强制check,就OK了

2,默认http和ssh是关闭通知的,是因为在localhost.cfg里这两个服务有一句 notifications_enabled       0。
也可以手动打开,点进去,再右边点enabled notifications for this service.


3,关闭ssh服务,刷新web界面,还是没有critical.
   点击ssh,可以看到下一次计划的check时间。如果不等的话,在右边点Re-schedule the next check of this service强制check,再刷新就critical

 


4,修改ssh的check时间间隔
# vim /usr/local/nagios/etc/objects/localhost.cfg
define service{
        use                             local-service --使用的这个模版,所以要去改这个模版里的时间
        host_name                       localhost
        service_description             SSH
        check_command                   check_ssh
        notifications_enabled           0
        }

 

 

# vim /usr/local/nagios/etc/objects/templates.cfg
 define service{
        name                            local-service
 .............
  normal_check_interval           1  --把这个五分钟改为1分钟
 .............
 }


# /etc/init.d/nagios reload


--再去web界面验证,check时间为1分钟了

 


========================================================

例1:在默认8个服务的基础上,如何增加监控本机的服务  如ftp

思路步骤:
1,看libexec/下是否有检测ftp的命令,如果没有,网上下载,或自己开发
2,在localhost.cfg里定义这个服务
3,在command.cfg里定义命令

#vim /usr/local/nagios/etc/objects/localhost.cfg  --加上下面一段

define service{
        use                             local-service
        host_name                       localhost
        service_description             FTP
        check_command                   check_ftp!3!6
        }


# vim /usr/local/nagios/etc/objects/commands.cfg  --下面一段默认就有,不需要加,直接改一下

define command{
        command_name    check_ftp
        command_line    $USER1$/check_ftp -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$
        }


# /etc/init.d/nagios restart

 

练习:
1,如果本机ftp服务为监听2121端口,应该如何监控

# vim /etc/vsftpd/vsftpd.conf 
listen_port=2121  --加上这一句


# netstat -ntlup |grep ftp
tcp        0      0 0.0.0.0:2121                0.0.0.0:*                   LISTEN      29883/vsftpd  

 

# vim /usr/local/nagios/etc/objects/localhost.cfg

---加下面一段
define service{
        use                             local-service
        host_name                       localhost
        service_description             FTP --标题改成FTP
        check_command                   check_ftp_2121!3!6!2121
--命令我这里是没有的,在command.cfg里默认有一个check_ftp,没有
--check_ftp_2121这个,所以要手动去加;!为参数分隔符,3是第一个参数,6是第二个参数,2121是第三个参数;它们对应于我下面定义的-w -c  -p
        }

# vim /usr/local/nagios/etc/objects/commands.cfg

define command{
        command_name    check_ftp_2121
        command_line    $USER1$/check_ftp -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $ARG3$
        }


--直接使用监控命令去手工check一下,OK的
# /usr/local/nagios/libexec/check_ftp -w 3 -c 6 -p 2121
FTP OK - 0.004 second response time on port 2121 [220-#############################
220-#]|time=0.003835s;3.000000;6.000000;0.000000;10.000000


# /etc/init.d/nagios reload
--reload后,再去web界面可以看到能监控本机的ftp这个服务了

 

2,监控本机的mysql
--注意:如果/usr/local/nagios/libexec/没有check_mysql命令,则把mysql相关的包装上,再cd到cd /usr/src/nagios-plugins-1.4.15/重新./configure然后make;make install,就有了


# vim /usr/local/nagios/etc/objects/localhost.cfg

define service{
        use                             local-service
        host_name                       localhost
        service_description             MYSQL
        check_command                   check_mysql!root!123
        }


# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
        command_name    check_mysql
        command_line    $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ --第一个参数对应上面的root,第二个对应密码123
        }

 

--手动check一下mysql,OK
# /usr/local/nagios/libexec/check_mysql -u root -p123
Uptime: 189  Threads: 1  Questions: 5  Slow queries: 0  Opens: 12  Flush tables: 1  Open tables: 6  Queries per second avg: 0.026


# /etc/init.d/nagios reload


--去nagios 的web界面刷新查看,OK

 

===========================================================


我们把监控的服务分为公共和私有

公共:如ssh,http,ftp,mysql等。监控本地或远程的公共服务,都可以直接配置
私有:如load,users,disk usage等。监控本地私有服务直接配置就好,监控远程私有服务,需要服务和被监控端安装nrpe


例:监控远程服务器的普通服务(公共服务)。如ssh,http,ftp,mysql等

 

如:我的被监控端IP为172.19.1.125
1.在nagios服务器的主配置文件里加上125的主机配置文件

# vim /usr/local/nagios/etc/nagios.cfg

cfg_file=/usr/local/nagios/etc/objects/125.cfg


2,创建这个125.cfg
# cd /usr/local/nagios/etc/objects/
# cp localhost.cfg 125.cfg

# vim 125.cfg

define host{
        use                     linux-server
 host_name               172.19.1.125 --主机名,最好/etc/hosts里对应好IP,我这里没有做,就直接写IP
        alias                   172.19.1.125 --显示到web上的名字
        address                 172.19.1.125 --实际被监控主机IP
        }


--下面是公共服务,这里我只写了五个,你可以自行增加
define service{
        use                             local-service 
        host_name                       172.19.1.125
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }


define service{
        use                             local-service  
        host_name                       172.19.1.125
        service_description             SSH
        check_command                   check_ssh
        }

define service{
        use                             local-service
        host_name                       172.19.1.125
        service_description             HTTP
        check_command                   check_http
        }


define service{
        use                             local-service
        host_name                       172.19.1.125
        service_description             FTP
        check_command                   check_ftp!3!6
        }


define service{
        use                             local-service
        host_name                       172.19.1.125
        service_description             MYSQL
        check_command                   check_mysql!root!123
        }

 

--验证配置文件,再重启服务
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# /etc/init.d/nagios reload

 

=====================================================


例:监控远程的私有服务


1,在nagios服务器上安装nrpe插件

# tar xf /share/soft/soft/monitor2/nrpe-2.12.tar.gz -C /usr/src/
# ./configure && make && make install

--安装完后,就有下面的脚本了
/usr/local/nagios/libexec/check_nrpe


2,增加check_nrpe命令到commands.conf文件里
# vim /usr/local/nagios/etc/objects/commands.cfg

define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$  --c参数后接command, 也就说check_nrpe可以调用别的check命令
        }

 

3,在nagios服务器上对125的配置文件增加远程私有服务

# vim 125.cfg

define service{
        use                             local-service
        host_name                       172.19.1.125
        service_description             Root Partition
        check_command                   check_nrpe!check_remote_root 
--check_remote_root就是check_nrpe的C参数要调用的命令,此命令在nagios服务器上的commands.cfg里是不存在,它会在后面的步骤中加到被监控端
        }


define service{
        use                             local-service
        host_name                       172.19.1.125
        service_description             Current Users
        check_command                   check_nrpe!check_remote_users
        }


define service{
        use                             local-service
        host_name                       172.19.1.125
        service_description             Total Processes
        check_command                   check_nrpe!check_remote_total_procs
        }


define service{
        use                             local-service
        host_name                       172.19.1.125
        service_description             Current Load
        check_command                   check_nrpe!check_remote_load
        }


define service{
        use                             local-service
        host_name                       172.19.1.125
        service_description             Swap Usage
        check_command                   check_nrpe!check_remote_swap
        }


# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
--检查一下配置文件正确性,OK的话则配置端配置完毕,先不reload nagios服务,等被监控端配置完后再reload

 

==============================================================


现在在被监控端125上安装

1,新建用户
# useradd nagios
# groupadd nagiosgroup
# usermod -G nagiosgroup nagios

 

2,安装plugins插件,包含了数据采集命令脚本
# tar xf nagios-plugins-1.4.15.tar.gz -C /usr/src/
# cd /usr/src/nagios-plugins-1.4.15/
# ./configure --with-nagios-user=nagios --with-nagios-group=nagiosgroup
# make && make install

 

3,安装nrpe
# tar xf nrpe-2.12.tar.gz -C /usr/src/
# cd /usr/src/nrpe-2.12/
# ./configure && make && make install

# make install-plugin
# make install-daemon
# make install-daemon-config
# make install-xinetd


4,修改nrpe的超级守护进程的配置文件

# vim /etc/xinetd.d/nrpe

service nrpe
{
        flags           = REUSE
        socket_type     = stream
        port            = 5666
        wait            = no
        user            = nagios
        group           = nagios
        server          = /usr/local/nagios/bin/nrpe
        server_args     = -c /usr/local/nagios/etc/nrpe.cfg --inetd
        log_on_failure  += USERID
        disable         = no
        only_from       = 127.0.0.1 172.19.1.17 --加上nagios服务器的IP,允许它来访问
}

# vim /etc/services  --最后面加一行
nrpe            5666/tcp        # NRPE

 

5,在nrpe配置文件里定义check命令,使nagios服务能调用

# vim /usr/local/nagios/etc/nrpe.cfg


command[check_remote_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_remote_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_remote_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2     --/dev/sda2是被监控端的根分区,也可以直接就写一个  / 就可以了
command[check_remote_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
command[check_remote_swap]=/usr/local/nagios/libexec/check_swap -w 80%% -c 60%%   --这句默认没有的,但nagios服务器有配置,所以加上这句
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z  --这个是默认有的,但nagios服务器那边我没有加,所以这个在这里没有用 


# /etc/init.d/xinetd restart --启动超级守护进程

# netstat -ntlup |grep 5666  --有端口被监听了
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      22120/xinetd 

 


6,在本地或nagios服务器测试
--在被监控端测试成功
# /usr/local/nagios/libexec/check_users -w 5 -c 10
USERS OK - 3 users currently logged in |users=3;5;10;0


--在nagios服务器上测试成功
# /usr/local/nagios/libexec/check_nrpe -H 172.19.1.125 -c check_remote_users
USERS OK - 3 users currently logged in |users=3;5;10;0

 

7,回到nagios服务器重启服务
# /etc/init.d/nagios restart

 

=================================================================


nagios邮件报警功能


报警方式:
1,声音报警
2,邮件报警
3,短信报警


这里我就用自带的sendmail简单实现


1,搭建邮件系统  --前面都有讲,这里省略,我直接用本地sendmail测试是否能收邮件
#yum install sendmail* m4 -y
#/etc/init.d/sendmail restart

# echo 'test' | mail root@li.cluster.com --发一个邮件给自己的root用户测试能接收

 

2,修改nagios服务器联系相关的子配置文件
# vim /usr/local/nagios/etc/objects/contacts.cfg

define contact{
        contact_name                    nagiosadmin     
        use                             generic-contact    
        alias                           Nagios Admin          
        email                           root@li.cluster.com    --改成收的邮件地址
        }

 

# vim /usr/local/nagios/etc/objects/templates.cfg  --保持默认


define contact{
        name                            generic-contact
        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    --主机通知命令
        register                        0                     
        }


# vim /usr/local/nagios/etc/objects/commands.cfg  --默认有这两条命令的定义,我这里保持默认就好了


define command{
        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
        }

 

define command{
        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
        }


# /etc/init.d/nagios restart


--关闭一些服务进行测试tail /var/mail/root来查看


===============================================================


使用免费139邮箱来进行短信通知


https://mail.10086.cn/Register/default.aspx上注册一个邮箱

我的为:
158xxxxxxxx@139.com


登录进去邮箱:
点设置-->邮件到达通知-->点开启


然后在本机发送一个测试邮件给你的邮箱,会发现手机会马上收到此邮件
# mail -s 'test' 158xxxxxxxx@139.com 

 

# vim /usr/local/nagios/etc/objects/contacts.cfg

define contact{
        contact_name                    nagiosadmin     
        use                             generic-contact    
        alias                           Nagios Admin          
        email                           158xxxxxxxx@139.com    --改成收的邮件地址
        }


# /etc/init.d/nagios restart


--然后就把几个服务关闭,开启一下,等邮件通知,通知过就会发到手机