import json, urllib2, sys, timezabbix认证登录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()