一,前言
有zabbix监控,也并不是时刻盯着数据,所以想着,每天固定某个时刻,自动发送服务器数据到指定群组,给其他人更直观的数据。
数据就可以从zabbix API获取。参考官方API文档:https://www.zabbix.com/documentation/current/zh/manual/api
二,功能实现
主要功能概览

代码步骤概览
三,代码实现
这里使用shell脚本
#!/bin/bash
# 信息验证
ZABBIX_URL=\”https://zabbix.example.com/api_jsonrpc.php\”
ZABBIX_GROUP=\”prod\” # 指定的服务器群组名称
ZABBIX_USER=\”Admin\”
ZABBIX_PASS=\”passoword\”
# TG 群组token和ID
TELEGRAM_BOT_TOKEN=\”5487418237:AAGibU…….\”
TELEGRAM_CHAT_ID=\”-452112….\”
# 登录并获取zabbix token
AUTH_TOKEN=$(curl –s –X POST –H \’Content-Type: application/json\’ –d \'{
\”jsonrpc\”: \”2.0\”,
\”method\”: \”user.login\”,
\”params\”: {
\”user\”: \”\’$ZABBIX_USER\’\”,
\”password\”: \”\’$ZABBIX_PASS\’\”
},
\”id\”: 1
}\’ $ZABBIX_URL | jq –r \’.result\’)
#echo $AUTH_TOKEN
# Step 1: 获取群组ID
GROUP_ID=$(curl –s –X POST –H \’Content-Type: application/json\’ –d \'{
\”jsonrpc\”: \”2.0\”,
\”method\”: \”hostgroup.get\”,
\”params\”: {
\”output\”: \”extend\”,
\”filter\”: {
\”name\”: [\”\’$ZABBIX_GROUP\’\”]
}
},
\”auth\”: \”\’$AUTH_TOKEN\’\”,
\”id\”: 1
}\’ $ZABBIX_URL | jq –r \’.result[0].groupid\’)
#echo $GROUP_ID
# Step 2: 获取群组内所有主机
HOSTS=$(curl –s –X POST –H \’Content-Type: application/json\’ –d \'{
\”jsonrpc\”: \”2.0\”,
\”method\”: \”host.get\”,
\”params\”: {
\”output\”: [\”hostid\”, &#
评论前必须登录!
注册