• 交换机巡检和监控


    # coding=utf8
    import json
    import sys
    from concurrent.futures import ThreadPoolExecutor
    
    
    def patrol(input_keys):
    
        host = input_keys['host']
        username = input_keys['username']
        password = input_keys['password']
        node_report = []
    
        terminal = Network.connect(host, username, password)
        if not terminal:
            print '连接失败'
            sys.exit(1)
        print '-->连接成功'
        # 获取设备名
        hostname=terminal.hostname.replace('>','')
        host_report = report_format('', childs=data_format(u'设备名称', hostname))
        print '-->获取设备名成功'
        # 获取型号/序列号
        import paramiko
        myclient = paramiko.SSHClient()
        myclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        myclient.connect(host, 22, username, password)
        stdin, stdout, stderr = myclient.exec_command('display device manuinfo')
        outprint = stdout.read().strip().split('
    ')
        #print outprint
        myclient.close()
    
        device_name = None
        device_sn = None
        for info in outprint:
            if info.startswith('DEVICE_NAME'):
                device_name = info.split(':')[-1].strip()
            if info.startswith('DEVICE_SERIAL_NUMBER'):
                device_sn = info.split(':')[-1].strip()
            if device_sn and device_name:
                break
        report = data_format('设备型号/序列号', '{}/
    {}'.format(device_name, device_sn), 0)
        sn_report = report_format(hostname, report)
        print '-->获取型号/序列号成功'
        #获取版本
        myclient = paramiko.SSHClient()
        myclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        myclient.connect(host, 22, username, password)
        stdin, stdout, stderr = myclient.exec_command('display version')
        outprint = stdout.read().strip().split('
    ')
        myclient.close()
        #print outprint
    
    
        result = outprint[7].split(',')
        version = ','.join(result[1:]).strip().replace(' ','
    ')
        report = data_format('软件版本', version, 0)
        version_report = report_format(hostname, report)
        print '-->获取版本成功'
        #获取设备运行时间
        sys_time = outprint[9].split(' ')
        sys_time = ' '.join(sys_time[4:]).strip().split(',')
        sys_time_list = []
        for i in sys_time:
            sys_time_list.append(int(i.lstrip(' ').split(' ')[0].strip()))
        sys_time = sys_time_list[0] * 7 + sys_time_list[1]
        sys_time_hours = sys_time_list[2]
        if sys_time == 0:
            result='{}小时'.format(sys_time_hours)
        else:
            result='{}天'.format(sys_time)
        report = data_format('设备运行时间',result , 0)
        time_report = report_format(hostname, report)
        print '-->获取设备运行时间成功'
        node_report.append(host_report)
        node_report.append(sn_report)
        node_report.append(version_report)
        node_report.append(time_report)
    
        terminal.close()
        return {host: node_report}
    
    
    try:
        host_list = hosts.split(',')
        max_length = len(host_list)
        if max_length >= 10:
            max_length = 10
    
        pool = ThreadPoolExecutor(max_workers=max_length)
        results = []
        for host in host_list:
            if not host.strip():
                continue
            try:
                input_keys = {'host': host, 'username': username, 'password': str(password)}
                result = pool.submit(patrol, (input_keys))
                results.append(result)
            except Exception as e:
                sys.exit(1)
    
        reports={}
        while results != []:
            for result in results:
                if result.done():
                    report = result.result()
                    reports.update(report)
                    results.remove(result)
    
        print reports
        reports = json.dumps(reports)
    except Exception as e:
        print e
        sys.exit(1)
    

      

    # coding=utf8
    import json
    import sys
    from concurrent.futures import ThreadPoolExecutor
    import paramiko
    
    def patrol(input_keys):
        host = input_keys['host']
        username = input_keys['username']
        password = input_keys['password']
        node_report = []
    
        terminal = Network.connect(host, username, password)
        if not terminal:
            print '连接失败'
            sys.exit(1)
        print '-->连接成功'
    
        # 获取设备名
        hostname=terminal.hostname.replace('>','')
        print '-->获取设备名成功'
        #获取cpu使用率
        myclient = paramiko.SSHClient()
        myclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        myclient.connect(host, 22, username, password)
        stdin, stdout, stderr = myclient.exec_command('display cpu-usage')
        res = stdout.read().split('
    ')
        myclient.close()
        # print res
        # code,res=  terminal.run('display cpu-usage',timeout=5, debug=True,slip='
    ',pure=True)
        # print repr(res)
        cpu_use_percent = res[-3].split()[0]
        if float(cpu_use_percent.rstrip('%')) > float(cpu_base_value):
            alert = 1
            print "Mem use {} > {}".format(cpu_use_percent, cpu_base_value)
        else:
            alert = 0
        result = data_format('CPU使用率(基线值<{}%)'.format(cpu_base_value), cpu_use_percent, alert)
        cpu_report = report_format(hostname, result)
        print '-->获取CPU使用率成功'
        myclient = paramiko.SSHClient()
        myclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        myclient.connect(host, 22, username, password)
        stdin, stdout, stderr = myclient.exec_command('display memory')
        res = stdout.read().split('
    ')
        print res
    
        #res = terminal.run('display memory')
        mem_use_percent = 100-float(res[-5].split()[-1].strip('
    ').rstrip('%'))
        if float(mem_use_percent) > float(mem_base_value):
            alert = 1
            print "Mem use {} > {}".format(mem_use_percent, mem_base_value)
        else:
            alert = 0
        result = data_format('内存使用率(基线值<{}%)'.format(mem_base_value), str(mem_use_percent)+'%', alert)
        mem_report = report_format(hostname, result)
        print '-->获取内存使用率成功'
        node_report.append(cpu_report)
        node_report.append(mem_report)
    
        terminal.close()
        return {host: node_report}
    
    
    try:
        host_list = hosts.split(',')
        max_length = len(host_list)
        if max_length >= 10:
            max_length = 10
    
        pool = ThreadPoolExecutor(max_workers=max_length)
        results = []
        for host in host_list:
            if not host.strip():
                continue
            try:
                input_keys = {'host': host, 'username': username, 'password': str(password)}
                result = pool.submit(patrol, (input_keys))
                results.append(result)
            except Exception as e:
                sys.exit(1)
    
        reports = {}
        while results != []:
            for result in results:
                if result.done():
                    report = result.result()
                    reports.update(report)
                    results.remove(result)
    
        print reports
        reports = json.dumps(reports)
    except Exception as e:
        print e
        sys.exit(1)
    

      

  • 相关阅读:
    【读书笔记】iOS-UDID
    【读书笔记】iOS-优化内存
    【读书笔记】iOS-处理内存警告
    【读书笔记】iOS-方法声明
    【读书笔记】iOS-属性
    【读书笔记】iOS-强类型与弱类型
    【读书笔记】iOS-Objective-C编程
    【读书笔记】iOS-Interface Builder
    【读书笔记】iOS-开发者证书
    【读书笔记】iOS-成为一名开发者
  • 原文地址:https://www.cnblogs.com/slqt/p/10907404.html
Copyright © 2020-2023  润新知