python tomcat 监管脚本通过钉钉机器人发送tomcat异常信息

import socket
import os,time
import configparser
import requests as rq
config=configparser.RawConfigParser()

def local():
    strtime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
    return strtime

class tomcat():
    def __init__(self):
        while True:
            config.read("config.ini", encoding="utf-8-sig")
            self.sections = config.sections()
            self.sections.remove('time')
            self.sections.remove('robot')
            self.time=int(config['time']['time'])
            self.access_token=config['robot']['access_token']
            print(self.sections)
            self.run()

    def isconnet(self,ip,port):
        sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sk.connect((ip, port))
            return True
        except Exception as e:
            return False
    def chongqi(self,path,judge):
        if judge:
            try:
                panfu=path.split("\\")[0]
                os.system(r"{0} && cd {1}\bin && startup.bat ".format(panfu,path))
            except Exception as e:
                print(e)
    def run(self):
        print(local())
        for i in self.sections:
            ipport = config[i]['ip']
            ip=ipport.split(':')[0]
            port = int(ipport.split(':')[1])
            root = config[i]['root']
            message = config[i]['message']
            isboot = config[i]['isreboot']
            print(ipport, root, message, bool(int(isboot)))
            if self.isconnet(ip,port):
                print("连接成功")
            else:
                print("连接失败")
                self.url = "https://oapi.dingtalk.com/robot/send?access_token={}".format(self.access_token)
                js = {"msgtype": "text", "text": {"content": message}, "at": {"atMobiles": [], "isAtAll": True}}
                try:
                    rq.post(self.url, json=js)
                except Exception as e:
                    print(e)
                    print("机器人发送消息异常")
                self.chongqi(root, bool(int(isboot)))
                print("发送信息"+message)

        time.sleep(self.time*60)

t=tomcat()

配置文件

[time]
#单位分钟,间隔时间
time=1
[robot]
#配置机器人
access_token=
[case1]
ip = 127.0.0.1:8080
#tomcat目录
root=C:/tomcat
#发送消息文本
message=
#1重启 0 不重启
isreboot=1
[case2]
ip = 
root=
message=
isreboot=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值