import xlsxwriter
class DrawChart(): def __init__(self,workbook): self.workbook = xlsxwriter.Workbook(workbook)
def cpu_takeSecond(self,elem): return int(elem[3]) def mem_taskSecond(self,elem): return int(elem[1])
def CreateDiskTable(self,worksheet,address,data,section): worksheet = self.workbook.add_worksheet(worksheet) merge_format = self.workbook.add_format( {'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#EEAEEE'})
header_count = len(data[1]) merge_format1 = self.workbook.add_format( {'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#AEEEEE'})
header_range = "A{}:B{}".format(section,section+header_count) worksheet.write_row(header_range, data[0], merge_format1)
header_merge_range = "A{}:B{}".format(section,section) worksheet.merge_range(header_merge_range, "巡检IP地址", merge_format1)
address_merge_range = "A{}:B{}".format(section+1,section+header_count) worksheet.merge_range(address_merge_range, address , merge_format)
merge_format2 = self.workbook.add_format( {'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC'})
index_range = "C{}".format(section+1) worksheet.write_column(index_range, data[1], merge_format2) index_range = "C{}:C{}".format(section+1,section+1) worksheet.set_column(index_range, 30)
merge_format3 = self.workbook.add_format( {'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "D{}".format(section + 1) worksheet.write_column(index_range, data[2], merge_format3)
index_range = "D{}:D{}".format(section + 1, section + 1) worksheet.set_column(index_range, 20)
merge_format4 = self.workbook.add_format( {'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'}) index_range = "E{}".format(section + 1) worksheet.write_column(index_range, data[3], merge_format4) index_range = "E{}:E{}".format(section + 1, section + 1) worksheet.set_column(index_range, 20)
merge_format5 = self.workbook.add_format( {'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "F{}".format(section + 1) worksheet.write_column(index_range, data[4], merge_format5) index_range = "F{}:F{}".format(section + 1, section + 1) worksheet.set_column(index_range, 20)
return section + header_count + 3
def CreateCpuUsedTable(self,worksheet,header,data): worksheet = self.workbook.add_worksheet(worksheet)
head_style = self.workbook.add_format({"bold": True, "align": "center", "fg_color": "#D7E4BC"}) worksheet.write_row("A1", header, head_style)
worksheet.set_column("A1:D1", 15)
data.sort(key=self.cpu_takeSecond, reverse=True)
for x in range(0,len(data)): worksheet.write_row("A{}".format(x + 2), data[x])
chart = self.workbook.add_chart({"type": "column"}) chart.add_series({ "name": "=CPU利用率!$B$1", "categories": "=CPU利用率!$A$2:$A$10", "values": "=CPU利用率!$B$2:$B$10" }) chart.add_series({ "name": "=CPU利用率!$C$1", "categories": "=CPU利用率!$A$2:$A$10", "values": "=CPU利用率!$C$2:$C$10" }) chart.add_series({ "name": "=CPU利用率!$D$1", "categories": "=CPU利用率!$A$2:$A$10", "values": "=CPU利用率!$D$2:$D$10" })
chart.set_title({"name": "CPU 性能统计柱状图"})
chart.set_x_axis({ 'major_gridlines': { 'visible': True, 'line': {'width': 1.25, 'dash_type': 'dash'} }, })
chart.set_size({'width': 900, 'height': 500}) chart.set_legend({'position': 'top'}) chart.set_table({'show_keys': True})
worksheet.insert_chart("F2", chart)
def CreateMemoryTable(self, worksheet, header, data): worksheet = self.workbook.add_worksheet(worksheet)
head_style = self.workbook.add_format({"bold": True, "align": "center", "fg_color": "#D7E4BC"}) worksheet.write_row("A1", header, head_style)
worksheet.set_column("A1:D1", 15)
data.sort(key=self.mem_taskSecond, reverse=True)
for x in range(0,len(data)): worksheet.write_row("A{}".format(x + 2), data[x])
chart = self.workbook.add_chart({"type": "bar"}) chart.add_series({ "name": "=内存利用率!$B$1", "categories": "=内存利用率!$A$2:$A$10", "values": "=内存利用率!$B$2:$B$10" }) chart.add_series({ "name": "=内存利用率!$C$1", "categories": "=内存利用率!$A$2:$A$10", "values": "=内存利用率!$C$2:$C$10" }) chart.set_title({"name": "内存利用率统计图"}) chart.set_x_axis({ 'major_gridlines': { 'visible': True, 'line': {'width': 1.25, 'dash_type': 'dash'} }, }) chart.set_size({'width': 900, 'height': 400}) chart.set_legend({'position': 'top'}) worksheet.insert_chart("F2", chart)
def CreateCpuLoadAvgTable(self, address,worksheet, header, data): worksheet = self.workbook.add_worksheet(worksheet)
head_style = self.workbook.add_format({"bold": True, "align": "center", "fg_color": "#D7E4BC"}) worksheet.write_row("A1", header, head_style)
worksheet.set_column("A1:D1", 15)
for x in range(0,len(data)): worksheet.write_row("A{}".format(x + 2), data[x])
head_style = self.workbook.add_format({"bold": True, "align": "center", "fg_color": "#D7E4BC"}) worksheet.set_column("A1:D1", 15)
worksheet.write_row("A1", header, head_style) for i in range(0, len(data)): worksheet.write_row("A{}".format(i + 2), data[i])
chart = self.workbook.add_chart({"type": "line"}) chart.add_series({ "name": "=CPU负载数据统计!$B$1", "categories": "=CPU负载数据统计!$A$2:$A$10", "values": "=CPU负载数据统计!$B$2:$B$10" }) chart.add_series({ "name": "=CPU负载数据统计!$C$1", "categories": "=CPU负载数据统计!$A$2:$A$10", "values": "=CPU负载数据统计!$C$2:$C$10" }) chart.add_series({ "name": "=CPU负载数据统计!$D$1", "categories": "=CPU负载数据统计!$A$2:$A$10", "values": "=CPU负载数据统计!$D$2:$D$10" })
chart.set_title({"name": "统计地址: {}".format(address)}) chart.set_size({'width': 900, 'height': 500}) chart.set_legend({'position': 'top'})
worksheet.insert_chart("F2", chart)
def Save(self): self.workbook.close()
if __name__ == "__main__": work = DrawChart("lyshark.xlsx") disk_val = [ ["IP地址", "IP地址", "磁盘路径", "总容量", "剩余容量", "利用率"], ["/etc/system/", "/proc/", "/sys", "/abc/lyshark"], ["1024GG", "2048GB", "111GB", "1111GB"], ["1024GG", "2048GB", "111GB", "22GB"], ["10%", "50%", "20%", "33%"] ] ref = work.CreateDiskTable("磁盘分区统计","127.0.0.1",disk_val,3) print("下个表格开头位置: {}".format(ref)) print("[+] 磁盘数据统计完成")
header = ["主机地址", "CPU内核态", "CPU用户态", "总利用率"] cpu_val = [ ["192.168.1.100", 88, 36, 100], ["192.168.1.200", 98, 89, 128], ["192.168.1.220", 88, 100, 190] ] ref = work.CreateCpuUsedTable("CPU利用率",header,cpu_val) print("[+] CPU利用率统计已完成")
header = ["主机地址", "通用内存利用率", "交换内存利用率"] mem_val = [ ["192.168.1.100", 25, 35], ["192.168.1.200", 44, 57], ["192.168.1.200", 24, 21], ["192.168.1.200", 78, 89] ] ref = work.CreateMemoryTable("内存利用率",header,mem_val) print("[+] 内存利用率统计已完成")
header = ["拉取日期","1分钟负载","5分钟负载","15分钟负载"] cpu_avg_val = [ ["12:11",0.1,0.2,1.3], ["12:12",1.4,3.3,6.9], ["12:13",2.6,3.2,6.9] ] ref = work.CreateCpuLoadAvgTable("127.0.0.1","CPU负载数据统计",header,cpu_avg_val) print("[+] CPU负载统计完成") work.Save()
|