调用zabbix api统计统计某个item的所有机器的history数据
2016-09-29 00:00
363 查看
这个脚本调用zabbix的api去抓取所有有$itemkey(数字类型的有效)的机器的item的指定时间的history的数据,计算出最大,最小和平均值,保存为csv文件,脚本一共输出5列(主机名,itemid,最大值,最小值,平均值)
#!/bin/bash apiurl="http://10.1.x.x/zabbix/api_jsonrpc.php" starttime="2016-09-01 00:00:00" stoptime="2016-09-25 00:00:00" username="username" password="password" itemkey="system.cpu.load[percpu,avg1]" csvfile=/tmp/cpuinfo.csv startts=`date +%s -d "$starttime"` stopts=`date +%s -d "$stoptime"` echo '"hostname","itemid","min","max","avg"' > $csvfile gettoken() { curl -d "{\"jsonrpc\": \"2.0\",\"method\": \"user.login\",\"params\": {\"user\": \"$username\",\"password\": \"$password\"},\"id\":1,\"auth\":null}" -H "Content-Type:application/json" $apiurl 2>/dev/null|sed 's/.*result":"//'|sed 's/".*$//'|| return 1 } getdatabyitemid() { itemdata=`curl -d "{\"jsonrpc\": \"2.0\",\"method\": \"history.get\",\"params\": {\"output\": \"extend\",\"history\": 0,\"itemids\": \"$1\",\"time_from\": $startts,\"time_till\": $stopts},\"id\": 1,\"auth\":\"$token\"}" -H "Content-Type:application/json" $apiurl 2>/dev/null` max=`echo $itemdata|jq .|grep ' "value":'|sed 's/.*": "//'|sed 's/",//'|sort -n|tail -1` min=`echo $itemdata|jq .|grep ' "value":'|sed 's/.*": "//'|sed 's/",//'|sort -n|head -1` lc=`echo $itemdata|jq .|grep ' "value":'|sed 's/.*": "//'|sed 's/",//'|wc -l` count=`echo $itemdata |jq .|grep ' "value":'|sed 's/.*": "//'|sed 's/",//'|awk '{i+=$1}'END'{print i}'` avg=`awk 'BEGIN{printf "%.2f\n", '$count'/'$lc'}'` echo "\"$host\",\"$1\",\"$min\",\"$max\",\"$avg\"" echo "\"$1\",\"$min\",\"$max\",\"$avg\"" >> $csvfile } getitems() { curl -d "{\"jsonrpc\": \"2.0\",\"method\": \"item.get\",\"params\": {\"output\": \"extend\",\"search\": {\"key_\":\"$itemkey\"}},\"id\": 1,\"auth\":\"$token\"}" -H "Content-Type:application/json" $apiurl 2>/dev/null } gethostbyitemids() { host=`curl -d "{\"jsonrpc\": \"2.0\",\"method\": \"host.get\",\"params\": {\"output\": \"extend\",\"itemids\": \"$1\"},\"id\": 1,\"auth\":\"$token\"}" -H "Content-Type:application/json" $apiurl 2>/dev/null |jq .|grep ' "host":'|awk -F'"' '{print $4}'` echo "search host \"$host\" item \"$itemkey\" data....." echo -n "\"$host\"," >> $csvfile } token=`gettoken`|| (echo "get token failed" && exit 1) echo $token itemids=`getitems`&& echo $itemids|jq .|grep ' "itemid":'|sed 's/.*itemid": "//'|sed 's/",//'|while read n;do gethostbyitemids $n;getdatabyitemid $n;done
相关文章推荐
- zabbix 二次开发之调用api接口获取历史数据
- python调用zabbix api接口实时展示数据
- python调用zabbix api接口实时展示数据 推荐
- Zabbix实践(五):基于java的zabbix api调用实现数据共享
- 基于java的zabbix api调用实现数据共享
- C#调用C++的DLL搜集整理的所有数据类型转换方式
- C#调用C++的DLL搜集整理的所有数据类型转换方式
- C#调用“数据链接属性”对话框 显示所有表与所有用户
- 01-Twitter Streaming API的调用 | 02.数据获取
- 数据库所有表中数据行数统计
- 很多机器,每台机器上都有很一部分数据,如何输出分布在所有机器上的所有数据的median
- C++数据类型与C#对应关系 c#调用WINDWOS API时,非常有用
- ArcGIS.Server.9.3和ArcGIS API for Flex在MapTips显示饼图数据统计(九)
- C++数据类型与C#对应关系 c#调用WINDWOS API时,非常有用
- C++数据类型与C#对应关系 c#调用WINDWOS API时,非常有用(转)
- WR调用windows的API实现文本框数据输入
- 打开一个jsp页面默认查询所有数据,调用action
- 淘宝TOP API 缓存实战 解决API频率过高,页面空白,调用不到数据等情况
- 用vbs列出机器上所有能调用的组件
- [转]ArcGIS.Server.9.3和ArcGIS API for Flex在MapTips显示饼图数据统计(九)