summaryrefslogtreecommitdiff
path: root/munin/owfs_temperature_
blob: 9a4f2dfa0466d35914e1cb164bca8fe526befaa9 (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
118
119
120
121
122
123
124
125
#!/usr/bin/ruby
#
# Copyright (c) 2006 Peter Palfrader
#
#%# family=auto
#%# capabilities=autoconf suggest
#
# Munin plugin to moniture temperatures reported by 1-wire devices
# 
# I use owfs to mount the 1-wire bus to /var/lib/owfs
#   http://owfs.sourceforge.net/
#
# Add the following to your /etc/munin/plugin-conf.d/munin-node:
# [owfs_temperature_10.D234EE000800]
# user root
# env.title in rack 3
# env.label rack 3
#

$owfs_path = '/var/lib/owfs'
$owfs_path = ENV['OWFSPATH'] if ENV['OWFSPATH']

def bail_out(m)
	STDERR.puts "#{$0}: #{m}"
	exit 1
end

def autoconf
	if File.directory?($owfs_path)
		if File.directory?($owfs_path + '/structure')
			puts "yes"
		else
			puts "no (#{$owfs_path}/structure does not exist or permission denied - owfs not mounted?)"
		end
	else
		puts "no (#{$owfs_path} does not exist or permission denied)"
	end
end
def suggest
	Dir.glob($owfs_path+'/[0-9A-F][0-9A-F].*/temperature').each do |path|
		puts File.basename(File.dirname(path))
	end
end

def normalize_sensor(s)
	s = s.downcase
	s = s.tr('-', 'M')
	s = s.tr('+', 'P')
	s.tr('^a-zA-Z0-9', '_')
end

def query_device
	match = /_([0-9A-F][0-9A-F]\..*?)$/.match($0)
	unless match
		bail_out "Could not figure out which device you want based on executeable name."
	end
	match[1]
end

def config
	device = query_device
	title = ENV['title'] ?
	        ENV['title'] :
	        'Temperatures #{device}'
	label = ENV['label'] ?
	        ENV['label'] :
	        device
	puts "graph_title #{title}"
	puts "graph_args --base 1000"
	puts "graph_vlabel degrees Celsius";
	puts "graph_category sensors"
	puts "graph_info Temperature reported by 1-wire devices"
	n = normalize_sensor device
	puts "#{n}.label #{label}"
end

def report
	device = query_device
	Process.gid=0
	Process.egid=0
	temp = nil

	# Sometimes the directory in OWFS does not exist, and cat will say
	# cat: /var/lib/owfs/10.D234EE000800/temperature: Invalid argument
	#
	# ls'ing the directory help, let's see if stating it does too.
	8.times do
		break if File.directory?($owfs_path + '/' + device)
		STDERR.puts "Directory '#{$owfs_path}/#{device}' does not exist right now (or isn't a directory).  Sleeping a bit."
		sleep 1
	end

	# 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)
	IO.popen("-") do |f|
		unless f  # child
			begin
				exec('cat', $owfs_path+'/'+device+'/temperature')
			rescue => e
				puts "Cannot exec cat: "+e.message
				exit 1
			end
		end
		temp = f.readlines
	end

	if $? != 0
		STDERR.puts "Child exited with non-zero exit code(%d): %s"%[$? >> 8, temp.join('')]
	else
		n = normalize_sensor device
		puts "#{n}.value #{temp.join('')}"
	end
end


case ARGV[0]
	when "autoconf"
		autoconf
	when "suggest"
		suggest
	when "config"
		config
	else
		report
end