cacti 重图形,有数据历史,需用到数据库支持,支持web配置,默认不支持告警,可以加插件;
nagios重状态和结果,没有数据历史,不成图像,不支持web配置,可以自己开发脚本定制个性化监控,支持多种插件;
zabbix有数据历史,可成图像,支持web配置,可以自动发现;

  http://www.aminglinux.com/bbs/thread-7418-1-1.html

第一部分:nagios安装配置:
Nagios官网 http://www.nagios.org


1. Nagios安装 - 服务端(192.168.0.11)
Centos6默认的yum源里没有nagios相关的rpm包,但是我们可以安装一个epel的扩展源: 
rpm -ivh  http://www.aminglinux.com/bbs/da ... ease-6-7.noarch.rpm
yum install -y httpd nagios nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe
设置登录nagios后台的用户和密码:htpasswd -c /etc/nagios/passwd nagiosadmin
vim /etc/nagios/nagios.cfg
nagios -v /etc/nagios/nagios.cfg 检测配置文件
启动服务:service httpd start; service nagios start
浏览器访问: http://ip/nagios

2. Nagios安装 - 客户端(192.168.0.12)
在客户端机器上 rpm -ivh  http://www.aminglinux.com/bbs/da ... ease-6-7.noarch.rpm
yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe 
vim /etc/nagios/nrpe.cfg  找到“allowed_hosts=127.0.0.1” 改为 “allowed_hosts=127.0.0.1,192.168.0.11” 后面的ip为服务端ip; 找到” dont_blame_nrpe=0” 改为  “dont_blame_nrpe=1” 
启动客户端 /etc/init.d/nrpe start

3. 监控中心(192.168.0.11)添加被监控主机(192.168.0.12)
cd /etc/nagios/conf.d/
vim 192.168.0.12.cfg                   //加入:
define host{
        use                     linux-server            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               192.168.0.12
        alias                   0.12
        address                 192.168.0.12
        }

define service{
        use                     generic-service
        host_name               192.168.0.12
        service_description     check_ping
        check_command           check_ping!100.0,20%!200.0,50%
        max_check_attempts 5
        normal_check_interval 1
}

define service{
        use                     generic-service
        host_name               192.168.0.12
        service_description     check_ssh
        check_command           check_ssh
        max_check_attempts      5    #当nagios检测到问题时,一共尝试检测5次都有问题才会告警,如果该数值为1,那么检测到问题立即告警
        normal_check_interval 1   #重新检测的时间间隔,单位是分钟,默认是3分钟
        notification_interval           60 #在服务出现异常后,故障一直没有解决,nagios再次对使用者发出通知的时间。单位是分钟。如果你认为,所有的事件只需要一次通知就够了,可以把这里的选项设为0。 
}

define service{
        use                     generic-service
        host_name               192.168.0.12
        service_description     check_http
        check_command           check_http
        max_check_attempts      5
        normal_check_interval 1
}


以上服务不依赖于客户端nrpe服务,我们可以想象,我们在自己电脑上可以使用ping或者telnet探测远程任何一台机器是否存活、是否开启某个端口或服务。 而当我们想要检测客户端上的某个具体服务的情况时,就需要借助于nrpe了,比如想知道客户端机器的负责或磁盘使用情况。

4.  继续添加服务
服务端vim /etc/nagios/objects/commands.cfg
增加:define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
继续编辑 vim /etc/nagios/conf.d/192.168.0.12.cfg 
增加如下内容:define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_load
        check_command           check_nrpe!check_load
        max_check_attempts 5
        normal_check_interval 1
}


define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}

define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_disk_hda2
        check_command           check_nrpe!check_hda2
        max_check_attempts 5
        normal_check_interval 1
}


说明:  check_nrpe!check_load :这里的check_nrpe就是在commands.cfg刚刚定义的,check_load是远程主机上的一个检测脚本
在远程主机上vim /etc/nagios/nrpe.cfg 搜索check_load,这行就是在服务端上要执行的脚本了,我们可以手动执行这个脚本
把check_hda1更改一下:/dev/hda1 改为 /dev/sda1
再加一行command[check_hda2]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda2
客户端上重启一下nrpe服务: service nrpe restart
服务端也重启一下nagios服务: service nagios restart

5.   配置告警
vim /etc/nagios/objects/contacts.cfg //增加:define contact{
        contact_name               123
        use                             generic-contact
        alias                           aming
        email              lishiming2009@139.com
        }


define contact{
        contact_name               456
        use                             generic-contact
        alias                            aaa
        email              aminglinux@139.com
        }


define contactgroup{
        contactgroup_name           common
        alias                                  common
        members                          123,456
        }




然后在要需要告警的服务里面加上contactgroup

define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_load
        check_command           check_nrpe!check_load
        max_check_attempts 5
        normal_check_interval 1
        contact_groups        common
        notifications_enabled  1    # 是否开启提醒功能。1为开启,0为禁用。一般,这个选项会在主配置文件(nagios.cfg)中定义,效果相同。
        service_notification_period   24*7   #发送提醒的时间段。非常重要的主机(服务)我定义为7×24,一般的主机(服务)就定义为上班时间。如果不在定义的时间段内,无论什么问题发生,都不会发送提醒。还有一个host_notification_period, 一样的设置方法。但需要加到host的定义配置文件里。
        service_notification_options:w,u,c,r   # 上面的是host的状态,这个是service的状态。w为waning, u为unknown, c为critical, r为recover(恢复了),类似的还有一个  host_notification_options:d,u,r   #这个参数定义了发送提醒包括的情况:d = 状态为DOWN, u = 状态为UNREACHABLE , r = 状态恢复为OK,同样需要加入到host的定义配置里。
}

参考: 调用短信接口   http://www.aminglinux.com/bbs/thread-7380-1-1.html


6.   配置图形显示 pnp4nagios (请自行演示,课上不演示)
(1)安装
yum install pnp4nagios rrdtool 

(2)配置主配置文件
vim /etc/nagios/nagios.cfg  //修改如下配置

  1. process_performance_data=1    

  2. host_perfdata_command=process-host-perfdata

  3. service_perfdata_command=process-service-perfdata 

  4. enable_environment_macros=1     



(3)修改commands.cfg
vim /etc/nagios/objects/commands.cfg  //注释掉原有对process-host-perfdata和process-service-perfdata,重新定义

  1. define command {  

  2.        command_name    process-service-perfdata  

  3.        command_line    /usr/bin/perl /usr/libexec/pnp4nagios/process_perfdata.pl  

  4. }  

  5.   

  6. define command {  

  7.        command_name    process-host-perfdata  

  8.        command_line    /usr/bin/perl /usr/libexec/pnp4nagios/process_perfdata.pl -d HOSTPERFDATA  

  9. }  



(4)修改配置文件templates.cfg
vim /etc/nagios/objects/templates.cfg define host {  
        name       hosts-pnp  
        register   0  
        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_  
        process_perf_data               1  
}  
define service {  
        name       srv-pnp  
        register   0  
        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$ 
        process_perf_data               1  
}  


(5)修改host和service配置
vim /etc/nagios/conf.d/192.168.0.12.cfg 
把 “define host{
        use                     linux-server” 
改为:
define host{
        use                     linux-server,hosts-pnp

修改对应的service,比如

define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}
改为:
define service{
        use     generic-service,srv-pnp
        host_name       192.168.0.12
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}

(6) 重启和启动各个服务:
service nagios restart
service httpd restart
service npcd start

(7) 访问测试
两种访问方法:
ip/nagios/
ip/pnp4nagios/



第二部分: zabbix安装配置
1 安装zabbix
rpm -ivh http://www.lishiming.net/data/at ... ease-6-7.noarch.rpm
安装rpm包的lamp环境 yum install -y  httpd mysql mysql-libs php php-mysql mysql-server php-bcmath php-gd php-mbstring
安装zabbix服务端:yum install  zabbix20 zabbix20-agent zabbix20-server  zabbix20-server-mysql zabbix20-web zabbix20-web-mysql net-snmp-devel
/etc/init.d/zabbix-server start; /etc/init.d/zabbix-agent start
/etc/init.d/httpd start; /etc/init.d/mysqld start
mysql -uroot -p -e  "create database zabbix"
mysql -uroot -p --default-character-set=utf8 zabbix < /usr/share/zabbix-mysql/schema.sql
mysql -uroot -p --default-character-set=utf8 zabbix < /usr/share/zabbix-mysql/p_w_picpaths.sql
mysql -uroot -p --default-character-set=utf8  zabbix < /usr/share/zabbix-mysql/data.sql

2.网页安装zabbix
浏览器访问 http://ip/zabbix, 默认会有“It is not safe to rely on the system‘s timezone settings ”这样的警告信息,需要vim /etc/php.ini 设置 date.timezone=“Asia/Shanghai”点next
解决相关的报错信息,点retry  (vim /etc/php.ini)
输入mysql相关信息, 首先要测试一下,如果不通过,则需要调试,测试通过后,点next
Name 写127.0.0.1,(可以自定义)点next,再点next,最后点finish
默认管理员账号为 admin:zabbix
这时会遇到“zabbix server is not running”这样的错误,需要编辑一下 /etc/zabbix/zabbix_server.conf ,配置DBUser, DBPassword

3. 接入要监控的主机
在客户端上yum install zabbix20-agent
vim /etc/zabbix_agentd.conf //更改Server=服务端ip; ServerActive=0.0.0.0:10050; Hostname=aming(自定义,但要唯一) 
启动客户端 /etc/init.d/zabbix-agent start
服务端上命令行测试:zabbix_get -s 客户端ip -p10050 -k "system.hostname"
在web界面下,点”configuration” --> “host” --> 右上角点”Create Host”  其中host name,  visible name自定义,可以选择groups,这里默认即可,ip address 写入客户端ip
配置监控项目模板:点“templates”, 点add, 在弹出的小窗口中选择Template OS Linux, 然后点select, 最后点save

4.自定义templates
Zabbix自带了很多模板,模板中有很多监控项目,比如CPU、网卡、内存、进程等等。使用系统自带模板有点太多了,所以我们可以自定义模板。点configuration 选择 templates,点右上角的create template
Template name和Visible name  自定义,Groups 选择templates, 点save
然后我们去挑选一些项目拷贝到该模板下:比如我们找到Template OS Linux 点一下items,选择我们想要的项目,然后在下面选择copy selected to … 然后点go 
Group 选择templates, 找到刚才我们自定义的templates,点copy
点configuration 选择 templates可以看到新建的templates中已经有刚刚我们copy的items了
我们可以使用和上面相同的方法自定义拷贝Triggers(触发器 ),它用来设置告警的阀值,当然我们也可以自定义编辑它

5. 配置发邮件
yum install -y sendmail ; 
mkdir -p /home/zabbix/bin
vim /home/zabbix/bin/baojing.sh //内容:
#! /bin/bash
echo "$3" |/bin/mail -s "$2" $1
chmod +x /home/zabbix/bin/baojing.sh 

在zabbix_server.conf配置文件中,有参数AlertScriptsPath和ExternalScripts
AlertScriptsPath=/home/zabbix/bin/ ---用户自定义的media types脚本
ExternalScripts=/home/zabbix/bin/ ---用户自定义的检查的脚本(item)
这样才能找到你的脚本,因为你在frontend中只是输入脚本的名称,没有路径。

创建mediea types: “Administration" -->”Media types",点击右上角“Create Media Type"其中Description填"baojing” 或其它自定义名称,Type选择"Script",Script填”baojing.sh”然后点”Save”.  
创建user: “Adimistration” --> “Users”在右上角,选择”Users”,点击”Create User”, alias: test1,自定义name和lastname password:123456;group 选择guest,回到上面点一下media,type 选择baojing,send to 写要发送邮件的邮箱,点add, 最后点save
创建action: “configuration” --> actions,右上角“Create Actions”, Name自定义,我这里写”baojing”,其他默认,然后点右侧的“Operations”下的“New”按钮,“Operation Type”选择“Send message”,“Send Message to”选择一个或多个要发送消息的用户组,Send to Users选择我们之前新增的test1, “Send only to”选择baojing , 点一下add
最后点save