python发送xml格式socket心跳告警
需求
- 1、行内,应用程序发送心跳,发送xml到socker.
- 2、通过ps获取程序是否存在,判断是否发送心跳.
- 3、使用xml.etree.ElementTree来格式化xml格式
实现
- 1、使用socket link来创建socket链接
- 2、使用xml.etree.ElementTree来格式化xml格式
- 3、根据需要监控process_name_list 列表来循环 判断是否发送心跳
- 4、根据发送socket返回信息判断是否发送socket是否成功
- 5、记录日志信息
代码
#!/usr/bin/python3
"""
需求:
1、行内,应用程序发送心跳,发送xml到socker.
2、通过ps获取程序是否存在,判断是否发送心跳.
"""
import xml.etree.ElementTree as ET
import socket,os,time,logging
from logging.handlers import RotatingFileHandler
def build_xml_message(heartbeat_interval, name, SystemName, AlarmRes):
local_time1 = int(round(time.time() * 1000))
local_time2 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
root = ET.Element("ALARMSTART")
name_element = ET.SubElement(root, "SystemName")
name_element.text = SystemName
age_element = ET.SubElement(root, "AlarmID")
age_element.text = str(local_time1)
city_element = ET.SubElement(root, "AlarmEquipment")
city_element.text = name
city_element = ET.SubElement(root, "AlarmRes")
city_element.text = AlarmRes
city_element = ET.SubElement(root, "EventTime")
city_element.text = local_time2
return ET.tostring(root, encoding="utf-8", method="xml").decode("utf-8")
def send_heartbeat(xml_message, server_address):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
try:
# print(xml_message.encode("utf-8"))
sock.connect(server_address)
sock.sendall(xml_message.encode("utf-8"))
response = sock.recv(1024)
local_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if "flag=true" in str(response):
log_info = str(local_time) + " info send info ok ,return info: " + str(response)
logger.debug(log_info)
# write_data_to_file_append(log_file_path, log_info)
else:
log_info = str(local_time) + " error send info fail ,return info: " + str(response)
logger.debug(log_info)
#write_data_to_file_app/end(log_file_path, log_info)
except Exception as e:
logger.error("Failed to send heartbeat to " + str(server_address) + " " + str(e))
finally:
sock.close()
def write_data_to_file_append(file_path, data):
with open(file_path, "a") as f:
f.write(data)
def is_process_running(ps_out,process_name):
# 遍历所有进程,查找具有给定名称的进程
for line in ps_out.splitlines():
if process_name in line:
return True
return False
if __name__ == "__main__":
#需要链接soket 服务端
server_address = ("localhost", 10000)
# log_file_path = "send_socket.log"
heartbeat_interval = 5
#需要监控的程序名
process_name_list = ["nginx","mysqld","cdesk_nginx1"]
#xml名称定义
SystemName = "ccc-test-001"
AlarmRes = "定制接口服务器"
##### logger #####
log_file = "./log/heratbeat.log"
shell = "mkdir -p " + log_file.split("heratbeat.log")[0]
os.popen(shell)
# 创建日志记录器
logger = logging.getLogger("my_logger")
logger.setLevel(logging.DEBUG)
# 创建RotatingFileHandler对象
max_log_size = 100 * 1024 * 1024 # 100 MB
backup_count = 50
handler = RotatingFileHandler(log_file, maxBytes=max_log_size, backupCount=backup_count)
# 定义日志格式
formatter = logging.Formatter("%(asctime)s %(thread)d %(filename)s:%(lineno)d %(levelname)s %(message)s")
handler.setFormatter(formatter)
# 将处理程序添加到日志记录器
logger.addHandler(handler)
while True:
#使用os.popen执行ps命令,获取所有进程
ps_output = os.popen("ps aux").read()
# ps_output = os.popen("ps -e").read()
for process_name in process_name_list:
if is_process_running(ps_output,process_name):
xml_message = build_xml_message(heartbeat_interval, process_name, SystemName, AlarmRes)
print(xml_message)
logger.debug(process_name + " is running.")
send_heartbeat(xml_message, server_address)
else:
logger.error(process_name + " is not running.")
time.sleep(heartbeat_interval)
# 程序结束时关闭处理程序
logger.removeHandler(handler)
handler.close()
解决运行情况
2023-09-19 10:32:49,369 140176451651392 heratbeat.py:103 DEBUG nginx is running.
2023-09-19 10:32:49,370 140176451651392 heratbeat.py:45 DEBUG 2023-09-19 10:32:49 error send info fail ,return info: b'<ALARMSTART><SystemName>ccc-test-001</SystemName><AlarmID>1695090769369</AlarmID><AlarmEquipment>nginx</AlarmEquipment><AlarmRes>\xe5\xae\x9a\xe5\x88\xb6\xe6\x8e\xa5\xe5\x8f\xa3\xe6\x9c\x8d\xe5\x8a\xa1\xe5\x99\xa8</AlarmRes><EventTime>2023-09-19 10:32:49</EventTime></ALARMSTART>'
2023-09-19 10:32:49,370 140176451651392 heratbeat.py:103 DEBUG mysqld is running.
2023-09-19 10:32:49,371 140176451651392 heratbeat.py:45 DEBUG 2023-09-19 10:32:49 error send info fail ,return info: b'<ALARMSTART><SystemName>ccc-test-001</SystemName><AlarmID>1695090769370</AlarmID><AlarmEquipment>mysqld</AlarmEquipment><AlarmRes>\xe5\xae\x9a\xe5\x88\xb6\xe6\x8e\xa5\xe5\x8f\xa3\xe6\x9c\x8d\xe5\x8a\xa1\xe5\x99\xa8</AlarmRes><EventTime>2023-09-19 10:32:49</EventTime></ALARMSTART>'
2023-09-19 10:32:49,371 140176451651392 heratbeat.py:106 ERROR cdesk_nginx1 is not running.
[devops@my-dev ABC]$ ./sock.py
服务器已启动,等待客户端连接...
客户端 ('127.0.0.1', 41400) 已连接
收到来自 ('127.0.0.1', 41400) 的数据: <ALARMSTART><SystemName>ccc-test-001</SystemName><AlarmID>1695090684048</AlarmID><AlarmEquipment>nginx</AlarmEquipment><AlarmRes>定制接口服务器</AlarmRes><EventTime>2023-09-19 10:31:24</EventTime></ALARMSTART>
客户端 ('127.0.0.1', 41402) 已连接
收到来自 ('127.0.0.1', 41402) 的数据: <ALARMSTART><SystemName>ccc-test-001</SystemName><AlarmID>1695090684049</AlarmID><AlarmEquipment>mysqld</AlarmEquipment><AlarmRes>定制接口服务器</AlarmRes><EventTime>2023-09-19 10:31:24</EventTime></ALARMSTART>