pd生成excel

#xlwt
#xlrd
import pandas  as pd
from share.ssh import SSH
from datetime import datetime
from conf.conf import ip_all, excel_dir




def excel():
    disk = """ df -h |awk  '$NF=="/"{print  $4,$5}' """
    cpu = "top -bn 1 -i -c  | grep Cpu |awk -Fid, '{print $1}' |awk -F, '{print $NF}'"
    disk_trs = """df -h |awk  '$NF=="/TRS"{print  $4,$5}'"""
    disk_data = """df -h |awk  '$NF=="/data"{print  $4,$5}'"""
    swap = """ free -h|grep  "Swap"|awk '{print $2,$4}' """
    mem = """ free -m | awk 'NR==2{printf "%.2f%%",$3*100/$2 }'"""
    ip_list = []
    disk_list = []
    cpu_list = []
    disk_trs_list = []
    disk_data_list = []
    swap_list = []
    mem_list=[]

    with  open(ip_all) as  f:
        for i in f:
            ip = i.strip()
            cli = SSH(ip)
            disk_out = cli.exec_command(disk.strip())
            cpu_out = 100 - float(cli.exec_command(cpu))
            disk_trs_out = cli.exec_command(disk_trs)
            disk_data_out = cli.exec_command(disk_data)
            swap_out = cli.exec_command(swap)
            mem_out=cli.exec_command(mem)

            ip_list.append(ip)
            disk_list.append(disk_out)
            cpu_list.append(cpu_out)
            disk_trs_list.append(disk_trs_out)
            disk_data_list.append(disk_data_out)
            swap_list.append(swap_out)
            mem_list.append(mem_out)
    # # 写
    dic1 = {'ip': ip_list,
            '/剩余及使用率': disk_list,
            'cpu使用率': cpu_list,
            '/TRS剩余及使用率': disk_trs_list,
            '/data剩余及使用率': disk_data_list,
            '物理内存剩余': mem_list,
            'swap总内存及剩余': swap_list
            }
    df = pd.DataFrame(dic1)
    file_name = "xunjian" + "_" + datetime.now().strftime("%Y%m%d") + ".xlsx"
    df.to_excel(f"{excel_dir}/{file_name}", index=False)

posted @   追梦nan  阅读(347)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示