summaryrefslogtreecommitdiff
path: root/munin/ipmi_sensor_
blob: aa76ee0a2fa638984916beb9fe2b6c1e5160684b (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/ruby
#
# munin plugin for the sensors data provided by ipmi
#
#
# Copyright (c) 2006 Peter Palfrader
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
#
#%# family=auto
#%# capabilities=autoconf suggest
#
#
#
# ipmitool probably needs to be run as root, and it may take more than 10 seconds on some hosts.
#
# Add the following to your /etc/munin/plugin-conf.d/munin-node:
# [ipmi_sensor_*]
# user root
# timeout 20
#

require 'yaml';

VALID_UNITS = %w{volts degrees_c rpm amps watts}

CACHEDIR = "/var/lib/munin/plugin-state"
CACHEFILE = "plugin-ipmi_sensor.cache"
CACHE_AGE = 275
IPMITOOL = '/usr/bin/ipmitool'

Thread.abort_on_exception = true

def bail_out(m)
	STDERR.puts "#{$0}: #{m}"
	exit 1
end
def normalize_sensor(s)
	s = s.downcase
	s = s.tr('-', 'M')
	s = s.tr('+', 'P')
	s.tr('^a-zA-Z0-9', '_')
end
def normalize_unit(s)
	s.downcase.tr('^a-z0-9', '_')
end
def fetch_cache
	fn = CACHEDIR+"/"+CACHEFILE
	return nil unless FileTest.exists? fn
	cache = YAML::load( File.open(fn) )
	return nil unless cache
	return nil unless cache[:version] == 1
	return nil if Time.now - cache[:timestamp] > CACHE_AGE
	return cache[:data]
end
def write_cache(data)
	fn = CACHEDIR+"/"+CACHEFILE
	return unless FileTest.directory? CACHEDIR
	return unless FileTest.writable? CACHEDIR or FileTest.writable? fn
	File.open(fn, "w", 0600) { |f|
		cache = {
				:version => 1,
				:timestamp => Time.now,
				:data => data
			}
		f.puts cache.to_yaml
	}
end
def get_sensor_data
	data = fetch_cache
	return data if data

	data = []
	names = {}

	rdout, wrout = IO.pipe
	rderr, wrerr = IO.pipe

	pid = fork
	unless pid
		# child
		rdout.close
		rderr.close
		STDOUT.reopen wrout
		STDERR.reopen wrerr
		exec(IPMITOOL, '-I', 'open', 'sensor')
		bail_out("fell through exec(). WTF.")
	end
	wrout.close
	wrerr.close


	out = []
	err = []
	tout = Thread.new { out = rdout.readlines }
	terr = Thread.new { err = rderr.readlines }
	tout.join
	terr.join
	Process.wait pid

	bail_out("ipmitool printed stuff on stdout: "+err.join('')) if (err.length > 0)

	out.each do |line|
		line.strip!
		# cpu1.vtt-s3      | 1.200      | Volts      | ok    | na        | 1.080     | na        | na        | 1.320     | na        
		# CPU0 IERR        | 0x0        | discrete   | 0x0100| na        | na        | na        | na        | na        | na
		items = line.split(/\s*\|\s*/)
		unless items.size == 10
			bail_out "Got weird number of fields in ipmitool output.  Expected 10 but got #{items.size} for line '#{line}'."
		end
		(name, value, unit, status, lower_non_recov, lower_crit, lower_non_crit, upper_non_crit, upper_crit, upper_non_recov) = items
		nname = normalize_sensor name;
		if names.has_key?(nname)
			bail_out "Sensor name #{name} (normalized to #{nname}) appears more than once in ipmitool output."
		end
		names[nname] = 1
		data << {
			:name => name,
			:value => value,
			:unit => unit,
			:status => status,
			:lower_non_recov => lower_non_recov,
			:lower_crit => lower_crit,
			:lower_non_crit => lower_non_crit,
			:upper_non_crit => upper_non_crit,
			:upper_crit => upper_crit,
			:upper_non_recov => upper_non_recov,
			:line => line
		}
	end

	write_cache data
	data
end

def autoconf
	if get_sensor_data.length > 0
		puts "yes"
	else
		puts "no (no ipmitool output)"
	end
end
def suggest
	#puts get_sensor_data.collect{ |i| normalize_unit(i[:unit]) }.uniq.sort.delete_if{ |i| i == "discrete" }
	VALID_UNITS.each do |u|
		puts "u_#{u}"
	end
end

def query_unit
	match = /_u_(.*?)$/.match($0)
	unless match
		bail_out "Could not figure out which unit you want based on executeable name."
	end
	match[1]
end

def config
	u = query_unit
	case u
		when "volts"
			puts "graph_title IPMI Sensors: Voltages"
			puts "graph_args --base 1000"
			puts "graph_vlabel Volts";
			puts "graph_category sensors"
			puts "graph_info This graph shows the voltages as reported by IPMI"
			get_sensor_data.each do |d|
				next unless normalize_unit(d[:unit]) == u
				n = normalize_sensor(d[:name])
				puts "#{n}.label #{d[:name]}"
				puts "#{n}.warning #{d[:lower_non_crit]}:#{d[:upper_non_crit]}"
				puts "#{n}.critical #{d[:lower_non_recov]}:#{d[:upper_non_recov]}"
			end
		when "degrees_c"
			puts "graph_title IPMI Sensors: Temperature"
			puts "graph_args --base 1000 -l 0"
			puts "graph_vlabel Degrees C";
			puts "graph_category sensors"
			puts "graph_info This graph shows the temperatures as reported by IPMI"
			get_sensor_data.each do |d|
				next unless normalize_unit(d[:unit]) == u
				n = normalize_sensor(d[:name])
				puts "#{n}.label #{d[:name]}"
				puts "#{n}.warning #{d[:lower_non_crit]}:#{d[:upper_non_crit]}"
				puts "#{n}.critical #{d[:lower_non_recov]}:#{d[:upper_non_recov]}"
			end
		when "rpm"
			puts "graph_title IPMI Sensors: RPMs"
			puts "graph_args --base 1000 -l 0"
			puts "graph_vlabel RPM";
			puts "graph_category sensors"
			puts "graph_info This graph shows the RPMs as reported by IPMI"
			get_sensor_data.each do |d|
				next unless normalize_unit(d[:unit]) == u
				n = normalize_sensor(d[:name])
				puts "#{n}.label #{d[:name]}"
				# no warning, only critical puts "#{n}.warning #{d[:lower_non_crit]}:#{d[:upper_non_crit]}"
				puts "#{n}.critical #{d[:lower_non_recov]}:#{d[:upper_non_recov]}"
			end
		when "amps"
			puts "graph_title IPMI Sensors: Amperes"
			puts "graph_args --base 1000"
			puts "graph_vlabel Amperes";
			puts "graph_category sensors"
			puts "graph_info This graph shows the amperes as reported by IPMI"
			get_sensor_data.each do |d|
				next unless normalize_unit(d[:unit]) == u
				n = normalize_sensor(d[:name])
				puts "#{n}.label #{d[:name]}"
			end
		when "watts"
			puts "graph_title IPMI Sensors: Watts"
			puts "graph_args --base 1000"
			puts "graph_vlabel Watts";
			puts "graph_category sensors"
			puts "graph_info This graph shows the watts as reported by IPMI"
			get_sensor_data.each do |d|
				next unless normalize_unit(d[:unit]) == u
				n = normalize_sensor(d[:name])
				puts "#{n}.label #{d[:name]}"
			end
		else
			bail_out "Do not know how to handle unit '#{u}'"
	end
end


def report
	u = query_unit
	get_sensor_data.each do |d|
		next unless normalize_unit(d[:unit]) == u
		n = normalize_sensor(d[:name])
		puts "#{n}.value #{d[:value]}"
	end
end


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