同时使用vi编辑器末行模式的r功能读取当前目录下的localhost.cfg档,删除“HOST DEFINITION”和“HOST GROUP DEFINITION”部分。只保留“SERVICE DEFINITION”部分并修改为下面的内容:
第一个命令定义:
通过check_ping脚本确保监测主机和被监测主机的连通性,如果丢包率到达20%则报warning,到达60%则报critical:
use local-service ; Name of service template to use
host_name 192.168.1.220
service_description PING REMOTE HOST
check_command check_ping!100.0,20%!500.0,60%
}
第二个命令定义:
监测远程主机根分区磁盘状况,如果可用空间低于20%会报Warning,如果可用空间低于10%则报Critical:
use local-service ; Name of service template to use
host_name 192.168.1.220
service_description Root Partition of Remote Server
check_command check_local_disk!20%!10%!/
}
第三个命令定义:
监测远程主机当前的登录用户数量,如果大于20用户则报warning,如果大于50则报critical:
use local-service ; Name of service template to use
host_name 192.168.1.220
service_description Current Users of Remote Server
check_command check_local_users!20!50
}
第四个命令定义:
监测远程主机当前的进程总数,如果大于250进程则报warning,如果大于400进程则报critical:
use local-service ; Name of service template to use
host_name 192.168.1.220
service_description Total Processes of Remote Machine
check_command check_local_procs!250!400!RSZDT
}
第五个命令定义:
监测远程主机当前的本地负载量:
use local-service ; Name of service template to use
host_name 192.168.1.220
service_description Current Load of Remote Machine
check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
第六个命令定义:
监测远程主机swap文件系统使用量,如果swap可用空间低于20%则报warning,低于10%则报critical:
use local-service ; Name of service template to use
host_name 192.168.1.220
service_description Swap Usage of Remote Server
check_command check_local_swap!20!10
}
第七个命令定义:
监测SSH连接可用性,但消息通知功能默认被关闭,因为并不是所有用户都有权限SSH。
use local-service ; Name of service template to use
host_name 192.168.1.220
service_description SSH of Remote Machine
check_command check_ssh
notifications_enabled 0
}
# Define a service to check HTTP on the remote machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.
第八个命令定义:
监测远程主机上的HTTP服务,但同SSH,该服务的消息通知功能默认关闭。
use local-service ; Name of service template to use
host_name 192.168.1.220
service_description HTTP of Remote Machine
check_command check_http
notifications_enabled 0
}
保存该档后,按照其它cfg文件的权限和属性为该文件指定所属用户和组:
# chown nagios.nagios /usr/local/nagios/etc/objects/remotehosts.cfg
至于想定义的其它内容,我就不再向该档中添加了,我想大家应该已经掌握了这种命令定义的方法了。
最后不要忘了一步关键的操作——在主配置文件中定义Nagios启动之后读取刚才修改的这些配置,也就是确保刚才修改的配置文件在nagios主配置文件/usr/local/nagios/etc/nagios.cfg中都有正确指定,信息如下:
cfg_file=/usr/local/nagios/etc/objects/remotehosts.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
最后校验配置文件正确性:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果校验完全通过,则重启Nagios服务:
# service nagios restart
此时如果再通过浏览器访问http://192.168.1.10/nagios,我们就可以看到被监测主机192.168.1.220上所反应出来的内容信息。
到此为止,Nagios的基本原理和强大的功能就基本介绍完了。而事实上Nagios不但能在现有的功能基础上实现功能扩展,而且还能够实现和第三方软件的结合,例如和前面所介绍的Mrtg和Cacti联用来构建动态显示图标;同时按照前面所说明的内容可以配置各种事件级别的邮件通知功能。但因为篇幅的限制我们会在下次有机会的时候向大家介绍。尽管在配置的难度上显得比较高,但是其强大的功能和灵活性却给我们留下了极深的印象。因此这也是在一些中型甚至是大型企业中所推崇并逐步采用的一种监测方案。