因为在前期需求的讨论中,团队对返回的数据格式有一定要求,所以以下代码中会出现列表中嵌套字典或者多层嵌套的形式,废话不多说,直接上代码:
一:获取CPU相关信息(几核,每个核的占用率)
# CPU特殊指标
if monitor_index == 201:
cpu_list = []
cpu_dic = {}
cpurate = self.queryByWql("\"select LoadPercentage,NumberOfCores from Win32_Processor\" |grep CPU |awk -F '|' '{print $2}'")
percpus = self.queryByWql("\"SELECT DPCRate, NAME From Win32_PerfRawData_PerfOS_Processor Where NAME != '_Total'\" | awk -F '|' 'NR > 2 {print $2\",\"$1}'")
cpurate = map(eval, cpurate)
logger.info(cpurate)
cpu_dic['server_ip'] = desthost
cpu_dic['cpu_info'] = ';'.join(percpus) + ";"
wmi_dic['monitor_value'] = cpurate[0]
cpu_list.append(cpu_dic)
final_dic['cpuinfo'] = cpu_list
#其实最重要还是wql查询语句,用Python实现就像炒菜一样,挺简单的!
二:磁盘分区使用率
# 磁盘分区使用率
elif monitor_index == 204:
part_list = []
partitions = self.queryByWql("\"SELECT Size,FreeSpace FROM Win32_LogicalDisk Where Size > 0\" | awk -F '|' 'NR > 2{print $1\"|\"$2\"|\"$3}'")
for partname in partitions:
part_dict = {}
partinfos = partname.strip('\n').split('|')
part_dict['ip'] = desthost
part_dict['disk_name'] = partinfos[0]
part_dict['disk_free_mem'] = long(partinfos[1])/1024/1024
part_dict['disk_total_mem'] = long(partinfos[2])/1024/1024
part_dict['disk_rate_mem'] = format((long(partinfos[2])-long(partinfos[1]))/float(partinfos[2])*100,'.2f')
part_list.append(part_dict)
final_dic['diskinfo'] = part_list
三:虚拟内存的相关指标
# MEM特殊指标
elif monitor_index == 202:
dispose_method = getattr(self, '_' + special_method)
mem_list = []
#物理内存
mem_dic = {}
#虚拟内存
mem_vir_dic = {}
mem_dic['server_ip'] = desthost
mem_vir_dic['server_ip'] = desthost
#物理内存
free = self.queryByWql("\"Select FreePhysicalMemory from Win32_OperatingSystem\" |grep -v CLASS: |grep -v FreePhysicalMemory |awk -F '|' '{print $1,$2,$3,$4}'")
# 将list的中的str类型转化为int或者float
free = map(eval,free)
total = self.queryByWql("\"Select TotalVisibleMemorySize from Win32_OperatingSystem\" |grep -v CLASS: |grep -v TotalVisibleMemorySize |awk -F '|' '{print $1,$2,$3,$4}'")
total = map(eval,total)
#虚拟内存
free_vir = self.queryByWql("\"Select FreeVirtualMemory from Win32_OperatingSystem\" |grep -v CLASS: |grep -v FreeVirtualMemory |awk -F '|' '{print $1,$2,$3,$4}'")
free_vir = map(eval,free_vir)
total_vir = self.queryByWql("\"Select TotalVirtualMemorySize from Win32_OperatingSystem\" |grep -v CLASS: |grep -v TotalVirtualMemorySize |awk -F '|' '{print $1,$2,$3,$4}'")
total_vir = map(eval,total_vir)
num1 = float((total_vir[0] - free_vir[0]))
num2 = float(total_vir[0])
avg = format(num1/num2*100,'.2f')
#物理内存
mem_dic['mem_total'] = total[0]
mem_dic['mem_free'] = free[0]
mem_dic['mem_rate'] = dispose_method()
wmi_dic['monitor_value'] = dispose_method()
# 1:物理内存 2:虚拟内存
mem_dic['mem_type'] = 1
#虚拟内存
mem_vir_dic['mem_total'] = total_vir[0]
mem_vir_dic['mem_free'] = free_vir[0]
mem_vir_dic['mem_rate'] = avg
mem_vir_dic['mem_type'] = 2
mem_list.append(mem_dic)
mem_list.append(mem_vir_dic)
final_dic['meminfo'] = mem_list
#查询方法类似SQL语句,主要记住Win32_OperatingSystem这个类,其他指标查询方法雷同!
四:安装的软件的相关信息
# 安装软件列表
elif monitor_index == 216:
soft_list = []
resultLines = self.queryByWql("\"Select Name,InstallDate from Win32_Product\" | awk -F '|' 'NR > 2{print $2\"|\"$3}'")
for k, eachline in enumerate(resultLines):
if eachline.find('|') > 0:
softinfos = eachline.strip('\n').split('|')
install_time = softinfos[0]
if install_time is not None:
soft_dic = {}
soft_dic['server_ip'] = desthost
soft_dic['soft_index'] = k
soft_dic['install_time'] = install_time
soft_dic['soft_name'] = softinfos[1]
soft_dic['soft_type'] = ''
soft_list.append(soft_dic)
final_dic['software'] = soft_list
其实写这个底层插件没有什么东西可以学到,但是我学到了AWK这个C的管道命令,对于抓取你所需要的信息很有帮助!之前这个监控程序是用C语言写的,大概写了差不多上万行代码而且还难以理解,用Python写一共才写了大概了300多行!