Zabbix:zabbix API初體驗(爽到爆)
阿新 • • 發佈:2019-05-15
rap swd 定義 use hid -- con bin graph 小生博客:http://xsboke.blog.51cto.com
-------謝謝您的參考,如有疑問,歡迎交流
當你有大量機器需要監控時,你是否還在做大量的重復性操作(比如你想創建一個聚合圖形,這個聚合圖形下面需要添加上百個graph).
這篇文章也許可以解除你的煩惱,解放你的雙手.
此篇文章通過一個示例 [根據graph的名字創建聚合圖形] 讓大家理解有關zabbix api的使用方法
環境:
python==3.7
requests==2.21.0
文件介紹:
config.py # 存放全局變量
base.py # 程序代碼
1、config.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
http_url = ‘http://192.168.0.1/zabbix/api_jsonrpc.php‘ # 你的zabbix文件api_jsonrpc.php所在的url
headers = {‘Content-Type‘:‘application/json-rpc‘}
jsonrpc = ‘2.0‘
username = ‘admin‘ # zabbix用戶名
password = ‘zabbix‘ # zabbix密碼
2、base.py
#!/usr/bin/env python # -*- coding:utf-8 -*- import json import random import requests import config class base: def __init__(self): self.jsonrpc = config.jsonrpc self.headers = config.headers self.url = config.http_url jsonrpc, result, id = self._get_user_token(user=config.username, passwd=config.password) self.auth = result self.id = id def _get_identifier(self): str = ‘[email protected]#$%^&*()1234567890‘ iden = ‘‘ i = 0 length = 10 while i < length: s = random.choice(str) iden += s i += 1 return iden def _post(self, u, d, h): ‘‘‘ 向zabbix服務器發送post請求 :param u: 請求的url :param d: 提交的數據 :param h: http頭信息 :return: ‘‘‘ jsondata = json.dumps(d) r = requests.post(url=u, data=jsondata, headers=h) r.encoding = ‘utf-8‘ transfer = json.loads(r.text) jsonrpc, result, id = transfer[‘jsonrpc‘], transfer[‘result‘], transfer[‘id‘] return jsonrpc, result, id def _get_user_token(self, user, passwd): ‘‘‘ 獲取token :param user: zabbix用戶名 :param passwd: zabbix密碼 :return: ‘‘‘ data = {‘jsonrpc‘: self.jsonrpc, ‘method‘: ‘user.login‘, ‘params‘: {‘user‘: user, ‘password‘: passwd }, ‘id‘: self._get_identifier(), ‘auth‘: None } jsonrpc, result, id = self._post(u=self.url, d=data, h=self.headers) return jsonrpc, result, id @property def get_host(self): ‘‘‘ 獲取監控的主機信息 :return: ‘‘‘ data = {"jsonrpc": self.jsonrpc, "method": "host.get", "params": {"output": ["hostid", "host" ], "selectInterfaces": ["interfaceid", "ip" ] }, "id": self.id, "auth": self.auth } jsonrpc, result, id = self._post(u=self.url, d=data, h=self.headers) return result @property def get_graph(self): ‘‘‘ 獲取所有的圖形(graph) :return: ‘‘‘ hostid_list = [host[‘hostid‘] for host in self.get_host] result_list = [] for hid in hostid_list: data = {"jsonrpc": self.jsonrpc, "method": "graph.get", "params": {"output": "extend", "hostids": int(hid), "sortfield": "name" }, "id": self.id, "auth": self.auth } jsonrpc, result, id = self._post(u=self.url, d=data, h=self.headers) result_list.extend(result) return result_list @property def get_screen(self): ‘‘‘ 獲取所有的聚合圖形(screen graph) :return: ‘‘‘ data = {"jsonrpc": self.jsonrpc, "method": "screen.get", "params": {"output": "extend", "selectScreenItems": "extend", }, "id": self.id, "auth": self.auth } jsonrpc, result, id = self._post(u=self.url, d=data, h=self.headers) return result def create_screen(self, name, resourceidlist, hsize=3, type=0, ): ‘‘‘ 創建聚合圖形 :param name: 聚合圖形的名稱 :param resourceidlist: 需要加入到具體圖形中的項目id(如果是graph則是graphid) :param hsize: 定義列數 :param type: 指定類型,默認是[圖形] :return: ‘‘‘ vsize = len(resourceidlist) // 3 + 1 x = 0 y = 0 # 生成screenitems需要的item列表 params_list = [] for resourceid in resourceidlist: params_dict = {} params_dict[‘resourcetype‘] = int(type) params_dict[‘resourceid‘] = resourceid params_dict[‘rowspan‘] = 1 params_dict[‘colspan‘] = 1 if x < hsize: params_dict[‘x‘] = x params_dict[‘y‘] = y else: x = 0 y += 1 params_dict[‘x‘] = x params_dict[‘y‘] = y x += 1 params_list.append(params_dict) data = {"jsonrpc": self.jsonrpc, "method": "screen.create", "params": {"name": name, "hsize": hsize, "vsize": vsize, "screenitems": [screenitem for screenitem in params_list] }, "id": self.id, "auth": self.auth } jsonrpc, result, id = self._post(u=self.url, d=data, h=self.headers) return result # 實例化程序,獲取所有的graph信息 handler = base() hostinfo = handler.get_graph # 過濾出所有的圖形名稱 graph_name_list = [] for host in hostinfo: graph_name_list.append(host[‘name‘]) graph_name_list = list(set(graph_name_list)) # 根據圖形名稱,將graphid列表賦值給相應的key dict = {} for graph_name in graph_name_list: gid_list = [] for host in hostinfo: if graph_name == host[‘name‘]: gid_list.append(host[‘graphid‘]) dict[graph_name] = gid_list # 以圖形名稱為分組,生成聚合圖形 for gname,gid_list in dict.items(): handler.create_screen(name=gname,resourceidlist=gid_list)
Zabbix:zabbix API初體驗(爽到爆)