Linux教程之python挪用zabbix的api接口添加主机、查询组、主机、模板
小知识:CentOS其实就是相当于免费版的RedHat,任何人可以自由使用,不需要向RedHat付任何的费用。当然,同样你也得不到任何有偿的技术支持和升级服务。zabbix有一个API接口,可以挪用这些几口来主动添加主机,查询zabbix中监控的主机,监控的模板、监控的主机组等信息,应用也异常的便利。以下是用python挪用zabbix的API接口来完成上述功效:
#!/usr/bin/envpython#-*-coding:utf-8-*-importjsonimporturllib2importsysclasszabbixtools:def__init__(self):self.url="http://192.168.100.200/zabbix/api_jsonrpc.php"self.header={"Content-Type":"application/json"}self.authID=self.user_login()defuser_login(self):data=json.dumps({"jsonrpc":"2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"id":0})request=urllib2.Request(self.url,data)forkeyinself.header:request.add_header(key,self.header)try:result=urllib2.urlopen(request)exceptURLErrorase:print"AuthFailed,PleaseCheckYourNameAndPassword:",e.codeelse:response=json.loads(result.read())result.close()authID=responsereturnauthIDdefget_data(self,data,hostip=""):request=urllib2.Request(self.url,data)forkeyinself.header:request.add_header(key,self.header)try:result=urllib2.urlopen(request)exceptURLErrorase:ifhasattr(e,reason):printWefailedtoreachaserver.printReason:,e.reasonelifhasattr(e,code):printTheservercouldnotfulfilltherequest.printErrorcode:,e.codereturn0else:response=json.loads(result.read())result.close()returnresponsedefhost_get(self,hostip):#hostip=raw_input("