#!/usr/bin/python#-*- coding: utf-8 -*-import xlwtimport sysimport saltimport subprocessimport timetry: import jsonexcept ImportError: import simplejson as jsondef cmdrun(): cli=salt.client.LocalClient() print json.dumps(cli.cmd(sys.argv[2], 'cmd.run', [sys.argv[3]]))def get_ip_address(tag): cli=salt.client.LocalClient() result = eval(json.dumps(cli.cmd(tag,'grains.item',['ipv4'])))# print json.dumps(cli.cmd(tag,'grains.item','ipv4')) return ','.join(result[tag]['ipv4'])def get_cpu_usage(tag): cli=salt.client.LocalClient() result = eval(json.dumps(cli.cmd(tag,'ps.cpu_percent'))) return result.values()[0]def get_fsuser_usage(tag): cli=salt.client.LocalClient() result = eval(json.dumps(cli.cmd(tag,'cmd.run',['gci_fs_usage']))) if len(result.values()[0]) !=0: return result.values()[0] def get_obuser_usage(tag): cli=salt.client.LocalClient() result = eval(json.dumps(cli.cmd(tag,'cmd.run',['gci_ob_usage'],timeout=30))) if len(result.values()[0]) !=0: return result.values()[0] def get_total_usage(tag): cli=salt.client.LocalClient() result = eval(json.dumps(cli.cmd(tag,'cmd.run',['gci_total_usage']))) if len(result.values()[0]) !=0: return result.values()[0] #print get_total_usage("s3ceph1")def get_mem_usage(tag): cli=salt.client.LocalClient() result= eval(json.dumps(cli.cmd(tag,'ps.physical_memory_usage'))) return result.values()[0]['percent']def get_disk_usage(tag): cli=salt.client.LocalClient() result= eval(json.dumps(cli.cmd(tag,'ps.disk_partition_usage'))) res='' for i in result.values()[0]: res=res+ str(i['device'])+" "+str(i['percent'])+" " return res#style0=xlwt.easyxf('font: name Times New Roman, color-index red, bold on,height 400', num_format_str='#,##0.00')#style0 = xlwt.easyxf('pattern: pattern solid, fore_colour 5;');#style1=xlwt.easyxf(num_format_str='D-MMM-YY')style0 = xlwt.XFStyle()font = xlwt.Font() font.name = 'Times New Roman'font.bold = Truefont.colour_index = 2font.height = 240style0.font = font style1 = xlwt.XFStyle()style1.num_format_str = '0'font = xlwt.Font() font.name = 'Times New Roman'#font.bold = True#font.colour_index = 2font.height = 200style1.font = font pattern = xlwt.Pattern() # Create the Patternpattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...pattern.pattern_back_colour = 3style0.pattern = pattern # Add Pattern to Stylealignment = xlwt.Alignment() # Create Alignmentalignment.wrap = 0x01alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTEDalignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTEDstyle0.alignment = alignment # Add Alignment to Stylestyle1.alignment = alignment # Add Alignment to Styleborders = xlwt.Borders() # Create Bordersborders.left = xlwt.Borders.THIN # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.borders.right = xlwt.Borders.THINborders.top = xlwt.Borders.THINborders.bottom = xlwt.Borders.THINborders.left_colour = 0x40borders.right_colour = 0x40borders.top_colour = 0x40borders.bottom_colour = 0x40#style = xlwt.XFStyle() # Create Stylestyle0.borders = borders # Add Borders to Stylestyle1.borders = borders # Add Borders to Stylewb = xlwt.Workbook()def host_list(): result=subprocess.Popen("salt-key --list=acc|sed -n '2,$p'",stdout=subprocess.PIPE,shell=True).communicate()[0].split('\n') return result[0:-1]def server_info(hostlist): ws=wb.add_sheet(u'服务器资源统计') ws.col(0).width = 5500 ws.col(1).width = 8000 ws.col(2).width = 5500 ws.col(3).width = 5500 ws.col(4).width = 4500 ws.write(0,0,u'主机名',style0) ws.write(0,1,u'IP地址', style0) ws.write(0,2,u'CPU使用率(%)',style0) #A3 ws.write(0,3,u'内存使用率(%) ',style0) #B3 ws.write(0,4,u'磁盘使用率(%)',style0) #result=subprocess.Popen("salt-key --list=acc|sed -n '2,$p'",stdout=subprocess.PIPE,shell=True).communicate()[0].split('\n') #hostlist= result[0:-1] num=1 for hostname in hostlist: cpu_usage= get_cpu_usage(hostname) mem_usage= get_mem_usage(hostname) disk_usage= get_disk_usage(hostname) ip_address= get_ip_address(hostname) ws.write(num,0,hostname,style1) ws.write(num,1,ip_address,style1) ws.write(num,2,cpu_usage,style1) ws.write(num,3,mem_usage,style1) ws.write(num,4,disk_usage,style1) num=num+1 return numdef fsuser_info(hostlist): ws=wb.add_sheet(u'文件存储统计') ws.col(0).width = 5500 ws.col(1).width = 4000 ws.col(2).width = 4000 ws.col(3).width = 4000 ws.col(4).width = 4000 ws.write(0,0,u'主机名',style0) ws.write(0,1,u'用户名', style0) ws.write(0,2,u'已使用量',style0) #A3 ws.write(0,3,u'总容量',style0) #B3 ws.write(0,4,u'文件总数',style0) num=1 num_total_file=0 for hostname in hostlist: if get_fsuser_usage(hostname): # print get_fsuser_usage(hostname) for i in get_fsuser_usage(hostname).split(";"): if i: user_info= i.split(",") username= user_info[0] used= user_info[1] total= user_info[2] num_file= user_info[3] ws.write(num,0,hostname,style1) ws.write(num,1,username,style1) ws.write(num,2,used,style1) ws.write(num,3,total,style1) ws.write(num,4,num_file,style1) num=num+1 num_total_file=int(num_file)+num_total_file return num,num_total_file #for info in user_info: # print info # print i.split(",")def obuser_info(tag): ws=wb.add_sheet(u'对象存储统计') ws.col(0).width = 5500 ws.col(1).width = 4000 ws.col(2).width = 4000 ws.col(3).width = 5300 ws.write(0,0,u'用户名', style0) ws.write(0,1,u'bucket总数',style0) #A3 ws.write(0,2,u'object总数',style0) #B3 ws.write(0,3,u'用户已用总量(MB)',style0) #B3 obuser_info=get_obuser_usage(tag).split(';')[0:-1] if len(obuser_info)>0: num=1 bucket_total=0 object_total=0 size_total=0 for ob_user in obuser_info: res = ob_user.split(',') username,num_bucket,num_object,total_size = res[0],res[1],res[2],res[3] # print username+" "+num_bucket+" "+num_object+" "+total_size ws.write(num,0,username,style1) ws.write(num,1,num_bucket,style1) ws.write(num,2,num_object,style1) ws.write(num,3,int(total_size)/1024,style1) bucket_total=bucket_total+int(num_bucket) object_total=object_total+int(num_object) size_total=size_total+int(total_size) num=num+1 return num,bucket_total,object_total,size_total #print obuser_infodef make_xls(tag): hostlist=host_list() server_info(hostlist) num_total_fsuser,num_total_file=fsuser_info(hostlist) num_total_obuser,num_bucket_total,num_object_total,num_size_total= obuser_info(tag) total_used,total_avail,total_all=get_total_usage(tag).split(';') #print num_total_fsuser #print num_total_obuser,num_bucket_total,num_object_total,num_size_total #print total_used,total_avail,total_all ws=wb.add_sheet(u'存储汇总表') ws.col(0).width = 4000 ws.col(1).width = 4000 ws.col(2).width = 4000 ws.col(3).width = 6000 ws.col(4).width = 6000 ws.col(5).width = 6000 ws.col(6).width = 4000 ws.col(7).width = 4000 ws.col(8).width = 7000 ws.write(0,0,u'存储已用总量', style0) ws.write(0,1,u'存储剩余总量', style0) ws.write(0,2,u'存储总容量', style0) ws.write(0,3,u'文件存储用户总数', style0) ws.write(0,4,u'文件存储文件总数', style0) ws.write(0,5,u'对象存储用户总数', style0) ws.write(0,6,u'bucket总数', style0) ws.write(0,7,u'object总数', style0) ws.write(0,8,u'对象存储已用总量(MB)', style0) ws.write(1,0,total_used, style1) ws.write(1,1,total_avail, style1) ws.write(1,2,total_all, style1) ws.write(1,3,num_total_fsuser-1, style1) ws.write(1,4,num_total_file, style1) ws.write(1,5,num_total_obuser-1, style1) ws.write(1,6,num_bucket_total, style1) ws.write(1,7,num_object_total,style1) ws.write(1,8,int(num_size_total)/1024,style1) ISOTIMEFORMAT='%Y-%m-%d-%H%M%S' nowtime=time.strftime( ISOTIMEFORMAT, time.localtime() ) sfile=nowtime+'_report.xls' wb.save(sfile) print sfilemake_xls('xxx.xxx.com')