- 论坛徽章:
- 3
|
5)修改联系人组
[root@nagios objects]# vi contacts.cfg
define contact{
contact_name nagiosadmin ; Short name of user //联系人命名
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email nagios@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
###############################################################################
###############################################################################
#
# CONTACT GROUPS
#
###############################################################################
###############################################################################
# We only have one contact in this simple configuration file, so there is
# no need to create more than one contact group.
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin
}
6)新建hosts.cfg,定义被监控主机和主机组
[root@tianway objects]#vi hosts.cfg
define host{ //定义主机
host_name 192.168.0.2 //定义主机名,可起任意名字
address 192.168.0.2 //主机名对应的ip 地址
notifications_enabled 1 ; Host notifications are enabled //主机状态通知功能激活
event_handler_enabled 1 ; Host event handler is enabled //主机事件处理激活
notification_period 24x7 ; Send host notifications at any time //发送通知的时间段
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 24x7 ; 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 5 ; Resend notifications every 2 hours //发送通知的时间间隔
notification_options d,u,r ; Only send notifications for specific host states //定义那
些情况下发送通知,
; 具体含义见contacts.cfg 部分
contact_groups admins ; Notifications get sent to the admins by default //发送通知给谁
}
通过复制粘贴修改就可以增加192.168.0.3、192.168.0.4 的定义。
define hostgroup{ //定义主机组
hostgroup_name tianway.net ; The name of the hostgroup //定义主机组的名字
alias Linux Server ; Long name of the group //别名
members 192.168.0.2,192.168.0.3,192.168.0.4 ; Comma separated list of hosts that belong to this group//组成员,
使用逗号分隔
}
使用简单的复制和修改即可增加192.168.0.3、192.168.04 的服务定义。
重启nagios 服务
[root@nagios ~]# service nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
点击nagios 界面中的host detail 就可以看到下图
3、使用插件监控更多的服务和信息
本章的开头部分就提到,nagios 本身并不提供任何的监控功能,所有的监控任
务都是由插件来完成。nagios 安装部分已经讲完如何安装nagios 插件,所谓插件就是
用来完成特定功能的小程序,默认的安装路径是/usr/local/nagios/libexec。现在我
们查看一下有哪些可供使用的插件
[root@nagios libexec]# ls
check_apt check_flexlm check_mailq check_overcr check_swap
check_breeze check_ftp check_mrtg check_ping check_tcp
check_by_ssh check_http check_mrtgtraf check_pop check_time
check_clamd check_icmp check_nagios check_procs check_udp
check_cluster check_ide_smart check_nntp check_real check_ups
check_dhcp check_ifoperstatus check_nntps check_rpc check_users
check_dig check_ifstatus check_nt check_sensors check_wave
check_disk check_imap check_ntp check_simap negate
check_disk_smb check_ircd check_ntp_peer check_smtp urlize
check_dns check_jabber check_ntp_time check_spop utils.pm
check_dummy check_load check_nwstat check_ssh utils.sh
check_file_age check_log check_oracle check_ssmtp
所有程序都是可以执行的,具体的用法可以使用“命令名 -h”进行查看。
以上所有的命令并不能直接使用,nagios 所调用的命令来源于commands.cfg,这些插件和nagios 所调用的命令有什么关系呢?我们打开
commands.cfg 文件,看看之前用到过 的check-host-alived 和check-ssh 命令。
define command{
command_name check-host-alive //定义可被nagios 调用的命令
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5 //定义插件路径和参数设置,
$USER1$表示插件的安装路径,在
//resources.cfg 文件中定义,
$HOSTADDRESS$表示主机地址
}
define command{
command_name check_ssh //定义可被nagios 调用的命令
command_line $USER1$/check_ssh $ARG1$ $HOSTADDRESS$ //定义插件路径和参数设置
}
新建services.cfg,定义被监控的服务器
[root@tianway ~]# vi /usr/local/nagios/etc/objects/services.cfg
define service{
host_name nagios.tianway.net //定义主机名
Service_description check-ssh //定义服务名
check_command check_ssh //指定检查服务使用的命令
notifications_enabled 1 ; Service notifications are enabled //服务通知
event_handler_enabled 1 ; Service event handler is enabled //时间处理激活
notification_period 24x7 ; Send host notifications at any time //发送通知的时间段
check_period 24x7 ; The service can be checked at any time of the day //检查的
时间段
max_check_attempts 3 ; Re-check the service up to 3 times in order to determine its
final (hard) state
normal_check_interval 5 ; Check the service every 10 minutes under normal conditions
//检查频率
retry_check_interval 1 ; Re-check the service every two minutes until a hard state
can be determined
contact_groups admins ; Notifications get sent out to everyone in the 'admins' group
//联系人组
notification_options w,u,c,r ; Send notifications about warning, unknown, critical, and
recovery events
//定义哪些情况发送通知
notification_interval 5 ; Re-notify about service problems every 5min //发送通知的
时间间隔
}
define service{
host_name nagios.tianway.net //定义主机名
Service_description check-ssh //定义服务名
check_command check_http //指定检查服务使用的命令
notifications_enabled 1 ; Service notifications are enabled //服务通知
event_handler_enabled 1 ; Service event handler is enabled //时间处理激活
notification_period 24x7 ; Send host notifications at any time //发送通知的时间段
check_period 24x7 ; The service can be checked at any time of the day //检查的
时间段
max_check_attempts 3 ; Re-check the service up to 3 times in order to determine its
final (hard) state
normal_check_interval 5 ; Check the service every 10 minutes under normal conditions
//检查频率
retry_check_interval 1 ; Re-check the service every two minutes until a hard state
can be determined
contact_groups admins ; Notifications get sent out to everyone in the 'admins' group
//联系人组
notification_options w,u,c,r ; Send notifications about warning, unknown, critical, and
recovery events
//定义哪些情况发送通知
notification_interval 5 ; Re-notify about service problems every 5min //发送通知的
时间间隔
}
按照同样的办法定义另外三个主机的服务监控
点击nagios 界面中的host detail 就可以看到下图
4、使用NRPE 监控远程服务器的本地信息
现在我们已经知道如何使用插件监控本地和远程服务器的外部信息,但是我们在运维工作中需要对服务器的本
地信息进行监测,确定是否存在安全隐患,例如:load average、内存使用情况、磁盘使用率的变化等等。如何才能获
取到这些信息呢?我们使用NRPE 来实现,使用NRPE 进行远程本地信息监控的时候监控架构图如下:
1) 现在我们对NRPE 做一个简单的介绍
NRPE 作为Nagios 的一个附加组件出现,是Nagios 解决方案中一个很重要的组成部分,它可以使你在远程执行Nagios
的插件,从而获取到远程服务器上的信息(例如:CPU load、内存使用情况等)。这些资源是不会暴露给外部机器的蓑衣
必须在远程服务器上安装一个agent 才能完成信息收集的功能。当然使用SSH 远程登陆之后再收集信息也是一种可行的方
案,但是随着监控设备数量上升,这种方式会占用监控主机和被监控机的大量资源,对正常的业务系统会带来不利影响,
甚至导致服务器过载,因此最终这种思路被放弃。
NRPE 由两个部分组成:工作在监控机一侧的check_nrpe 插件、工作在被监控机一侧的NRPE 守护进程
Nagios 服务器执行check_nrpe 插件并告诉他检查哪个服务,check_nrpe 插件通过SSL 连接方式联系远程服务器上
的NRPE 守护进程,NRPE 守护进程执行相应的插件完成指定的检查,并返回结果
注意:Nagios 插件必须安装在被监控的远程机器上,否则NRPE 将无法实现服务监控
2) NRPE 相关概念介绍
直接检测:从Nagios 可以直接检测到的外部服务或者使用NRPE 可以检测到的内部服务,称之为直接检测
间接检测:某些情况下Nagios 服务器可能不能直接访问到被监控的机器,这种情况下就可以使用NRPE 作为中间代理去访
问远程服务器,并将结果返回给Nagios,这种检测办法称为间接检测。
3) NRPE 安装
使用NRPE 你需要分别在监控机和被监控机上分别安装对应的软件。安装使用NRPE 的前提条件是你的系统必须支持TCP
wrappers 以及xinetd 超级守护进程,大多数Linux 发行版都自带这些功能。
监控机安装NRPE plugin
监控机上不需要安装完整的NRPE,只要安装NRPE plugin 就可以了
[root@nagios ~]# tar xzvf nrpe-2.12.tar.gz
[root@nagios ~]# cd nrpe-2.12
[root@nagios nrpe-2.12]# ./configure
[root@nagios nrpe-2.12]# make all
[root@nagios nrpe-2.12]# make install-plugin
被监控机安装
被监控机需要安装nagios 插件和NRPE
[root@nagios ~]# /usr/sbin/useradd nagios //增加nagios 用户
[root@nagios ~]# tar xzvf nagios-plugins-1.4.13.tar.gz //解压缩nagios 插件安装包
[root@nagios ~]# cd nagios-plugins-1.4.13
[root@nagios nagios-plugins-1.4.13]#./configure --prefix=/usr/local/nagios/ //编译安
装
[root@nagios nagios-plugins-1.4.13]#make
[root@nagios nagios-plugins-1.4.13]#make install
[root@nagios ~]# tar xzvf nrpe-2.12.tar.gz //解压缩NRPE
[root@nagios nrpe-2.12]#./configure --prefix=/usr/local/nagios/
[root@nagios nrpe-2.12]#make all
[root@nagios nrpe-2.12]#make install-plugin
[root@nagios nrpe-2.12]#make install-daemon
[root@nagios nrpe-2.12]#make install-daemon-config
[root@nagios nrpe-2.12]#make install-xinetd //NRPE 安装为xinetd 服务
编辑/etc/xinetd.d/nrpe 文件,允许监控机访问被监控机上的这个服务,增加监控机的地址
only_from = 127.0.0.1 192.168.0.1 //多个地址之间使用空格相隔
修改/etc/services 文件,增加如下一行
nrpe 5666/tcp # NRPE
重启 xinetd 服务
[root@nagios ~]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
修改iptables,开放NRPE 的端口
[root@nagios ~]# iptables -t filter -I INPUT -p tcp --dport 5666 -j ACCEPT //开放NRPE 的5666
端口
修改commands.cfg 文件,增加check_nrpe 的定义
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
4) NRPE 测试
安装完成了,我们进行一个测试
在被监控机上确认服务是否启动
[root@nagios ~]# netstat -at|grep nrpe
tcp 0 0 *:nrpe *:* LISTEN
服务已经正常启动,如果输出结果与此不一致,请检查/etc/services 文件是否按照要求进行了修改,xinetd
服务是否运行正常
在监控机上测试是被监控机的NRPE 工作是否正常
[root@nagios ~]# /usr/local/nagios/libexec/check_nrpe -H web1.tianway.net
NRPE v2.12 //NRPE 版本信息,说明NRPE 工作正常
[root@nagios ~]# /usr/local/nagios/libexec/check_nrpe -H web1.tianway.net -c check_load
OK - load average: 0.01, 0.00, 0.00|load1=0.010;15.000;30.000;0; load5=0.000;10.000;25.000;0;
load15=0.000;5.000;20.000;0;
//能够正常获取信息,NRPE 工作正常
5) NRPE 配置
NRPE 的配置文件位于/usr/local/nagios/etc/nrpe.cfg,NRPE 调用的命令必须在配置文件中定义,默认的配置文件中定
义了如下命令:
command[check_users]=/usr/local/nagios//libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios//libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios//libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios//libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios//libexec/check_procs -w 150 -c 200
也就是默认的配置我们只能检查很少的几个资源使用情况,为了能够监控到更多的资源,我们可以自己对配置
文件进行修改,增加可使用的命令,如:增加对交换空间的监控命令就在配置文件中增加如下几行就可以了
command[check_total_procs]=/usr/local/nagios//libexec/check_swap -w 1024000000 -c 800000000
6) 完成服务配置文件
[root@nagios ~]# vi /usr/local/nagios/etc/objects/services.cfg
增加远程服务器磁盘和负载的监控,以web1.tianway.net 在/usr/local/nagios/et c/services.cfg 文件中增加如
下内容
define service{
host_name web1.tianway.net
service_description check-load
check_command check_nrpe!check_load
notifications_enabled 1 ; Service notifications are enabled
event_handler_enabled 1 ; Service event handler is enabled
notification_period 24x7 ; Send host notifications at any time
check_period 24x7 ; The service can be checked at any time of the
day
max_check_attempts 3 ; Re-check the service up to 3 times in order to
determine its final (hard) state
normal_check_interval 5 ; Check the service every 10 minutes under
normal conditions
retry_check_interval 1 ; Re-check the service every two minutes until
a hard state can be determined
contact_groups admins ; Notifications get sent out to everyone in the
'admins' group
notification_options w,u,c,r ; Send notifications about warning, unknown,
critical, and recovery events
notification_interval 5 ; Re-notify about service problems every 5min
}
define service{
host_name web1.tianway.net
service_description check-disk
check_command check_nrpe!check_hda1
notifications_enabled 1 ; Service notifications are enabled
event_handler_enabled 1 ; Service event handler is enabled
notification_period 24x7 ; Send host notifications at any time
check_period 24x7 ; The service can be checked at any time of
the day
max_check_attempts 3 ; Re-check the service up to 3 times in order to
determine its final (hard) state
normal_check_interval 5 ; Check the service every 10 minutes under
normal conditions
retry_check_interval 1 ; Re-check the service every two minutes until a
hard state can be determined
contact_groups admins ; Notifications get sent out to everyone in
the 'admins' group
notification_options w,u,c,r ; Send notifications about warning, unknown,
critical, and recovery events
notification_interval 5 ; Re-notify about service problems every 5min
}
check_nrpe 后边指定在被监控机上执行的命令,使用!进行分隔,该命令必须是在nrpe.cfg 文件中进行定义过的方可使
用。
依次添加其他服务器的监控项,完成后重启nagios。在浏览器页面上点击Service detail,可以看到下图
5、报警
服务器监控设置已经完成,可以从nagios 的监控页面上看到监控的结果了,但是怎样才能在服务器出现故障的时候及时通
知到维护人员呢?nagios 常见的报警方式有飞信报警、短信报警、邮件报警、QQ 报警、MSN 报警等等。我们重点讲述一
下邮件报警、飞信报警。
1) 邮件报警
邮件报警是一种广为使用的报警方式,具有成本低廉,性能稳定等优势,我们采用系统自带的sendmail 作为我们的邮件
服务器。
sendmail 安装配置
[root@nagios RPMS]# rpm -ivh sendmail-8.13.1-3.RHEL4.5.i386.rpm //安装系统自
带的sendmail 版本
[root@nagios ~]# service sendmail start //启动
sendmail 服务
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
[root@nagios ~]# echo test |sendmail -v address@domainname.com //发送测试邮
件
如果能正常收到测试邮件,就进行下一步
邮件报警配置
如果设置正常,发生故障时nagios 就会发送邮件给指定的E-mail 信箱。该邮件地址在contacts.cfg 定义联
系人的时候指定
2) 飞信报警
发生故障时nagios 发送邮件到指定的信箱是一个可行的通知办法,但是对于关键的业务服务而言,这样的报警方式显然
不能满足需要。对于关键业务的运行情况,我们需要在发生故障后的第一时间获取到相关信息,甚至希望能够在故障发生
前就进行处理。例如对磁盘空间的监控可以在磁盘被占满之前发出预警,是我们有足够的时间进行处理。综合各种情况,
最合适的关键业务报警方式无非是短信、电话两种方式。短信及时性好、花费低廉对终端要求低等各种优势,往往成为最
佳的选择。对于大企业用户而言,可以使用短信网关来发送端新,对于小型企业可以购买短信猫来实现相同的功能,对于
不原意花钱又想用短信报警的用户,飞信就成为最好的选择(139 邮箱也可以实现类似的功能但效果较差)。
飞信客户端安装
[root@localhost ~]# wget http://www.it-adv.net/fetion/lib_lin_32.tar.gz //下载需要
的库文件
[root@localhost ~]# tar xzvf lib_lin_32.tar.gz //解压缩
[root@localhost ~]# cd library_linux32/
[root@localhost library_linux32]# cp * /lib/ //把解压缩
出来的文件cp 到/lib/目录下
[root@localhost library_linux32]# cp * /usr/lib/ //把解压缩
出来的文件cp 到/lib/目录下
[root@localhost ~]# wget http://www.it-adv.net/fetion/fetion_linux_20080402.tar.gz //下载飞信
安装文件
[root@localhost ~]# tar xzvf fetion_linux_20080402.tar.gz //解压缩
[root@localhost ~]# cp sms /usr/bin/ //复制到
/usr/bin 目录下
相关设置
修改commands.cfg 文件,为nagios 增加两个报警命令
[root@nagios objects]# vi commands.cfg //增加以下内容
define command{
command_name notify-service-by-sms //命令名称
command_line /usr/bin/sms -f 139000000 -p xxx -t $CONTACTPAGER$ -m "$HOSTNAME$ $SERVIC
EDESC$ is $SERVICESTATE$ on $TIME$ result is $SERVICEOUTPUT$" $CONTACTPAGER$
}
define command{
command_name notify-host-by-sms
command_line /usr/bin/sms -f 139000000 -p xxx -t $CONTACTPAGER$ -m $NOTIFICATIONTYPE$
Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTPAGER$
}
修改contacts.cfg,使用飞信作为报警方式
[root@nagios ~]# more /usr/local/nagios/etc/contacts.cfg
define contact{
contact_name sa
alias system admin
host_notification_period 24x7
service_notification_period 24x7
host_notification_options d,r,
service_notification_options c,w,r
service_notification_commands notify-service-by-sms
host_notification_commands notify-host-by-sms
pager 1390000000
}
配置文件修改完成后重启nagios 服务,使配置文件生效就可以使用飞信报警了 |
|