#!/usr/bin/python ########################################################################### # # netinfo2nagios.py - Parse the Nagios log file for messages about # missing host services. If the service doesn't # already exist, add it to the configuration. # # (c) 2013 Todd Shadburn # # Licensed under the GNU GPL version 2 # ########################################################################### import os,sys import re from pynag.Parsers import config # Alter these paths for your environment nagios_config_file = '/etc/nagios3/nagios.cfg' nagios_log_file = '/var/log/nagios3/nagios.log' nagios_object_file = '/etc/nagios3/conf.d/netinfo2nagios-services.cfg' # No user serviceable parts below this point nc = config(nagios_config_file) nc.parse() lf = open(nagios_log_file, 'r') if not lf: sys.stderr.write("ERROR: Could not open: %s\n" % (nagios_log_file)) sys.exit(2) of = open(nagios_object_file, 'a') if not of: sys.stderr.write("ERROR: Could not open: %s\n" % (nagios_object_file)) sys.exit(2) new_services = {} match_str = "service '(?P.+)' on host '(?P.+)', but the service could not be found" for line in lf: match = re.search(match_str, line); if match: target_host = match.group('hostname') service_description = match.group('service') service = nc.get_service(target_host, service_description) if not service: hs = target_host + ':' + service_description if not hs in new_services: sys.stdout.write("DEBUG: Adding service: %s %s\n" % (target_host, service_description)) new_services[hs] = {'host': target_host, 'service': service_description} svc_object = "define service{\n\tuse performance-service\n\thost_name %s\n\tservice_description %s\n\tcheck_command check-host-alive\n\t}\n\n" % (target_host, service_description) of.write(svc_object) lf.close() of.close() sys.exit(0)