当前位置:Gxlcms > Python > 详解python3-zabbixapi的使用

详解python3-zabbixapi的使用

时间:2021-07-01 10:21:17 帮助过:51人阅读

这篇文章详解python3-zabbixapi的使用

python3 使用zabbix api的一些案例。。具体可以去zabbix官网找API借口,替换一下就可以。

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import urllib.request
  4. import json
  5. import re
  6. url = 'http://xxxxxxxxxxxxxxxx/api_jsonrpc.php'
  7. username = 'xxxxxxxxxxxxxxx'
  8. password = 'xxxxxx'
  9. # 登陆
  10. def requestJson(url, values):
  11. data = json.dumps(values).encode('utf-8')
  12. req = urllib.request.Request(url, data, {'Content-Type': 'application/json-rpc'})
  13. response = urllib.request.urlopen(req, data)
  14. a = response.read().decode(encoding='utf-8')
  15. output = json.loads(a)
  16. # print output
  17. try:
  18. message = output['result']
  19. except:
  20. message = output['error']['data']
  21. print(message)
  22. quit()
  23. return output['result']
  24. ##登陆的API
  25. def authenticate(url, username, password):
  26. values = {'jsonrpc': '2.0',
  27. 'method': 'user.login',
  28. 'params': {
  29. 'user': username,
  30. 'password': password
  31. },
  32. 'id': '0'
  33. }
  34. idvalue = requestJson(url, values)
  35. return idvalue
  36. # auth的值
  37. auth = authenticate(url, username, password)
  38. ##查询组ID {'groupid': '8', 'name': 'Switch'}
  39. def groups(auth):
  40. values = {
  41. "jsonrpc": "2.0",
  42. "method": "hostgroup.get",
  43. "params": {
  44. "output": ["groupid", "name"],
  45. },
  46. 'auth': auth,
  47. 'id': '1'
  48. }
  49. output = requestJson(url, values)
  50. return output
  51. # b = groups(auth)
  52. # print(b)
  53. ##查询主机 {'hostid': '10108',
  54. def hosts(auth):
  55. values = {
  56. "jsonrpc": "2.0",
  57. "method": "host.get",
  58. "params": {
  59. "output": ["groupid", "name"],
  60. "groupids": "8",
  61. },
  62. 'auth': auth,
  63. 'id': '1'
  64. }
  65. output = requestJson(url, values)
  66. return output
  67. host = hosts(auth)
  68. host1 = []
  69. host2 = []
  70. for i in range(len(host)):
  71. host1.append(host[i]['name'])
  72. host2.append(host[i]['hostid'])
  73. host3 = dict(zip(host1, host2))
  74. ##查询主机项目 {'key_': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'itemid': '26399'}
  75. def item(auth):
  76. values = {
  77. "jsonrpc": "2.0",
  78. "method": "item.get",
  79. "params": {
  80. "output": ["itemids", "key_"],
  81. "hostids": "10108",
  82. },
  83. 'auth': auth,
  84. 'id': '1'
  85. }
  86. output = requestJson(url, values)
  87. return output
  88. # print(item(auth))
  89. ##查询项目的历史值 'lastvalue': '-14760.0000'
  90. def his(auth, itemids):
  91. values = {
  92. "jsonrpc": "2.0",
  93. "method": "item.get",
  94. "params": {
  95. "output": "extend",
  96. "history": 0,
  97. "itemids": itemids,
  98. "sortfield": "itemid",
  99. "sortorder": "DESC",
  100. "limit": 1
  101. },
  102. 'auth': auth,
  103. 'id': '1'
  104. }
  105. output = requestJson(url, values)
  106. return output
  107. # print(his(auth,26399))
  108. ##查询触发项目值和监控项目 {'description': 'xxxxxxxxxxxxxxx', 'hostname': 'xxxxxxxxxxxxxxx', 'items': [{'itemid': '26399'}], 'triggerid': '17030'}
  109. def trigger(auth, hostid):
  110. values = {
  111. "jsonrpc": "2.0",
  112. "method": "trigger.get",
  113. "params": {
  114. "output": [
  115. "description",
  116. ],
  117. "filter": {
  118. "hostid": hostid,
  119. },
  120. "selectItems": "",
  121. "sortfield": "hostname",
  122. "sortorder": "DESC"
  123. },
  124. 'auth': auth,
  125. 'id': '1'
  126. }
  127. output = requestJson(url, values)
  128. return output
  129. ###简单使用案例,可查考,根据触发器查找历史。
  130. t1 = trigger(auth, host3[msg['Content']])
  131. t2 = []
  132. t3 = []
  133. for i in range(len(t1)):
  134. t5 = t1[i]['items'][0] ## 'items': [{'itemid': '26399'}]
  135. t6 = his(auth, t5['itemid']) ## his(auth,26399)
  136. t2.append(t1[i]['description']) ##监控项目描述
  137. t3.append(round(float(t6[0]['lastvalue'])) / 1000) ##项目ID 的值
  138. t4 = dict(zip(t2, t3))
  139. t8 = []
  140. for k in t4:
  141. t7 = k + ":" + "{}".format(t4[k]) + "db"
  142. t8.append(t7)
  143. t9 = "\n".join(t8)

以上就是详解python3-zabbixapi的使用的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行