define host{
name linux-server ; The name of this host template
use generic-host ; This template inherits other values from the generic-host template
check_period 24x7 ; By default, Linux hosts are checked round the clock
check_interval 5 ; Actively check the host every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each Linux host 10 times (max)
check_command check-host-alive ; Default command to check Linux hosts
notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day
; Note that the notification_period variable is being overridden from
; the value that is inherited from the generic-host template!
notification_interval 120 ; Resend notifications every 2 hours
notification_options d,u,r ; Only send notifications for specific host states
contact_groups admins ; Notifications get sent to the admins by default
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}
这里的信息后面都有说明,把他们拿上谷歌翻译一下便是,
其中check_period 这个是定义监控的时间
check_interval这个是每多少时间执行一次命令
max_check_attempts这个是多少次异常就会报警
这几个便是常用的配置
回到刚才的例子,然后define service就是定义要监控的东西
host_name 就是被监控机的IP
service_description 命令的描述,会在nagios主面上显示
check_command 要执行的命令
max_check_attempts 5 异常多少次会报警
normal_check_interval 1 每1分钟执行一次命令
check_command 这个是根据什么来填写的呢,其实就是根据command.cfg这份配置来填写,打开这份文件找一个ping的命令来看看
define command{
command_name check_ping
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5
}
这份是定义ping命令的配置,很简单,一个名称,然后就是要执行的东西
-w就是警告的值
-c就是严重的值
要看看命令怎么使用很简单
/usr/local/nagios/libexec/check_ping -H 192.168.1.211 -w 100,20% -c 200,40%
这样执行下去,可看见返来的信息:
PING OK - Packet loss = 0%, RTA = 0.54 ms|rta=0.543000ms;100.000000;200.000000;0.000000 pl=0%;20;40;0
那么很明显
执行命令是这样的
/usr/local/nagios/libexec/check_ping -H 192.168.1.211 -w 100,20% -c 200,40%
而写在配置文件上是这样的:
check_ping!100.0,20%!200.0,50%
check_ping后面是要跟参数的,在写配置文件的时候参数是要用“!”这个符号来隔开,要注意顺序
明白这个之后,面后都差不多了
现在我们重启nagios来看看效果,养成习惯重启前先检查配置文件有没有错误
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
如果出现如下信息就是正常:
Total Warnings: 0
Total Errors: 0
现在可以安心重启服务了
service nagios restart
然后在nagios主页上会看见我们配置的东西出来了:
我的被监控机上没有安装http服务,所以报警了
(未完……太累了睡觉去,明天再写)
回来继续配置!
4、利用nrpe监控linux主机的本地信息
首先是解决nagios怎么去调用nrpe命令,之前我们配置时有说过nagios调用的命令是从command.cfg文件中定义过的,那么要使用nrpe也须要在该文件中定义
vi command.cfg
在文件中添加以下配置:
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
在添加监控信息之前我们先测试一下监控主机与被监控机的Nrpe的连通性
首先在被监控机上配置允许的监控主机:
vi nrpe.cfg
allowed_hosts=127.0.0.1,192.168.1.210 #添加上我的监控主机的IP
然后在被监控机上启动nrpe服务:
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
查看端口是否正常运行:
[root@localhost ~]# netstat -tunlp | grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 4304/nrpe
