#!/bin/bash
domain_url=”zabbix.yibang.com/api_jsonrpc.php”
#得到auth 加密串。
auth_key=`curl -s -X POST -H ‘Content-Type: application/json’ -d ‘{“jsonrpc”:”2.0″,”method”:”user.login”,”params”:{“user”:”my_username”,”password”:”123456″},”auth”:null,”id”:0}’ $domain_url | jq .result | tr -d \”`
#取所有主机的ID 和 名字 ,保存为 文本 。
curl -s -X POST -H ‘Content-Type: application/json’ -d ‘{“jsonrpc”: “2.0”,”method”:”host.get”,”params”:{“output”:[“host”]},”auth”: “‘$auth_key'”,”id”: 0}’ $domain_url | jq | tr -d \” > /tmp/host_id.txt
curl -X POST -H ‘Content-Type: application/json’ -d ‘{“jsonrpc”: “2.0”,”method”:”host.get”,”params”:{“output”:[“host”]},”auth”: “‘$auth_key'”,”id”: 0}’ $domain_url | jq .result[].host | tr -d \” > /tmp/host_name.txt
echo ‘{“resourcetype”: 0,”resourceid”: “GRAPH_ID”,”width”: 500,”height”: 100,”rowspan”: 1,”colspan”: 1,”x”: SCREEN_X,”y”: SCREEN_Y}’ > /tmp/temp.txt
while read i ; do
echo -e “\n$i”
hostid=`grep -w “$i” -B 1 /tmp/host_id.txt | grep -Eo ‘[0-9]{5}’`
#查找指定主机 的所有图形名,id 。 extend可加【】 ,无[],extend:返回图形的所有参数。
curl -s -X POST -H ‘Content-Type: application/json’ -d ‘{“jsonrpc”: “2.0”,”method”:”graph.get”,”params”:{“output”:”[extend]”,”hostids”: “‘$hostid'”,”sortfield”: “name”},”auth”: “‘$auth_key'”,”id”: 0}’ $domain_url | jq > /tmp/graph.txt
grep -B 1 ‘CPU load’ /tmp/graph.txt | grep -Eo ‘[0-9]{3,6}’ > /tmp/graph_id.txt
grep -B 1 ‘CPU utilization’ /tmp/graph.txt | grep -Eo ‘[0-9]{3,6}’ >> /tmp/graph_id.txt
grep -B 1 ‘Memory free’ /tmp/graph.txt | grep -Eo ‘[0-9]{3,6}’ >> /tmp/graph_id.txt
grep -B 1 ‘Memory utilization’ /tmp/graph.txt | grep -Eo ‘[0-9]{3,6}’ >> /tmp/graph_id.txt
grep -B 1 ‘TCP STATUS’ /tmp/graph.txt | grep -Eo ‘[0-9]{3,6}’ >> /tmp/graph_id.txt
grep -B 1 ‘Network traffic on’ /tmp/graph.txt | grep -Eo ‘[0-9]{3,6}’ >> /tmp/graph_id.txt
grep -B 1 ‘Disk utilization and queue’ /tmp/graph.txt | grep -Eo ‘[0-9]{5,6}’ >> /tmp/graph_id.txt
grep -B 1 ‘Disk space usage’ /tmp/graph.txt | grep -Eo ‘[0-9]{3,6}’ >> /tmp/graph_id.txt
sed -i ‘/^$/d’ /tmp/graph_id.txt
line=`cat /tmp/graph_id.txt | wc -l`
screen_x=(0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1)
screen_y=(0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20)
#如果graph_id.txt文本有10行,那下标为0-9 , 行数为1-10 , 每行行末加个逗号 。 最后一行行末去掉逗号。
graph_json=$(for ((x=0;x<$line;x++)) ; do
line_num=`expr $x + 1`
graph_id=`sed -n “${line_num}p” /tmp/graph_id.txt`
cat /tmp/temp.txt | sed ‘s/GRAPH_ID/’$graph_id’/g’ | sed ‘s/SCREEN_X/’${screen_x[$x]}’/g’ | sed ‘s/SCREEN_Y/’${screen_y[$x]}’/g’ | sed ‘s/$/,/g’
done | sed ‘$s/,$//’)
Y_axis=`expr $((line+2))/2|bc`
## “hsize”:”2″,”vsize”:”‘${Y_axis}'” 这是按2列 , XX 行 , 建的screen .
my_json=’
{“jsonrpc”:”2.0″,”method”:”screen.create”,”params”:{“name”:”‘$i'”,”hsize”:”2″,”vsize”:”‘${Y_axis}'”,”screenitems”: [
‘$graph_json’
]},”auth”:”‘$auth_key'”,”id”:0} ‘
curl -s -X POST -H ‘Content-Type: application/json’ -d “$my_json” $domain_url
done < /tmp/host_name.txt