From d6f78d938ca0ea95e0dad0dfd6d5ddd375809a1e Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Thu, 1 Feb 2007 12:33:59 +0000 Subject: Add nagios-check-owfs-temp git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@253 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- nagios-check-owfs-temp | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100755 nagios-check-owfs-temp diff --git a/nagios-check-owfs-temp b/nagios-check-owfs-temp new file mode 100755 index 0000000..65cf851 --- /dev/null +++ b/nagios-check-owfs-temp @@ -0,0 +1,117 @@ +#!/usr/bin/ruby +# +# Copyright (c) 2006,2007 Peter Palfrader + +require 'optparse' + +NAGIOS_STATUS = { :OK => 0, :WARNING => 1, :CRITICAL => 2, :UNKNOWN => -1 }; + +$owfs_path = '/var/lib/owfs' +$owfs_path = ENV['OWFSPATH'] if ENV['OWFSPATH'] + +def bail_out(m) + STDERR.puts "#{$0}: #{m}" + exit 1 +end + +def runcmd(command, input) + rdin , wrin = IO.pipe + rdout, wrout = IO.pipe + rderr, wrerr = IO.pipe + + pid = fork + unless pid + # child + wrin.close + rdout.close + rderr.close + STDIN.reopen rdin + STDOUT.reopen wrout + STDERR.reopen wrerr + exec(*command) + throw("fell through exec(). WTF.") + end + rdin.close + wrout.close + wrerr.close + + out = [] + err = [] + tin = Thread.new { wrin.print input; wrin.close } + tout = Thread.new { out = rdout.readlines } + terr = Thread.new { err = rderr.readlines } + tin.join + tout.join + terr.join + Process.wait pid + + exitstatus = $?.exitstatus + + [exitstatus, out, err] +end + +def report(device) + Process.gid=0 + Process.egid=0 + + # fuse does weird checks. this fails: + # File.new($owfs_path+'/'+device+'/temperature', "r") ---> FAILS: in `initialize': Permission denied - /var/lib/owfs/10.D234EE000800/temperature (Errno::EACCES) + + # Sometimes the directory in OWFS does not exist, and cat will say + # cat: /var/lib/owfs/10.D234EE000800/temperature: Invalid argument + # so retry it a few times, since ls'ing the directory helps on the command line at least + 5.times do + exitstatus, out, err = runcmd(['cat', $owfs_path+'/'+device+'/temperature'], '') + if exitstatus != 0 + STDERR.puts "Child exited with non-zero exit code(%d): %s"%[exitstatus >> 8, err.join] + runcmd(['ls', $owfs_path], '') + sleep 1 + else + STDERR.puts "command succeeded but returned something on stderr: #{err.join}" if err.size > 0 + return out.join.to_f + end + end + puts "Could not get data for #{device}." + exit NAGIOS_STATUS[:UNKNOWN]; +end + + +@lowcrit = 10; +@lowwarn = 15; +@highwarn = 25; +@highcrit = 30; + +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 +ARGV.options do |opts| + opts.on_tail("-h", "--help" , "Display this help screen") { show_help(opts) } + opts.on("-d", "--device=" , String, "Device ID") { |@device| } + opts.on("-n", "--name=" , String, "Human readable device specifier") { |@name| } + opts.on("-w", "--high-warn=integer" , Integer, "Upper warning limit") { |@highwarn| } + opts.on("-c", "--high-crit=integer" , Integer, "Upper critical limit") { |@highcrit| } + opts.on("-W", "--low-warn=integer" , Integer, "Lower warning limit") { |@lowwarn| } + opts.on("-C", "--low-crit=integer" , Integer, "Lower critical limit") { |@lowcrit| } + opts.parse! +end +show_help(ARGV.options, 1, STDERR) if ARGV.length != 0 +show_help(ARGV.options, 1, STDERR) unless @device + +@name = @device unless @name; + +temp = report @device + + +if temp > @highcrit or temp < @lowcrit + puts "CRITICAL: device #{@name} is at #{temp}" + exit NAGIOS_STATUS[:CRITICAL] +elsif temp > @highwarn or temp < @lowwarn + puts "WARN: device #{@name} is at #{temp}" + exit NAGIOS_STATUS[:WARNING] +else + puts "OK: device #{@name} is at #{temp}" + exit NAGIOS_STATUS[:OK] +end -- cgit v1.2.3