一阶剑,接口輪詢
import time
import requests
from xmlrpc.client import ServerProxy
# 替換為你的企業(yè)微信API地址
API_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_API_KEY"
# 多個(gè)Supervisor服務(wù)器配置
supervisor_servers = [
{
"host": "192.168.1.110",
"port": 9001,
"user": "admin",
"password": "123456",
}
# ... 添加更多Supervisor服務(wù)器配置
]
def create_server_proxy(server_config):
server_url = f"http://{server_config['user']}:{server_config['password']}@{server_config['host']}:{server_config['port']}/RPC2"
return ServerProxy(server_url)
# 為每個(gè)Supervisor服務(wù)器創(chuàng)建ServerProxy實(shí)例
server_proxies = [create_server_proxy(server_config) for server_config in supervisor_servers]
def send_message(content):
payload = {
"msgtype": "text",
"text": {
"content": content
}
}
response = requests.post(API_URL, json=payload)
if response.status_code == 200:
print("消息發(fā)送成功")
else:
print("消息發(fā)送失敗吃沪,錯(cuò)誤代碼:", response.status_code)
def main():
while True:
try:
for server, server_config in zip(server_proxies, supervisor_servers):
processes = server.supervisor.getAllProcessInfo()
for process in processes:
if process['statename'] in ['FATAL', 'EXITED','STOPPED']:
message = f"Host: {server_config['host']} - 進(jìn)程 {process['name']} 狀態(tài)異常:{process['statename']} 請(qǐng)及時(shí)關(guān)注!"
print(message)
#send_message(message)
time.sleep(60) # 每分鐘檢查一次進(jìn)程狀態(tài)
except Exception as e:
print(f"監(jiān)控過(guò)程中出現(xiàn)錯(cuò)誤:{e}")
if __name__ == "__main__":
main()
二汤善,事件監(jiān)聽(tīng)