Python-端口监控-钉钉告警
一个python写的简单的端口监控例子,配合crontab和钉钉实现定时监控和告警。
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| [root@aws-sit-server-dev-01 moni] gis|172.31.45.42|6066 gis|172.31.45.42|8083 gis|172.31.45.42|8084 sit-mq|172.31.40.27|9876 sit-gitlab|172.31.40.27|8090 sit-jenkins|172.31.40.27|8089 sit-nacos|172.31.40.27|8848 sit-redis|172.31.40.125|6379 sit-mysql|172.31.40.125|3306 sit-nexus|172.31.45.187|8081 show-mq|172.31.73.65|9876 show-mysql|172.31.73.65|3306 show-nacos|172.31.73.65|8848 show-redis|172.31.73.65|6379 jx-mq|172.31.71.117|9876 jx-mysql|172.31.71.117|3306 jx-nacos|172.31.71.117|8848 jx-redis|172.31.71.117|6378
|
监控脚本
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
| import datetime import socket,sys import requests,json import re
def send_dingding(url, msg): headers = {'Content-Type': 'application/json'} data = { "msgtype": "text", "text": { "content": msg } } r = requests.post(url, headers=headers, data=json.dumps(data)) print(r.text)
if __name__ == "__main__": url_dingding = 'https://oapi.dingtalk.com/robot/send?access_token=abcd' msg='' with open(r'/data/moni/moni_port/moni_port.lst') as f: for row in f.readlines(): if not re.search('^#',row): r1,r2,r3=row.strip().split('|',2) moni_host=r1 moni_ip=r2 moni_port=r3 port_state=socket.socket(socket.AF_INET,socket.SOCK_STREAM).connect_ex((moni_ip,int(moni_port))) if port_state != 0 : msg=msg+moni_host+" "+moni_ip+":"+moni_port+" has been down. Please check.\n" if len(msg) > 2: print(datetime.datetime.now()) print(msg) send_dingding(url_dingding,msg)
|
老年佛系运维 | biglovewheat@126.com