Skip to content

sec_dev

遇见王斌 edited this page Nov 9, 2017 · 2 revisions

日常使用报表二次开发

1 日常使用报表调用关系

日常使用报表 _report 函数

1.1 获取报表的时间范围

time_from = int(time.mktime(startTime))+1
time_till = int(time.mktime(endTime))

1.2 获取需要输出的 host 列表 -- 获取 hostid

host_list = self._hosts_get()

如果没有 --hostid 和 --hostgroupid 参数,此函数将会返回所有的主机列表

1.3 获取对应 hostid 的相关 item 列表

itemid_all_list = self.item_get(host_info[0],itemName)

item_get 调用的是 zabbix 官方 api item.get

此部分不同的导出报表使用的不同的参数进行获取 item 列表,具体如下

  • report-------- 此处调用 item_get 函数根据 item_name 模糊搜索获取某个 host 的 item 列表
  • report_app---- 此处调用 __appitem_get 函数根据特定的 applicationid 获取某个 host 的 item 列表
  • report_key---- 此处调用 __item_get2 函数根据特定的 item key 获取某个 host 的 item 列表,此处为特定搜索,__item_get2 只处理单个 item key 所以返回的列表中只有单个 item

1.4 依次对要查询的 itemid 进行求最大值最小值以及平均值

report_min,report_max,report_avg = self.__trend_get(itemid,time_from,time_till)

这个函数的主要参数就是 itemid 和 time_from,time_till __trend_get 调用的是 zabbix 官方 api trend.get, 同时对返回的数据进行了下求值运算(最大值,最小值,平均值) 计算方法为: 最大值:特定时间范围内,trend.get 获取特定 itemid 的最大值中的最大值 最小值:特定时间范围内,trend.get 获取特定 itemid 的最小值中的最小值 平均值:特定时间范围内,trend.get 获取特定 itemid 的平均值重新求平均值 trend.get 为 zabbix3.0 加的 api,故低于 zabbix3.0 的版本需要使用 history.get 进行计算

1.5 对输出的信息进行处理

if history_type=="3":
    report_min=int(report_min)
    report_max=int(report_max)
    report_avg=int(report_avg)
report_min=str(report_min)
report_max=str(report_max)
report_avg=str(report_avg)

类型是 3 时,结果为整数型的 0 - numeric float; 1 - character; 2 - log; 3 - numeric unsigned; 整型 4 - text.

1.6 输出显示部分

输出显示包括终端表格excel表格输出,主要部分是个列表,即report_output

report_output.append([host_info[0],host_info[2],item_name,report_min,report_max,report_avg])

2 二次开发思路

2.1 数据在 excel 表格中的显示部分

如:某位大哥想输出到 excel 表格中的值自动更换单位,如获取到的 1024B 显示为 1KB 【此需求已实现】

想要对数据进行处理时,可以从第 5 步方面着手处理

2.2 report_key 输出到 excel 中时多个 key 并列显示

目前 report_key 只能输出单 host 单 key 显示,如想要达到以上需求

  • 修改上面步骤第 3 步,修改__item_get2 程序使 __item_get2 可以处理多个 key 返回多个 item 的列表
  • 修改上面步骤第 6 步,修改为对同一个 host 得到的 item 追加后放到 report_output 列表,如 report_output.append([host_info[0],host_info[2],item_name1,report_min1,report_max1,report_avg1,item_name2,report_min2,report_max2,report_avg2])

2.3 添加 IP 列

  • 在第 6 步骤 report_output的列表参数中增加 host_info[3] ,host_info[3]即为host IP

report_output.append([host_info[0],host_info[2],host_info[3],item_name,report_min,report_max,report_avg])

行到水穷处 坐看云起时 good luck