#!/usr/bin/env python
#-- coding:utf-8 --
import salt.client
import re, xlwt
remote_server_name = '*'
remost_cmd = 'df -h |grep -v Filesystem |grep -v tmpfs |grep -v /var/lib/docker |grep -v /alidata1/docker|grep -v udev|grep -v cm_processes|grep -v sh
m|grep -v overlay'
salt_client = salt.client.LocalClient() # 获取执行命令后的所有的服务的键
server_host = salt_client.cmd(remote_server_name,'cmd.run',[remost_cmd])
disk_list_info = []
workbook = xlwt.Workbook(encoding='ascii') # 创建sheet
data_sheet = workbook.add_sheet('disk_info', cell_overwrite_ok=True)
row0 = ['IP', 'diskname', 'all', 'used', 'left', 'usage', 'mount']
data_sheet.write(0, 0, row0[0])
data_sheet.write(0, 1, row0[1])
data_sheet.write(0, 2, row0[2])
data_sheet.write(0, 3, row0[3])
data_sheet.write(0, 4, row0[4])
data_sheet.write(0, 5, row0[5])
data_sheet.write(0, 6, row0[6])
i = 1
for serverhost in server_host.keys():
if server_host[serverhost] == False or re.match(r'.command,\n.', server_host[serverhost]): #将错误数据过滤掉
pass
else:
if re.match(r'.\n.', server_host[serverhost]): #判断机器有几个磁盘,并分别处理数据
disk_dev = server_host[serverhost].splitlines() # 将获取的值以'/n'分割
else:
disk_dev = [server_host[serverhost]]
for row_data in disk_dev:
disk_list = row_data.split( )
print len(disk_list)
if 'or' in disk_list or len(disk_list) < 6: #过滤掉不符合的数据,循环导入表格
pass
else:
data_sheet.write(i, 0, serverhost)
data_sheet.write(i, 1, disk_list[0])
data_sheet.write(i, 2, disk_list[1])
data_sheet.write(i, 3, disk_list[2])
data_sheet.write(i, 4, disk_list[3])
data_sheet.write(i, 5, disk_list[4])
data_sheet.write(i, 6, disk_list[5])
i += 1
workbook.save('/tmp/disk_info_prd.xls') #保存文件