在操作zabbix的时候,经常会遇到批量添加,更改的操作,一种方法是可以操作数据库,不过对于某些操作,zabbix数据库外键约束不强,是不太好操作数据库来解决的。另一种方式是使用zabbix的api。

下面是八牛同学对zabbix api做的封装,有兴趣的可以拿来用下,感谢八牛同学的支持。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import  simplejson as json
import  urllib2, subprocess, re, time
class  ZabbixAPIException(Exception):
     pass
class  ZabbixAPI( object ):
     __auth  =  ''
     __id  =  0
     _state  =  {}
     def  __new__( cls * args,  * * kw):
         if  not  cls ._state.has_key( cls ):
             cls ._state[ cls =  super (ZabbixAPI,  cls ).__new__( cls * args,  * * kw)
         return  cls ._state[ cls ]
     def  __init__( self , url, user, password):
         self .__url  =  url.rstrip( '/' +  '/api_jsonrpc.php'
         self .__user  =  user
         self .__password  =  password
         self ._zabbix_api_object_list  =  ( 'Action' 'Alert' 'APIInfo' 'Application' 'DCheck' 'DHost' 'DRule' ,
                 'DService' 'Event' 'Graph' 'Grahpitem' 'History' 'Host' 'Hostgroup' 'Image' 'Item' ,
                 'Maintenance' 'Map' 'Mediatype' 'Proxy' 'Screen' 'Script' 'Template' 'Trigger' 'User' ,
                 'Usergroup' 'Usermacro' 'Usermedia' )
     def  __getattr__( self , name):
         if  name  not  in  self ._zabbix_api_object_list:
             raise  ZabbixAPIException( 'No such API object: %s'  %  name)
         if  not  self .__dict__.has_key(name):
             self .__dict__[name]  =  ZabbixAPIObjectFactory( self , name)
         return  self .__dict__[name]
     def  login( self ):
         user_info  =  { 'user'      self .__user,
                      'password'  self .__password}
         obj  =  self .json_obj( 'user.login' , user_info)
         content  =  self .postRequest(obj)
         try :
             self .__auth  =  content[ 'result' ]
         except  KeyError, e:
             =  content[ 'error' ][ 'data' ]
             raise  ZabbixAPIException(e)
     def  isLogin( self ):
         return  self .__auth ! =  ''
     def  __checkAuth__( self ):
         if  not  self .isLogin():
             raise  ZabbixAPIException( "NOT logged in" )
     def  json_obj( self , method, params):
         obj  =  'jsonrpc'  '2.0' ,
                 'method'   : method,
                 'params'   : params,
                 'auth'     self .__auth,
                 'id'       self .__id}
         return  json.dumps(obj)
     def  postRequest( self , json_obj):
         #print 'Post: %s' % json_obj
         headers  =  'Content-Type'  'application/json-rpc' ,
                     'User-Agent'    'python/zabbix_api' }
         req  =  urllib2.Request( self .__url, json_obj, headers)
         opener  =  urllib2.urlopen(req)
         content  =  json.loads(opener.read())
         self .__id  + =  1
         #print 'Receive: %s' % content
         return  content
     '''
     /usr/local/zabbix/bin/zabbix_get is the default path to zabbix_get, it depends on the 'prefix' while install zabbix.
     plus, the ip(computer run this script) must be put into the conf of agent.
     '''
     @staticmethod
     def  zabbixGet(ip, key):
         zabbix_get  =  subprocess.Popen( '/apps/svr/zabbix/bin/zabbix_get  -s %s -k %s'  %  (ip, key), shell = True , stdout = subprocess.PIPE, stderr = subprocess.PIPE)
         result, err  =  zabbix_get.communicate()
         if  err:
             return  'ERROR'
         return  result.strip()
     def  createObject( self , object_name,  * args,  * * kwargs):
         return  object_name( self * args,  * * kwargs)
     def  getHostByHostid( self , hostids):
         if  not  isinstance (hostids, list ):
             hostids  =  [hostids]
         return  [ dict [ 'host' for  dict  in  self .host.get({ 'hostids' :hostids, 'output' : 'extend' })]
#################################################
#    Decorate Method
#################################################
def  checkAuth(func):
     def  ret( self * args):
         self .__checkAuth__()
         return  func( self , args)
     return  ret
def  postJson(method_name):
     def  decorator(func):
         def  wrapper( self , params):
             try :
                 content  =  self .postRequest( self .json_obj(method_name, params))
                 return  content[ 'result' ]
             except  KeyError, e:
                 =  content[ 'error' ][ 'data' ]
                 raise  ZabbixAPIException(e)
         return  wrapper
     return  decorator
def  ZabbixAPIObjectMethod(func):
     def  wrapper( self , method_name, params):
         try :
             content  =  self .postRequest( self .json_obj(method_name, params))
             return  content[ 'result' ]
         except  KeyError, e:
             =  content[ 'error' ][ 'data' ]
             raise  ZabbixAPIException(e)
     return  wrapper
#################################################
#    Zabbix API Object (host, item...) 
#################################################
class  ZabbixAPIObjectFactory( object ):
     def  __init__( self , zapi, object_name = ''):
         self .__zapi  =  zapi
         self .__object_name  =  object_name
     def  __checkAuth__( self ):
         self .__zapi.__checkAuth__()
     def  postRequest( self , json_obj):
         return  self .__zapi.postRequest(json_obj)
     def  json_obj( self , method, param):
         return  self .__zapi.json_obj(method, param)
     def  __getattr__( self , method_name):
         def  method(params):
             return  self .proxyMethod( '%s.%s'  %  ( self .__object_name,method_name), params)
         return  method
     # 'find' method is a wrapper of get. Difference between 'get' and 'find' is that 'find' can create object you want while it dosn't exist
     def  find( self , params, attr_name = None , to_create = False ):
         filtered_list  =  []
         result  =  self .proxyMethod( '%s.get'  %  self .__object_name, { 'output' : 'extend' , 'filter' : params})
         if  to_create  and  len (result)  = =  0 :
             result  =  self .proxyMethod( '%s.create'  %  self .__object_name, params)
             return  result.values()[ 0 ]
         if  attr_name  is  not  None :
             for  element  in  result:
                 filtered_list.append(element[attr_name])
             return  filtered_list
         else :
             return  result
     @ZabbixAPIObjectMethod
     @checkAuth
     def  proxyMethod( self , method_name, params):
         pass
def  testCase():
     zapi  =  ZabbixAPI(url = 'xxxx' , user = 'xxxx' , password = 'xxxx' )
     zapi.login()
     print  zapi.Graph.find({ 'graphid' : '221' }, attr_name = 'graphid' )[ 0 ]
     #hostid = zapi.Host.find({'ip':'xxxxxx'}, attr_name='hostid')[0]
     print  zapi.Host.find({ 'ip' : '10.200.100.25' })
     print  zapi.Host.exists({ 'filter' :{ 'host' : 'GD9-DNS-001' }})
     #host = zapi.createObject(Host, 'HostToCreate')
     #item = host.getItem('444107')
     #zapi.host.get({'hostids':['16913','17411'],'output':'extend'})
     #group = zapi.createObject(Hostgroup, '926')
     #print zapi.getHostByHostid('16913')
if  __name__  = =  '__main__' :
     testCase()

本文转自菜菜光 51CTO博客,原文链接:http://blog.51cto.com/caiguangguang/1377955,如需转载请自行联系原作者