Nagios钉钉报警配置
PS:添加钉钉报警需要在钉钉创建群及接收信息机器人;
具体操作可以参考官方文档:https://open.dingtalk.com/document/org/create-orgapp
配置好机器人后可以通过命令测试一下
[root@june ~]# curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxx' -H 'Content-Type: application/json' -d '{"msgtype": "text",
"text": {
"content": "钉钉机器人群消息测试"
}
}'
1.添加报警信息脚本
此脚本需放到nagios对应的libexec目录下,此文档目录为/usr/local/nagios/libexec
#此脚本来源于强大的度娘。
[root@june libexec]# vim notify_dingtalk_rebot
#!/bin/bash
#define receive parameter values
notifyToken=$1
notifyActionType=$2
notifyHostAlias=$3
notifyServiceDesc=$4
notifyServiceState=$5
notifyType=$6
notifyService=$5
notifyHost=$3
notifyAddress=$9
notifyState=$6
notifyInfo=$7
#define dingtalk config
dingtalkUrl="https://oapi.dingtalk.com/robot/send"
#define notify msg template
msgTopic="'***** Nagios *****'"
msgService="Service: ${notifyService}"
msgHost="Host: ${notifyHost}"
msgState="Status: ${notifyState}"
msgDate="Date/Time: `date '+%Y-%m-%d %H:%M:%S'`"
msgInfo="Additional Info:\\n\\n${notifyInfo}"
msgTemplate=${msgTopic}\\n\\n${msgService}\\n${msgHost}\\n${msgState}\\n\\n${msgDate}\\n\\n${msgInfo}
#define dingtalk send msg template
dingtalkMsgTemplate='{"msgtype": "text","text": {"content": "<DING_TALK_MSG_CONTENT>"}}'
#dingtalk rebot send group message
function dingtalkRebotSendMsg(){
notifyMsg=$1
sendMsg=${dingtalkMsgTemplate/<DING_TALK_MSG_CONTENT>/$notifyMsg}
sendUrl="${dingtalkUrl}?access_token=${notifyToken}"
curl $sendUrl -H 'Content-Type: application/json' -d "$sendMsg"
}
dingtalkRebotSendMsg "$msgTemplate"
[root@june libexec]# chmod 755 notify_dingtalk_rebot
[root@june libexec]# chown -R nagios.nagios notify_dingtalk_rebot
2.修改nrpe配置文件
[root@june etc]# vim nrpe.cfg
......
command[notify_dingtalk_rebot]=/usr/local/nagios/libexec/notify_dingtalk_rebot
3.添加用户组
[root@june objects]# vim contacts.cfg
......
define contact{
contact_name 用户名
use generic-contact
alias Nagios Admin
pager 钉钉token号
}
......
define contactgroup{
contactgroup_name 用户组名
alias Nagios Administrators
members 用户名
}
4.添加可执行命令
[root@june objects]# vim commands.cfg
......
define command{
command_name notify-service-by-dingtalk-rebot
command_line $USER1$/notify_dingtalk_rebot "$CONTACTPAGER$" "$NOTIFICATIONTYPE$" "$HOSTALIAS$" "$HOSTADDRESS$" "$SERVICEDESC$" "$SERVICESTATE$" "$SERVICEOUTPUT$"
}
define command{
command_name notify-host-by-dingtalk-rebot
command_line $USER1$/notify_dingtalk_rebot "$CONTACTPAGER$" "$NOTIFICATIONTYPE$" "$HOSTALIAS$" "$HOSTADDRESS$" "down" "down" "down"
}
5.修改模板文件
[root@june objects]# vim templates.cfg
......
define contact{
name generic-contact
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,notify-service-by-dingtalk-rebot
host_notification_commands notify-host-by-email,notify-host-by-dingtalk-rebot
register 0
}
6.修改监控信息配置文件
[root@june objects]# vim dx_disk.cfg
define service{
host_name 192.168.1.11
service_description 磁盘监控
check_period 24x7
max_check_attempts 1
normal_check_interval 2
retry_check_interval 1
contact_groups admins,xxx #添加组名
notification_interval 60
notification_period 24x7
notification_options w,u,c,r
check_command check_nrpe!check_disks
}
7.重启服务
#重启nrpe和nagios服务
[root@bj-1-11 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#查看下配置文件是否有误
[root@bj-1-11 objects]# systemctl restart nagios
[root@bj-1-11 objects]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d