summaryrefslogtreecommitdiff
path: root/files/munin/apcupsd_ww
blob: b0b86f7cb051eff376c955263974a8276717606a (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/perl -w
# 
# Plugin to monitor apcupsd via apcaccess
#
# Version 1.3
# 
# Copyright (C) 2005-2008 Behan Webster <behanw AT websterwood DOT com>
# Licenced under GPL 2.0
#
# Written by: Behan Webster <behanw AT websterwood DOT com>
# German translation by: Bianco Veigel <bianco.veigel AT zivillian DOT de>
# 
#%# family=auto
#%# capabilities=autoconf

use strict;
use warnings;
use vars qw(%attrs %data %num);

my $apcaccess='/sbin/apcaccess';
#$apcaccess='/home/behanw/bin/apcaccess';
my $config='/etc/munin/plugin-conf.d/apcupsd_ww';

my $language = $ENV{LANG} || 'en';

# Example apcaccess output
# KEY      : VALUE
#
# UPSNAME  : Elfhild
# MODEL    : SMART-UPS 1400 RM XL
# STATUS   : ONLINE 
# LINEV    : 123.5 Volts
# LOADPCT  :  24.9 Percent Load Capacity
# BCHARGE  : 100.0 Percent
# TIMELEFT :  63.0 Minutes
# OUTPUTV  : 123.5 Volts
# ITEMP    : 39.1 C Internal
# BATTV    : 54.5 Volts
# NOMOUTV  : 115 Volts
# NOMBATTV :  48.0 Volts

# Possible values to graph in munin
# Only the ones which are available will be graphed
%attrs = (
#	APCACCESS_KEY => {
#		name => 'attribute_name',
#		label => {
#			en => 'English title',
#                       de => 'Titel in Deutsch',
#			fr => 'titre en Francais',
#		},
#		type => 'name_of_functio_for_type', # Default is 'num'
#		# Can use one value, or list of values. If the first value can't be used, try the next.
#		#   KEY          Key from apcaccess
#		#   num,num,num  Specify a list of possible nominal values to guess from
#		#   num          Specify fixed nominal value
#		nominal => [ 'KEY', '100' ],
#		#   KEY          Key from apcaccess
#		#   +-num%       Calculate percentage min:max from nominal value
#		#   +-num        Calculate min:max from nominal value
#		#   -num:+num    Calculate min:max from nominal value
#		#   num:num      Specify fixed min:max values
#		#   num or :num  Specify fixed max value
#		#   num:         Specify fixed min value
#		warning => [ 'KEY:KEY', '+-10%', '-10:+15' ],
#		critical => [ 'KEY:KEY', '+-10%', '-10:+15' ],
#	},
	BCHARGE	=> {	# BCHARGE  : 100.0 Percent
		name => 'battery',
		label => {
			en => 'Percent battery charge',
			de => 'Batterieladung in Prozent',
		},
		warning  => '33:',	# %
		critical => '5:',	# %
	},
	LOADPCT => {	 # LOADPCT  :  28.6 Percent Load Capacity
		name => 'upsload',
		label => {
			en => 'Percent load capacity',
			de => 'Auslastung in Prozent',
		},
		warning  => '75',	# %
		critical => '90',	# %
	},
	TIMELEFT => {	# TIMELEFT :  17.0 Minutes
		name => 'timeleft',
		label => {
			en => 'Minutes of run time',
			de => 'Akkulaufzeit in Minuten',
		},
		warning  => '5:',	# mins
		critical => [ 'DLOWBATT:', '2:' ],	# DLOWBATT : 02 Minutes
	},
	LINEV => {	# LINEV    : 121.5 Volts
		name => 'linevolts',
		label => {
			en => 'Line voltage',
			de => 'Eingangsspannung',
		},
		nominal => [ 'NOMINV', 'NOMOUTV', '115,230' ], # NA=115V, Europe=230V
		warning  => [ '+-10%', '108:128' ],
		critical => [ 'LOTRANS:HITRANS', '+-15%', '104:132' ],
	},
	BATTV => {	# BATTV    : 27.7 Volts
		name => 'batteryvolts',
		label => {
			en => 'Battery voltage',
			de => 'Batteriespannung',
		},
		nominal => [ 'NOMBATTV', '12,24,48' ],	# NOMBATTV :  48.0 Volts
		warning =>  '-5%:+15%',
		critical => '-10%:+25%',
	},
	OUTPUTV => {	# OUTPUTV  : 122.2 Volts
		name => 'outputvolts',
		label => {
			en => 'Output voltage',
			de => 'Ausgangsspannung',
		},
		nominal => [ 'NOMOUTV',	'115,230' ], # NOMOUTV  : 115 Volts
		warning =>  [ '+-10%', '108:128' ],
		critical => [ 'LOTRANS:HITRANS', '+-15%', '104:132' ],
	},
	ITEMP => {	# ITEMP    : 44.1 C Internal
		name => 'temperature',
		label => {
			en => 'UPS temperature',
			de => 'USV Temperatur',
		},
		warning => 50,	# C
		critical => 60,	# C
	},
	LINEFAIL => {	# LINEFAIL : OK
		name => 'linefail',
		label => {
			en => 'Line voltage status',
			de => 'Status Eingangsspannung',
		},
		type => 'bool',
		critical => '0:',	# Failed
	},
	BATTSTAT => {	# BATTSTAT : OK
		name => 'battstat',
		label => {
			en => 'Battery status',
			de => 'Batteriestatus',
		},
		type => 'bool',
		critical => '0:',	# Failed
	},
	MAINS => {	# MAINS    : OK
		name => 'mains',
		label => {
			en => 'Mains status',
			de => 'Status Eingangsspannung',
		},
		type => 'bool',
		critical => '0:',	# Failed
	},
	#STATUS => {	# STATUS   : ONLINE
	#	name => 'status',
	#	label => {
	#		en => 'Status',
	#	},
	#	type => 'status',
	#	critical => 0,
	#},
);

# Read config file
# Can be used to override settings in %attrs
if (-f $config) {
	require $config;
}

# Determine plugin capabilities
if (defined $ARGV[0] && $ARGV[0] =~ /autoconf|detect/) {
	if (-x $apcaccess) {
		print "yes\n";
		exit 0;
	} else {
		print "no (apcaccess not found)\n";
		exit 1;
	}
}

# Read info from apcupsd using apcaccess
die "$apcaccess: not found\n" unless -x $apcaccess;
open (APCACCESS, "$apcaccess 2>&1 |") || die "$apcaccess: $!\n";
while (<APCACCESS>) {
	chomp;
	die "$apcaccess: $_\n" if /Error contacting apcupsd/;
	$data{$1} = $2 if /^(\S+?)\s*:\s+(.+?)$/;
	$num{$1} = $2 if /^(\S+?)\s*:\s+([\d.x]+)/;
}
close APCACCESS;

# Auto-configure plugin
if (defined $ARGV[0] && $ARGV[0] eq 'config') {
	if (defined $data{UPSNAME}) {
		print "graph_title $data{UPSNAME} ($data{MODEL})\n";
	} else {
		print "graph_title $data{MODEL}\n";
	}
	#print "graph_vlabel Units\n";
	print "graph_category ups\n";
	print "graph_info This graph shows information about your APC uninterruptible power supply.\n";

	foreach my $what (sort keys %attrs) {
		&label("$what");
	}

# Print current values
} else {
	foreach my $what (sort keys %attrs) {
		next unless defined $data{$what};
		my $func = $attrs{$what}{type} || 'num';
		my $value = eval "\&$func('$what')";
		print "$attrs{$what}{name}.value $value\n";
	}
}

exit 0;

##############################################################################
# Print label/title for value
sub label {
	my $what = shift;
	return unless defined $data{$what};

	my $attr = $attrs{$what};

	# Determine language to use for labels
	my $lang = $language;
	$lang =~ s/_.*$// unless defined $attr->{label}{$lang};
	# Failback to english if translation isn't available
	$lang = 'en' unless defined $attr->{label}{$lang};

	print "$attr->{name}.label $attr->{label}{$lang}\n";
	&info($what, 'warning');
	&info($what, 'critical');
}

##############################################################################
# Makes a scalar or array into an array (used in &info)
sub list {
	return (ref($_[0]) eq 'ARRAY') ? @{$_[0]} : @_;
}

##############################################################################
# Used to setup warning or critical levels for munin
sub info {
	my $what = shift;
	my $level = shift;	# 'warning' or 'critical'

	my $attr = $attrs{$what};
	return unless defined $attr->{$level};

	# Determine nominal value for info calculation
	my $nom = undef;
	if (defined $attr->{nominal}) {
		for my $n (&list($attr->{nominal})) {
			# Guess list: compare guesses to value of $num{$what}
			if ($n =~ /,/) {
				my $fitness = ~0;
				next unless $num{$what};
				foreach my $possibility (split /[,\s]+/, $n) {
					my $diff = abs($num{$what} - $possibility);
					($nom, $fitness) = ($possibility, $diff) if $fitness >= $diff;
				}

			# Absolute nominal value
			} elsif ($n =~ /^[\d.]+$/) {
				$nom = $n;
				last;

			# Lookup nominal value as an APCUPSD key
			} elsif (defined $num{$n}) {
				$nom = $num{$n};
				last;
			}
		}
	}

	# Calculate info value for $level
	foreach my $value (&list($attr->{$level})) {
		$value =~ s/([^:]+)/&calc($1,$nom)/eg;
		if ($value =~ /^[\d.:]+$/) {
			print "$attr->{name}.$level $value\n";
			return;
		}
	}
}

##############################################################################
# Change warning/critical ranges into numbers for munin
sub calc {
	my $v = shift;
	my $nom = shift;

	return $v if $v =~ /^[\d.]+$/;
	return $num{$v} if defined $num{$v};
	return '' unless defined $nom;
	if ($v =~ /^\+-([\d.]+)%$/) {
		return sprintf "%.0f:%.0f", (100 - $1) * $nom / 100, (100 + $1) * $nom / 100;
	} elsif ($v =~ /^([-+][\d.]+)%$/) {
		return sprintf "%.0f", (100 + $1) * $nom / 100;
	} elsif ($v =~ /^\+-([\d.]+)$/) {
		return sprintf "%d:%d", $nom - $1, $nom + $1;
	} elsif ($v =~ /^([-+][\d.]+)$/) {
		return $nom + $1;
	} elsif ($v =~ /^\*([\d.]+)$/) {
		return $nom * $1;
	} elsif ($v =~ /^\/([\d.]+)$/) {
		return sprintf "%.0f", $nom / $1;
	}
	return '';
}

##############################################################################
# Default "type" routine to display values
sub num {
	my $what = shift;
	return $num{$what};
}

##############################################################################
# "type" routine to change Ok/Not Ok into 1/0
sub bool {
	my $what = shift;
	return $num{$what} eq "OK" ? "1" : "0";
}

#sub status {
#	my $what = shift;
#	return unless defined $data{$what};
#	print "$attrs{$what}{name}.value ";
#	print $num{$what} eq "ONLINE" ? "1" : "0";
#	print "\n";
#}

# vim: sw=4 ts=4