python 一键添加 zabbix 监控脚本

由于时间关系没有在脚本里把创建主机组和模板的功能,只是单单实现了添加主机的功能

zabbix_tools.py


  1. #!/usr/bin/python 
  2. #coding:utf-8 
  3.  
  4. import json 
  5. import urllib2 
  6. from urllib2 import URLError 
  7. import sys 
  8.  
  9. class ZabbixTools: 
  10.     def __init__(self): 
  11.         self.url = 'http://lihuipeng.blog.51cto.com/zabbix/api_jsonrpc.php' 
  12.         self.header = {"Content-Type":"application/json"
  13.          
  14.          
  15.          
  16.     def user_login(self): 
  17.         data = json.dumps({ 
  18.                            "jsonrpc""2.0"
  19.                            "method""user.login"
  20.                            "params": { 
  21.                                       "user""Admin"
  22.                                       "password""lihuipeng" 
  23.                                       }, 
  24.                            "id"0 
  25.                            }) 
  26.          
  27.         request = urllib2.Request(self.url, data) 
  28.         for key in self.header: 
  29.             request.add_header(key, self.header[key]) 
  30.      
  31.         try
  32.             result = urllib2.urlopen(request) 
  33.         except URLError as e: 
  34.             print "Auth Failed, please Check your name and password:", e.code 
  35.         else
  36.             response = json.loads(result.read()) 
  37.             result.close() 
  38.             #print response['result'] 
  39.             self.authID = response['result'
  40.             return self.authID 
  41.          
  42.     def host_get(self): 
  43.         data = json.dumps({ 
  44.                            "jsonrpc":"2.0"
  45.                            "method":"host.get"
  46.                            "params":{ 
  47.                                      "output":["hostid","name"], 
  48.                                      "filter":{"host":""} 
  49.                                      }, 
  50.                            "auth":self.user_login(), 
  51.                            "id":1
  52.                            }) 
  53.          
  54.         request = urllib2.Request(self.url, data) 
  55.         for key in self.header: 
  56.             request.add_header(key, self.header[key]) 
  57.              
  58.      
  59.         try
  60.             result = urllib2.urlopen(request) 
  61.         except URLError as e: 
  62.             if hasattr(e, 'reason'): 
  63.                 print 'We failed to reach a server.' 
  64.                 print 'Reason: ', e.reason 
  65.             elif hasattr(e, 'code'): 
  66.                 print 'The server could not fulfill the request.' 
  67.                 print 'Error code: ', e.code 
  68.         else
  69.             response = json.loads(result.read()) 
  70.             result.close() 
  71.             print "Number Of Hosts: ", len(response['result']) 
  72.             for host in response['result']: 
  73.                 print "Host ID:",host['hostid'],"Host Name:",host['name'
  74.                  
  75.     def hostgroup_get(self, hostgroupName): 
  76.         data = json.dumps({ 
  77.                            "jsonrpc":"2.0"
  78.                            "method":"hostgroup.get"
  79.                            "params":{ 
  80.                                      "output""extend"
  81.                                      "filter": { 
  82.                                                 "name": [ 
  83.                                                          hostgroupName, 
  84.                                                          ] 
  85.                                                 } 
  86.                                      }, 
  87.                            "auth":self.user_login(), 
  88.                            "id":1
  89.                            }) 
  90.          
  91.         request = urllib2.Request(self.url, data) 
  92.         for key in self.header: 
  93.             request.add_header(key, self.header[key]) 
  94.               
  95.         try
  96.             result = urllib2.urlopen(request) 
  97.         except URLError as e: 
  98.             print "Error as ", e 
  99.         else
  100.             response = json.loads(result.read()) 
  101.             result.close() 
  102.             print "hostgroup : %s ------ id : %s" % (response['result'][0]['name'], response['result'][0]['groupid']) 
  103.             self.hostgroupID = response['result'][0]['groupid'
  104.             return response['result'][0]['groupid'
  105.              
  106.     def template_get(self, templateName): 
  107.         data = json.dumps({ 
  108.                            "jsonrpc":"2.0"
  109.                            "method""template.get"
  110.                            "params": { 
  111.                                       "output""extend"
  112.                                       "filter": { 
  113.                                                  "host": [ 
  114.                                                           templateName, 
  115.                                                           ] 
  116.                                                  } 
  117.                                       }, 
  118.                            "auth":self.user_login(), 
  119.                            "id":1
  120.                            }) 
  121.          
  122.         request = urllib2.Request(self.url, data) 
  123.         for key in self.header: 
  124.             request.add_header(key, self.header[key]) 
  125.               
  126.         try
  127.             result = urllib2.urlopen(request) 
  128.         except URLError as e: 
  129.             print "Error as ", e 
  130.         else
  131.             response = json.loads(result.read()) 
  132.             result.close() 
  133.             print "template : %s ------ id : %s" % (response['result'][0]['name'], response['result'][0]['templateid']) 
  134.             self.templateID = response['result'][0]['templateid'
  135.             return response['result'][0]['templateid'
  136.                  
  137.     def host_create(self, hostip, hostgroupName, templateName): 
  138.         data = json.dumps({ 
  139.                            "jsonrpc":"2.0"
  140.                            "method":"host.create"
  141.                            "params":{ 
  142.                                      "host": hostip, 
  143.                                      "interfaces": [ 
  144.                                                         { 
  145.                                                             "type"1
  146.                                                             "main"1
  147.                                                             "useip"1
  148.                                                             "ip": hostip, 
  149.                                                             "dns": "", 
  150.                                                             "port""10050" 
  151.                                                         } 
  152.                                                     ], 
  153.                                     "groups": [ 
  154.                                                     { 
  155.                                                         "groupid"self.hostgroup_get(hostgroupName) 
  156.                                                     } 
  157.                                                ], 
  158.                                     "templates": [ 
  159.                                                     { 
  160.                                                         "templateid"self.template_get(templateName) 
  161.                                                     } 
  162.                                                   ], 
  163.                                      }, 
  164.                            "auth"self.user_login(), 
  165.                            "id":1                   
  166.         }) 
  167.         request = urllib2.Request(self.url, data) 
  168.         for key in self.header: 
  169.             request.add_header(key, self.header[key]) 
  170.               
  171.         try
  172.             result = urllib2.urlopen(request) 
  173.         except URLError as e: 
  174.             print "Error as ", e 
  175.         else
  176.             response = json.loads(result.read()) 
  177.             result.close() 
  178.             print "host : %s ------ id : %s" % (hostip, response['result']['hostids']) 
  179.             self.hostid = response['result']['hostids'
  180.             return response['result']['hostids'
  181.          
  182.                  
  183.                  
  184. if __name__ == "__main__"
  185.     if len(sys.argv) != 4
  186.         print "Usage: %s ip hostgroupName templateName" % sys.argv[0
  187.         sys.exit(1
  188.          
  189.     test = ZabbixTools() 
  190.     test.host_create(sys.argv[1], sys.argv[2], sys.argv[2]) 
  191.                  
  192.                  
  193.                  
  194.                  
  195.                  

user_login  是帐号密码验证

host_get    是列出监控机  

hostgroup_get  是获取主机组的ID

template_get   是获取模板的ID

host_create    添加主机

运行方式:(前提是test-group这个主机组和test-template这个模板必须存在)

./zabbix_tools.py  192.168.3.100  test-group  test-template   

 

参考文章:

https://www.zabbix.com/documentation/2.0/manual/appendix/api

本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1193026如需转载请自行联系原作者


lihuipeng

上一篇:JS实现局部打印和预览


下一篇:python脚本监控网站状态