Move files into specific directories in source
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-statusfile
1 #!/usr/bin/ruby
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 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 require 'resolv'
28 require 'optparse'
29
30 NAGIOS_STATUS = { "OK" => 0, "WARNING" => 1, "CRITICAL" => 2, "UNKNOWN" => 3 }
31 UNITS_TO_SECONDS = { 's' => 1, 'm' => 60, 'h' => 60*60, 'd' => 24*60*60 }
32
33 def show_help(parser, code=0, io=STDOUT)
34   program_name = File.basename($0, '.*')
35   io.puts "Usage: #{program_name} [options] <statusfile>"
36   io.puts parser.summarize
37   exit(code)
38 end
39
40 max_age = "26h"
41 ARGV.options do |opts|
42         opts.on_tail("-h", "--help" , "Display this help screen")                                               { show_help(opts) }
43         opts.on("-a", "--age=AGE"  , String, "maximum age, in seconds (or use Nm, Nh or Nd) - default is 26h")  { |max_age| }
44         opts.parse!
45 end
46 show_help(ARGV.options, 1, STDERR) if ARGV.length != 1
47
48 statusfile = ARGV.shift
49
50 # find out what the max age is that we accept
51 unless (m = /^([0-9]+)([smhd])?$/.match max_age)
52         STDERR.puts "Invalid age #{age}."
53         show_help(ARGV.options, 1, STDERR) if ARGV.length != 1
54 end
55 max_age = m[1].to_i * UNITS_TO_SECONDS[m[2] ? m[2] : 's']
56
57 # let's see if it exists
58 unless File.exists? statusfile
59         puts "UNKNOWN: #{statusfile} does not exist."
60         exit NAGIOS_STATUS['UNKNOWN']
61 end
62
63
64 mtime = File.stat(statusfile).mtime
65 if mtime + max_age < Time.now
66         puts "WARNING: #{statusfile} is old: #{mtime}"
67         exit NAGIOS_STATUS['WARNING']
68 end
69
70 status = File.new(statusfile)
71 returnvalue = status.readline.chomp
72
73 unless NAGIOS_STATUS.has_key? returnvalue
74         puts "UNKNOWN: #{statusfile} has invalid return value: #{returnvalue}"
75         exit NAGIOS_STATUS['UNKNOWN']
76 end
77
78 status.readlines.each do |line|
79         print line
80 end
81 exit NAGIOS_STATUS[returnvalue]