Generalize (and simplify) the puppet file age check so that we can reuse
authorStephen Gran <steve@lobefin.net>
Thu, 21 May 2009 19:51:10 +0000 (20:51 +0100)
committerStephen Gran <steve@lobefin.net>
Thu, 21 May 2009 19:51:10 +0000 (20:51 +0100)
it for other files
Signed-off-by: Stephen Gran <steve@lobefin.net>
config/nagios-master.cfg
dsa-nagios-checks/checks/dsa-check-file_age [new file with mode: 0644]
dsa-nagios-checks/checks/dsa-check-puppet [deleted file]

index ca00fe5..4e8e79c 100644 (file)
@@ -893,7 +893,7 @@ services:
 
   -
     name: puppet
-    nrpe: "/usr/lib/nagios/plugins/dsa-check-puppet"
+    nrpe: "/usr/lib/nagios/plugins/dsa-check-file-age -f /var/lib/puppet/state/state.yaml"
     hostgroups: lenny
     excludehosts: agnesi
 
diff --git a/dsa-nagios-checks/checks/dsa-check-file_age b/dsa-nagios-checks/checks/dsa-check-file_age
new file mode 100644 (file)
index 0000000..e94cfad
--- /dev/null
@@ -0,0 +1,49 @@
+#!/bin/sh
+# vim: set fileencoding=utf-8 ai noet sts=8 sw=8 tw=0:
+
+FILE=''
+INTERVAL=60
+EXIT=0
+
+usage(){
+       ret=$1
+
+       cat <<EOF
+$0: usage:
+       $0 <options>
+
+       File age checker for nagios.  Will alert if the named file is
+       older than the interval (interval default is 60 minutes)
+
+       -h      This help message
+       -i      Interval in minutes at which to alert
+       -f      File to check
+EOF
+
+       exit $ret
+}
+
+while getopts f:i:h opt ; do
+       case "$opt" in
+               f) FILE="$OPTARG" ;;
+               i) INTERVAL="$OPTARG" ;;
+               h) usage 0
+       esac
+done
+
+if [ -z "$FILE" ]; then
+       echo "Need file argument!" >&2
+       usage 1
+fi
+
+if [ ! -r "$FILE" ]; then
+       printf "state file %s is missing or unreadable\n" $FILE
+       exit 2
+fi
+
+if [ "$(( $( date +%s ) - $(stat -c %Y $FILE) ))" -gt "$(( $INTERVAL * 60 ))" ]; then
+       printf "state file %s is older than %d minutes\n" $FILE $INTERVAL
+       exit 2
+fi
+
+printf "state file %s OK: updated on %s\n" $FILE "$(stat -c %y $FILE)"
diff --git a/dsa-nagios-checks/checks/dsa-check-puppet b/dsa-nagios-checks/checks/dsa-check-puppet
deleted file mode 100755 (executable)
index 332261e..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-#!/usr/bin/env ruby
-
-require 'optparse'
-
-class CheckPuppet
-
-  VERSION = '0.1'
-  script_name = File.basename($0)
-
-  # default options
-  OPTIONS = {
-     :statefile   => "/var/lib/puppet/state/state.yaml",
-     :interval    => 60,
-  }
-
-  o = OptionParser.new do |o|    
-    o.set_summary_indent('  ')
-    o.banner =    "Usage: #{script_name} [OPTIONS]"
-    o.define_head "The check_puppet Nagios plug-in checks that the specified " +
-                  "puppet state file is no older than specified interval."
-    o.separator   ""
-    o.separator   "Mandatory arguments to long options are mandatory for " +
-                  "short options too."
-  
-    o.on("-s", "--statefile=statefile", String, "The state file",
-         "Default: #{OPTIONS[:statefile]}") { |OPTIONS[:statefile]| }
-    o.on("-i", "--interval=value", Integer, 
-         "Default: #{OPTIONS[:interval]} minutes")  { |OPTIONS[:interval]| }
-     
-    o.separator ""
-    o.on_tail("-h", "--help", "Show this help message.") do 
-      puts o
-      exit  
-    end
-  
-    o.parse!(ARGV)
-   end
-
-  def check_state
-
-    # Set variables
-    curt = Time.now
-    intv = OPTIONS[:interval] * 60
-
-    # Check file time
-    @modt = 0
-    begin
-      @modt = File.mtime("#{OPTIONS[:statefile]}")
-    rescue
-      @file = 3
-    end
-    diff = (curt - @modt).to_i
-
-    @file = 2
-    @file = 0 if diff <= intv
-
-  end
-
-  def output_status
-   
-    case @file
-    when 0
-      state = "state file status okay updated on " + @modt.strftime("%m/%d/%Y at %H:%M:%S")
-    when 2
-      state = "state file is missing or older than #{OPTIONS[:interval]} minutes"
-    when 3
-      state = "state file status unknown"
-    end
-
-    case @file
-    when 0
-      status = "OK"
-      exitcode = 0
-    when 2
-      status = "CRITICAL"
-      exitcode = 2
-    when 3
-      status = "UNKNOWN"
-      exitcide = 3
-    end
-
-    puts "PUPPET " + status + ": " + state
-    exit(exitcode)
- end
-end
-
-cp = CheckPuppet.new
-cp.check_state
-cp.output_status
-