Munin alert/notification 没有被处决
Munin alert/notification's not being executed
我有一个自定义通知脚本,我想在发生关键事件时向其提供来自 munin 的数据。不幸的是,在遵循官方文档的同时,我无法让它工作。它自己的通知脚本已经过测试,并且在使用假数据从 shell 调用时工作正常。它的权限在 755 上,因此执行也不应该成为问题。因此可能没有调用联系挂钩。
我所做的是:
# /etc/munin/munin-conf.d/custom.cnf
[...]
contact.slack.command MUNIN_SERVICESTATE="${var:worst}" MUNIN_HOST="${var:host}" MUNIN_SERVICE="${var:graph_title}" MUNIN_GROUP=${var:group} /usr/local/bin/notify_slack_munin
contact.slack.always_send warning critical
contact.slack.text ${if:cfields \u000A* CRITICALs:${loop<,>:cfields ${var:label} is ${var:value} (outside range [${var:crange}])${if:extinfo : ${var:extinfo}}}.}${if:wfields \u000A* WARNINGs:${loop<,>:wfields ${var:label} is ${var:value} (outside range [${var:wrange}])${if:extinfo : ${var:extinfo}}}.}${if:ufields \u000A* UNKNOWNs:${loop<,>:ufields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}${if:fofields \u000A* OKs:${loop<,>:fofields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}
在这些行之上定义了节点和目录,它们工作正常。但是通知发不出去。你知道它可能是什么吗?
我刚刚使用了最初在 gist 上发现的相同脚本,并在修复了两个微不足道的错误后使其正常运行:
- 确保将联系人放在配置中主机树之前。我把它放在配置文件的末尾,直到我把它移到放置示例联系人的地方才调用它
- 确保 Munin 配置中的命令使用完整的文件名,因此如果您将脚本放在
/usr/local/bin/notify_slack_munin
中,然后检查文件是否没有文件扩展名
如 Munin Guide 中所写,查看 munin-limits.log 以了解发生了什么。
最后请注意,如果您有 contact.slack.always_send warning critical
它会重复推送通知,而不仅仅是严重性更改。
供参考(以防要点 link 刹车),调用 Slack webhook 的脚本如下(为了清楚起见略作修改):
#!/bin/bash
# Slack notification script for Munin
# Mark Matienzo (@anarchivist)
# https://gist.github.com/anarchivist/58a905515b2eb2b42fe6
#
# To use:
# 1) Create a new incoming webhook for Slack
# 2) Edit the configuration variables that start with "SLACK_" below
# 3) Add the following to your munin configuration before the host tree
# in the part where sample contacts are listed:
#
# # -- Slack contact configuration
# # notify_slack_munin.sh is the full file name
# contact.slack.command MUNIN_SERVICESTATE="${var:worst}" MUNIN_HOST="${var:host}" MUNIN_SERVICE="${var:graph_title}" MUNIN_GROUP=${var:group} /usr/local/bin/notify_slack_munin.sh
# # This line will spam Slack with notifications even if no state change happens
# contact.slack.always_send warning critical
# # note: This has to be on one line for munin to parse properly
# contact.slack.text ${if:cfields \u000A* CRITICALs:${loop<,>:cfields ${var:label} is ${var:value} (outside range [${var:crange}])${if:extinfo : ${var:extinfo}}}.}${if:wfields \u000A* WARNINGs:${loop<,>:wfields ${var:label} is ${var:value} (outside range [${var:wrange}])${if:extinfo : ${var:extinfo}}}.}${if:ufields \u000A* UNKNOWNs:${loop<,>:ufields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}${if:fofields \u000A* OKs:${loop<,>:fofields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}
SLACK_CHANNEL="#insert-your-channel"
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/insert/your/hookURL"
SLACK_USERNAME="munin"
SLACK_ICON_EMOJI=":munin:"
# If you want to test the script, you may have to comment this out to avoid hanging console
input=`cat`
#Set the message icon based on service state
if [ "$MUNIN_SERVICESTATE" = "CRITICAL" ]
then
ICON=":exclamation:"
COLOR="danger"
elif [ "$MUNIN_SERVICESTATE" = "WARNING" ]
then
ICON=":warning:"
COLOR="warning"
elif [ "$MUNIN_SERVICESTATE" = "ok" ]
then
ICON=":white_check_mark:"
COLOR="good"
elif [ "$MUNIN_SERVICESTATE" = "OK" ]
then
ICON=":white_check_mark:"
COLOR="good"
elif [ "$MUNIN_SERVICESTATE" = "UNKNOWN" ]
then
ICON=":question:"
COLOR="#00CCCC"
else
ICON=":white_medium_square:"
COLOR="#CCCCCC"
fi
# Generate the JSON payload
PAYLOAD="{\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"icon_emoji\": \"${SLACK_ICON_EMOJI}\", \"attachments\": [{\"color\": \"${COLOR}\", \"fallback\": \"Munin alert - ${MUNIN_SERVICESTATE}: ${MUNIN_SERVICE} on ${MUNIN_HOST}\", \"pretext\": \"${ICON} Munin alert - ${MUNIN_SERVICESTATE}: ${MUNIN_SERVICE} on ${MUNIN_HOST} in ${MUNIN_GROUP} - <http://central/munin/|View Munin>\", \"fields\": [{\"title\": \"Severity\", \"value\": \"${MUNIN_SERVICESTATE}\", \"short\": \"true\"}, {\"title\": \"Service\", \"value\": \"${MUNIN_SERVICE}\", \"short\": \"true\"}, {\"title\": \"Host\", \"value\": \"${MUNIN_HOST}\", \"short\": \"true\"}, {\"title\": \"Current Values\", \"value\": \"${input}\", \"short\": \"false\"}]}]}"
#Send message to Slack
curl -sX POST -o /dev/null --data "payload=${PAYLOAD}" $SLACK_WEBHOOK_URL 2>&1
我有一个自定义通知脚本,我想在发生关键事件时向其提供来自 munin 的数据。不幸的是,在遵循官方文档的同时,我无法让它工作。它自己的通知脚本已经过测试,并且在使用假数据从 shell 调用时工作正常。它的权限在 755 上,因此执行也不应该成为问题。因此可能没有调用联系挂钩。 我所做的是:
# /etc/munin/munin-conf.d/custom.cnf
[...]
contact.slack.command MUNIN_SERVICESTATE="${var:worst}" MUNIN_HOST="${var:host}" MUNIN_SERVICE="${var:graph_title}" MUNIN_GROUP=${var:group} /usr/local/bin/notify_slack_munin
contact.slack.always_send warning critical
contact.slack.text ${if:cfields \u000A* CRITICALs:${loop<,>:cfields ${var:label} is ${var:value} (outside range [${var:crange}])${if:extinfo : ${var:extinfo}}}.}${if:wfields \u000A* WARNINGs:${loop<,>:wfields ${var:label} is ${var:value} (outside range [${var:wrange}])${if:extinfo : ${var:extinfo}}}.}${if:ufields \u000A* UNKNOWNs:${loop<,>:ufields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}${if:fofields \u000A* OKs:${loop<,>:fofields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}
在这些行之上定义了节点和目录,它们工作正常。但是通知发不出去。你知道它可能是什么吗?
我刚刚使用了最初在 gist 上发现的相同脚本,并在修复了两个微不足道的错误后使其正常运行:
- 确保将联系人放在配置中主机树之前。我把它放在配置文件的末尾,直到我把它移到放置示例联系人的地方才调用它
- 确保 Munin 配置中的命令使用完整的文件名,因此如果您将脚本放在
/usr/local/bin/notify_slack_munin
中,然后检查文件是否没有文件扩展名
如 Munin Guide 中所写,查看 munin-limits.log 以了解发生了什么。
最后请注意,如果您有 contact.slack.always_send warning critical
它会重复推送通知,而不仅仅是严重性更改。
供参考(以防要点 link 刹车),调用 Slack webhook 的脚本如下(为了清楚起见略作修改):
#!/bin/bash
# Slack notification script for Munin
# Mark Matienzo (@anarchivist)
# https://gist.github.com/anarchivist/58a905515b2eb2b42fe6
#
# To use:
# 1) Create a new incoming webhook for Slack
# 2) Edit the configuration variables that start with "SLACK_" below
# 3) Add the following to your munin configuration before the host tree
# in the part where sample contacts are listed:
#
# # -- Slack contact configuration
# # notify_slack_munin.sh is the full file name
# contact.slack.command MUNIN_SERVICESTATE="${var:worst}" MUNIN_HOST="${var:host}" MUNIN_SERVICE="${var:graph_title}" MUNIN_GROUP=${var:group} /usr/local/bin/notify_slack_munin.sh
# # This line will spam Slack with notifications even if no state change happens
# contact.slack.always_send warning critical
# # note: This has to be on one line for munin to parse properly
# contact.slack.text ${if:cfields \u000A* CRITICALs:${loop<,>:cfields ${var:label} is ${var:value} (outside range [${var:crange}])${if:extinfo : ${var:extinfo}}}.}${if:wfields \u000A* WARNINGs:${loop<,>:wfields ${var:label} is ${var:value} (outside range [${var:wrange}])${if:extinfo : ${var:extinfo}}}.}${if:ufields \u000A* UNKNOWNs:${loop<,>:ufields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}${if:fofields \u000A* OKs:${loop<,>:fofields ${var:label} is ${var:value}${if:extinfo : ${var:extinfo}}}.}
SLACK_CHANNEL="#insert-your-channel"
SLACK_WEBHOOK_URL="https://hooks.slack.com/services/insert/your/hookURL"
SLACK_USERNAME="munin"
SLACK_ICON_EMOJI=":munin:"
# If you want to test the script, you may have to comment this out to avoid hanging console
input=`cat`
#Set the message icon based on service state
if [ "$MUNIN_SERVICESTATE" = "CRITICAL" ]
then
ICON=":exclamation:"
COLOR="danger"
elif [ "$MUNIN_SERVICESTATE" = "WARNING" ]
then
ICON=":warning:"
COLOR="warning"
elif [ "$MUNIN_SERVICESTATE" = "ok" ]
then
ICON=":white_check_mark:"
COLOR="good"
elif [ "$MUNIN_SERVICESTATE" = "OK" ]
then
ICON=":white_check_mark:"
COLOR="good"
elif [ "$MUNIN_SERVICESTATE" = "UNKNOWN" ]
then
ICON=":question:"
COLOR="#00CCCC"
else
ICON=":white_medium_square:"
COLOR="#CCCCCC"
fi
# Generate the JSON payload
PAYLOAD="{\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"icon_emoji\": \"${SLACK_ICON_EMOJI}\", \"attachments\": [{\"color\": \"${COLOR}\", \"fallback\": \"Munin alert - ${MUNIN_SERVICESTATE}: ${MUNIN_SERVICE} on ${MUNIN_HOST}\", \"pretext\": \"${ICON} Munin alert - ${MUNIN_SERVICESTATE}: ${MUNIN_SERVICE} on ${MUNIN_HOST} in ${MUNIN_GROUP} - <http://central/munin/|View Munin>\", \"fields\": [{\"title\": \"Severity\", \"value\": \"${MUNIN_SERVICESTATE}\", \"short\": \"true\"}, {\"title\": \"Service\", \"value\": \"${MUNIN_SERVICE}\", \"short\": \"true\"}, {\"title\": \"Host\", \"value\": \"${MUNIN_HOST}\", \"short\": \"true\"}, {\"title\": \"Current Values\", \"value\": \"${input}\", \"short\": \"false\"}]}]}"
#Send message to Slack
curl -sX POST -o /dev/null --data "payload=${PAYLOAD}" $SLACK_WEBHOOK_URL 2>&1