码迷,mamicode.com
首页 > Windows程序 > 详细

Zabbix api脚本

时间:2015-09-11 19:26:27      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

import json, urllib2, sys, time

zabbix认证登录
class zabbixtools:
   def __init__(self):
       self.url = "http://127.0.0.1/zabbix/api_jsonrpc.php"
       self.header = {"Content-Type": "application/json"}
       self.authID = self.user_login()
   def user_login(self):
       data = json.dumps(
               {
                   "jsonrpc": "2.0",
                   "method": "user.login",
                   "params": {
                       "user": "admin",
                       "password": "zabbix"
                       },
                   "id": 0
                   })
       request = urllib2.Request(self.url,data)
       for key in self.header:
           request.add_header(key,self.header[key])
       try:
           result = urllib2.urlopen(request)
       except URLError as e:
           print "Auth Failed, Please Check Your Name And Password:",e.code
       else:
           response = json.loads(result.read())
           result.close()
           authID = response[‘result‘]
           print ‘Auth Successed!   AuthID is:‘,authID
           return authID
           
zabbix获取组列表
   def hostgroup_get(self):
       data = json.dumps(
       {
           "jsonrpc":"2.0",
           "method":"hostgroup.get",
           "params":{
               "output":["groupid","name"],
               },
           "auth":self.authID, 
           "id":1,
       })
       # create request object
       request = urllib2.Request(self.url,data)
       for key in self.header:
           request.add_header(key,self.header[key])
       # get host list
       try:
           result = urllib2.urlopen(request)
           #print result
       except URLError as e:
           if hasattr(e, ‘reason‘):
               print ‘We failed to reach a server.‘
               print ‘Reason: ‘, e.reason
           elif hasattr(e, ‘code‘):
               print ‘The server could not fulfill the request.‘
               print ‘Error code: ‘, e.code
       else:
           #print "heh"
           response = json.loads(result.read())
           #print response
           result.close()
           print "Number Of HostGroup: ", len(response[‘result‘])
           #print response
           for group in response[‘result‘]:
               print "Group ID:",group[‘groupid‘],"\tGroupName:",group[‘name‘]
               
zabbix获取主机列表
   def host_get(self):
       data = json.dumps(
       {
           "jsonrpc":"2.0",
           "method":"host.get",
           "params":{
               "output":["hostid","name"],
               #"output":["extend"],
               "filter":{"host":""}
               #"groupids":groupid
               },
           "auth":self.authID,
           "id":1,
       })
       # create request object
       request = urllib2.Request(self.url,data)
       for key in self.header:
           request.add_header(key,self.header[key])
       # get host list
       try:
           result = urllib2.urlopen(request)
       except URLError as e:
           if hasattr(e, ‘reason‘):
               print ‘We failed to reach a server.‘
               print ‘Reason: ‘, e.reason
           elif hasattr(e, ‘code‘):
               print ‘The server could not fulfill the request.‘
               print ‘Error code: ‘, e.code
       else:
           response = json.loads(result.read())
           result.close()
           print "Number Of Hosts: ", len(response[‘result‘])
           for host in response[‘result‘]:
               print host
               #print "Host ID:",host[‘hostid‘],"Host Name:",host[‘name‘]
zabbix获取监控项列表
   def itemsid_get(self, hostid):
       data = json.dumps(
       {
       "jsonrpc":"2.0",
       "method":"item.get",
       "params":{
           "output":["itemids","key_"],
           #"output":"extend",
           "hostids":hostid,
       },
       "auth":self.authID,
       "id":1,
       })
       # create request object
       request = urllib2.Request(self.url,data)
       for key in self.header:
           request.add_header(key,self.header[key])
           # get host list
       try:
           result = urllib2.urlopen(request)
       except URLError as e:
           if hasattr(e, ‘reason‘):
               print ‘We failed to reach a server.‘
               print ‘Reason: ‘, e.reason
           elif hasattr(e, ‘code‘):
               print ‘The server could not fulfill the request.‘
               print ‘Error code: ‘, e.code
       else:
           response = json.loads(result.read())
           result.close()
           print "Number Of Item: ", len(response[‘result‘])
           for item in response[‘result‘]:
               print item
               #print "Host ID:",host[‘hostid‘],"HostName:",host[‘name‘]
zabbix获取监控项例示数据列表
   def history_item_get(self, itemid):
       data = json.dumps(
       {
           "jsonrpc":"2.0",
           "method":"history.get",
           "params":{
               "output":"extend",
               "history":3,
               "itemids":itemid,
               "limit":5
           },
           "auth":self.authID,
           "id":1,
       })
       # create request object
       request = urllib2.Request(self.url,data)
       for key in self.header:
           request.add_header(key,self.header[key])
       # get host list
       try:
           result = urllib2.urlopen(request)
       except URLError as e:
           if hasattr(e, ‘reason‘):
               print ‘We failed to reach a server.‘
               print ‘Reason: ‘, e.reason
           elif hasattr(e, ‘code‘):
               print ‘The server could not fulfill the request.‘
               print ‘Error code: ‘, e.code
       else:
           response = json.loads(result.read())
           print response[‘result‘]
           print type(response[‘result‘])
           result.close()
           print "Number Of history_data: ", len(response[‘result‘])
           data1={}
           data2=[]
           for host in response[‘result‘]:                
               value=int(host[‘value‘])/1024/1024
               time1=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(int(host[‘clock‘])))
               data1[‘time‘]=time1
               data1[‘value‘]=value
               data2.append(data1)
               print "value:",value,"\t","time:",time1
               #print "Host ID:",host[‘hostid‘],"HostName:",host[‘name‘]
           print data2
           
           
def main():
   test = zabbixtools()
   test.hostgroup_get()
   test.host_get()
   test.itemsid_get("10084")
   test.history_item_get("23699")
   
   
if __name__ == "__main__":
   main()

Zabbix api脚本

标签:

原文地址:http://my.oschina.net/jlan/blog/505103

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!