Commit d113e907 by JR Dalrymple

Right now the python will create the list of services that need to be created in nagios.cfgs

parent 7d1accc0
......@@ -2,7 +2,7 @@ This plugin seeks to solve the problem of my passive checks that don't have serv
I have a bunch using my ultra cool check_citrix_user plugin that does passive submission, but don't want to go through and create all the users that might be logging in. I'd rather have the services just magically show up.
The process is that this plugin runs every 5 minutes - looks for all the undefined services that come in via NSCA. These are identifiable by the string "Warning: Passive check result was received for service '<servicename>' on host '<hostname>', but the service could not be found!" When it sees them it will compare them to services that do exist (in case it has already done it's magic) and if it still doesn't exist it will create it using a template (not Nagios template, but rather a .cfg file template) specified on the command line.
The process is that this plugin runs every 5 minutes - looks for all the undefined services that come in via NSCA. These are identifiable by the string "Warning: Passive check result was received for service '<servicename>' on host '<hostname>', but the service could not be found!" in nagios.log. When it sees them it will compare them to services that do exist (in case it has already done it's magic) and if it still doesn't exist it will create it using a template (not Nagios template, but rather a .cfg file template) specified on the command line.
Arguments:
......
#!/usr/bin/python
import os
import sys
import getopt
import re
nagiosLog = '/usr/local/nagios/var/nagios.log'
nagiosObjectsFile = '/usr/local/nagios/var/objects.cache'
cfgInFile = ''
cfgOutDir = ''
host = ''
servicesToCreate = []
servicesToPop = []
hostServiceList = []
existingServices = []
unconfiguredServiceMatch = re.compile(r"(^.*Passive check result was received for service ')(.*?)(' on host ')(.*?)(', but the service could not be found)")
# Borrowing some code from my other project (should turn it into a module I suppose)
startObjMatch = re.compile(r"(^\s*define\s*)(\S*)(.*{\s*$)")
endObjMatch = re.compile(r"^\s*}\s*$")
midObjMatch = re.compile(r"^\s*(\S*)\s*(.*)\s*")
#USAGE
def usage():
print('Usage: check_passive_autocreate.py -H hostname -i /path/to/template.cfg -d /path/to/passive/service.cfg \n')
try:
opts, args = getopt.getopt(sys.argv[1:], 'H:i:d:')
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(3)
for opt, arg in opts:
if opt in ('-H'):
host = arg
elif opt in ('-i'):
cfgInFile = arg
elif opt in ('-d'):
cfgOutDir = arg
else:
usage()
sys.exit(3)
if not 'cfgInFile' in locals():
print('No input template specified.')
sys.exit(3)
if not os.path.isfile(cfgInFile):
print('Could not find input template.')
sys.exit(3)
if not 'host' in locals():
print('No host specified - we don\'t want to create a service for any random host sending passive checks do we?')
sys.exit(3)
if not 'cfgOutDir' in locals():
print('No output directory specified.')
sys.exit(3)
if not os.path.isdir(cfgOutDir):
print('Could not find output directory, check and make sure it exists.')
sys.exit(3)
with open(nagiosObjectsFile) as f:
nagiosObjectsLines = f.read().splitlines()
f.close
interested = 0
newService = ['','']
for line in nagiosObjectsLines:
if startObjMatch.match(line):
if startObjMatch.search(line).group(2) == "service":
interested = 1
continue
if midObjMatch.search(line).group(1) == 'host_name' and interested == 1:
newService[0] = midObjMatch.search(line).group(2)
continue
if midObjMatch.search(line).group(1) == 'service_description' and interested == 1:
newService[1] = midObjMatch.search(line).group(2)
continue
if endObjMatch.match(line) and interested == 1:
existingServices.append(newService)
newService = ['','']
interested = 0
for existingService in existingServices:
if existingService[0] != host:
servicesToPop.append(existingService)
for service in servicesToPop:
existingServices.remove(service)
for remaining in existingServices:
hostServiceList.append(remaining[1])
with open(nagiosLog) as f:
nagiosLogLines = f.read().splitlines()
f.close
for line in nagiosLogLines:
if unconfiguredServiceMatch.match(line):
if unconfiguredServiceMatch.search(line).group(4) == host:
servicesToCreate.append(unconfiguredServiceMatch.search(line).group(2))
servicesToCreate = list(set(servicesToCreate))
for servicesNotToCreate in hostServiceList:
if servicesNotToCreate in servicesToCreate:
servicesToCreate.remove(servicesNotToCreate)
### Here we are finally with the list of services we need to create for the host - list servicesToCreate
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment