之前部署了Zabbix(3.4.4版本)監控環境,由於主機比較多,分的主機組也比較多,新增聚合圖形比較麻煩,故採用python指令碼進行批量新增聚合圖形。指令碼下載地址:https://pan.baidu.com/s/1bpApIPp (密碼:wpts)指令碼操作如下:
[root@zabbix01 ~]# cd /opt/ [root@zabbix01 opt]# ls create_Disk_space_usage_screen.py create_screen.py ====================================================================================== [root@zabbix01 opt]# cat create_screen.py #!/bin/env python import json import urllib2 import argparse import re re_digits = re.compile(r'(\d+)') def emb_numbers(s): pieces=re_digits.split(s) pieces[1::2]=map(int,pieces[1::2]) return pieces def sort_strings_with_emb_numbers2(alist): return sorted(alist, key=emb_numbers) def requestjson(url, values): data = json.dumps(values) req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'}) res = urllib2.urlopen(req, data) output = json.loads(res.read()) return output def authenticate(url, username, password): values = {'jsonrpc': '2.0', 'method': 'user.login', 'params': { 'user': username, 'password': password }, 'id': '0' } output = requestjson(url, values) return output['result'] def gethosts(groupname, url, auth): host_list = {} values = {'jsonrpc': '2.0', 'method': 'hostgroup.get', 'params': { 'output': 'extend', 'filter': {'name': groupname}, 'selectHosts': ['host'] }, 'auth': auth, 'id': '2' } output = requestjson(url, values) for host in output['result'][0]['hosts']: host_list[host['host']]=(host['hostid']) #return host_list hosts_sort = [] for host in sort_strings_with_emb_numbers2(host_list.keys()): hosts_sort.append(host_list[host]) return hosts_sort 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' graphs = [] for host in host_list: values = ({'jsonrpc': '2.0', 'method': 'graph.get', 'params': { select: [selecttype, 'name'], 'output': ['graphid', 'name'], 'hostids': host, 'filter': {'name': name_list}, 'sortfield': 'name' }, 'auth': auth, 'id': '3' }) output = requestjson(url, values) bb = sorted(output['result']) if (graphtype == 0): for i in bb: 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: graph_list.append({ 'resourcetype': graphtype, 'resourceid': graph, 'width': '600', 'height': '100', 'x': str(x), 'y': str(y), 'colspan': '1', 'rowspan': '1', }) x += 1 if x == int(columns): x = 0 y += 1 return graph_list 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 values = {'jsonrpc': '2.0', 'method': 'screen.create', 'params': [{ 'name': screen_name, 'hsize': columns, 'vsize': vsize, 'screenitems': [] }], 'auth': auth, 'id': 2 } for i in graphids: values['params'][0]['screenitems'].append(i) output = requestjson(url, values) def main(): url = 'http://10.0.8.40/api_jsonrpc.php' username = 'Admin' password = 'qwkg@monitor' auth = authenticate(url, username, password) host_list = gethosts(groupname, url, auth) graph_ids = getgraphs(host_list, graphname, url, auth, columns) screencreate(url, auth, screen_name, graph_ids, columns) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-g', dest='groupname', nargs='+', metavar='groupname', type=str, help='which group you want to select') parser.add_argument('-G', dest='graphname', nargs='+', metavar='graphname', type=str, help='which graph you want to select') parser.add_argument('-c', dest='columns', metavar='columns', type=int, help='the screen columns') parser.add_argument('-n', dest='screen_name', metavar='screen_name', type=str, help='the screen name') args = parser.parse_args() groupname = args.groupname graphname = args.graphname columns = args.columns screen_name = args.screen_name main() ====================================================================================== [root@zabbix01 opt]# cat create_Disk_space_usage_screen.py #!/bin/env python import json import urllib2 import argparse import re re_digits = re.compile(r'(\d+)') def emb_numbers(s): pieces=re_digits.split(s) pieces[1::2]=map(int,pieces[1::2]) return pieces def sort_strings_with_emb_numbers2(alist): return sorted(alist, key=emb_numbers) def requestjson(url, values): data = json.dumps(values) req = urllib2.Request(url, data, {'Content-Type': 'application/json-rpc'}) res = urllib2.urlopen(req, data) output = json.loads(res.read()) return output def authenticate(url, username, password): values = {'jsonrpc': '2.0', 'method': 'user.login', 'params': { 'user': username, 'password': password }, 'id': '0' } output = requestjson(url, values) return output['result'] def gethosts(groupname, url, auth): host_list = {} values = {'jsonrpc': '2.0', 'method': 'hostgroup.get', 'params': { 'output': 'extend', 'filter': {'name': groupname}, 'selectHosts': ['host'] }, 'auth': auth, 'id': '2' } output = requestjson(url, values) for host in output['result'][0]['hosts']: host_list[host['host']]=(host['hostid']) #return host_list hosts_sort = [] for host in sort_strings_with_emb_numbers2(host_list.keys()): hosts_sort.append(host_list[host]) return hosts_sort 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' graphs = [] for host in host_list: values = ({'jsonrpc': '2.0', 'method': 'graph.get', 'params': { select: [selecttype, 'name'], 'output': ['graphid', 'name'], 'hostids': host, 'filter': {'name': name_list}, 'sortfield': 'name' }, 'auth': auth, 'id': '3' }) output = requestjson(url, values) bb = sorted(output['result']) if (graphtype == 0): for i in bb: 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: graph_list.append({ 'resourcetype': graphtype, 'resourceid': graph, 'width': '400', 'height': '400', 'x': str(x), 'y': str(y), 'colspan': '1', 'rowspan': '1', }) x += 1 if x == int(columns): x = 0 y += 1 return graph_list 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 values = {'jsonrpc': '2.0', 'method': 'screen.create', 'params': [{ 'name': screen_name, 'hsize': columns, 'vsize': vsize, 'screenitems': [] }], 'auth': auth, 'id': 2 } for i in graphids: values['params'][0]['screenitems'].append(i) output = requestjson(url, values) def main(): url = 'http://10.0.8.40/api_jsonrpc.php' username = 'Admin' password = 'qwkg@monitor' auth = authenticate(url, username, password) host_list = gethosts(groupname, url, auth) graph_ids = getgraphs(host_list, graphname, url, auth, columns) screencreate(url, auth, screen_name, graph_ids, columns) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-g', dest='groupname', nargs='+', metavar='groupname', type=str, help='which group you want to select') parser.add_argument('-G', dest='graphname', nargs='+', metavar='graphname', type=str, help='which graph you want to select') parser.add_argument('-c', dest='columns', metavar='columns', type=int, help='the screen columns') parser.add_argument('-n', dest='screen_name', metavar='screen_name', type=str, help='the screen name') args = parser.parse_args() groupname = args.groupname graphname = args.graphname columns = args.columns screen_name = args.screen_name main() ====================================================================================== 上面兩個指令碼create_screen.py和create_Disk_space_usage_screen.py,指令碼中只需要按照自己的實際zabbix訪問情況修改三處: url username password 前者用於批量生成負載、頻寬、IO等監控的聚合圖形,後者用於生成磁碟監控的聚合圖形。 兩個指令碼唯一不同的就是width和height的值不一樣。 批量生成聚合圖形的操作如下: [root@zabbix01 opt]# python create_screen.py -g '財務系統' -G 'Network traffic on bond0' -c 2 -n '財務系統---Network traffic on bond0' [root@zabbix01 opt]# python create_screen.py -g '財務系統' -G 'CPU load' -c 2 -n '財務系統---CPU load' [root@zabbix01 opt]# python create_screen.py -g '財務系統' -G 'CPU utilization' -c 2 -n '財務系統---CPU utilization' [root@zabbix01 opt]# python create_screen.py -g '財務系統' -G 'Memory usage' -c 2 -n '財務系統---Memory usage' [root@zabbix01 opt]# python create_screen.py -g '財務系統' -G 'Read and Write speed on /' -c 2 -n '財務系統---Read and Write speed on /' [root@zabbix01 opt]# python create_screen.py -g '財務系統' -G 'Read and Write speed on /data' -c 2 -n '財務系統---Read and Write speed on /data' [root@zabbix01 opt]# python create_Disk_space_usage_screen.py -g '財務系統' -G 'Disk space usage /' -c 3 -n '財務系統---Disk space usage /' [root@zabbix01 opt]# python create_Disk_space_usage_screen.py -g '財務系統' -G 'Disk space usage /data' -c 3 -n '財務系統---Disk space usage /data' 對於以上命令中的引數解釋: -g 要顯示zabbix的群組(注意這個是在zabbix介面的"圖形"裡顯示的"群組"選項,不能隨便寫,必須是存在zabbix裡的)。 -G 要顯示的zabbix圖形(注意這個是在zabbix介面的"圖形"裡顯示的"圖形"選項,不能隨便寫,必須是存在zabbix裡的)。 -c 顯示幾列,注意要調整指令碼里的:'width': ,'height': 引數來設定大小。 -n 在screen 裡面顯示的名稱。 上面的命令的意思就是:把"財務系統"組裡面每個伺服器的bond0網路卡、load負載、磁碟等監控圖形,接每列4個圖形(磁碟的按美3個圖形)的顯示在聚合圖形裡。 同理:如果新增其他主機組,即將上面的"財務系統" 統一替換掉即可!
如果後續zabbix監控的主機群組裡又追加了新機器,那麼就把之前生產的該群組的聚合圖形刪除,然後再按照上面的指令碼命令再次重新生成聚合圖形,這樣新加入到主機群組內機器的聚合圖形就會顯示出來了。