def create_config(): import ConfigParser config = ConfigParser.ConfigParser() config.read(zabbix_tool) applitions = config.get("create_file", "applitions") monit_config = config.get("create_file","monit_config") ignore_key_list = config.get("create_file","ignore_key").split(",") zabbix=zabbix_api(output=False) # 主机名和 IP 字典 hostname_ip={} # 主机名和 监控项字典 hostname_key={} # key_和监控项字典 service_key={} host_list=zabbix.host_list() for host in host_list: # host[0]---hostid ,host[1]---hostname,host[2]---hostip hostid=host[0] hostname=host[1] hostip=host[2] hostname_ip[hostname]=hostip items = zabbix.item_list(hostid,applitions) if not items: items = [] else: for ignore_key_item in ignore_key_list: if ignore_key_item in items: items.remove(ignore_key_item) for key_item in items: service_key[key_item]="" hostname_key[hostname]=items for key_item in service_key: service_name = key_item.replace(".","_").replace(",","").replace("[","").replace("]","") service_key[key_item]=service_name hostname_ip=json.dumps(hostname_ip,indent=4) #print hostname_ip hostname_key=json.dumps(hostname_key,indent=4) #print hostname_key service_key=json.dumps(service_key,indent=4) #print service_key # 生成配置文件 config_file = "#!/usr/bin/python\n#coding=utf8\n" config_file = config_file + "hostname_ip="+hostname_ip+"\n" config_file = config_file + "hostname_key="+hostname_key+"\n" config_file = config_file + "service_key="+service_key+"\n" # 写入配置文件 fo = open(monit_config, "wb") fo.write(config_file) fo.close()
def status(): zabbix=zabbix_api(output=False) host_ip={} host_list=zabbix.host_list() #print host_list for host in host_list: host_ip[host[1]]=host[2] # print host_ip # for host in host_list: issues_dict = zabbix.issues() exception_host=[] ## 异常主机 if isinstance(issues_dict,dict): for issues in issues_dict: exception_host.append(issues) exception_host = list(set(exception_host)) else: exception_host = [] print "exception_host",exception_host ## 正常主机 normal_host=[] for host in host_ip: if host not in exception_host: normal_host.append(host) print "normal_host",normal_host
def week_report(): import date import datetime import pyMail weekreport_name = "/opt/weekreport.xls" d = datetime.datetime.now() date_from,date_to = date.week_get(d) print date_from,date_to terminal_table = False zabbix=zabbix_api(terminal_table) itemkey_list=['vm.memory.size[available]', 'agent.ping', 'vfs.fs.size[/,pfree]', 'system.cpu.load[percpu,avg1]'] export_xls = {"xls":"ON", "xls_name":weekreport_name, "title":"ON", "title_name":u"周报" } select_condition = {"hostgroupID":"", "hostID":"" } zabbix.report_available2(str(date_from),str(date_to),export_xls,select_condition,itemkey_list=itemkey_list) # 1 初始化发送邮件类 # 25 端口时,usettls = False # 465 端口时,usettls = True usettls = False sml = pyMail.SendMailDealer('mail_address','mail_pwd','smtp.gmail.com','25',usettls = usettls) # 2 设置邮件信息 # 参数包括("收件人","标题","正文","格式html/plain","附件路径1","附件路径2") sml.setMailInfo('*****@*****.**','测试','正文','plain',weekreport_name) # 3 发送邮件 sml.sendMail()
def create_config(): import ConfigParser config = ConfigParser.ConfigParser() config.read(zabbix_tool) applitions = config.get("create_file", "applitions") monit_config = config.get("create_file", "monit_config") ignore_key_list = config.get("create_file", "ignore_key").split(",") zabbix = zabbix_api(output=False) # 主机名和 IP 字典 hostname_ip = {} # 主机名和 监控项字典 hostname_key = {} # key_和监控项字典 service_key = {} host_list = zabbix.host_list() for host in host_list: # host[0]---hostid ,host[1]---hostname,host[2]---hostip hostid = host[0] hostname = host[1] hostip = host[2] hostname_ip[hostname] = hostip items = zabbix.item_list(hostid, applitions) if not items: items = [] else: for ignore_key_item in ignore_key_list: if ignore_key_item in items: items.remove(ignore_key_item) for key_item in items: service_key[key_item] = "" hostname_key[hostname] = items for key_item in service_key: service_name = key_item.replace(".", "_").replace(",", "").replace( "[", "").replace("]", "") service_key[key_item] = service_name hostname_ip = json.dumps(hostname_ip, indent=4) #print hostname_ip hostname_key = json.dumps(hostname_key, indent=4) #print hostname_key service_key = json.dumps(service_key, indent=4) #print service_key # 生成配置文件 config_file = "#!/usr/bin/python\n#coding=utf8\n" config_file = config_file + "hostname_ip=" + hostname_ip + "\n" config_file = config_file + "hostname_key=" + hostname_key + "\n" config_file = config_file + "service_key=" + service_key + "\n" # 写入配置文件 fo = open(monit_config, "wb") fo.write(config_file) fo.close()
def action_create(self): i = snack.EntryWindow(self.screen, self.task_type[0], "Please enter action Information", ["action_name ", "group_name"], buttons=['Ok', 'Back'], width=65, entryWidth=40) if i[0] != 'back': self.action = i action_name=self.action[1][0] group_name=self.action[1][1] zabbix=zabbix_api(False,False,output=False) info = zabbix.action_autoreg_create(action_name,group_name) info_dirt=eval(info) snack.ButtonChoiceWindow(self.screen, "%s" % self.task_type[0], "Info:\n\n" "%s: %s" % (info_dirt["status"],info_dirt["output"]), buttons=['Ok'], width=65) self.get_task() else: self.get_task()
def week_report_xls(): ''' 生成 excel 表 ''' import date import datetime import pyMail weekreport_name = "/opt/weekreport.xls" d = datetime.datetime.now() date_from,date_to = date.week_get(d) print date_from,date_to terminal_table = False zabbix=zabbix_api(terminal_table) itemkey_list=['vm.memory.size[available]', 'agent.ping', 'vfs.fs.size[/,pfree]', 'system.cpu.load[percpu,avg1]'] export_xls = {"xls":"ON", "xls_name":weekreport_name, "title":"ON", "title_name":u"周报" } select_condition = {"hostgroupID":"", "hostID":"" } zabbix._report_available2(str(date_from),str(date_to),export_xls,select_condition,itemkey_list=itemkey_list) # 1 初始化发送邮件类 # 25 端口时,usettls = False # 465 端口时,usettls = True usettls = False sml = pyMail.SendMailDealer('mail_address','mail_pwd','smtp.gmail.com','25',usettls = usettls) # 2 设置邮件信息 # 参数包括("收件人","标题","正文","格式html/plain","附件路径1","附件路径2") sml.setMailInfo('*****@*****.**','测试','正文','plain',weekreport_name) # 3 发送邮件 sml.sendMail()
def status(): zabbix = zabbix_api(output=False) host_ip = {} host_list = zabbix.host_list() #print host_list for host in host_list: host_ip[host[1]] = host[2] # print host_ip # for host in host_list: issues_dict = zabbix.issues() exception_host = [] ## 异常主机 if isinstance(issues_dict, dict): for issues in issues_dict: exception_host.append(issues) exception_host = list(set(exception_host)) else: exception_host = [] print "exception_host", exception_host ## 正常主机 normal_host = [] for host in host_ip: if host not in exception_host: normal_host.append(host) print "normal_host", normal_host
def zabbix_add(lineid, name, ip, linetype, pe): hostgroup = checkhostgroup(pe) template = checkpe(pe) from zabbix_api import zabbix_api zabbix = zabbix_api() return zabbix.host_create(lineid, lineid + '-' + linetype + '-' + name, ip, hostgroup, template)
def addmonitor(request,url,id): #dgb_proxyid=14800;sha_proxyid=14753;szb_proxyid=14801;zabbix_serverid=10166;other=0; res=id conn = MySQLdb.connect(host="127.0.0.1", user="******", passwd="", db="newtellme", charset="utf8") cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) cursor.execute("select linejiankongip,linejifangtype,lineserver,sitename from tellmeview4 where lineid like '%s'"%id) line_result=cursor.fetchone() lineserver="%(lineserver)s " %line_result sitename="%(sitename)s " %line_result linejifangtype="%(linejifangtype)s " %line_result linejiankongip="%(linejiankongip)s " %line_result linejiankongip=linejiankongip.strip() PE=lineserver[0:3] PE=PE.upper() ser=lineserver.upper() ser=ser.strip() ser="FNET-"+ser+"-CPE" grouplist=[] grouplist.append(ser) conn.close() hostname=id+"-"+linejifangtype+"-"+sitename import sys sys.path.append('/var/www/django/tellme2014/tellme2014/tellme/') from zabbix_api import zabbix_api zabbix=zabbix_api() if PE in "DGB" and "bk" not in lineserver: proxyid=14800 peproxy=PE+"-PROXY" elif PE in "SHA": proxyid=14753 peproxy=PE+"-PROXY" elif PE in "SZB" and "bk" not in lineserver: proxyid=14801 peproxy=PE+"-PROXY" else : proxyid=0 peproxy="" if peproxy != "": grouplist.append(peproxy) grouplist.append("Fnet-ISO-3") grouplist.append("FNET-CPE") pestr=u",".join(grouplist) zabbix.host_create(id,hostname,linejiankongip,pestr,'Fnet-CPE ICMP Ping',proxyid) return render_to_response("messages.html",locals(),context_instance=RequestContext(request))
def create_config(): import ConfigParser config = ConfigParser.ConfigParser() config.read(zabbix_tool) applitions = config.get("create_file", "applitions") monit_config = config.get("create_file","monit_config") zabbix=zabbix_api(output=False) # 主机名和 IP 字典 hostname_ip={} # 主机名和 监控项字典 hostname_key={} host_list=zabbix.host_list() for host in host_list: # host[0]---hostid ,host[1]---hostname,host[2]---hostip hostid=host[0] hostname=host[1] hostip=host[2] hostname_ip[hostname]=hostip items = zabbix.item_list(hostid,applitions) if not items: items = [] hostname_key[hostname]=items hostname_ip=json.dumps(hostname_ip,indent=4) #print hostname_ip hostname_key=json.dumps(hostname_key,indent=4) #print hostname_key # 生成配置文件 config_file = "#!/usr/bin/python\n#coding=utf8\n" config_file = config_file + "hostname_ip="+hostname_ip+"\n" config_file = config_file + "hostname_key="+hostname_key+"\n" # 写入配置文件 fo = open(monit_config, "wb") fo.write(config_file) fo.close()
def create_config(): import ConfigParser config = ConfigParser.ConfigParser() config.read(zabbix_tool) applitions = config.get("create_file", "applitions") monit_config = config.get("create_file", "monit_config") zabbix = zabbix_api(output=False) # 主机名和 IP 字典 hostname_ip = {} # 主机名和 监控项字典 hostname_key = {} host_list = zabbix.host_list() for host in host_list: # host[0]---hostid ,host[1]---hostname,host[2]---hostip hostid = host[0] hostname = host[1] hostip = host[2] hostname_ip[hostname] = hostip items = zabbix.item_list(hostid, applitions) if not items: items = [] hostname_key[hostname] = items hostname_ip = json.dumps(hostname_ip, indent=4) #print hostname_ip hostname_key = json.dumps(hostname_key, indent=4) #print hostname_key # 生成配置文件 config_file = "#!/usr/bin/python\n#coding=utf8\n" config_file = config_file + "hostname_ip=" + hostname_ip + "\n" config_file = config_file + "hostname_key=" + hostname_key + "\n" # 写入配置文件 fo = open(monit_config, "wb") fo.write(config_file) fo.close()
# Author: Bill # Created Time : Wed 02 Dec 2015 10:17:28 AM CST # File Name: wb_zabbix.py # Description: """ import os import sys root_path = os.path.dirname(__file__) print root_path sys.path.insert(0, os.path.join(root_path)) sys.path.insert(0, os.path.join(root_path, 'mylib')) from zabbix_api import zabbix_api #新增帮助信息,可直接执行脚本 zabbix=zabbix_api() #获取所有主机列表 zabbix.host_get() #查询单个主机列表 #zabbix.host_get('WP227') #获取所有主机组列表 #zabbix.hostgroup_get() #查询单个主机组列表 #zabbix.hostgroup_get('test01') #获取所有模板列表 #zabbix.template_get() #查询单个模板信息 # zabbix.template_get('Template OS Linux') #添加一个主机组 #zabbix.hostgroup_create('test01') #添加一个主机,支持将主机添加进多个组,多个模板,多个组、模板之间用逗号隔开,如果添加的组不存在,新创建组
def week_report_image(): ''' 生成 图片 ''' import time import tarfile import json import image_merge import pyMail reload(sys) sys.setdefaultencoding("utf-8") # date_from = "2019-01-10 00:00:00" # date_till = "2019-01-17 00:00:00" dateFormat = "%Y-%m-%d %H:%M:%S" now = time.time() one_week_before = now - 604800 date_from = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(one_week_before)) date_till = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(now)) zabbix_servers = [ "aliyunzabbix", "dvdfabzabbix", "viduszabbix", ] for server in zabbix_servers: # #登陆 zapi = zabbix_api(profile=server) # #抓图 img_dir = "/code/png/" + server res = zapi.get_graph_images(date_from=date_from,date_till=date_till,img_dir=img_dir) # #注销 zapi.logout() # #获取图片名称 for item in res: img_list = [] for name in item['hostimages']: print (name['image_name']) img_list.append(name['image_name']) # #合并图片 output_dir = "/code/output/" + server image_merge.image_merge(img_list,output_dir=output_dir,output_name=item['hostname']+".jpg") # #创建压缩包名 tar_file = "/code/zabbix_images.tar.gz" tar = tarfile.open(tar_file,"w:gz") # #创建压缩包 for root,dir,files in os.walk("/code/output"): for file in files: fullpath = os.path.join(root,file) tar.add(fullpath) tar.close() # #发送邮件 usettls = False # sml = pyMail.SendMailDealer('mail_address','mail_pwd','smtp.gmail.com','25',usettls = usettls) # #2 设置邮件信息 send_to = [ "*****@*****.**", "*****@*****.**" ] for user in send_to: sml.setMailInfo('zabbix数据图','zabbix 监控服务器数据图','plain',tar_file) # 3 发送邮件 sml.sendMail(rec_user=user)
def zabbix_update(lineid, oldip, newip): from zabbix_api import zabbix_api zabbix = zabbix_api() return zabbix.host_update(lineid, oldip, newip)
def zabbix_del(lineid): from zabbix_api import zabbix_api zabbix = zabbix_api() return zabbix.host_delete(lineid)