summaryrefslogtreecommitdiff
path: root/nagios-check-owfs-temp
blob: 65cf8517cf7c476b047007319e135427abac4e22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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=<ow device>"  , String, "Device ID")   { |@device| }
	opts.on("-n", "--name=<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