#coding=utf-8 require "aio/core" class Aio::Module::OutputStyle::ExcelTableWps < Aio::Module::OutputStyle include Aio::Module include Aio::Device include Aio::Base::Toolkit::ExcelWps include Aio::Ui::Verbose attr_accessor :worksheet #TitleBanner = "湖北中行现场巡检报告" TitleBanner = "现场巡检报告" def initialize super({ :author => "Elin", :description => "这个模块按照表格输出excel文件, 以WPS运行", :file_suffix => "xls", :platform => ['windows'] }) end def generate excel = WorkBook.new #excel.show excel.display_alerts = false @worksheet = excel.add_worksheet("sheet") # 设置整体列宽 @worksheet.width("a", 18) @worksheet.width("b", 10) @worksheet.width("c", 15) @worksheet.width("d", 15) @worksheet.width("e", 17) @worksheet.width("f", 7) @worksheet.width("g", 7) # 设置整体行高 @worksheet.height(20) # DevicePool.size.times do |t| # device_name = DevicePool[t][0] device_manager.devices.each_key do |device_name| device_klass = device_manager[device_name] device_info = { :device_klass => device_klass, :manager_ip => "", #DevicePool[t][1], #:device_index => t } case device_klass when Cisco, H3C, Maipu generate_table(device_info) else print_error "生成为空白表格的设备: #{device_name}" generate_white_table(device_name) end end # 设置垂直分页符 @worksheet.pagebreak("H") if @worksheet.has_pagebreak? # 恢复警告提示 excel.display_alerts = true # 保存 begin excel.save(output_file.to_s) rescue Exception => e puts e.message ensure excel.close end end # 当无法生成表格的时候,自动生成一个空白表格,只有设备名称 def generate_white_table(device_name) device_klass = Aio::Device::Cisco.new device_klass.device_name = device_name device_info = { device_klass: device_klass, manager_ip: "" } generate_table(device_info) end def generate_table(device_info) generate_hander generate_software(device_info) generate_clock(device_info) generate_inventory(device_info) generate_line generate_cpu_memory(device_info) generate_environment(device_info) generate_interface(device_info) pagebreak end def generate_hander @worksheet.add_row do |row| row.merge("a", "g") row.height = 30 row << TitleBanner row.style("a", "TitleStyle") end @worksheet.add_row do |row| row.merge("b", "g") row << ["巡检命令", "1.基本信息"] row.style("a", "BoldStyle") row.style("b", "BoldStyle") end end def generate_software(device_info) device_klass = device_info[:device_klass] cmd = CmdSwitch.version(device_klass) @worksheet.add_row do |row| row.merge("c", "d") row.merge("f", "g") row << [ cmd, "设备名称", device_klass.device_name, "设备型号", device_klass.device_info[:device_model] ] row.style("a", "BoldStyle") end @begin_row = @worksheet.current_row_id if !device_info[:manager_ip].empty? manager = device_info[:manager_ip] else manager = device_klass.get_manager_ip end @worksheet.add_row do |row| row.merge("c", "d") row.merge("f", "g") row.height = 24 row << [ cmd, #"管理IP", device_info[:manager_ip], "管理IP", manager, "IOS版本", device_klass.ios_version ] row.wraptext("f") end @worksheet.add_row do |row| row.merge("c", "g") row << [ cmd, "IOS特性集", device_klass.soft_image ] end @worksheet.add_row do |row| row.merge("c", "g") row << [ cmd, "运行时间", device_klass.uptime ] end @end_row = @worksheet.current_row_id @worksheet.merge("a#{@begin_row}", "a#{@end_row}") end def generate_clock(device_info) device_klass = device_info[:device_klass] cmd = case device_klass when Cisco, Maipu "show clock" when H3C "display clock" end @worksheet.add_row do |row| row.merge("c", "g") tmp = device_klass.clock time = Aio::Base::Toolkit::Date.time_to_s(tmp) time.insert(0, "`") if time.size > 5 row << [ cmd, "巡检时间", time ] row.style("a", "BoldStyle") end end def generate_inventory(device_info) device_klass = device_info[:device_klass] cmd = CmdSwitch.inventory(device_klass) @worksheet.add_row do |row| row.merge("b", "c") row.merge("f", "g") row << [ cmd, "硬件信息", "接口卡型号", "序列号", "备注" ] row.style("a", "BoldStyle") row.wraptext("a") end @begin_row = @worksheet.current_row_id # 循环添加inventory条目 inv = device_klass.inventory inv.each do |info| @worksheet.add_row do |row| row.merge("b", "c") row.merge("f", "g") row << [ cmd, info[:name], info[:pid], info[:sn], "" ] row.wraptext("b") row.wraptext("e") end end # 当不足10条的时候,填充空白条目至10条 if inv.size < 10 (10 - inv.size).times do |t| @worksheet.add_row do |row| row.merge("b", "c") row.merge("f", "g") row << [ cmd, "", "", "", "" ] end end end @end_row = @worksheet.current_row_id @worksheet.merge("a#{@begin_row}", "a#{@end_row}") end def generate_line @worksheet.add_row do |row| row.merge("b", "g") row << [ "", "2.运行状态巡检信息采集" ] row.style("b", "BoldStyle") end @worksheet.add_row do |row| row.merge("d", "e") row.merge("f", "g") row << [ "", "项目", "内容", "结果", "备注" ] row.style("b", "BoldStyle") row.style("c", "BoldStyle") row.style("d", "BoldStyle") row.style("f", "BoldStyle") end end def generate_cpu_memory(device_info) device_klass = device_info[:device_klass] cmd_cpu = CmdSwitch.cpu(device_klass) cmd_mem = CmdSwitch.memory(device_klass) @worksheet.add_row do |row| row.merge("d", "e") row.merge("f", "g") row << [ cmd_cpu, "路由状态", "cpu 利用率(%)", device_klass.cpu_percent, "" ] row.style("a", "BoldStyle") end @begin_row = @worksheet.current_row_id @worksheet.add_row do |row| row.merge("d", "e") row.merge("f", "g") row << [ cmd_mem, "路由状态", "memory 利用率(%)", device_klass.memory_percent, "" ] row.style("a", "BoldStyle") end end def generate_environment(device_info) device_klass = device_info[:device_klass] cmd = case device_klass when Cisco ["show environment"] * 3 when H3C [nil, "display power", "display fan"] when Maipu ["show environment", "show system power", "show system fan" ] else ["show environment"] * 3 end unless cmd[0].nil? @worksheet.add_row do |row| tmp = alarm_text(device_klass.warning_env?) row.merge("d", "e") row.merge("f", "g") row << [ cmd[0], "路由状态", "温度", tmp, "" ] row.style("a", "BoldStyle") end end beg_row = @worksheet.current_row_id @worksheet.add_row do |row| tmp = alarm_text(device_klass.warning_env?) row.merge("d", "e") row.merge("f", "g") row << [ cmd[1], "路由状态", "电源", tmp, "" ] row.style("a", "BoldStyle") end @worksheet.add_row do |row| tmp = alarm_text(device_klass.warning_env?) row.merge("d", "e") row.merge("f", "g") row << [ cmd[2], "路由状态", "风扇", tmp, "" ] row.style("a", "BoldStyle") end end_row = @worksheet.current_row_id # 如果cmd都是一样的,那么就合并 if cmd[0] == cmd[1] and cmd[1] == cmd[2] @worksheet.merge("a#{beg_row}", "a#{end_row}") end end def generate_interface(device_info) device_klass = device_info[:device_klass] cmd = CmdSwitch.interface(device_klass) @worksheet.add_row do |row| tmp = alarm_text(device_klass.warning_env?) row.merge("d", "e") row.merge("f", "g") row << [ cmd, "路由状态", "接口状态", tmp, "" ] row.style("a", "BoldStyle") end @end_row = @worksheet.current_row_id @worksheet.merge("b#{@begin_row}", "b#{@end_row}") end def pagebreak @worksheet.current_row.pagebreak end def alarm_text(bool) if bool == true return "正常 □ 异常 ■" else return "正常 ■ 异常 □" end end end