首页 > 编程语言 >使用python对交换机进行排障自动化运维

使用python对交换机进行排障自动化运维

时间:2024-09-15 20:22:53浏览次数:3  
标签:运维 re python list 排障 command result pd time

import paramiko
import os
import time
import random
import datetime
import pandas as pd
import re
import numpy as np
# from sqlalchemy import text, create_engine
# import psycopg2
# from psycopg2 import sql
# from sqlalchemy.orm import sessionmaker
# from sqlalchemy.dialects.postgresql import insert as pg_insert
# from sqlalchemy import create_engine, MetaData, Table, inspect
import schedule

import telnetlib
import threading
import time


def traffic_info_get():

    # SSH登录采集
    hostname = "10.89.164.70"
    username = "root"
    password = "PON@jkfx"

    try:
        # 创建SSH客户端
        ssh = paramiko.SSHClient()
        # 自动添加主机密钥
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        # 连接服务器
        ssh.connect(hostname=hostname, port=22, username=username, password=password)
    except Exception as e:
        print(f"无法连接到服务器: {e}")

    try:
        stdin, stdout, stderr = ssh.exec_command("ifconfig -a")
        ifconfig_output = stdout.read().decode()
        # 使用正则表达式提取网口名称
        pattern = re.compile(r'(\S+)\s+Link encap:Ethernet.*?\n.*?RX bytes:(\d+) .*?TX bytes:(\d+)', re.S)

        # 查找所有匹配的网口信息
        interfaces = pattern.findall(ifconfig_output)
        ssh.close()

        pd_data = pd.DataFrame(interfaces)
        pd_data.columns = ['port_name', 'rx_bytes', 'tx_bytes']
        pd_data["time"] = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H%M%S')
        pd_data["id"] = pd_data["port_name"] + '_' + pd_data["time"]
    except Exception as e:
        print(f"获取网口信息时出错: {e}")


def fake_data():
    # 假数据写入
    pd_data = pd.DataFrame([{
        "time": datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H%M%S'),
        "port_name": 'em1',
        "rx_bytes": random.randint(10000000, 20000000),
        "tx_bytes": random.randint(10000000, 20000000),
    }])
    pd_data["id"] = pd_data["port_name"] + '_' + pd_data["time"]
    return pd_data


# def database_write(pd_data):
#     # 写入数据库
#     database_name = 'postgres'
#     table_name = 'traffic_table'
#     DATABASE_URI = 'postgresql://5ga-cmcc:[email protected]:5432/' + database_name
#     engine = create_engine(DATABASE_URI)
#     metadata = MetaData()  # 创建MetaData对象
#
#     table = Table(table_name, metadata, autoload_with=engine)  # 正确使用autoload_with
#     Session = sessionmaker(bind=engine, autocommit=False)  # 关闭自动提交
#     session = Session()
#
#     pd_same_period = pd_data.fillna(value=0)
#     dict_rows = pd_same_period.to_dict('records')

    try:
        # 根据表的主键或唯一索引构建upsert语句
        stmt = pg_insert(table)
        primary_keys = [key.name for key in inspect(table).primary_key]
        stmt = stmt.on_conflict_do_update(
            index_elements=primary_keys,  # 使用表中的主键列表
            set_={key: value for key, value in stmt.excluded.items()}
        )
        # 或者使用execute执行单个操作
        session.execute(stmt, dict_rows)
        session.commit()  # 提交所有更改
    except Exception as e:
        session.rollback()  # 如果发生异常则回滚
        print("Error saving data to database:", e)
    finally:
        session.close()


def get_info_telnet(host_ip, username, password):
    tn = telnetlib.Telnet()
    try:

        tn.open(host_ip, port=23, timeout=5)
        print('%s connected ssuccess !' % host_ip)

        tn.read_until(b'Username:', timeout=5)
        tn.write(username.encode('ascii') + b'\n')

        tn.read_until(b'Password:', timeout=5)
        tn.write(password.encode('ascii') + b'\n')
        time.sleep(1)

        command_result = tn.read_until(b'#', timeout=5)
        if b'#' not in command_result:
            print('%s登录失败' % host_ip)
        else:
            print('%s登录成功' % host_ip)

    except:

        print('%s网络连接失败' % host_ip)

    command = "show clock"
    command = bytes(command, encoding='utf-8')
    tn.write(command + b'\r\n')
    run_time = tn.read_until(b'#')
    run_time = re.findall(r"\d+:\d+:\d+\s+\w+\s+\w+\s+\w+\s+\d+\s+2024", run_time.decode('GB18030'))[0]

    # command = "show interface brief"
    # command = bytes(command, encoding='utf-8')
    # tn.write(command + b'\n')
    # time.sleep(1)

    # result_list = []
    # while (True):
    #     command_result = tn.read_very_eager().decode('ascii')
    #     # print(command_result)
    #     result_list.append(command_result)
    #     if re.findall(r"--More--", command_result.strip()):
    #         tn.write(b" ")

    #     elif re.findall(r"#", command_result.strip()):
    #         break
    #     else:
    #         time.sleep(0.05)
    #         continue

    # result_str = "\n".join(result_list)
    # list_str = result_str.split('\n')

    # pd_result = pd.DataFrame()
    # list_temperature_vec = []
    # for j in list_str:
    #     regex = re.compile(r'\w+gei.+\s+.+\s+.+\s+.+\s+.+\s+.+\s+.+', re.S)
    #     # print(regex.findall(j))
    #     # print(len(regex.findall(j)))
    #     if len(re.findall(r"Interface", j)) > 0:
    #         new_columns = list_find_str = re.split(r'\s+', j)
    #         new_columns = new_columns[0:8]

    #     if len(regex.findall(j)) > 0:
    #         list_find_str = regex.findall(j)[0]
    #         list_find_str = re.split(r'\s+', list_find_str)
    #         list_temperature_vec.append(list_find_str)
    # pd_result = pd.DataFrame(list_temperature_vec)
    # pd_result.columns = new_columns

    command = "show ip interface brief"
    command = bytes(command, encoding='utf-8')
    tn.write(command + b'\n')
    time.sleep(1)

    result_list = []
    while (True):
        command_result = tn.read_very_eager().decode('ascii')
        # print(command_result)
        result_list.append(command_result)
        if re.findall(r"--More--", command_result.strip()):
            tn.write(b" ")

        elif re.findall(r"#", command_result.strip()):
            break
        else:
            time.sleep(0.05)
            continue

    result_str = "\n".join(result_list)
    list_str = result_str.split('\n')

    pd_result_2 = pd.DataFrame()
    list_temperature_vec = []
    for j in list_str:
        regex = re.compile(r'\w+gei.+\s+.+\s+.+\s+.+\s+.+\s+.+\s+.+', re.S)
        if len(re.findall(r"Interface", j)) > 0:
            new_columns = list_find_str = re.split(r'\s+', j)
            new_columns = new_columns[0:7]

        if len(regex.findall(j)) > 0:
            list_find_str = regex.findall(j)[0]
            list_find_str = re.split(r'\s+', list_find_str)
            list_temperature_vec.append(list_find_str)
    pd_result_2 = pd.DataFrame(list_temperature_vec)
    pd_result_2.columns = new_columns
    pd_result_2 = pd_result_2[pd_result_2['Phy'] == 'up']

    # 端口流量及状态检查

    pd_output = pd.DataFrame()
    for check_port in pd_result_2['Interface']:
        pd_each_port = pd.DataFrame()
        dict_ouput = {}

        dict_ouput["port_name"] = check_port
        dict_ouput["time"] = run_time

        command = " show interface " + check_port
        command = bytes(command, encoding='utf-8')
        tn.write(command + b'\n')
        time.sleep(1)

        result_list = []
        while (True):
            command_result = tn.read_very_eager().decode('ascii')
            # print(command_result)
            result_list.append(command_result)
            if re.findall(r"--More--", command_result.strip()):
                tn.write(b" ")

            elif re.findall(r"#", command_result.strip()):
                break
            else:
                time.sleep(0.05)
                continue
        result_str = "\n".join(result_list)

        startpattern = re.compile(r'In_Bytes\s+(\d+)\s+')
        strtext = re.search(startpattern, str(result_str)).group(1)
        strtext = int(strtext)
        dict_ouput["rx_bytes"] = strtext

        startpattern = re.compile(r'E_Bytes\s+(\d+)\s+')
        strtext = re.search(startpattern, str(result_str)).group(1)
        strtext = int(strtext)
        dict_ouput["tx_bytes"] = strtext
        pd_output = pd.concat([pd_output, pd.DataFrame.from_dict([dict_ouput])], axis=0)

    pd_output['time'] = pd.to_datetime(pd_output['time'])
    pd_output['time'] = pd_output['time'].apply(lambda x: x.strftime('%Y-%m-%d %H:%M:%S'))
    pd_output["id"] = pd_output["port_name"] + '_' + pd_output["time"]

    tn.close()
    return pd_output


def monitor_task():
    host_ip = '129.60.161.169'
    username = 'zte'
    password = 'zte'
    pd_output = get_info_telnet(host_ip, username, password)
    database_write(pd_output)


if __name__ == '__main__':

    schedule.every(10).seconds.do(monitor_task)
    while True:
        schedule.run_pending()
        time.sleep(1)

标签:运维,re,python,list,排障,command,result,pd,time
From: https://blog.51cto.com/u_16223744/12025215

相关文章

  • python+flask计算机毕业设计社区医疗服务管理系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着城市化进程的加快和人口老龄化的加剧,社区医疗服务在居民健康管理中扮演着越来越重要的角色。传统的社区医疗服务模式面临着信息孤岛、......
  • python+flask计算机毕业设计失物招领系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景在快节奏的现代生活中,人们频繁地穿梭于各种公共场所,如学校、商场、交通枢纽等,不可避免地会出现物品遗失或错拿的情况。传统的失物招领方式......
  • python+flask计算机毕业设计汽车租赁系统设计与实现(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着城市化进程的加速和人们生活水平的提高,汽车租赁作为一种灵活便捷的出行方式,日益受到广大消费者的青睐。传统的租车模式存在信息不对称......
  • python+flask计算机毕业设计基于协同过滤算法的个性化智能图书推荐系统(程序+开题+论文
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景在信息爆炸的时代,图书馆作为知识传播与积累的重要场所,面临着如何高效、精准地向读者推荐其可能感兴趣的图书资源的挑战。传统的图书推荐方......
  • Python中的内存池机制
    在Python中,内存管理是一个复杂但至关重要的主题,它直接关系到程序的性能和稳定性。Python的内存管理机制包括对象的分配、追踪以及回收,其中内存池(MemoryPool)是这一机制中的一个重要组成部分。内存池机制通过预先分配和重用小块内存来减少内存分配和释放的开销,从而提高程序的执......
  • Python编写简单登录系统的完整指南
    在现代应用中,用户认证和登录系统是一个非常重要的功能。通过登录系统,应用能够识别用户的身份,并为其提供相应的权限和服务。本文将介绍如何使用Python编写一个简单的登录系统,包括用户注册、登录验证、密码加密等功能。通过这一教程,将学习如何构建一个基本的用户登录系统,并理解其......
  • Python互相关统计学 地震学 心理学 数学物理和算法模型及数据科学应用
    ......
  • Tenacity -- Retrying library for Python
    RetryinglibraryforPythonhttps://github.com/jd/tenacity Pleaserefertothetenacitydocumentationforabetterexperience.TenacityisanApache2.0licensedgeneral-purposeretryinglibrary,writteninPython,tosimplifythetaskofaddingretry......
  • python+flask计算机毕业设计商城可视化系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着电子商务的迅猛发展,商城系统已成为连接消费者与商品的重要桥梁。然而,传统商城管理系统往往依赖于繁琐的文本输入与复杂的表格操作,难以......
  • python+flask计算机毕业设计梅州红色文化传承小程序(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景在数字化浪潮的推动下,文化遗产的传承与保护面临着前所未有的机遇与挑战。梅州,作为中国革命老区之一,拥有丰富的红色文化资源,这些资源不仅是......