1.1 zabbix api常用操作
#! -*- coding:utf8 -*- import urllib2 import json #定义URL账户密码 url = ‘http://1.1.1.5/zabbix/api_jsonrpc.php‘ username = ‘Admin‘ password = ‘1‘ header = {"Content-Type": "application/json"} ################################ 一:登陆脚本 login.py ########################### #1、定义通过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()) try: message = output[‘result‘] except: message = output[‘error‘][‘data‘] print message quit() return output[‘result‘] #2、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 # 结果是一个token值:cc75ed2a314906a835ac0786266468ac
1、和2、zabbix认证和发送请求获取数据
# 3、获取所有报警信息 def trigger_get(): values = ( { "jsonrpc": "2.0", "method": "trigger.get", "params": { "output": [ "host", "description", "triggerid", "eventid", "templateids" ], "selectGroups": [ "name" ], "selectHosts": [ "name", "host" ], "selectItems": [ "name", "lastvalue", "units" ], "filter": { "value": 1 }, "monitored": 1, "selectLastEvent": "extend", "expandComment": 1, "expandDescription": 1, "sortfield": "priority", "sortorder": "DESC", "withLastEventUnacknowledged":1 }, "auth":auth, # 获取认证session值 "id": 1 } ) result = requestJson(url,values) return result
3、获取所有报警信息
# 4、获取所有报警信息(简单信息) def triggerget(): values = {‘jsonrpc‘: ‘2.0‘, "method":"trigger.get", "params": { "output": [ "triggerid", "description", "priority" ], "filter": { "value": 1 }, "expandData":"hostname", "sortfield": "priority", "sortorder": "DESC" }, ‘auth‘: auth, ‘id‘: 1, } output = requestJson(url,values) return output ‘‘‘ 获取简单的报警信息 [{ u ‘priority‘: u ‘3‘, u ‘triggerid‘: u ‘15579‘, u ‘description‘: u ‘登录用户超过2人报警测试‘ }, { u ‘priority‘: u ‘3‘, u ‘triggerid‘: u ‘13491‘, u ‘description‘: u ‘Zabbix agent on {HOST.NAME} is unreachable for 5 minutes‘ }] ‘‘‘
4、获取所有报警信息(简单信息)
# 5、定义通过ip获取主机id的函数 def ipgetHostsid(ip=‘1.1.1.3‘): values = {‘jsonrpc‘: ‘2.0‘, ‘method‘: ‘host.get‘, ‘params‘: { ‘output‘: [ "host" ], ‘filter‘: { ‘ip‘: ip }, }, ‘auth‘: auth, ‘id‘: ‘3‘ } output = requestJson(url,values) return output # [{u‘host‘: u‘1.1.1.3‘, u‘hostid‘: u‘10255‘}]
5、定义通过ip获取主机id的函数
# 6、定义通过主机id获取开启关闭agent监控 def idupdatehost(status=0,hostid=None): # status=0 启用agent,status=1 禁用agent hostid = ipgetHostsid(ip=‘1.1.1.3‘)[0][‘hostid‘] values = {‘jsonrpc‘: ‘2.0‘, ‘method‘: ‘host.update‘, ‘params‘: { "hostid": hostid, "status": status }, ‘auth‘: auth, ‘id‘: ‘4‘ } output = requestJson(url,values) return output
6、定义通过主机id获取开启关闭agent监控
# 7、定义通过项目hostid获取itemid函数 def getHostsitemsid(hostid=None, itemsname=None): # 查询items表 hostid = ipgetHostsid(ip=‘1.1.1.3‘)[0][‘hostid‘] itemsname = ‘log_user‘ values = {‘jsonrpc‘: ‘2.0‘, ‘method‘: "item.get", "params": { "output": ["itemids","name","key_"], # 输出什么值可以在这里填写 "hostids": hostid, "filter": { # 如果没有给 filter 参数那么就会过滤出这个主机关联的所有item "key_": itemsname, }, }, ‘auth‘: auth, ‘id‘: ‘5‘ } output = requestJson(url, values) if len(output) == 0: return output else: return output[0][‘itemid‘] # 28337 获取 # return output # [{u‘itemid‘: u‘28337‘, u‘key_‘: u‘log_user‘, u‘name‘: u‘login_user‘}]
7、定义通过项目hostid获取itemid函数
# 8、定义通过itemid获取监控项目最近n次,值信息的函数 def getHostsitemsvalue(itemid=None): itemid = getHostsitemsid() values = {‘jsonrpc‘: ‘2.0‘, ‘method‘: "history.get", "params": { "output": "extend", "history": 3, "itemids": itemid, "sortfield": "clock", "sortorder": "DESC", "limit": 1, # 限制显示多少条历史记录 }, ‘auth‘: auth, ‘id‘: ‘6‘ } output = requestJson(url, values) if len(output) == 0: return output else: # return output[0]["value"] # 3 这个监控项获取的最近一次值为 3 return output # [{u‘itemid‘: u‘28337‘, u‘ns‘: u‘205638409‘, u‘value‘: u‘3‘, u‘clock‘: u‘1527743117‘}]
8、定义通过itemid获取监控项目最近n次,值信息的函数
# 9、定义通过 组名 获取groupid 等信息 def groupnameGroupid(groupname=None): groupname = ‘Nginx‘ values = {‘jsonrpc‘: ‘2.0‘, ‘method‘: ‘hostgroup.get‘, "params": { "output": "extend", "filter": { "name": [ groupname ] } }, ‘auth‘: auth, ‘id‘: ‘11‘ } output = requestJson(url,values) return output # [{u‘internal‘: u‘0‘, u‘flags‘: u‘0‘, u‘groupid‘: u‘15‘, u‘name‘: u‘Nginx‘}]
9、定义通过 组名 获取groupid 等信息
# 10、定义通过hostid 和 图形名称 获取图形id graphid的函数 def getgraphid(hostid=None,graphname=None): graphname = ‘loguser‘ values = {‘jsonrpc‘: ‘2.0‘, ‘method‘: ‘graph.get‘, ‘params‘: { "output": [‘graphid‘,‘name‘], # "output": "extend", "hostids": hostid, "sortfield": "name", ‘filter‘: { "name": graphname }, }, ‘auth‘: auth, ‘id‘: ‘14‘ } output = requestJson(url,values) return output # [{u‘graphid‘: u‘811‘, u‘name‘: u‘loguser‘}]
10、定义通过hostid 和 图形名称 获取图形id graphid的函数
# 11、获取这个zabbix server监控的所有主机信息 def getallhost(): values = { "jsonrpc": "2.0", "method": "host.get", "params": { "output": [ "hostid", "host" ], "selectInterfaces": [ "interfaceid", "ip" ] }, "id": 2, "auth": auth } output = requestJson(url,values) return output ‘‘‘ [{ u ‘host‘: u ‘Zabbix server‘, u ‘hostid‘: u ‘10084‘, u ‘interfaces‘: [{ u ‘interfaceid‘: u ‘1‘, u ‘ip‘: u ‘1.1.1.5‘ }] }, { u ‘host‘: u ‘1.1.1.3‘, u ‘hostid‘: u ‘10255‘, u ‘interfaces‘: [{ u ‘interfaceid‘: u ‘3‘, u ‘ip‘: u ‘1.1.1.3‘ }] }] ‘‘‘
11、获取这个zabbix server监控的所有主机信息
原文地址:https://www.cnblogs.com/xiaonq/p/9116301.html
时间: 2024-10-28 14:15:15