Munin监控结合Telegram Bot 通知报警
0x00 创建Telegram Bot
去找
BotFather
可参考官方文档:https://core.telegram.org/bots#creating-a-new-bot将新生成的 bot 加入对话(或者加入需要被告警的群组) 随便发一些文本
GET
https://api.telegram.org/botXXX:YYY/getUpdates
获取信息
botXXX:YYY
中的XXX:YYY
替换为bot的token
POST
https://api.telegram.org/botXXX:YYY/sendMessage
测试发送消息
指定参数chat_id
从上面获得
关于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:
- There is NO space between the ‘>’ and the first ‘script’
- ‘script’ is listed twice and
- 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
不理解...
contact.person.command | /etc/munin/telegram_bot.py /etc/munin/telegram_bot.py
有关完整列表,请参阅插件数据源属性参考
0x03 Result
执行个命令while true; do true; done
跑满负载测试通知
参考
CentOS 7 : Munin : Set Thresholds : Server World
zabbix 配置 telgram 告警 - 简书