X-Git-Url: https://git.adam-barratt.org.uk/?p=mirror%2Fdsa-nagios.git;a=blobdiff_plain;f=dsa-nagios-checks%2Fchecks%2Fdsa-check-statusfile;h=841cd01dce6d136d7488125510e4373baadfb8ed;hp=a69d977bc5ff666f8c247f87f829a309faf3a065;hb=a13c9d2ff8a1cf7af89bbd3db1806679bc7ec108;hpb=42e99da9d896a33803e763c746e9a103183b6b34 diff --git a/dsa-nagios-checks/checks/dsa-check-statusfile b/dsa-nagios-checks/checks/dsa-check-statusfile index a69d977..841cd01 100755 --- a/dsa-nagios-checks/checks/dsa-check-statusfile +++ b/dsa-nagios-checks/checks/dsa-check-statusfile @@ -1,9 +1,9 @@ -#!/usr/bin/ruby +#!/usr/bin/python3 # Relay the status of a check that was previously run and which stored # its result in a file to nagios. # -# Copyright 2008 Peter Palfrader +# Copyright 2008, 2012 Peter Palfrader # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -24,58 +24,67 @@ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -require 'resolv' -require 'optparse' +import optparse +import os +import re +import sys +import time -NAGIOS_STATUS = { "OK" => 0, "WARNING" => 1, "CRITICAL" => 2, "UNKNOWN" => 3 } -UNITS_TO_SECONDS = { 's' => 1, 'm' => 60, 'h' => 60*60, 'd' => 24*60*60 } +NAGIOS_STATUS = { "OK": 0, "WARNING": 1, "CRITICAL": 2, "UNKNOWN": 3 } +UNITS_TO_SECONDS = { 's': 1, 'm': 60, 'h': 60*60, 'd': 24*60*60 } -def show_help(parser, code=0, io=STDOUT) - program_name = File.basename($0, '.*') - io.puts "Usage: #{program_name} [options] " - io.puts parser.summarize - exit(code) -end +parser = optparse.OptionParser() +parser.set_usage("%prog [options] ") +parser.add_option("-a", "--age", dest="age", metavar="AGE", + type="string", default="26h", + help="maximum age, in seconds (or use Nm, Nh or Nd) - default is 26h.") +(options, args) = parser.parse_args() -max_age = "26h" -ARGV.options do |opts| - opts.on_tail("-h", "--help" , "Display this help screen") { show_help(opts) } - opts.on("-a", "--age=AGE" , String, "maximum age, in seconds (or use Nm, Nh or Nd) - default is 26h") { |max_age| } - opts.parse! -end -show_help(ARGV.options, 1, STDERR) if ARGV.length != 1 +if len(args) != 1: + parser.print_help(file=sys.stderr) + sys.exit(1) -statusfile = ARGV.shift +statusfile = args[0] # find out what the max age is that we accept -unless (m = /^([0-9]+)([smhd])?$/.match max_age) - STDERR.puts "Invalid age #{age}." - show_help(ARGV.options, 1, STDERR) if ARGV.length != 1 -end -max_age = m[1].to_i * UNITS_TO_SECONDS[m[2] ? m[2] : 's'] +m = re.match('([0-9]+)([smhd])?$', options.age) +if not m: + print("Invalid age %s" % options.age, file=sys.stderr) + parser.print_help(file=sys.stderr) + sys.exit(1) +(count, unit) = m.groups() +if unit is None: unit = 's' +max_age = int(count) * UNITS_TO_SECONDS[unit] # let's see if it exists -unless File.exists? statusfile - puts "UNKNOWN: #{statusfile} does not exist." - exit NAGIOS_STATUS['UNKNOWN'] -end - - -mtime = File.stat(statusfile).mtime -if mtime + max_age < Time.now - puts "WARNING: #{statusfile} is old: #{mtime}" - exit NAGIOS_STATUS['WARNING'] -end - -status = File.new(statusfile) -returnvalue = status.readline.chomp - -unless NAGIOS_STATUS.has_key? returnvalue - puts "UNKNOWN: #{statusfile} has invalid return value: #{returnvalue}" - exit NAGIOS_STATUS['UNKNOWN'] -end - -status.readlines.each do |line| - print line -end -exit NAGIOS_STATUS[returnvalue] +if not os.path.exists(statusfile): + print("UNKNOWN: %s does not exist." % statusfile) + sys.exit(NAGIOS_STATUS['UNKNOWN']) + + +mtime = os.path.getmtime(statusfile) +if mtime + max_age < time.time(): + print("WARNING: %s is old: %.1f hours." % (statusfile, (time.time() - mtime)/3600)) + sys.exit(NAGIOS_STATUS['WARNING']) + +status = open(statusfile, "r") +returnvalue = status.readline().strip() + +if returnvalue not in NAGIOS_STATUS: + print("UNKNOWN: %s has invalid return value: %s." % (statusfile, returnvalue)) + sys.exit(NAGIOS_STATUS['UNKNOWN']) + +linecnt = 0 +for line in status: + print(line, end='') + linecnt += 1 + +if linecnt == 0: + print("Found no output. Something is probably wrong") + sys.exit(NAGIOS_STATUS['UNKNOWN']) + +sys.exit(NAGIOS_STATUS[returnvalue]) + +# vim:set et: +# vim:set ts=4: +# vim:set shiftwidth=4: