0x00 创建Telegram Bot

  1. 去找BotFather icon_rolleyes.png
    可参考官方文档:https://core.telegram.org/bots#creating-a-new-bot

    BotFather

  2. 将新生成的 bot 加入对话(或者加入需要被告警的群组) 随便发一些文本

  3. GET https://api.telegram.org/botXXX:YYY/getUpdates 获取信息
    botXXX:YYY 中的 XXX:YYY 替换为bottoken

    postman

  4. POST https://api.telegram.org/botXXX:YYY/sendMessage 测试发送消息
    指定参数 chat_id 从上面获得

    postman

关于Telegram Bot API

All methods in the Bot API are case-insensitive. We support GET and POST HTTP methods. Use either URL query string or application/json or application/x-www-form-urlencoded or multipart/form-data for passing parameters in Bot API requests.
On successful call, a JSON-object containing the result will be returned.

详见官方Telegram Bot API文档:https://core.telegram.org/bots/api

0x01 写一个通知调用脚本

Alerts to or through external scripts

To run a script (in this example, ‘script’) from Munin use a command such as this in your munin.conf.

Make sure that:

  1. There is NO space between the ‘>’ and the first ‘script’
  2. ‘script’ is listed twice and
  3. The munin user can find the script – by either using an absolute path or putting the script somewhere on the PATH – and has permission to execute the script.

contact.person.command >script script

This syntax also will work (this time, it doesn’t matter if there is a space between ‘|’ and the first ‘script’ … otherwise, all the above recommendations apply):

contact.person.command | script script

Either of the above will pipe all of Munin’s warning/critical output to the specified script. Below is an example script to handle this input and write it to a file:

简单来说的思路就是将管道STDIN处理发送给API接口 要记得 o+x 确保munin用户有权限执行

    #! /usr/bin/env python

    import sys
    import requests
    import time

    bot_Token = 'XXX:YYY'
    chat_id = ''

    data = "*{}*\n".format(time.asctime(time.localtime(time.time()))) 
    if not sys.stdin.isatty():
        data += sys.stdin.read()
    else:
        data += raw_input()

    payload = {'chat_id':chat_id, 'text':data, 'parse_mode':'Markdown'}
    r = requests.post("https://api.telegram.org/bot{}/sendMessage".format(bot_Token), data=payload) 

0x02 Munin配置报警

以监控 CPU time spent by normal programs and daemons 为例 设置警告级别为 50% 紧急级别为 80%
编辑munin.conf 添加到一个节点的配置上去
2021.1.1补充: 报警监控的项目变量名 为面板上的 Internal name 而不是 Field 可以先telnet到munin-node监听端口检查一下变量名到底是啥

[localhost]
      address 127.0.0.1
      use_node_name yes
      cpu.user.warning 50
      cpu.user.critical 80

警报外部脚本调用指向刚刚写好的脚本 至于这里为什么要 'script' is listed twice and 不理解... f23.png

contact.person.command | /etc/munin/telegram_bot.py /etc/munin/telegram_bot.py

有关完整列表,请参阅插件数据源属性参考

0x03 Result

执行个命令while true; do true; done 跑满负载测试通知

BotNB


参考

CentOS 7 : Munin : Set Thresholds : Server World
zabbix 配置 telgram 告警 - 简书