#!/usr/bin/env python #coding:utf-8 import json with open('minfo') as f,open('minfoMiddle','w') as fw: for data in f.readlines(): if '-----' in data or 'mem_total' in data or 'num_cpus' in data: continue fw.write(data) with open('minfoMiddle') as f,open('minfoMiddle1','w') as fw: for data in f.readlines(): if data.startswith('minion-'): fw.write(' ') data = data.strip(' ') data = data + ' ' fw.write(data)
salt取到的数据:salt '*' grains.item num_cpus mem_total localhost > minfo,然后通过上面的代码处理,然后将minfoMiddle1里的内容直接粘贴到excel里即可。
说明:
minfo里的数据如下:
minion-34.16: ---------- localhost: nkt-service01-34-16 mem_total: 7872 num_cpus: 4 minion-34.12: ---------- localhost: recommend-job-34-12 mem_total: 7872 num_cpus: 4
minfoMiddle里的数据如下:
minion-34.16: localhost: nkt-service01-34-16 7872 4 minion-34.12: localhost: recommend-job-34-12 7872 4 #去掉无用的信息,便于之后的更好处理。
minfoMiddle1里的数据:
minion-34.16: localhost: nkt-service01-34-16 7872 4 minion-34.12: localhost: recommend-job-34-12 7872 4 minion-34.13: localhost: mktadmin-34-13 7872 4 #这些数据可以直接粘贴到excel里
以上统计的是不包含磁盘使用的数据,下面将加上磁盘使用。
salt不能直接取到磁盘信息(后来得知可以通过disk.usage获取单个分区的使用情况),使用salt "*" cmd.run 'df -hl' | egrep -v '/dev/sda2|/dev/sda1|tmpfs' > /tmp/diskinfo得到硬盘信息,然后把硬盘和上面取到的cpu、内存、主机名信息整合,代码如下:
#!/usr/bin/env python #coding:utf-8 list1 = [] dict_disk = {} with open('diskinfoMiddle1') as f: for data in f.readlines(): key = data.split(':')[0] value = data.split(':')[1] dict_disk[key] = value with open('minfoMiddle') as f: for data in f.readlines(): list1.append(data) flag = 0 while flag == 0: for i in list1: if 'minion-38.132' in i: #最后一个salt节点,循环到这个后就退出while循环 flag = 1 if i.startswith('minion-'): delmh = i.strip(': ') index1 = list1.index(i, ) if dict_disk.get(delmh): list1.insert(index1 + 5, dict_disk[delmh]) del dict_disk[delmh] else: print('没找到:',delmh) with open('ccc','w') as f: for i in list1: f.write(i) with open('ccc') as f,open('ccc1','w') as fw: for data in f.readlines(): if data.startswith('minion-'): fw.write(' ') data = data.strip(' ') data = data + ' ' fw.write(data)
minfoMiddle的内容同上;
diskinfoMiddle1的内容如下:
minion-34.12: /dev/sda8 47G 11G 33G 25% /home minion-34.13: /dev/sda8 47G 8.2G 36G 19% /home minion-34.15: /dev/sda8 47G 877M 44G 2% /home minion-34.16: /dev/sda8 47G 861M 44G 2% /home #通过salt得到/home的分区情况后,再处理成K-V的形式。
ccc1文件里的内容可以直接拷贝到excel里。
以下是网上搜的方法,目前用以下方法统计一台服务器的信息可以,但是统计“*”的总是报异常程序退出,代码如下:
#!/usr/bin/env python #coding:utf-8 import salt.client as sc import json ###salt调用 local = sc.LocalClient() ###目标主机指定 tgt = "minion-25.183" ###获取grains,disk信息 grains = local.cmd(tgt,"grains.items") print grains diskusage = local.cmd(tgt,"disk.usage") cols = "主机名,IP地址,内存(GB),CPU核数,操作系统,数据盘/data(GB)" ###打开一个.csv文件,以便写入 ret_file = open("ret.csv","w") ###首先写入开头,有点字段名的意思 ret_file.write(cols + " ") try: for i in grains.keys(): ###可能一些主机没有/data数据盘1048576是1024x1024 if "/home" not in diskusage[i]: print "diskusage" + ":" + "have no /data disk" else: data_vol = int(diskusage[i]["/home"]["1K-blocks"]) print "diskusage" + ":" , data_vol / 1048576 ###去掉127.0.0.1这个地址 ipv4 = str(grains[i]["ipv4"]).replace(", '127.0.0.1'","") ###因为一些历史遗留问题,这里取得不是主机名,而是salt-minion的id名,用以判断主要应用 hostname = grains[i]["id"] ipv4 = str(grains[i]["ipv4"]).replace(", '127.0.0.1'","") ipv4 = ipv4.replace(",","and") mem = grains[i]["mem_total"] / 1024 + 1 num_cpu = grains[i]["num_cpus"] OS = grains[i]["osfullname"] + grains[i]["lsb_distrib_release"] if "/home" not in diskusage[i]: disk_data = "None" else: disk_data = data_vol / 1048576 c = "," ###连接并写入 line = hostname + c + ipv4 + c + str(mem) + c + str(num_cpu) + c + str(OS) + c + str(disk_data) ret_file.write(line + " ") except Exception: print "Exception: " finally: ret_file.close()