3 # Relay the status of a check that was previously run and which stored
4 # its result in a file to nagios.
6 # Copyright 2008, 2012 Peter Palfrader
8 # Permission is hereby granted, free of charge, to any person obtaining
9 # a copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sublicense, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so, subject to
14 # the following conditions:
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 NAGIOS_STATUS = { "OK": 0, "WARNING": 1, "CRITICAL": 2, "UNKNOWN": 3 }
34 UNITS_TO_SECONDS = { 's': 1, 'm': 60, 'h': 60*60, 'd': 24*60*60 }
36 parser = optparse.OptionParser()
37 parser.set_usage("%prog [options] <statusfile>")
38 parser.add_option("-a", "--age", dest="age", metavar="AGE",
39 type="string", default="26h",
40 help="maximum age, in seconds (or use Nm, Nh or Nd) - default is 26h.")
41 (options, args) = parser.parse_args()
44 parser.print_help(file=sys.stderr)
49 # find out what the max age is that we accept
50 m = re.match('([0-9]+)([smhd])?$', options.age)
52 print >> sys.stderr, "Invalid age %s"%(options.age)
53 parser.print_help(file=sys.stderr)
55 (count, unit) = m.groups()
56 if unit is None: unit = 's'
57 max_age = int(count) * UNITS_TO_SECONDS[unit]
59 # let's see if it exists
60 if not os.path.exists(statusfile):
61 print "UNKNOWN: %s does not exist."%(statusfile)
62 sys.exit(NAGIOS_STATUS['UNKNOWN'])
65 mtime = os.path.getmtime(statusfile)
66 if mtime + max_age < time.time():
67 print "WARNING: %s is old: %.1f hours."%(statusfile, (time.time() - mtime)/3600)
68 sys.exit(NAGIOS_STATUS['WARNING'])
70 status = open(statusfile, "r")
71 returnvalue = status.readline().strip()
73 if not returnvalue in NAGIOS_STATUS:
74 print "UNKNOWN: %s has invalid return value: %s."%(statusfile, returnvalue)
75 sys.exit(NAGIOS_STATUS['UNKNOWN'])
83 print "Found no output. Something is probably wrong"
84 sys.exit(NAGIOS_STATUS['UNKNOWN'])
86 sys.exit(NAGIOS_STATUS[returnvalue])
90 # vim:set shiftwidth=4: