博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Zabbix api脚本
阅读量:5872 次
发布时间:2019-06-19

本文共 6397 字,大约阅读时间需要 21 分钟。

  hot3.png

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()

转载于:https://my.oschina.net/jlan/blog/505103

你可能感兴趣的文章
央行下属的上海资信网络金融征信系统(NFCS)签约机构数量突破800家
查看>>
[转] Lazy evaluation
查看>>
常用查找算法总结
查看>>
grep 零宽断言
查看>>
被神话的大数据——从大数据(big data)到深度数据(deep data)思维转变
查看>>
修改校准申请遇到的问题
查看>>
Linux 进程中 Stop, Park, Freeze【转】
查看>>
文件缓存
查看>>
远程协助
查看>>
Scrum实施日记 - 一切从零开始
查看>>
关于存储过程实例
查看>>
配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法...
查看>>
AIX 7.1 install python
查看>>
PHP盛宴——经常使用函数集锦
查看>>
重写 Ext.form.field 扩展功能
查看>>
Linux下的搜索查找命令的详解(locate)
查看>>
福利丨所有AI安全的讲座里,这可能是最实用的一场
查看>>
开发完第一版前端性能监控系统后的总结(无代码)
查看>>
Python多版本情况下四种快速进入交互式命令行的操作技巧
查看>>
MySQL查询优化
查看>>