以监控http://192.168.1.106:8080/checkstatus.php为例
commands.cfg加入以下内容
1 2 3 4 5 6 |
vi /usr/local/nagios/etc/objects/commands.cfg define command{ command_name check_http_page command_line $USER1$/check_http -H $ARG1$ -p $ARG2$ -u $ARG3$ -R $ARG4$ } |
linuxsever.cfg中加入以下内容
1 2 3 4 5 6 7 8 9 |
vi /usr/local/nagios/etc/server/linuxsever.cfg define service{ use generic-service ; Name of service template to use host_name linuxsever1 service_description check_http_page_status check_command check_http_page!192.168.1.106!8080!/checkstatus.php!'ok'! notifications_enabled 1 } |
注:这里是用的check_http的-R参数来做搜索的。-R参数是不区分大小的,另外还有-s和-r也可达到类似目的。如下面的例子:
1 2 3 |
./check_http -H 192.168.1.106 -p 8080 -u /checkstatus.php -R 'ok' ./check_http -H 192.168.1.106 -p 8080 -u /checkstatus.php -s 'Ok' ./check_http -H 192.168.1.106 -p 8080 -u /checkstatus.php -r 'Ok' |
-s
-r
-R
4、监控SSH
linuxsever.cfg中加入以下内容
1 2 3 4 5 6 7 |
define service{ use generic-service ; Name of service template to use host_name linuxsever1 service_description SSH check_command check_ssh notifications_enabled 1 } |
5、监控PING
linuxsever.cfg中加入以下内容
1 2 3 4 5 6 |
define service{ use generic-service ; Name of service template to use host_name linuxsever1 service_description PING check_command check_ping!100.0,20%!500.0,60% } |
六、一些说明
我整体的Nagios的配置都是以官方自带模板文件(objects/templates.cfg)为基础进行扩展的,这里把模板文件中一些常用选项做一个简单的说明。
主要修改的也只有templates.cfg的一些选项,监控时段定义采用的缺省定义(objects/timeperiods.cfg)。
1 2 3 4 5 6 |
check_interval 5 #每5分钟检测一次 retry_check_interval 1 #出现问题时每1分钟重检测一次 max_check_attempts 3 #检测失败后最大重试次数 notification_interval 10 #10分钟发送一次警报 notification_period 24x7 #发送警报的时间段 notification_options w,u,c,r #什么情况发送警报 |
检测间隔的单位是由nagios.cfg里interval_length定义的,缺省nagios.cfg里面的nterval_length为60秒,就是1分钟!
报警状态说明:
w—报警(warning)
u—未知(unkown)
c—严重(critical)
r—从异常情况恢复正常(recoveries)
d—当机(down)
f—(flapping)检测主机或服务处于抖动状态
s—(scheduled)计划的开始和结束停机。
注:当主机状态过度频繁地变换状态时可以考虑状态处于“抖动”(flapping)。一个明显的例子就是一台主机由于加载操作系统而不断地重启动,这种状态就是处于抖动。
七、给Nagios加入性能图表
PNP4Nagios是一个用于显示Nagios性能图表的插件,基于RRDTool日志分析,并以Kohana(一个PHP开发框架)呈现在Web端,可以以Action Url的形式集成到Nagios中。
A、安装相关环境
1 |
apt-get install rrdtool librrds-perl php5-gd |
Apache启用rewrite模块
1 |
cp /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/ |
B、安装PNP4Nagios
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
wget http://sourceforge.net/projects/pnp4nagios/files/PNP-0.6/pnp4nagios-0.6.13.tar.gz tar zxvf pnp4nagios-0.6.13.tar.gz cd pnp4nagios-0.6.13 ./configure --with-nagios-user=nagios --with-nagios-group=nagcmd make all make install make install-webconf make install-config make install-init #使用下面这条命令可以替换所有的make命令 make fullinstall #重启Apache,让PNP4Nagios的Web访问生效 apache2ctl restart |
注:如果单独编译安装的rrdtoool,则需使用–with-rrdtool显示指定rrdtool的位置。如:–with-rrdtool=/usr/local/rrdtool-1.2.xx/bin/rrdtool这样的形式
C、配置PNP4Nagios
PNP4Nagios有三种工作模式,分别是Synchronous Mode、Bulk Mode和Bulk Mode with NPCD,下面以Synchronous Mode来配置PNP4Nagios。
修改Nagios的主配置文件
1 2 3 4 5 |
vi /usr/local/nagios/etc/nagios.cfg process_performance_data=1 service_perfdata_command=process-service-perfdata host_perfdata_command=process-host-perfdata |
修改Nagios的command.cfg配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
vi /usr/local/nagios/etc/objects/commands.cfg #增加以下两个命令 #process-service-perfdata command definition define command { command_name process-service-perfdata command_line /usr/local/pnp4nagios/libexec/process_perfdata.pl } #process-host-perfdata command definition define command { command_name process-host-perfdata command_line /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA } |
注意:commands.cfg已经定义了相应命令,需要先注释掉。否则后面重启Nagios时,会因为配置文件中命令重复而报错。
D、检查PNP4Nagios是否正确安装
访问http://ip/php4nagios,会进行一些必要的环境测试,如果页面最下方出现[Your environment passed all requirements. Remove or rename the /usr/local/pnp4nagios/share/install.php file now.]这一行绿色的提示就表示PNP4Nagios环境已正确。
如果所有的环境都配置正确了,删除或重命名install.php文件
1 |
mv /usr/local/pnp4nagios/share/install.php /usr/local/pnp4nagios/share/install.php.bak |
E、将性能图集成到nagios的web页面中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
vi /usr/local/nagios/etc/objects/templates.cfg #在最后添加 define host { name host-pnp action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_ register 0 } define service { name srv-pnp action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$ register 0 } |
让PNP4Nagios显示的性能图表在Nagios的主机或服务中显示,需要在定义主机和服务时加入host-pnp和srv-pnp配置实例。
具体修改文件视Nagios中定义而定,下面只是一个示例,主要的修改就是在use行加入相应的模板。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
vi /usr/local/nagios/etc/server/linuxsever.cfg define host{ use linux-server,host-pnp ; 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.1.106 alias 192.168.1.106 address 192.168.1.106 } define service{ use generic-service,srv-pnp ; Name of service template to use host_name 192.168.1.106 service_description PING check_command check_ping!100.0,20%!500.0,60% } |
注:如果有多个主机和服务,每一个定义中都需要加入。
F、重启Nagios服务
1 |
/etc/init.d/nagios restart |
访问http://ip/nagios,点击主机或服务后面小太阳图标就可查看相应图表。
G、弹窗方式显示性能图表
上面的方法必须点击主机或服务后面小太阳图标才能查看相应的性能图表,下面的方法可直接在Nagios页面中以弹出窗口显示性能图表。
1 |
cp pnp4nagios-0.6.13/contrib/ssi/status-header.ssi /usr/local/nagios/share/ssi/ |
注:contrib/ssi/status-header.ssi在源代码目录中
status-header.ssi文件必须没有执行权限
修改Nagios的模板文件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
vi /usr/local/nagios/etc/objects/templates.cfg define host { name host-pnp action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=_HOST_ register 0 } define service { name srv-pnp action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$' class='tips' rel='/pnp4nagios/index.php/popup?host=$HOSTNAME$&srv=$SERVICEDESC$ register 0 } |
注:如果按文中之前的方式定义过,请先注释掉。
重启Nagios服务
1 |
/etc/init.d/nagios restart |
访问http://ip/nagios,鼠标移动到主机或服务后面小太阳图标上就会显示相应的性能图表。
