dsa-check-statusfile: port to python3
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-statusfile
1 #!/usr/bin/python3
2
3 # Relay the status of a check that was previously run and which stored
4 # its result in a file to nagios.
5 #
6 # Copyright 2008, 2012 Peter Palfrader
7 #
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:
15 #
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
18 #
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.
26
27 import optparse
28 import os
29 import re
30 import sys
31 import time
32
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 }
35
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()
42
43 if len(args) != 1:
44     parser.print_help(file=sys.stderr)
45     sys.exit(1)
46
47 statusfile = args[0]
48
49 # find out what the max age is that we accept
50 m = re.match('([0-9]+)([smhd])?$', options.age)
51 if not m:
52     print("Invalid age %s" % options.age, file=sys.stderr)
53     parser.print_help(file=sys.stderr)
54     sys.exit(1)
55 (count, unit) = m.groups()
56 if unit is None: unit = 's'
57 max_age = int(count) * UNITS_TO_SECONDS[unit]
58
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'])
63
64
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'])
69
70 status = open(statusfile, "r")
71 returnvalue = status.readline().strip()
72
73 if returnvalue not in NAGIOS_STATUS:
74     print("UNKNOWN: %s has invalid return value: %s." % (statusfile, returnvalue))
75     sys.exit(NAGIOS_STATUS['UNKNOWN'])
76
77 linecnt = 0
78 for line in status:
79     print(line, end='')
80     linecnt += 1
81
82 if linecnt == 0:
83     print("Found no output.  Something is probably wrong")
84     sys.exit(NAGIOS_STATUS['UNKNOWN'])
85
86 sys.exit(NAGIOS_STATUS[returnvalue])
87
88 # vim:set et:
89 # vim:set ts=4:
90 # vim:set shiftwidth=4: