esp32开发的第一个小工具

esp32获取zabbix数据进行亮灯提醒

因为报警太多了,有时候真看不过来,而又不能常打开一个屏幕看,所以用esp32进行亮灯提醒报警事件

思路

esp32通过zabbix获取到problem时进行亮灯操作,没有获取时灭灯

实现方式

因为环境问题和esp32才上手,arduino的代码还在学习中,所以间接通过一台中转server进行中转
1.使用一台server(网络环境问题),获取zabbix api的get.problem并且进行相应过滤,获取到problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import json

# Zabbix服务器的URL和API路径
zabbix_url = 'http://zabbix/api_jsonrpc.php'
# Zabbix登录信息
username = 'username'
password = 'password'

# 创建一个会话
session = requests.Session()

# 获取Zabbix API令牌
def get_auth_token():
payload = {
'jsonrpc': '2.0',
'method': 'user.login',
'params': {
'user': username,
'password': password,
},
'id': 1,
}

response = session.post(zabbix_url, json=payload)
result = response.json()

if 'result' in result:
return result['result']
else:
raise Exception("Failed to authenticate with Zabbix API")

# 获取当前正在报警的问题
def get_current_alerts():
auth_token = get_auth_token()

payload = {
'jsonrpc': '2.0',
'method': 'problem.get',
'params': {
'output': 'extend',
'groupids': ['104','173'], #可以设定监控的主机组
'filter': {
'value': 1, # 1表示问题未解决,0表示问题已解决
},
'sortfield': 'eventid',
'sortorder': 'DESC',
#'limit': 10,
},
'auth': auth_token,
'id': 2,
}

response = session.post(zabbix_url, json=payload)
result = response.json()

if 'result' in result:
return result['result']
else:
raise Exception("Failed to retrieve current alerts from Zabbix")

if __name__ == "__main__":
data = {
"problem": []
}
current_alerts = get_current_alerts()

if current_alerts:
for alert in current_alerts:
#print(alert)
data["problem"].append(alert['name'])
# print(f"Event ID: {alert['eventid']}")
# print(f"Description: {alert['name']}")
# print(f"Severity: {alert['severity']}\n")
json_data = json.dumps(data,ensure_ascii=False)
print(json_data)
else:
print("No current alerts found.")

上面获取problem 并且用crontabl将json结果保存在nginx的html中,进行展示

2.通过esp32从这个服务器每10秒钟进行get数据,判断是否json数据,不是进行灭灯,如果有problem进行亮灯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "wifi_ssid";
const char* password = "wifi_mima";
const char* url = "http://server/index.html"; // 更改为您要检查的页面URL

const int LED = 2;

void setup() {
Serial.begin(115200);
delay(1000);

// 连接到WiFi网络
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
// 执行HTTP GET请求
}
void get_zabbix(){
HTTPClient http;
http.begin(url);

int httpResponseCode = http.GET();

if (httpResponseCode == HTTP_CODE_OK) {
String response = http.getString();
const size_t bufferSize = JSON_OBJECT_SIZE(1000); // 根据你的 JSON 数据结构设置
DynamicJsonDocument jsonDoc(bufferSize);
DeserializationError error = deserializeJson(jsonDoc, response);

// 检查解析是否成功
if (error) {
Serial.println("no json: LED OFF ");
digitalWrite(LED, LOW); // 关闭LED
return;
}
else {
digitalWrite(LED, HIGH); // 开启LED
Serial.println("json: LED ON ");
}

http.end();
jsonDoc.clear();
}
}
void loop() {
get_zabbix();
delay(10000);
// Your code here
}

现在就能正常的烧录程序进行使用了

坑点

1.zabbix api的get.problem出来的结果会出现禁用主机的problem,这个是无法过滤的,需要删除禁用主机或者清楚告警
2.esp32针脚不一样,需要自己查,我这个gpio 2控制的是wifi蓝色指示灯


esp32开发的第一个小工具
http://example.com/2023/09/13/esp32开发的第一个小工具/
作者
WangQiang
发布于
2023年9月13日
许可协议