jvmthread_graph_creater.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #! /usr/bin/env python
  2. # coding: utf-8
  3. import requests
  4. import json
  5. from pprint import pprint
  6. class zabbixApi(object):
  7. def __init__(self):
  8. self.url = 'http://127.0.0.1:9091/zabbix/api_jsonrpc.php'
  9. self.headers = {'Content-Type': 'application/json'}
  10. auth = {
  11. "jsonrpc": "2.0",
  12. "method": "user.login",
  13. "params": {
  14. "user": "duh",
  15. "password":"EPy++z@BUT3X"
  16. },
  17. "id": 1,
  18. "auth":None,
  19. }
  20. response = requests.post(self.url, data=json.dumps(auth), headers=self.headers)
  21. self.authid = json.loads(response.text)['result']
  22. self.colors = ["1A7C11", "F63100", "2774A4", "A54F10", "FC6EA3", "6C59DC", "AC8C14", "611F27", "F230E0", "5CCD18", "BB2A02", "5A2B57", "89ABF8", "274482", "2B5429", "2774A4", "AC8C14", "611F27", "F230E0", "5CCD18", "BB2A02", "5A2B57", "89ABF8", "274482", "8048B4", "2B5429", "FD5434", "790E1F", "87AC4D", "000088", "000088"]
  23. def from_itemid_to_value(self, itemid):
  24. sql = "select value_avg from trends_uint where itemid=%s order by clock desc limit 1" % itemid
  25. def get_hosts(self):
  26. neirong={
  27. "jsonrpc": "2.0",
  28. "method": "host.get",
  29. "params": {
  30. "output": [
  31. "hostid",
  32. "host"
  33. ],
  34. "selectInterfaces": [
  35. "interfaceid",
  36. "ip"
  37. ]
  38. },
  39. "id": 2,
  40. "auth": self.authid
  41. }
  42. response = requests.post(self.url, data=json.dumps(neirong), headers=self.headers)
  43. host_ids = []
  44. for host in response.json()['result']:
  45. host_ids.append(host['hostid'])
  46. return host_ids
  47. # java.pid.thread
  48. def get_jvmthread_host_item(self):
  49. host_ids = self.get_hosts()
  50. filter_result ={}
  51. d = {}
  52. for hostid in host_ids:
  53. neirong={
  54. "jsonrpc": "2.0",
  55. "method": "item.get",
  56. "params": {
  57. "output": "extend",
  58. "hostids": hostid,
  59. "sortfield": "name"
  60. },
  61. "auth": self.authid,
  62. "id": 1
  63. }
  64. response = requests.post(self.url, data=json.dumps(neirong), headers=self.headers)
  65. result = response.json()['result']
  66. l = []
  67. for item in result:
  68. if "Java pid " in item['name']:
  69. l.append(item['itemid'])
  70. if l :
  71. d[hostid] = l
  72. return d
  73. def get_graph_by_name(self,hostid):
  74. neirong={
  75. "jsonrpc": "2.0",
  76. "method": "graph.get",
  77. "params": {
  78. "output": "extend",
  79. "hostids": hostid,
  80. "sortfield": "name"
  81. },
  82. "auth": self.authid,
  83. "id": 1
  84. }
  85. response = requests.post(self.url, data=json.dumps(neirong), headers=self.headers)
  86. result = response.json()['result']
  87. for item in result:
  88. if item['name'] == "jvm threads":
  89. return item["graphid"]
  90. return
  91. # action graph.update / graph.create
  92. def graph_define(self, action, hostid, itemids, *graphid):
  93. gitems = []
  94. for tup in zip(itemids, self.colors):
  95. gitems.append({"itemid":tup[0], "color": tup[1]})
  96. neirong={
  97. "jsonrpc": "2.0",
  98. "method": action,
  99. "params": {
  100. "name": "jvm threads",
  101. "width": 900,
  102. "height":200,
  103. "hostids": hostid,
  104. "gitems": gitems
  105. },
  106. "auth": self.authid,
  107. "id": 1
  108. }
  109. if graphid:
  110. graphid = graphid[0]
  111. neirong["params"]["graphid"] = graphid
  112. response = requests.post(self.url, data=json.dumps(neirong), headers=self.headers)
  113. result = response.json()
  114. print result
  115. def run(self):
  116. host_item = self.get_jvmthread_host_item()
  117. for host,items in host_item.items():
  118. graphid = self.get_graph_by_name(host)
  119. if not graphid:
  120. self.graph_define("graph.create" ,host, items)
  121. else:
  122. self.graph_define("graph.update", host, items, graphid)
  123. if __name__ == "__main__":
  124. zabbixApi().run()