nagios 报警发送邮件
以下就来介绍一下sendEmail这个软件的使用.
sendEmail的主页http://caspian.dotconf.net/menu/Software/SendEmail/
下载地址http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.55.tar.gz
软件十分小,是一个通过命令来发smtp邮件的程序.安装也十分简单(查看其README文件即可).
解压缩tar –zxvf sendEmail-v1.55.tar.gz
cd sendEmail-v1.55
将可执行程序复制cp sendEmail /usr/local/bin
然后给确认确实它具有执行权限
ll /usr/local/bin/sendEmail
-rwxr-xr-x 1 root root 77882 11-03 14:23 /usr/local/bin/sendEmail
这样程序就装好了,使用也很简单.直接运行sendEmail就会显示详细的用法
先看一个典型的例子
/usr/local/bin/sendEmail –f nagios@test.com –t yahoon@test.com –s mail.test.com –u “from nagios” –xu nagios –xp p#3isoda –m happy
解释:
-f 表示发送者的邮箱
-t 表示接收者的邮箱
-s 表示SMTP服务器的域名或者ip
-u 表示邮件的主题
-xu 表示SMTP验证的用户名
-xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)
-m 表示邮件的内容
-cc 表示抄送
-bcc 表示暗抄送
如果你不带-m参数的话,就会提示你自行输入
Reading message body from STDIN because the ‘-m’ option was not used.
If you are manually typing in a message:
- First line must be received within 60 seconds.
- End manual input with a CTRL-D on its own line
输入完成后使用CTRL-D来结束
当然我们也可以将一个文件的内容作为邮件的正文发出去的
那么就可以使用:
cat 文件名 | /usr/local/bin/sendEmail –f nagios@test.com –t yahoon@test.com –s mail.test.com –u “from nagios” –xu nagios –xp p#3isoda
--------------------------------------------------------------------------
2. 修改commands.cfg, 替换掉原来发送邮件的两个命令
默认的输出宏$HOSTOUTPUT$和$SERVICEOUTPUT$只会输出第一行信息, 我们使用$LONGHOSTOUTPUT$和$LONGSERVICEOUTPUT$输出完整信息.
注意: 为了能接受到格式化良好, 且中文无乱码的超文本邮件, 可以对sendEmail设置 "-o message-content-type=html -o message-charset=utf8" 选项.
# vi /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name notify-host-by-email
command_line /usr/local/bin/sendEmail -f 手机号@139.com -t $CONTACTEMAIL$ -s smtp.139.com:25 -u "** Host Alert:$HOSTALIAS$ **" -m "$HOSTNAME$($HOSTALIAS$)的状>态异常,请及时处理!" -o message-content-type=html -o message-charset=utf8 -xu 手机号@139.com -xp 邮箱密码
}
# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
command_line /usr/local/bin/sendEmail -f 手机号@139.com -t $CONTACTEMAIL$ -s smtp.139.com:25 -u "** Service Alert:$HOSTALIAS$ **" -m "$HOSTNAME$($HOSTALIAS$)的监控项 $SERVICEDESC$ 状态为$SERVICESTATE$,请及时处理!" -o message-content-type=html -o message-charset=utf8 -xu 手机号@139.com -xp 邮箱密码
}
-------------------------------------------------------------------
3.配置接收nagios告警的邮件地址
# vi /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name 联系人名称
use generic-contact
alias 联系人别名
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email 手机号@139.com
}
define contact{
contact_name 联系人名称
use generic-contact
alias 联系人别名
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email 手机号@139.com
}
define contactgroup{
contactgroup_name system
alias system
members 联系人1,联系人2
}
-------------------------------------------------------------------------
4.自定义需要监控的主机和相关服务
检测主机是否宕机
例如:
define host{
use windows-server ; Inherit default values from a template
host_name XX服务器 ; The name we're giving to this host
alias 192.168.12.216 ; A longer name associated with the host
address 192.168.12.216 ; IP address of the host
添加如下语句
check_command check-host-alive
max_check_attempts 1 ;最大重复检测失败次数
contact_groups system ;通知发送组
normal_check_interval 2 ;正常命令检测频率 2分钟
notification_interval 0 ;警报发送频率,0为只发送一次,10为每隔10分钟发送一次
}
检测相关服务
例如:
define service{
use generic-service
host_name XX服务器
service_description XX进程
check_command check_nt!PROCSTATE!-d SHOWALL -l OAAdmin.exe
contact_groups system ;选择需要发送警告的联系组
notification_interval 0 ;选择重复发送警告的频率,0为发送一次,10为10分钟发送一次!
}
-----------------------------------------------------------------------------
5. 修改完主配置文件后, 都需要重启Nagios服务, 重启前先验证配置文件是否有误
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# service nagios reload
如果有问题则查询Nagios运行日志, 进行进一步诊断:
#tail -50f /usr/local/nagios/var/nagios.log
参考地址http://61898.blog.51cto.com/51898/1180769
十佳人物