Nagios 通过 NRPE 监控远程主机系统状况
刚刚时监控了一个远程主机提供的服务的状态,如果要监控远程主机的状态,如内存,磁盘占用等情况的话就需要用NRPE了,简单来说NRPE的工作原理是在远程主机端不安装nagios,只安装NRPE插件,插件中包含了check_mysql,check_disk等命令,在远程主机端执行这些命令以收集远程主机信息包从在远程主机的NRPE中,而nagios主机只需要执行check_nrpe命令连接远程主机的NRPE,并通过SSL加密通道传输主机数据,下面时实现方法:
远程主机设定
- yum install openssl openssl-devel gcc
- useradd nagios
- tar xzf nagios-plugins-1.4.15.tar.gz
- cd nagios-plugins-1.4.15
- ./configure --with-nagios-user=nagios --with-nagios-group=nagios
- make
- make install
- yum install xinetd -y
- tar zxf nrpe-2.12.tar.gz
- cd nrpe-2.12
- ./configure
- make all
- make install-plugin
- make install-daemon
- make install-daemon-config
- make install-xinetd
- vi /etc/xinetd.d/nrpe 修改如下行
- only_from = 192.168.0.71 #nagios 主机 ip 地址
- vi /etc/services 添加如下行
- nrpe 5666/tcp #nrpe 服务监听端口
- vi /usr/local/nagios/etc/nrpe.cfg 修改如下行
- command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p / #根分区监测
- service xinetd restart
- netstat -antlp |grep 5666 #看到如下输出说明 NRPE 正常启动,并确保防火墙允许此端口
- tcp 0 0 :::5666 :::* LISTEN 26359/xinetd
监控主机设定
- tar zxf nrpe-2.12.tar.gz
- cd nrpe-2.12
- ./configure --with-nagios-user=nagios --with-nagios-group=nagios
- make all
- make install-plugin
- /usr/local/nagios/libexec/check_nrpe -H 192.168.0.36
- NRPE v2.12 #看到这个说明可以使用了
vi /usr/local/nagios/etc/objects/commands.cfg 添加如下行
- # 'check_nrpe' command definition
- define command{
- command_name check_nrpe
- command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
- }
$ARG1$表示第一个参数
vi /usr/local/nagios/etc/objects/services.cfg 添加如下行
- define service{
- use generic-service
- host_name mysql主机
- service_description CPU Load
- check_command check_nrpe!check_load
- }
- define service{
- use generic-service
- host_name mysql主机
- service_description Current Users
- check_command check_nrpe!check_users
- }
- define service{
- use generic-service
- host_name mysql主机
- service_description / Free Space
- check_command check_nrpe!check_disk
- }
- define service{
- use generic-service
- host_name mysql主机
- service_description Total Processes
- check_command check_nrpe!check_total_procs
- }
- define service{
- use generic-service
- host_name mysql主机
- service_description Zombie Processes
- check_command check_nrpe!check_zombie_procs
- }
- /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
- service nagios reload
Nagios 整合飞信
nagios可以通过短信发出告警,在企业中有专门的短信平台,此处我们拿飞信做实验,但是在生产环境中不推荐。
http://www.it-adv.net/ 下载 fetion 主程序与支持库
>> 使用说明 <<
以下参数提供登录用的账号密码(三种方式,手机号-密码 飞信号-密码 文件--索引)
--mobile=[手机号]
--sid=[飞信号]
--pwd=[密码]
--config=[文件名]
--index=[索引号]
登录手机号
登录飞信号
登录密码
存储手机号、密码的文件。
索引
以下参数提供接收者
--to=[手机号/飞信号/URI] 接收消息的手机号/飞信号/URI.如果知道对方 URI,则只需自己在对方
好友列表,无需对方在自己好友列表就能发送,支持多个号码,中间用,逗号分隔
--msg-utf8=[信息] 发送的消息,UTF8 编码
--msg-gb=[信息]
发送的消息,GB 编码
--file-utf8=[文件 utf8 格式] 发送文件内容
--file-gb=[文件 gb 格式]
发送文件内容
--msg-type=[0/1/2] 发送消息类型:普通消息 长消息 智能短信
--query-cmcc-no
查询移动公司手机段
以下为可选项
--debug
显示调试信息
--hide
隐身登录
--exit-on-verifycode
服务器需要进行图形验证时,程序退出(1)或者等待用户手工输入识别信息(程序默认)
--proxy-ip=http
--proxy-port=http
代理 ip
代理端口
(机器人需要 HTTP CONNECT 代理,大家常用的 ccproxy 是支持的)
>> 举例 <<
linux 下,请使用如下命令:
LD_LIBRARY_PATH=. ./fetion --mobile=手机号 --pwd=密码 --to=接收者手机号 --msg-utf8=测试
LD_LIBRARY_PATH=. ./fetion --sid=飞信号 --pwd=密码 --to=接收者手机号 --msg-utf8=测试
LD_LIBRARY_PATH=. ./fetion --config=sample.conf --index=1 --to= 接收者手机号 --msg-utf8=测试
发送消息中如果需要换行,请用 \n
// 以下为 sample.conf 内容,文件内容中, #号为注释行
# This config file is for fetion robot tool.
# Usage demo: ./fetion --config=/etc/fetion.conf --index=1
# ID Mobile Password
1 13677098080 123456
新版提示:
1. 使用复杂的密码(数字+字母+符号),将不会弹出图形验证码(我测试时是这种表现)。
2. 第一次使用机器人时,即使是复杂密码,也可能会弹出图形验证码,以后将不再提示。
3. 如果您是使用其它程序调用本程序,请在运行时,指定--exit-on-verifycode=1,否则,程序将无限期等待。当���以该参数运行机器人时,当弹出图形验证码时,程序将以退出码 29 结束( 在 linux 中,通过$?获取,在 windows 中,通过 %ERRORLEVEL% 获取)
4. 请确保目录权限可写.
1) 安装飞信
cp fetion /usr/local/fetion
chown -R nagios /usr/local/fetion
tar zxf linuxso_20101113.tar.gz -C /usr/lib
注:由于发行版本不同,可能会覆盖您机器中的核心库,导致严重系统问题,在解压之前确保没有文件重复
创建运行 fetion 的脚本: fetion.sh
vi /usr/local/nagios/libexec/fetion.sh
/usr/local/fetion/fetion --mobile=136xxxxxxx --pwd=yakexi --to="$1" --msg-utf8="$2" > /dev/null
chown nagios.nagios /usr/local/nagios/libexec/fetion
chmod +x /usr/local/nagios/libexec/fetion
执行 fetion.sh 脚本看是否可以发送信息
注: 第一次调用飞信脚本时会要求你输入验证码,在飞信程序所在的 /usr/local/fetion 目录下会生成以你手机号命名的 png 图片文件,上面存放着验证码。
2) 增加 fetion 报警选项: templates.cfg
修改 /usr/local/nagios/etc/objects/templates.cfg
在 define contact{...} 部分,将以下两行:
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
改为:
service_notification_commands notify-service-by-email,service-notify-by-fetion
host_notification_commands notify-host-by-email,host-notify-by-fetion
3) 增加调用脚本: commands.cfg
修改 /usr/local/nagios/etc/objects/commands.cfg 在该文件的最后增加以下部分:
##### host-notify-by-fetion command definition
define command{
command_name host-notify-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$ alert -Host
$HOSTNAME$ is $HOSTSTATE$"
}
##### service-notify-by-fetion command definition
define command{
command_name service-notify-by-fetion
command_line $USER1$/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$:
$HOSTalias$/$SERVICEDESC$ is $SERVICESTATE$"
4) 修改联系人选项: contact.cfg
修改 /usr/local/nagios/etc/objects/contact.cfg
在 define contact{...} 部分增加如下一行
pager 136xxxxxxxx ;
#接收飞信信息的手机号码
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#校验后重新载入 nagios 配置
/etc/init.d/nagios reload
