在使用zabbix的过程中,我们经常有这样的需求:查看CPU或内存等监控项在某一时间段内的graph曲线图。通常的做法是进入"Latest data"界面,找到主机的监控项,点击右侧的"Graph",然后拖动时间条来寻找自己想要查看的时间段。这样做太麻烦了!

    因此我写了一个脚本,来获取监控项在任意时间段的graph,并保存到本地(png格式)。此脚本可以跟Email告警脚本结合起来,实现在告警邮件里附上监控项的graph图片。

    一、效果图

    假如某一设备CPU load的监控项ID为25222。下面是获取25222在不同时间段的graph。

    1、CPU load当前一个小时内的graph,图形宽度800

wKioL1P3TnfjZsGUAAE8q4tNc7M559.jpg

wKiom1P3S-zAX1xiAAKlYalMO54892.jpg

    2、CPU load当前2个小时内的graph,图形宽度800

wKiom1P3TW3Q2pohAAFuroFtmoM909.jpg

wKioL1P3TTKScagAAALLhWDlsTI651.jpg

    3、CPU load在2014-08-20 09:30:00到10:30:00的graph,图形宽度800

wKiom1P3TYTAMdG6AAFBqs00SWc275.jpg

wKioL1P3TerRkCzqAAKuE_9YrxM642.jpg

    4、CPU load在2014-08-20 09:30:00到10:30:00的graph,图形宽度1000

wKioL1P3TqzxXMM6AAGGpO0Hw24658.jpg

wKioL1P3Tgzxqt2aAAOfN3tYV-M164.jpg

    二、getItemGraph.sh脚本

    1、脚本功能

    获取某一个监控项在某个时间段的graph,默认时间段为当前时间一个小时以内,可根据传入参数而改变。graph以png格式保存在/tmp/graph目录。

    2、脚本命令行参数

wKiom1P6jsfyL5jzAAHlC4pkMy8344.jpg

    脚本共有3个必需参数,3个可选参数,一个help参数:

    -U USERNAME     zabbix登录用户名,必需参数

    -P PASSWORD     zabbix登录密码,必需参数

    -I ITEMID       监控项ID,必需参数

    -s STIME        graph起始时间start time,可选参数,格式:年月日时分秒

                    如果不传入此参数,则stop time默认为当前时间。

    -p PERIOD       graph持续时长,可选参数,默认为一个小时

                    一般情况下:stop time = start time + period

    -w WIDTH        graph图形宽度,默认为800

    -h              输出帮助信息然后退出

    执行脚本前,需要先修改脚本内的ZBX_URL变量:

1
2
# 自行修改ZBX_URL变量,改为你的zabbix的web访问地址,其他变量不需要改变
ZBX_URL= "http://zabbix-frontend-hostname/zabbix"

    3、脚本执行实例

    获取当前一个小时内的graph

1
shell # ./getItemGraph.sh -U Admin -P xxxx -I xxxx

    获取当前两个小时内的graph

1
shell # ./getItemGraph.sh -U Admin -P xxxx -I xxxx -p 7200

    获取自定义时间段内的graph,比如:2014-08-20 09:30:00到11:30:00

1
shell # ./getItemGraph.sh -U Admin -P xxxx -I xxxx -s 20140820093000 -p 7200

    获取自定义时间段内的graph,且图形宽度为1000,比如:2014-08-20 09:30:00到11:30:00

1
shell # ./getItemGraph.sh -U Admin -P xxxx -I xxxx -s 20140820093000 -p 7200 -w 1000

    4、脚本代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
#
# Filename:    getItemGraph.sh
# Revision:    1.0
# Date:        2014/08/22
# Author:      Qicheng
# Email:
# Website:     http://qicheng0211.blog.51cto.com/
# Description: 获取某一个监控项在某个时间段的graph
# Notes:       默认时间段为当前时间一个小时以内,可根据传入参数而改变
#              graph以png格式保存在/tmp/graph目录
#
 
# 自行修改ZBX_URL变量,改为你的zabbix的web访问地址,其他变量不需要改变
ZBX_URL= "http://zabbix-frontend-hostname/zabbix"
 
USERNAME= ""
PASSWORD= ""
ITEMID= ""
STIME= ""
PERIOD=3600
WIDTH=800
GRAPH_DIR= "/tmp/graph"
COOKIE= "/tmp/zabbix_cookie"
CURL= "/usr/bin/curl"
INDEX_URL= "$ZBX_URL/index.php"
CHART_URL= "$ZBX_URL/chart.php"
 
function  help()
{
     cat  << HELP
Usage:
     $0 -U <username> -P <password> -I <itemid> [-s <stime>] [-p <period>] [-w <width>] [-h]
Options:
     -U USERNAME     zabbix login username, mandatory parameter
     -P PASSWORD     zabbix login password, mandatory parameter
     -I ITEMID       itemid, integer, mandatory parameter
     -s STIME        graph start  time , integer
                     Example: 20140820093000
     -p PERIOD       period  in  seconds, integer
                     Default: $PERIOD
     -w WIDTH        graph width, integer
                     Default: $WIDTH
     -h              show this help and  exit
HELP
         exit  1
}
 
function  check_integer()
{
     # 判断参数,如果不是整数返回1
     local  ret=` echo  "$*"  sed  's/[0-9]//g' `
     [ -n  "$ret"  ] &&  return  1
     return  0
}
 
if  [ $ # == 0 ]; then
     help
fi
 
while  getopts  U:P:I:s:p:w:h OPTION; do
     case  $OPTION  in
         U) USERNAME=$OPTARG
         ;;
         P) PASSWORD=$OPTARG
         ;;
         I) ITEMID=$OPTARG
         check_integer  "$ITEMID"  || {  echo  "ERROR: Field 'ITEMID' is not integer." exit  1;}
         ;;
         s) STIME=$OPTARG
         check_integer  "$STIME"  || {  echo  "ERROR: Field 'STIME' is not integer." exit  1;}
         ;;
         p) PERIOD=$OPTARG
         check_integer  "$PERIOD"  || {  echo  "ERROR: Field 'PERIOD' is not integer." exit  1;}
         [ $PERIOD -lt 3600 -o $PERIOD -gt 63072000 ] && {  echo  "ERROR: Incorrect value $PERIOD for PERIOD field: must be between 3600 and 63072000." exit  1;}
         ;;
         w) WIDTH=$OPTARG
         check_integer  "$WIDTH"  || {  echo  "ERROR: Field 'WIDTH' is not integer." exit  1;}
         ;;
         h|\?) help
         ;;
     esac
done
 
# USERNAME、PASSWORD、ITEMID为必需参数
[ -z  "$USERNAME"  -o -z  "$PASSWORD"  -o -z  "$ITEMID"  ] && {  echo  "ERROR: Missing mandatory parameter." ; help;}
# 如果没有传入STIME参数,STIME的值为当前时间减去PERIOD
"$STIME"  ==  ""  ] && STIME=` date  -d  "now -${PERIOD} second"  +%Y%m%d%H%M%S`
 
echo  USERNAME=$USERNAME PASSWORD=$PASSWORD ITEMID=$ITEMID STIME=$STIME PERIOD=$PERIOD WIDTH=$WIDTH
 
if  [ ! -s  "$COOKIE"  ]; then
     # 如果cookie文件不存在或者为空,则重新登录zabbix,并保存cookie文件
     ${CURL} -c ${COOKIE} -d  "name=${USERNAME}&password=${PASSWORD}&autologin=1&enter=Sign+in"  $INDEX_URL |  egrep  -o  "(Login name or password is incorrect|Account is blocked.*seconds)"
     # 如果登录失败则退出脚本,并清空cookie文件
     [ ${PIPESTATUS[1]} - eq  0 ] && { :> "$COOKIE" exit  1;}
fi
 
zbx_sessionid=$( grep  'zbx_sessionid'  ${COOKIE} |  awk  '{printf $NF}' )
post_item_get=$(curl -X POST -H  'Content-Type: application/json-rpc'  -d "
{
     \"jsonrpc\": \"2.0\",
     \"method\": \"item.get\",
     \"params\": {
         \"output\": \"extend\",
         \"itemids\": \"$ITEMID\",
         \"filter\": {
             \"value_type\": [
             \"0\",\"3\"
             ]
         }
     },
     \"auth\": \"$zbx_sessionid\",
     \" id \": 1
}" ${ZBX_URL} /api_jsonrpc .php 2> /dev/null )
if_numeric=$( echo  "$post_item_get"  grep  'value_type' )
[ -z  "$if_numeric"  ] && {  echo  "The value of the item is not a valid numeric value." exit  1;}
 
[ -d  "$GRAPH_DIR"  ] ||  mkdir  -p  "$GRAPH_DIR"
PNG_PATH= "$GRAPH_DIR/$ITEMID.$STIME.$PERIOD.${WIDTH}.png"
# 获取item的graph,保存为png图片(zabbix2.2版本)
${CURL} -b ${COOKIE} -d  "itemid=${ITEMID}&period=${PERIOD}&stime=${STIME}&width=${WIDTH}"  $CHART_URL >  "$PNG_PATH"
# zabbix3.0版本使用下面的URL
#${CURL} -b ${COOKIE} -d "itemids%5B0%5D=${ITEMID}&period=${PERIOD}&stime=${STIME}&width=${WIDTH}" $CHART_URL > "$PNG_PATH"
[ -s  "$PNG_PATH"  ] &&  echo  "Saved the graph as $PNG_PATH"  ||  echo  "Failed to get the graph."
echo  "If the graph is not correct, please check whether the parameters are correct or clean $COOKIE file."

    脚本文件在附件提供下载,如果有意见请指正~转载请注明出处http://qicheng0211.blog.51cto.com/3958621/1543673