巡检WINDOWS服务器CPU,内存和磁盘多个分区的PYTHON脚本

接上一次实验的内容,输出相同格式的EXCEL巡检表格

https://blog.csdn.net/lsysafe/article/details/106927633

这次巡检的服务器平台为WINDOWS的机器,从网络调用WMIC接口获取服务器的信息,代码如下:

# -*- coding: utf-8 -*-
import wmi
import sys
reload(sys)
import xlsxwriter
sys.setdefaultencoding('utf-8')
import time


def trywexrestrdiskp(files):
    nowtime=time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))+""
    filenames=nowtime+'systeminfo.xlsx'
    workbook1 = xlsxwriter.Workbook(filenames)
    worksheet = workbook1.add_worksheet()
    t1='自定义WINDOWS服务器运行情况'
    format=workbook1.add_format()
    format.set_bold()
    yellow=workbook1.add_format({'align':'center','valign':'vcenter','font_size':22,'fg_color':'FFC1C1'})
    yellow.set_bold()
    worksheet.merge_range('A1:I1',t1,yellow)
    worksheet.set_row(0, 38)
    worksheet.set_column("A:A",20)
    worksheet.set_column("B:B",11)
    worksheet.set_column("C:C",12)
    worksheet.set_column("D:D",12)
    worksheet.set_column("E:E",20)
    title=[u'IP地址',u'CPU使用率%',u'内存总量M',u'内存使用率%',u'巡检结果']
    format=workbook1.add_format()
    format=workbook1.add_format({'align':'center','valign':'vcenter'})
    format.set_bold()
    worksheet.write_row('A2',title,format)
    worksheet.set_row(1, 25)
    row=2
    try:
        myfile=open(files,'r')
        for line in myfile:
            con=line.split()
            h=con[0]
            u=con[1]
            p=con[2]
            try:
                conn =  wmi.WMI(computer=h,user=u,password=p)
                cu=0
                cn=0
                for cpu in conn.Win32_Processor():
                    cl=int(cpu.LoadPercentage)
                    cu=cu+cl
                    cn=cn+1
                    cp=("%0.2f%%" %(cu/cn))  #CPU使用率
                mcs=conn.Win32_ComputerSystem()
                mos=conn.Win32_OperatingSystem()
                mpfu=int(mcs[0].TotalPhysicalMemory)/1024/1024  #内存总量M
                mfree=int(mos[0].FreePhysicalMemory)/1024
                um=mpfu-mfree
                mp=("%0.2f%%" %(100*um/mpfu))                   #内存使用率

                worksheet.write(row,0,h)
                worksheet.write(row,1,cp)
                worksheet.write(row,2,mpfu)
                worksheet.write(row,3,mp)
                remark="正常"
                try:
                    # b=round(float(mp))
                    # print b
                    if int(100*um/mpfu)>80:
                        remark="评估是否扩内存"              #内存使用率达到80%进行提示
                except Exception:
                    remark="NONE"
                worksheet.write(row,4,remark)

                n=5
                for disk in conn.Win32_LogicalDisk (DriveType=3):
                    worksheet.set_column(n,n,10)
                    worksheet.write(row,n,"分区 "+disk.DeviceID)
                    worksheet.set_column(n+1,n+1,15)
                    worksheet.write(row,n+1,"分区大小 "+str(int(disk.Size)/1073741824) + "G")
                    worksheet.set_column(n+2,n+2,20)
                    try:
                        du=int(int(disk.Size)-int(disk.FreeSpace))
                        dp=("%0.2f%%" %(100*du/int(disk.Size)))
                        if int(100*du/int(disk.Size))>80:
                            formatmred = workbook1.add_format({'bold': True, 'font_color': 'red'})
                            worksheet.write(row,n+2,"分区使用率 "+dp,formatmred)  #磁盘使用率达到80%用红色字体显示
                        else:
                            worksheet.write(row,n+2,"分区使用率 "+dp)
                    except Exception:

                        worksheet.write(row,n+2,"分区使用率无法计算")
                    n=n+3


            except Exception:
                print h +" error"
                worksheet.write(row,0,h)
                worksheet.write(row,1,"none")
                worksheet.write(row,2,"none")
                worksheet.write(row,3,"none")
            row=row+1
    except Exception:
        print "cannot open files"

    workbook1.close()


trywexrestrdiskp("windows.txt")

同一目录的windows.txt,写上IP用户名密码,也可以写进数据库再加密读取比较安全

 

运行脚本,输出的格式和上一篇博文CENTOS版本的输出格式一样

主要就是能够支持任意多个分区,每个机器只用一行记录

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页