Munin警报/通知未被执行

时间:2015-10-29 21:10:07

标签: munin

我有一个自定义通知脚本,我想在发生重大事件时提供来自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}}}.}

在这些行之上定义了节点和目录,它们工作正常。但通知不会消失。你知道它可能是什么吗?

1 个答案:

答案 0 :(得分:3)

我刚刚使用了gist上发现的相同脚本,并在解决了两个微不足道的错误之后才开始工作:

  • 确保将联系人放在主机树之前的配置中。我把它放在配置文件的末尾,在我将其移动到放置样本联系人的地方之前,它根本没有被调用
  • 确保Munin配置中的命令使用完整文件名,因此如果您将脚本放在/usr/local/bin/notify_slack_munin中,则检查文件上没有文件扩展名

Munin Guide所述,请观看munin-limits.log以了解正在发生的事情。

最后请注意,如果您有contact.slack.always_send warning critical,它会反复发送通知,而不仅仅是严重性更改。

作为参考(如果gist链接制动),调用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