• Python实现自动检测设备连通性并发送告警到企业微信


    背景:门禁机器使用的WiFi连接,因为某些原因会不定期自动断开连接,需要人工及时干预,以免影响门禁数据同步,故写此脚本,定时检测门禁网络联通性。

    #首次使用要安装tcping模块
    pip install tcping
    
    • 1
    • 2
    from tcping import Ping
    import csv
    from datetime import datetime
    import requests, json
    
    def SendWeiXinWork(user,context):
        corpid='填企业ID' #企业ID
        appsecret='填secret'  #secret
        agentid=填AgentID  #AgentID
        #获取accesstoken
        token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
        req=requests.get(token_url)
        accesstoken=req.json()['access_token']
        #发送消息
        msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
        touser=user
        params={
                "touser": touser,
        #       "toparty": toparty,
                "msgtype": "text",
                "agentid": agentid,
                "text": {
                        "content": context
                },
                "safe":0
        }
        req=requests.post(msgsend_url, data=json.dumps(params))
     
    
     
    def pingip(ipAddress,request_nums):
        """
        ping ip
        :param ipAddress:
        :param request_nums: 请求次数
        :return: 丢包率loss和统计结果res
        """
        ping = Ping(ipAddress,3718,3)#3718是端口号,我的门禁机器只有这个端口号可以ping通
        ping.ping(request_nums)
        res = ping.result.table
        ret = ping.result.raw
        retlist = list(ret.split('\n'))
        loss = retlist[2].split(',')[3].split(' ')[1]  # 获取数据包发送成功率
        return loss, res
         
    def main():
    
        # 获取待ping的设备地址信息
        with open('C:\\Users\\Junson\\Desktop\\Script\\巡检\\门禁\\门禁设备列表.csv','r') as ipList_csv:
            ipList = csv.reader(ipList_csv)
            next(ipList)    #跳过首行
            for ipAddress in ipList:
                
                # 调用pingip方法得到数据包发送成功率
                loss, res = pingip(ipAddress[0], 4)
                if float(loss.strip('%')) / 100 <= 0.3:   # 0.3为自定义数据包丢包率阈值,可修改
    
                    #数据包发送成功率低于30%时,发送消息到企微机器人
                    SendWeiXinWork('@all','%s %s无法ping通,请检查设备的网络连接!'%(ipAddress[0],ipAddress[1]))
                    
                    #记录日志
                    file_handle=open('C:\\Users\\Junson\\Desktop\\Script\\巡检\\门禁\\log.txt',mode='a')
                    file_handle.write('\n%s   %s   %s无法ping通,请检查设备的网络连接!'%(datetime.now(),ipAddress[0],ipAddress[1]))
                    file_handle.close()
                else:
                    #记录日志
                    file_handle=open('C:\\Users\\Junson\\Desktop\\Script\\巡检\\门禁\\log.txt',mode='a')
                    file_handle.write('\n%s   %s   %sping访问正常。'%(datetime.now(),ipAddress[0],ipAddress[1]))
                    file_handle.close()
                    
     
    if __name__ == '__main__':
        #实现服务器网络状态监控
        
        main()
        pass
    
    • 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



    csv文件结构
    在这里插入图片描述


    创建计划任务
    在这里插入图片描述



    最终效果:
    在这里插入图片描述

  • 相关阅读:
    英雄算法7月24号
    剑指 Offer 03. 数组中重复的数字
    液压系统中比较常用的密封件是什么?
    博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
    深度学习-nlp系列(2)文本分类(Bert)pytorch
    【教3妹学算法-每日3题(2)】通过翻转子数组使两个数组相等
    自定义注解(切面实现)
    小程序 步骤条组件
    AI作诗,模仿周杰伦创作歌词<->实战项目
    Jmeter接口测试 —— jmeter对图片验证码的处理
  • 原文地址:https://blog.csdn.net/Junson142099/article/details/132670078