Python exports monitored service contacts and other information to an excel table based on the nagios configuration file

Source: Internet
Author: User

Python exports monitored service contacts and other information to an excel table based on the nagios configuration file

A few times ago, I had to count the services monitored by nagios to the table. I thought that if every change was required to maintain the table, there would inevitably be omissions or problems, so I observed the host of nagios. cfg, services. cfg and other configuration files write a script, including the processing of multiple hosts associated with the same service. The script also has limitations, that is, each host needs to be classified into a group, for example, if a Linux host is classified into a Linux Group and the actual environment is classified as such, files are generated during the script running process, but all files are small. The following are the corresponding module installation and script files, we recommend that you run the test on the testing machine before running it in the actual environment.

1. Install xlsxwriter

An excel operation module that allows you to operate on texts, numbers, formulas, and charts of multiple worksheets.

Pip installation method:

#pipinstallXlsxWriter

Source code installation method (download required)

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

Ii. script file:

Importxlsxwriterimportsysreload (sys) sys. setdefaultencoding ('utf8') # note the path 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 ") ##################### handle multiple host_name in one row ############# ########## 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 ") # OS that contains the complete row and all the host_name files. 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") # Remove duplicate rows withopen ("/root/autocount/serviceip3") asf3: # host 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 () # at this time, testfile still contains long host names, that is, multiple hosts are displayed as the same row (associated services) in the configuration file jiji = 0 foriina: try: whileTrue: ind = B. index ("host_name" + I) B. pop (ind) B. pop (ind) B. pop (ind) print "##############" jiji + = 1 Tb: continue # Use host1, remove the associated host such as host2 and host3 from B. Print "jijiis % s" % jijifinall = open ("/root/autocount/serviceip", 'w') forkkinb: print> finall, kk, finall. close () # close the save file. Now there is no line similar to multiple host names. 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 = 0foriinf1. readlines (): totalhost + = 1 print> f, I, f2 = file (file2, "r") m = 2forkinf2. readlines (): ifi = k: # first judge whether the host name is equal, or if the serviceip file service has no contact, print "testtesttest" m = 0 continueelifm <2and ('host _ name' notink): print> f, k, m + = 1 else: m = 2continuef1. close () f2.close () f. close () totalhost = totalhost/3 withopen ("/root/autocount/joinout") asf: a = f. readlines () linen = 0con = [] totalservice = 0 foriina: if 'service _ description' ini: totalservice + = 1con. append (linen) linen + = 1 else: linen + = 1 printconm = 1 # handle situations without contacts forjincon: if 'contacts' notina [j + m]:. insert (j + m, 'contactsnone \ n') m + = 1 forxina: print> done') 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 ('# cccccccc') format_title.set_align ('center') worksheet. merge_range ('a1: f1', 'nagios monitoring information', merge_format) worksheet. merge_range ('h1: i1', 'Total number of hosts services', merge_format1) worksheet. write ('h2 ', totalhost, bold) worksheet. write ('i2 ', totalservice, bold) worksheet. write_row ('a2 ', title, format_title) ####################################### service_list = [] count = 0slist_end = 0 initline = 3 defwritetoexecl (numb, host, ip, group, service_list): globalinitlineglobalslist_endslist_len = str (len (service_list) initl = str (initline) slist_end = int (initl) + int (slist_len) -1slist_end1 = str (slist_end) # The write method 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 # output service and contact 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_end1initline = int (initl) + int (slist_len) foriina: service_dict ={}# define the dictionary storage service and contact if 'host _ name' ini: host = I. split () [1] printhostif 'service _ description' ini: key = I. split () [1] service_dict [key] = ''if 'contacts' ini: service_dict [key] = I. split () [1] # printservice_dictservice_list.append (service_dict) # printservice_listif 'address' ini: ip = I. split () [1] printipif 'hostgroups' ini: groups = I. split () [1] printservice_listprintgroupscount + = 1 writetoexecl (count, host, ip, groups, service_list) service_list = [] continueworkbook. close ()
 

The statistical results are as follows:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.