#!/usr/sbin/python3
# -*- coding: utf8 -*-
import time
import psutil
import platform
import xlsxwriter

BFH = r'%'
PFH = "GB"
Boot_Start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(psutil.boot_time()))
type(Boot_Start)
Cpu_value = psutil.cpu_percent()
RAM = int(psutil.virtual_memory().total / (1024 * 1024))
RAM_precent = psutil.virtual_memory().percent
Net_sent = int(psutil.net_io_counters().bytes_recv) / 1048576
Net_recv = int(psutil.net_io_counters().bytes_recv) /  1048576
Net_spkg = psutil.net_io_counters().packets_sent
Net_rpkg = psutil.net_io_counters().packets_recv
disk_value = psutil.disk_partitions()
IP_host = psutil.net_if_addrs()
system_OS = platform.system()

if system_OS == "Windows":
    print('本机IP:%s' % (IP_host['本地连接'][1][1]))
    IP_HOST = IP_host['本地连接'][1][1]
elif system_OS == "Linux":
    print('本机IP: %s' % (psutil.net_if_addrs()['eth0'][0][1]))
    IP_HOST = psutil.net_if_addrs()['eth0'][0][1]
else:
    print("sorry,Temporarily do not support this system")
    exit(0)

print("操作系统:%s" % system_OS)
print("开机时间:%s" % Boot_Start  )
print("CPU使用率:%s%s" %(Cpu_value, BFH))
print("内存:%dM \t 内存使用率:%s%s" % (RAM,RAM_precent,BFH))
print("发送:%dM \t 发送包: %d个" % (Net_sent,Net_spkg))
print("接收: %dM \t 接收包数: %d个" % (Net_recv,Net_rpkg))



if (system_OS == 'Windows'):

    for i in  disk_value:
            disk_path = i[0]
            v = psutil.disk_usage(disk_path)
            disk_dir = ("盘符:%s" % (i[1]))
            print("磁盘名称:%s 磁盘大小:%s%s  磁盘使用率:%s%s" % (disk_path,int((int(v[0])) / 1073741824),PFH,(int(v[3])),BFH))
elif (system_OS == "Linux"):
    for i in disk_value:
        disk_linux = i[0][0]
        """
        disk_du 磁盘大小
        disk_free 剩余空间
        disk_percent 磁盘使用率
        """
        disk_du = int(psutil.disk_usage(disk_linux)[0])  / 1073741824
        disk_free = int(psutil.disk_usage(disk_linux)[2]) / 1073741824
        disk_percent = int(psutil.disk_usage(disk_linux)[3]) / 1073741824
        print(disk_du,disk_free,disk_percent)
        print("挂载盘符: %s 磁盘大小: %s%s 磁盘剩余空间: %s%s 磁盘使用率: %s%s" % (disk_linux,disk_du,PFH,disk_free,PFH,disk_percent,BFH))

workbook = xlsxwriter.Workbook('自动化系统监控周报1551.xlsx')
worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type':'column'})
title = [u'操作系统'u'开机时间',u'CPU使用率(%)',u'内存',u'内存使用率(%)',u'发送包数',u'接收包数','本机IP']
worksheet.write_row('A1',title)
worksheet.write_string('A2',system_OS)
worksheet.write('B2',Cpu_value)
worksheet.write('C2',RAM)
worksheet.write_number('D2',RAM_precent)
worksheet.write_number('E2',Net_spkg)
worksheet.write_number('F2 ',Net_rpkg)
worksheet.write_string('G2',IP_HOST)

workbook.close()

展示效果:

打赏作者

Leave a Reply

Your email address will not be published.