cat libexec/set_to_weixin.sh
#!/bin/bash
http_proxy=IP:808
https_proxy=IP:808 内网代理需配置,外网环境忽略
export http_proxy
export https_proxy
get_access_token () {
curl -s https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww****e042\&corpsecret=Ss*****znLQ >/tmp/$$.txt
access_token=`awk -F "\"" '{print $10}' /tmp/$$.txt`
}
sed_to_weixin () {
curl https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token -d "{ \
\"toparty\": \"2\", \
\"msgtype\": \"text\", \
\"agentid\": 1000002, \
\"text\": { \
\"content\": \"$1\" \
}, \
\"safe\":\"0\" \
}"
}
main () {
get_access_token
sed_to_weixin "$*"
}
main "$*"
cat commands.cfg
# For weixin
define command{
command_name notify-host-by-weixin
command_line /usr/local/nagios/libexec/set_to_weixin.sh "$NOTIFICATIONTYPE$-$HOSTNAME$\n$HOSTALIAS$\nSTATE--$HOSTSTATE$\n$HOSTADDRESS$--$HOSTOUTPUT$\n$CONTACTALIAS$ $LONGDATETIME$"
}
cat timeperiods.cfg
define timeperiod{
name china
timeperiod_name china
alias china
january 1 00:00-00:00 ; 1.1
may 1 00:00-00:00 ; 5.1
october 1 00:00-00:00 ; 10.1
monday 08:00-18:00
tuesday 08:00-18:00
wednesday 08:00-18:00
thursday 08:00-18:00
friday 08:00-18:00
saturday 08:00-18:00
}
# vim nagios.cfg
添加下边内容
cfg_file=/usr/local/nagios/etc/objects/escalations.cfg
新建 escalations.cfg
define hostescalation{
host_name IP ; 被监控主机名称,与hosts.cfg中一致,多个主机以逗号隔开
first_notification 2 ; 第2条信息起,改变频率间隔
last_notification 0 ; 第n条信息起,恢复频率间隔
notification_interval 600 ; 通知间隔(10小时)
contact_groups admins ; 联系组
}
cat templates.cfg | grep host_notification_commands
host_notification_commands notify-host-by-email,notify-host-by-weixin
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 检查问题
restart OK