| 
- import threading
- import time
- import subprocess
- import datetime
- import os
- import json
- import requests
- class WeChatPub:
- s = requests.session()
- token = None
- def __init__(self):
- self.token = self.get_token("ww59b93a64878f4b8c", "iQG2sm9cy23do4886uiqv4AuD6Z1cCfNIDgIi2-uZg0")
- print("token is " + self.token)
- def get_token(self, corpid, secret):
- url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}".format(corpid, secret)
- rep = self.s.get(url)
- if rep.status_code == 200:
- return json.loads(rep.content.decode('utf-8'))['access_token']
- else:
- print("request failed.")
- return None
- def send_msg(self, content):
- url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + self.token
- header = {
- "Content-Type": "application/json"
- }
- form_data = {
- "touser": "@all",
- "toparty": " PartyID1 | PartyID2 ",
- "totag": " TagID1 | TagID2 ",
- "msgtype": "textcard",
- "agentid": 1000002,
- "textcard": {
- "title": "GS异常通知",
- "description": content,
- "url": "URL",
- "btntxt": "更多"
- },
- "safe": 0
- }
- rep = self.s.post(url, data=json.dumps(form_data).encode('utf-8'), headers=header)
- if rep.status_code == 200:
- return json.loads(rep.content.decode('utf-8'))
- else:
- print("request failed.")
- return None
- wechat = WeChatPub()
- def restart_process(process_name):
- red = subprocess.Popen('tasklist', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
- tasklist_str = red.stdout.read().decode(encoding='gbk')
- re_path = process_name.split("\")[-1]
- re_path = re_path.split(" ")[0]
- formattime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
- if re_path not in tasklist_str:
- global count
- count += 1
- wechat.send_msg("<div class="gray">"+formattime + '第' + str(count) + '次检测GS服务异常'+"</div>")
- print(formattime + '第' + str(count) + '次检测发现异常重连')
- cmd = process_name
- ret = os.system(process_name)
- if ret == 0:
- wechat.send_msg("<div class="gray">"+formattime +'GS服务自动重连成功'+"</div>")
- else:
- wechat.send_msg("<div class="gray">"+formattime +'GS服务自动重连失败,请管理员检查'+"</div>")
- return
- else:
- global error_count
- error_count += 1
- print(formattime + '第' + str(error_count) + '次检测正在运行中')
- global timer
- timer = threading.Timer(10, restart_process, (r"start D:\MuOnline\GameServer\GameServer.exe 127.0.0.1 55970 127.0.0.1 55960 55901",))
- timer.start()
- count = 0
- error_count = 0
- timer = threading.Timer(10, restart_process, (r"start D:\MuOnline\GameServer\GameServer.exe 127.0.0.1 55970 127.0.0.1 55960 55901",))
- timer.start()
复制代码
|
|