zoukankan      html  css  js  c++  java
  • Zabbix 快速创建screen

    1. #!/usr/bin/env python
    2. import urllib2
    3. import json
    4. import argparse
    5. def authenticate(url, username, password):
    6. values ={'jsonrpc':'2.0',
    7. 'method':'user.login',
    8. 'params':{
    9. 'user': username,
    10. 'password': password
    11. },
    12. 'id':'0'
    13. }
    14. data = json.dumps(values)
    15. req = urllib2.Request(url, data,{'Content-Type':'application/json-rpc'})
    16. response = urllib2.urlopen(req, data)
    17. output = json.loads(response.read())
    18. try:
    19. message = output['result']
    20. except:
    21. message = output['error']['data']
    22. print message
    23. quit()
    24. return output['result']
    25. def getGraph(hostname, url, auth, graphtype, dynamic, columns):
    26. if(graphtype ==0):
    27. selecttype =['graphid']
    28. select ='selectGraphs'
    29. if(graphtype ==1):
    30. selecttype =['itemid','value_type']
    31. select ='selectItems'
    32. values ={'jsonrpc':'2.0',
    33. 'method':'host.get',
    34. 'params':{
    35. select: selecttype,
    36. 'output':['hostid','host'],
    37. 'searchByAny':1,
    38. 'filter':{
    39. 'host': hostname
    40. }
    41. },
    42. 'auth': auth,
    43. 'id':'2'
    44. }
    45. data = json.dumps(values)
    46. req = urllib2.Request(url, data,{'Content-Type':'application/json-rpc'})
    47. response = urllib2.urlopen(req, data)
    48. host_get = response.read()
    49. output = json.loads(host_get)
    50. # print json.dumps(output)
    51. graphs =[]
    52. if(graphtype ==0):
    53. for i in output['result'][0]['graphs']:
    54. graphs.append(i['graphid'])
    55. if(graphtype ==1):
    56. for i in output['result'][0]['items']:
    57. if int(i['value_type'])in(0,3):
    58. graphs.append(i['itemid'])
    59. graph_list =[]
    60. x =0
    61. y =0
    62. for graph in graphs:
    63. graph_list.append({
    64. "resourcetype": graphtype,
    65. "resourceid": graph,
    66. "width":"500",
    67. "height":"100",
    68. "x": str(x),
    69. "y": str(y),
    70. "colspan":"1",
    71. "rowspan":"1",
    72. "elements":"0",
    73. "valign":"0",
    74. "halign":"0",
    75. "style":"0",
    76. "url":"",
    77. "dynamic": str(dynamic)
    78. })
    79. x +=1
    80. if x == columns:
    81. x =0
    82. y +=1
    83. return graph_list
    84. def screenCreate(url, auth, screen_name, graphids, columns):
    85. # print graphids
    86. if len(graphids)% columns ==0:
    87. vsize = len(graphids)/ columns
    88. else:
    89. vsize =(len(graphids)/ columns)+1
    90. values ={"jsonrpc":"2.0",
    91. "method":"screen.create",
    92. "params":[{
    93. "name": screen_name,
    94. "hsize": columns,
    95. "vsize": vsize,
    96. "screenitems":[]
    97. }],
    98. "auth": auth,
    99. "id":2
    100. }
    101. for i in graphids:
    102. values['params'][0]['screenitems'].append(i)
    103. data = json.dumps(values)
    104. req = urllib2.Request(url, data,{'Content-Type':'application/json-rpc'})
    105. response = urllib2.urlopen(req, data)
    106. host_get = response.read()
    107. output = json.loads(host_get)
    108. try:
    109. message = output['result']
    110. except:
    111. message = output['error']['data']
    112. print json.dumps(message)
    113. def main():
    114. url ='http://10.1.173.215/zabbix/api_jsonrpc.php'
    115. username ="Admin"
    116. password ="zabbix"
    117. parser = argparse.ArgumentParser(description='Create Zabbix screen from all of a host Items or Graphs.')
    118. parser.add_argument('hostname', metavar='H', type=str,
    119. help='Zabbix Host to create screen from')
    120. parser.add_argument('screenname', metavar='N', type=str,
    121. help='Screen name in Zabbix. Put quotes around it if you want spaces in the name.')
    122. parser.add_argument('-c', dest='columns', type=int, default=3,
    123. help='number of columns in the screen (default: 3)')
    124. parser.add_argument('-d', dest='dynamic', action='store_true',
    125. help='enable for dynamic screen items (default: disabled)')
    126. parser.add_argument('-t', dest='screentype', action='store_true',
    127. help='set to 1 if you want item simple graphs created (default: 0, regular graphs)')
    128. args = parser.parse_args()
    129. hostname = args.hostname
    130. screen_name = args.screenname
    131. columns = args.columns
    132. dynamic =(1if args.dynamic else0)
    133. screentype =(1if args.screentype else0)
    134. auth = authenticate(url, username, password)
    135. graphids = getGraph(hostname, url, auth, screentype, dynamic, columns)
    136. print"Screen Name: "+ screen_name
    137. print"Total Number of Graphs: "+ str(len(graphids))
    138. screenCreate(url, auth, screen_name, graphids, columns)
    139. if __name__ =='__main__':
    140. main()
     
    Usage:
    1. [root@zabbix ~]#./Auto_add_screens.py '10.1.185.114''10.1.185.114'
    2. ScreenName:10.1.185.114
    3. TotalNumber of Graphs:14
    4. {"screenids":["26"]}
     





  • 相关阅读:
    C#中的Json序列化
    c#在sqlserver中使用EF框架
    Mvc中模拟模型
    localdb启动
    List泛型用法(半转载半原创)
    C#中真正的属性
    委托的简介、使用和简单事件
    类嵌套_list泛型_餐馆点菜例
    JavaIO
    JavaIO
  • 原文地址:https://www.cnblogs.com/gyming/p/5781358.html
Copyright © 2011-2022 走看看