|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
小知识: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[key])try:result=urllib2.urlopen(request)exceptURLErrorase:print"AuthFailed,PleaseCheckYourNameAndPassword:",e.codeelse:response=json.loads(result.read())result.close()authID=response[result]returnauthIDdefget_data(self,data,hostip=""):request=urllib2.Request(self.url,data)forkeyinself.header:request.add_header(key,self.header[key])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("\033[1;35;40m%s\033[0m"%EnterYourCheckHost:Host_ip:)data=json.dumps({"jsonrpc":"2.0","method":"host.get","params":{"output":["hostid","name","status","host"],"filter":{"host":[hostip]}},"auth":self.authID,"id":1})res=self.get_data(data)[result]if(res!=0)and(len(res)!=0):#forhostinres:host=res[0]ifhost[status]==1:print" ","\033[1;31;40m%s\033[0m"%"Host_IP:","\033[1;31;40m%s\033[0m"%host[host].ljust(15), ,"\033[1;31;40m%s\033[0m"%"Host_Name:","\033[1;31;40m%s\033[0m"%host[name].encode(GBK), ,"\033[1;31;40m%s\033[0m"%u未在监控状况.encode(GBK)returnhost[hostid]elifhost[status]==0:print" ","\033[1;32;40m%s\033[0m"%"Host_IP:","\033[1;32;40m%s\033[0m"%host[host].ljust(15), ,"\033[1;32;40m%s\033[0m"%"Host_Name:","\033[1;32;40m%s\033[0m"%host[name].encode(GBK), ,"\033[1;32;40m%s\033[0m"%u在监控状况.encode(GBK)returnhost[hostid]printelse:print ,"\033[1;31;40m%s\033[0m"%"GetHostErrororcannotfindthishost,pleasecheck!"return0defhost_del(self):hostip=raw_input("\033[1;35;40m%s\033[0m"%EnterYourCheckHost:Host_ip:)hostid=self.host_get(hostip)ifhostid==0:print ,"\033[1;31;40m%s\033[0m"%"Thishostcannotfindinzabbix,pleasecheckit!"sys.exit()data=json.dumps({"jsonrpc":"2.0","method":"host.delete","params":[{"hostid":hostid}],"auth":self.authID,"id":1})res=self.get_data(data)[result]ifhostidsinres.keys():print" ","\033[1;32;40m%s\033[0m"%"DeletHost:%ssuccess!"%hostipelse:print" ","\033[1;31;40m%s\033[0m"%"DeletHost:%sfailure!"%hostipdefhostgroup_get(self):data=json.dumps({"jsonrpc":"2.0","method":"hostgroup.get","params":{"output":"extend",},"auth":self.authID,"id":1,})res=self.get_data(data)ifresultinres.keys():res=res[result]if(res!=0)or(len(res)!=0):print"\033[1;32;40m%s\033[0m"%"NumberOfGroup:","\033[1;31;40m%d\033[0m"%len(res)forhostinres:print" ","HostGroup_id:",host[groupid]," ","HostGroup_Name:",host[name].encode(GBK)printelse:print"GetHostGroupError,pleasecheck!"deftemplate_get(self):data=json.dumps({"jsonrpc":"2.0","method":"template.get","params":{"output":"extend",},"auth":self.authID,"id":1,})res=self.get_data(data)#[result]ifresultinres.keys():res=res[result]if(res!=0)or(len(res)!=0):print"\033[1;32;40m%s\033[0m"%"NumberOfTemplate:","\033[1;31;40m%d\033[0m"%len(res)forhostinres:print" ","Template_id:",host[templateid]," ","Template_Name:",host[name].encode(GBK)printelse:print"GetTemplateError,pleasecheck!"defhost_create(self):hostip=raw_input("\033[1;35;40m%s\033[0m"%Enteryour:Host_ip:)groupid=raw_input("\033[1;35;40m%s\033[0m"%Enteryour:Group_id:)templateid=raw_input("\033[1;35;40m%s\033[0m"%Enteryour:Tempate_id:)g_list=[]t_list=[]foriingroupid.split(,):var={}var[groupid]=ig_list.append(var)foriintemplateid.split(,):var={}var[templateid]=it_list.append(var)ifhostipandgroupidandtemplateid:data=json.dumps({"jsonrpc":"2.0","method":"host.create","params":{"host":hostip,"interfaces":[{"type":1,"main":1,"useip":1,"ip":hostip,"dns":"","port":"10050"}],"groups":g_list,"templates":t_list,},"auth":self.authID,"id":1,})res=self.get_data(data,hostip)ifresultinres.keys():res=res[result]ifhostidsinres.keys():print"\033[1;32;40m%s\033[0m"%"Createhostsuccess"else:print"\033[1;31;40m%s\033[0m"%"Createhostfailure:%s"%res[error][data]else:print"\033[1;31;40m%s\033[0m"%"EnterError:iporgroupidortempateidisNULL,pleasecheckit!"defmain():test=zabbixtools()#test.template_get()#test.hostgroup_get()#test.host_get()test.host_del()#test.host_create()if__name__=="__main__":main()
复制代码
相干的资料的可以参考官方文档。这个只是一些功效模块,包括获得主机,主机组、模板、删除主机等功效,可以依据须要停止调剂,完成zabbix的批量化和主动化治理。由于是在linux运转,所以设置了输入终真个字体色彩便利辨别,假如不须要,自行删除便可。
小知识:社区支持的免费Linux发行版一般不会从商业支持中寻求什么好处,但对CentOS来说,这种情况即将改变。 |
|