在日常的监控运维中,因监控的内容较多,人较少的情况下,监控对本省的日常巡检成了一个重大的累赘(我们不可能每天对每一个检测项的状态进行检查,工作量,我的天呢),虽然我们可以使用zabbix自身报警,邮件等手段,但是,填写巡检表的时候,嘿嘿嘿,麻烦的让人想吐。监控自身巡检就成了必要的手段了。
由于时间问题和需求不定,我就暂将存在故障检测项的IP输出,未做处理(可以做成表格输出,大方,简洁,方便),还望抱歉。
前期准备:
1.修改zabbix的url,用户,密码
2.修改检测项目对应的key:keyinfo(巡检需要列出大量需要检测的key)
3.由于我含有多个省份xxxx,yyyy,可以酌情添加减少
#!/usr/bin/env python # coding:utf-8 ########################## # Function : 该script可以针对部署的监控进行的检测,也可进行日常巡检 # Editer : graysky # Version : 1.0.0 # Mail : [email protected] # Date : 2016-08-14 ########################## import json import urllib2 import xlrd class api_work: def __init__(self, url, user, passwd): self.url = url self.user = user self.passwd = passwd self.login_authid = self.zabbix_login() def json_work(self, work_json): zabbix_header = {"Content-Type": "application/json"} self.work_json = work_json used_json = json.dumps(self.work_json) used_json_reques = urllib2.Request(self.url, used_json) for key in zabbix_header: used_json_reques.add_header(key, zabbix_header[key]) try: used_json_result = urllib2.urlopen(used_json_reques) except Exception: print "Get failed" else: used_json_respones = json.loads(used_json_result.read()) used_json_group = used_json_respones[‘result‘] used_json_result.close() return used_json_group def zabbix_login(self): login_json = {"jsonrpc": "2.0", "method": "user.login", "params": {"user": self.user, "password": self.passwd}, "id": 0} login_authid = self.json_work(login_json) return login_authid def get_hostinfo(self): hostinfo_group = {} host_json = {"jsonrpc": "2.0", "method": "host.get", "params": {"output": "extend", }, "auth": self.zabbix_login(), "id": 0,} for hostinfo in self.json_work(host_json): hostinfo_group[hostinfo["hostid"]] = hostinfo["host"] return hostinfo_group def get_itemsinfo_by_hostidmonitor(self,hostid,monitor_key): itemsinfo_group = {} items_json = {"jsonrpc": "2.0", "method": "item.get","params": {"output": "extend", "hostids": hostid, "search":{"key_": monitor_key}, "sortfield": "name" }, "auth": self.login_authid, "id": 0} for itemsinfo in self.json_work(items_json): itemsinfo_group[itemsinfo[‘itemid‘]] = itemsinfo[‘status‘] return itemsinfo_group if __name__ == "__main__": def provincework(provinceinfo,key_info): if provinceinfo == "xxxx": zabbix_url = "http://xxxx/zabbix/api_jsonrpc.php" zabbix_user = "Admin" zabbix_passwd = "xxxx" elif provinceinfo == "yyyy": zabbix_url = "http://yyyyy/zabbix/api_jsonrpc.php" zabbix_user = "Admin" zabbix_passwd = "yyyy" api_init = api_work(zabbix_url,zabbix_user,zabbix_passwd) hostinfogroup = api_init.get_hostinfo() ‘‘‘ for hostip in hostinfogroup.values(): print hostip ‘‘‘ ItemsErrorHostIP = [] for hostid in hostinfogroup.keys(): itemsstatusinfo_group = api_init.get_itemsinfo_by_hostidmonitor(hostid,key_info) for itemsstatusinfo in itemsstatusinfo_group.values(): if int(itemsstatusinfo) > 0 : ItemsErrorHostIP.append(hostinfogroup[hostid]) return ItemsErrorHostIP provincegroup = [‘xxxx‘,‘yyyy‘] keyinfo = ‘OS.Core.Dir.Change[/etc/init.d/]‘ for province in provincegroup: erroritemsipgroup = provincework(province,keyinfo) if len(erroritemsipgroup) == 0 : print "省份%s检测结果: 添加完成-通过 "%province else: print "省份%s检测结果: 未添加完成-未通过IP如下:"%province for erroritemsip in erroritemsipgroup: type(erroritemsip) print "\t\t\t\t%s"%erroritemsip
时间: 2024-10-17 09:41:33