332261e8ab649194b0286d4d6e91938ed744b312
[mirror/dsa-nagios.git] / dsa-nagios-checks / dsa-check-puppet
1 #!/usr/bin/env ruby
2
3 require 'optparse'
4
5 class CheckPuppet
6
7   VERSION = '0.1'
8   script_name = File.basename($0)
9
10   # default options
11   OPTIONS = {
12      :statefile   => "/var/lib/puppet/state/state.yaml",
13      :interval    => 60,
14   }
15
16   o = OptionParser.new do |o|    
17     o.set_summary_indent('  ')
18     o.banner =    "Usage: #{script_name} [OPTIONS]"
19     o.define_head "The check_puppet Nagios plug-in checks that the specified " +
20                   "puppet state file is no older than specified interval."
21     o.separator   ""
22     o.separator   "Mandatory arguments to long options are mandatory for " +
23                   "short options too."
24   
25     o.on("-s", "--statefile=statefile", String, "The state file",
26          "Default: #{OPTIONS[:statefile]}") { |OPTIONS[:statefile]| }
27     o.on("-i", "--interval=value", Integer, 
28          "Default: #{OPTIONS[:interval]} minutes")  { |OPTIONS[:interval]| }
29      
30     o.separator ""
31     o.on_tail("-h", "--help", "Show this help message.") do 
32       puts o
33       exit  
34     end
35   
36     o.parse!(ARGV)
37    end
38
39   def check_state
40
41     # Set variables
42     curt = Time.now
43     intv = OPTIONS[:interval] * 60
44
45     # Check file time
46     @modt = 0
47     begin
48       @modt = File.mtime("#{OPTIONS[:statefile]}")
49     rescue
50       @file = 3
51     end
52  
53     diff = (curt - @modt).to_i
54
55     @file = 2
56     @file = 0 if diff <= intv
57
58   end
59
60   def output_status
61    
62     case @file
63     when 0
64       state = "state file status okay updated on " + @modt.strftime("%m/%d/%Y at %H:%M:%S")
65     when 2
66       state = "state file is missing or older than #{OPTIONS[:interval]} minutes"
67     when 3
68       state = "state file status unknown"
69     end
70
71     case @file
72     when 0
73       status = "OK"
74       exitcode = 0
75     when 2
76       status = "CRITICAL"
77       exitcode = 2
78     when 3
79       status = "UNKNOWN"
80       exitcide = 3
81     end
82
83     puts "PUPPET " + status + ": " + state
84     exit(exitcode)
85  end
86 end
87
88 cp = CheckPuppet.new
89 cp.check_state
90 cp.output_status
91