一 应用场景描述
公司自己运营平台服务器租用的是Ucloud云主机,最近老板反映服务器租用成本太贵了,需要降低成本。于是我想到可以通过添加自定义screen来对服务器进行容量规划,即根据CPU使用情况,内存使用情况等将同一个主机分组的所有主机的相关图表组成一个screen便于随时观察使用情况。
二 编写脚本思路
参考地址
http://caiguangguang.blog.51cto.com/1652935/1387401
https://www.zabbix.org/wiki/Python_script_to_create_Screen_from_all_Items/Graphs_of_a_host
使用Zabbix API编写脚本需要了解zabbix的api的使用,可以参见官方文档
https://www.zabbix.com/documentation/2.2/manual/api/reference
这里主要用到的几个API是
hostgroup.get 通过给定的主机分组名称获得这个分组下所有主机的hostid
user.login 用于登录API并得到一个token值
graph.get 通过给定的图表名称获得相关属性
screen.get 用于判断给定screen是否存在
screen.create 用于创建新的screen
主要思路是通过一个API请求发送HTTP请求数据,然后调用指定的API接口实现相应的功能。
在发送请求的时候需要增加一个HTTP请求头Content-Type: application/json
发送数据的格式参考文档就行
可以利用python的urlib2模块发送HTTP请求到http://zabbix.xxx.com/api_jsonrpc.php
创建screen的基本思路是先通过user.login获取给定主机分组的各个主机的hostid,然后根据hostid和给定的graph 名称获取相应的graphid,然后根据获得的graphid和给定的screen名称创建screen
三 参考脚本
#!/usr/bin/env python import urllib2 import sys import json import argparse #定义通过HTTP方式访问API地址的函数,后面每次请求API的各个方法都会调用这个函数 def requestJson(url,values): data = json.dumps(values) req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'}) response = urllib2.urlopen(req, data) output = json.loads(response.read()) # print output try: message = output['result'] except: message = output['error']['data'] print message quit() return output['result'] #API接口认证的函数,登录成功会返回一个Token def authenticate(url, username, password): values = {'jsonrpc': '2.0', 'method': 'user.login', 'params': { 'user': username, 'password': password }, 'id': '0' } idvalue = requestJson(url,values) return idvalue #定义更加主机分组名称获取各个hostid的函数 def getHosts(groupname,url,auth): host_list = [] values = {'jsonrpc': '2.0', 'method': 'hostgroup.get', 'params': { 'output': 'extend', 'filter': { 'name': groupname }, 'selectHosts' : ['hostid','host'], }, 'auth': auth, 'id': '2' } output = requestJson(url,values) for host in output[0]['hosts']: host_list.append(host['hostid']) return host_list #定义获取graphid的函数 def getGraphs(host_list,name_list, url, auth, columns, graphtype=0 ,dynamic=0): if (graphtype == 0): selecttype = ['graphid'] select = 'selectGraphs' if (graphtype == 1): selecttype = ['itemid', 'value_type'] select = 'selectItems' values=({'jsonrpc' : '2.0', 'method' : 'graph.get', 'params' : { 'output' : ['graphid','name'], select : [selecttype,'name'], 'hostids' : host_list, 'sortfield' : 'name', 'filter' : { 'name' : name_list, }, }, 'auth' : auth, 'id' : 3 }) output = requestJson(url,values) bb = sorted(output,key = lambda x:x['graphid']) graphs = [] if (graphtype == 0): for i in bb: print i graphs.append(i['graphid']) if (graphtype == 1): for i in bb: if int(i['value_type']) in (0, 3): graphs.append(i['itemid']) graph_list = [] x = 0 y = 0 for graph in graphs: print "x is " + str(x) print "y is " + str(y) graph_list.append({ "resourcetype": graphtype, "resourceid": graph, "width": "500", "height": "200", "x": str(x), "y": str(y), "colspan": "0", "rowspan": "0", "elements": "0", "valign": "0", "halign": "0", "style": "0", "url": "", "dynamic": str(dynamic) }) x += 1 # print type(x) # print type(columns) if x == int(columns): x = 0 y += 1 # print graph_list return graph_list #定义创建screen的函数 def screenCreate(url, auth, screen_name, graphids, columns): columns = int(columns) if len(graphids) % columns == 0: vsize = len(graphids) / columns else: vsize = (len(graphids) / columns) + 1 #先使用screen.get判断给定的screen name是否存在 values0 = { "jsonrpc" : "2.0", "method" : "screen.get", "params" : { "output" : "extend", "filter" : { "name" : screen_name, } }, "auth" : auth, "id" : 2 } values = { "jsonrpc": "2.0", "method": "screen.create", "params": { "name": screen_name, "hsize": columns, "vsize": vsize, "screenitems": [] }, "auth": auth, "id": 2 } output0 = requestJson(url,values0) print output0 #如果给定的screen name不存在则直接创建screen if output0 == []: print "The Given Screen Name Not Exists" print "Creating Screen %s" %screen_name for i in graphids: values['params']['screenitems'].append(i) output = requestJson(url,values) else: #如果给定的screen name已经存在,直接创建screen是不行的, #要么先使用screen.delete把原来的screen删除掉,然后再创建, #要么直接使用screen.update更新原来那个screen, #使用screen.delete会产生新的screenid, #使用screen.update比较合理一点。 print "The Given Screen Name Already Exists" update_screenid=output0[0]["screenid"] print update_screenid print "Updating Screen Name %s Screen ID %s" %(screen_name,update_screenid) values1 = { "jsonrpc" : "2.0", "method" : "screen.update", "params" : { "screenid" : update_screenid, "screenitems": [] }, "auth" : auth, "id" : 2 } output1 = requestJson(url,values1) print output1 print "Updating Screen Name %s" %screen_name for i in graphids: values1['params']['screenitems'].append(i) output = requestJson(url,values1) def main(): url = 'http://zabbix.xxxxx.com/api_jsonrpc.php' username = 'Admin' password = 'xxxxx' auth = authenticate(url, username, password) host_list = getHosts(groupname,url,auth) # print host_list graph_ids = getGraphs(host_list,graphname, url, auth, columns) screenCreate(url, auth, screenname, graph_ids, columns) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Create Zabbix screen from all of a host Items or Graphs.') parser.add_argument('-G', dest='graphname', nargs='+',metavar=('grah name'), help='Zabbix Host Graph to create screen from') # parser.add_argument('-H', dest='hostname', nargs='+',metavar=('10.19.111.145'), # help='Zabbix Host to create screen from') parser.add_argument('-g', dest='groupname', nargs='+',metavar=('linux server'), help='Zabbix Group to create screen from') parser.add_argument('-n', dest='screenname', type=str, help='Screen name in Zabbix. Put quotes around it if you want spaces in the name.') parser.add_argument('-c', dest='columns', type=int, help='number of columns in the screen') args = parser.parse_args() print args # hostname = args.hostname groupname = args.groupname screenname = args.screenname columns = args.columns graphname = args.graphname if columns is None: columns = len(graphname) # print columns main()
使用方法:
python zabbix_screen.py -g Ucloud_Servers -G 'CPU utilization' -n 'Ucloud_Servers CPU utilization' -c 2
Ucloud_Servers是主机分组名称,CPU utilization是graph名称,Ucloud_Servers CPU utilization是要创建的screen名称,2表示两列
这样每次可以根据主机分组添加自定义screen,如果这个主机分组内有新的主机加入,screen也不会更新,如果有新的主机被删除,这个screen对应的位置将变为空白,可以添加一个crontab定时任务定期执行脚本更新screen。
如设置每天凌晨更新screen,将被删除的主机的graph移除,添加新主机的graph。
0 0 * * * python zabbix_screen.py -g Ucloud_Servers -G 'CPU utilization' -n 'Ucloud_Servers CPU utilization' -c 2
有一种情况是,我定义的Hosts Groups是随时变更的,或者有很多个分组,并且是动态变更的,但是需要添加到screen的graph是确定的,为了能够动态的更加每个分组添加相应graph的screen,最好使用API定期获得有哪些分组。将以上的代码修改一下就可以实现系统自动定期根据各个主机分组添加或更新相应的screen
#新增getHostgroups函数,用于获取定义的各个host group def getHostgroups(url,auth): hostgroup_list = [] values = {'jsonrpc' : '2.0', 'method' : 'hostgroup.get', 'params' : { 'output' : ['groupid','name'], 'sortfield' : 'name', }, 'auth' : auth, 'id' : '2' } output = requestJson(url,values) # print output for hostgroup in output: # print hostgroup['name'] #排除一些不想要的分组 if hostgroup['name'].startswith(('Discovered','Templates','templates')) or hostgroup['name'].endswith(('Templates','templates')): print "Skip this group %s" %hostgroup['name'] else: hostgroup_list.append(hostgroup['name']) return hostgroup_list #去掉传递参数的功能,根据系统获得的host group自动匹配 def main(): url = 'http://zabbix.xxxx.com/api_jsonrpc.php' username = 'Admin' password = 'xxxxx' columns = 2 #定义需要哪些graph graphname_list = ['CPU utilization','CPU load','Mem_Usage','Network traffic on eth0','Network traffic on eth1','Swap usage','CPU jumps'] auth = authenticate(url, username, password) hostgroup_list = getHostgroups(url,auth) print hostgroup_list #根据不同的groupname和graph创建相应的screen for groupname in hostgroup_list: print groupname host_list = getHosts(groupname,url,auth) print host_list for graphname in graphname_list: print graphname graph_ids = getGraphs(host_list,graphname, url, auth, columns) screenname = groupname + ' ' + graphname screenCreate(url, auth, screenname, graph_ids, columns) if __name__ == '__main__': main()
添加定时任务,每天自动添加或更新screen,这样就方便很多了,不用手动去添加大量的screen
0 0 * * * /usr/bin/python /data/tools/zabbix_screen.py