Python监控Nginx 4、7层健康检查
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | [root@acs - hk - ctos7 - prod - 01 scripts] # cat upstrem.py #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2023/6/25 17:18 # @File : nginx_upstram.py # @Software: PyCharm import requests from urllib.request import urlopen import os,socket,datetime,hashlib,base64,hmac,urllib import time def get_digest(): timestamp = str ( round (time.time() * 1000 )) #测试 #secret = '' #生产 secret = '' secret_enc = secret.encode( 'utf-8' ) # utf-8编码 string_to_sign = '{}\n{}' . format (timestamp, secret) # 字符串格式化拼接 string_to_sign_enc = string_to_sign.encode( 'utf-8' ) # utf-8编码 hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod = hashlib.sha256).digest() # HmacSHA256算法计算签名 sign = urllib.parse.quote_plus(base64.b64encode(hmac_code)) # Base64编码后进行urlEncode # 返回时间戳和计算好的编码拼接字符串,后面直接拼接到Webhook即可 return f "×tamp={timestamp}&sign={sign}" def get_system_info(): # 系统时间 dt = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M:%S' ) # 系统公网IP ip = requests.get( 'http://ifconfig.me' ).text.strip() # 内网IP s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(( '8.8.8.8' , 80 )) local_ip = s.getsockname()[ 0 ] s.close() #系统load system_load = os.popen( "uptime|awk '{print $NF,$NF-1,$NF-2}'" ) load = system_load.read().strip() host_name = socket.gethostname() return dt, ip, local_ip,load,host_name class Send_Dingding: def __init__( self , title,upst,stra): self .title = title self .upst = upst self .stra = stra def send_dingnding( self ): MSG = "**告警主题**:" + "\n" + self .title + "\n\n" \ ">当前时间:" + "\n" + get_system_info()[ 0 ] + "\n\n" \ " >当前主机名:" + "\n" + get_system_info()[ 4 ] + "\n\n" \ " >当前服务器IP:" + "\n" + get_system_info()[ 1 ] + "\n" + get_system_info()[ 2 ] + "\n\n" \ " >当前系统load:" + "\n" + str (get_system_info()[ 3 ]) + "\n\n" \ "七层异常:" + "\n" + "\n" + str ( self .upst) + "\n\n" \ "四层异常:" + "\n" + "\n" + str ( self .stra) + "\n\n" \ data = { "msgtype" : "markdown" , "markdown" : { "title" : self .title, "text" : MSG, }, "at" : { "atMobiles" : [ "@" ], "isAtAll" : False } } # 测试 #webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=' # 生产 webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=' # 利用requests发送post请求 rec = requests.post(webhook_url + get_digest(), json = data) class Get_target: def __init__( self , url): self .url = url def Get_Upstream_Target( self ): upstre = requests.get( self .url) upstre_json_str = upstre.json() up_list = [] for up_stre in upstre_json_str[ "servers" ][ "http" ]: if up_stre[ "status" ] = = "down" : up_list.append([up_stre[ "upstream" ],up_stre[ "name" ],up_stre[ "status" ]]) #up_text="Upstream:{}\tstaus: {}\thost: {}".format(up_stre["upstream"], up_stre['status'],up_stre["name"]) if len (up_list) = = 0 : return "程序正常" else : return up_list def Get_Stream_Target( self ): stream = requests.get( self .url) stream_json = stream.json() st_list = [] for strem in stream_json[ 'servers' ][ "stream" ]: if strem[ "status" ] = = "down" : st_list.append([strem[ "upstream" ],strem[ "status" ],strem[ "name" ]]) #return "stream: {}\tstatus: {} \thost: {}".format(strem["upstream"], strem["status"], strem["name"]) if len (st_list) = = 0 : return "程序正常" else : return st_list if "__name__ == __main__" : res = Get_target( 'http:///status?format=json' ) up = res.Get_Upstream_Target() st = res.Get_Stream_Target() if st and up ! = "程序正常" : send = Send_Dingding( "香港集群环境" ,up,st) send.send_dingnding() else : print ( "不告警" ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2018-07-20 Grafana安装