现在的位置: 主页 > 商讯 > 文章正文

python根据nagios配置文件将监控的服务联系人等信息导

作者:成都渝祥金属丝网制品有限公司 来源:www.cdyuxiang.com 未知发布时间:2017-09-09 10:25:12
python根据nagios配置文件将监控的服务联系人等信息导出到excel表格

前阵子,遇到需要把nagios监控的服务统计到表格的需求,想着如果每一次改动,都要去维护表格的话,难免会有疏漏或者问题,于是观察了nagios的host.cfg,services.cfg等配置文件写了一个脚本,包含对多个主机关联到同一个服务的处理等,脚本也有局限性,就是需要把每个主机,都归类到某个组,比如Linux主机归类到Linux组,实际环境,一般也是有这样做归类的,脚本运行过程中会有文件产生,但都很小,以下是相应模块安装和脚本文件,建议在测试机上面跑完再到实际环境跑。

一,安装xlsxwriter

一个excel操作模块,可以操作多个工作表的文字、数字、公式、图表等。

pip安装方法:

#pipinstallXlsxWriter

源码安装方法(需下载)

#tar-zxvfXlsxWriter-master.tar.gz#cdXlsxWriter-master#pythonsetup.pyinstall

二,脚本文件:

importxlsxwriter importsys reload(sys) sys.setdefaultencoding('utf8') #注意路径 os.system("cat/usr/local/nagios/etc/hosts.cfg|grep-E'host_name|address|hostgroups'|\ seds/[[:space:]]//|awk'{print$1,$2,$3}'|sed-e's/[[:space:]]$//g'>/root/autocount/hostip") #####################处理一行多个host_name情况######################## os.system("cat/usr/local/nagios/etc/services.cfg|grep-E'host_name|service_description|contacts'|seds/[[:space:]]//|\ awk'{print$1,$2,$3,$4,$5,$6}'|sed-e's/[[:space:]]*$//g'|sed-e's/,[[:space:]]/,/g'>/root/autocount/serviceip2") #含有完整行的文件,及host_name都有的 os.system("cat/usr/local/nagios/etc/services.cfg|grep'host_name'|seds/[[:space:]]//|awk'{print$2,$3,$4,$5,$6}'|seds/[[:space:]]//g|awk'/,/{print$0}'>/root/autocount/serviceip1") os.system("sort-u/root/autocount/serviceip1>/root/autocount/serviceip3") #去除重复行 withopen("/root/autocount/serviceip3")asf3: #a中保存有一行多个host_name的主机 a=f3.readlines() withopen("/root/autocount/serviceip2")asf4: b=f4.readlines() j=0 foriina: k=i.strip('\n').split(',') l=3 whilej>testfile,bb, testfile.close() #此时testfile中还是含有长主机名的,即多个主机在配置文件中显示为同一行的(关联服务) jiji=0 foriina: try: whileTrue: ind=b.index("host_name"+i) b.pop(ind) b.pop(ind) b.pop(ind) print"###############" jiji+=1 except: continue #利用a中先前存的类似host1,host2,host3这样关联的主机,从b中剔除。 print"jijiis%s"%jiji finall=open("/root/autocount/serviceip",'w') forkkinb: print>>finall,kk, finall.close() #关闭保存文件,现在就是不存在类似多个主机名的行了。 file1="/root/autocount/hostip" file2="/root/autocount/serviceip" f=open("/root/autocount/joinout",'w') final=open("/root/autocount/final",'w') f1=file(file1,"r") j=0 totalhost=0 foriinf1.readlines(): totalhost+=1 print>>f,i, f2=file(file2,"r") m=2 forkinf2.readlines(): ifi==k:#先判断是否主机名相等,要不如果serviceip文件服务无联系人,就会出现错乱 print"testtesttest" m=0 continue elifm<2and('host_name'notink): print>>f,k, m+=1 else: m=2 continue f1.close() f2.close() f.close() totalhost=totalhost/3 withopen("/root/autocount/joinout")asf: a=f.readlines() linen=0 con=[] totalservice=0 foriina: if'service_description'ini: totalservice+=1 con.append(linen) linen+=1 else: linen+=1 printcon m=1 #处理一些没有contacts的情况 forjincon: if'contacts'notina[j+m]: a.insert(j+m,'contactsnone\n') m+=1 forxina: print>>final,x, title=[u'序列号',u'host',u'ip',u'service',u'联系人',u'所属部门'] workbook=xlsxwriter.Workbook('Nagios监控信息统计.xlsx') worksheet=workbook.add_worksheet() worksheet.set_column('A:C',30) worksheet.set_column('D:F',30) worksheet.set_column('H:I',10) #worksheet.set_row(3,30) #worksheet.set_row(6,30) #worksheet.set_row(7,30) merge_format=workbook.add_format({ 'bold':1, 'border':1, 'align':'center', 'valign':'vcenter', 'fg_color':'#CC0033'}) merge_format1=workbook.add_format({ 'bold':1, 'border':1, 'align':'center', 'valign':'vcenter', 'fg_color':'#D7E4BC'}) bold=workbook.add_format({ 'bold':1, 'border':1, 'align':'center', 'valign':'vcenter'}) merge_format2=workbook.add_format({ #'bold':1, 'border':1, 'align':'center', 'valign':'vcenter'}) format_title=workbook.add_format() format_title.set_border(1) format_title.set_bg_color('#cccccc') format_title.set_align('center') worksheet.merge_range('A1:F1','Nagios监控信息',merge_format) worksheet.merge_range('H1:I1','主机总计服务总计',merge_format1) worksheet.write('H2',totalhost,bold) worksheet.write('I2',totalservice,bold) worksheet.write_row('A2',title,format_title) ####################################### service_list=[] count=0 slist_end=0 initline=3 defwritetoexecl(numb,host,ip,group,service_list): globalinitline globalslist_end slist_len=str(len(service_list)) initl=str(initline) slist_end=int(initl)+int(slist_len)-1 slist_end1=str(slist_end) #一行的就调用write方法 ifinitl==slist_end1: worksheet.write('A'+initl,numb,merge_format2) worksheet.write('B'+initl,host,merge_format2) worksheet.write('C'+initl,ip,merge_format2) worksheet.write('F'+initl,group,merge_format2) else: worksheet.merge_range('A'+initl+':'+'A'+slist_end1,numb,merge_format2) worksheet.merge_range('B'+initl+':'+'B'+slist_end1,host,merge_format2) worksheet.merge_range('C'+initl+':'+'C'+slist_end1,ip,merge_format2) worksheet.merge_range('F'+initl+':'+'F'+slist_end1,group,merge_format2) servicell=initl #输出服务和联系人 forelementinservice_list: print'++++++++' service1=''.join(element.keys()) contacts1=''.join(element.values()) worksheet.write('D'+servicell,service1,merge_format2) worksheet.write('E'+servicell,contacts1,merge_format2) servicell=str(int(servicell)+1) print'++++++++' #worksheet.merge_range('A50:A60',num,merge_format) #printinitl #printslist_end1 initline=int(initl)+int(slist_len) foriina: service_dict={} #定义字典存放服务和联系人 if'host_name'ini: host=i.split()[1] printhost if'service_description'ini: key=i.split()[1] service_dict[key]='' if'contacts'ini: service_dict[key]=i.split()[1] #printservice_dict service_list.append(service_dict) #printservice_list if'address'ini: ip=i.split()[1] printip if'hostgroups'ini: groups=i.split()[1] printservice_list printgroups count+=1 writetoexecl(count,host,ip,groups,service_list) service_list=[] continue workbook.close()

统计的效果如下:

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:恩施SEO http://enshi.4567w.com

上一篇:HDU1492-The number of divisors(约数) about Humble 下一篇:最后一页