【问题标题】:Munin alert/notification's not being executed未执行 Munin 警报/通知
【发布时间】:2016-01-20 00:52:30
【问题描述】:

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

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

【问题讨论】:

标签: munin


【解决方案1】:

我刚刚使用了最初在 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

【讨论】:

  • 嗨,Abel,如果您还可以在答案中包含脚本(或至少相关部分),也许会有所帮助。要点可能并不总是可用。这将使您的答案更加完整。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多