-
Notifications
You must be signed in to change notification settings - Fork 683
/
batteries
executable file
·359 lines (319 loc) · 14.1 KB
/
batteries
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/perl -w
# -*- perl -*-
=head1 NAME
batteries Munin plugin to monitor the battery states through procfs and sysfs
=head1 APPLICABLE SYSTEMS
Systems with avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
=head1 CONFIGURATION
none
=head1 INTERPRETATION
The plugin shows:
Design capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
Last full capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
Design capacity low -> avialable only if avialable /proc/acpi/battery/BATx
Design capacity warning -> avialable only if avialable /proc/acpi/battery/BATx
Capacity granularity 1 -> avialable only if avialable /proc/acpi/battery/BATx
Capacity granularity 2 -> avialable only if avialable /proc/acpi/battery/BATx
Remaining capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
Present rate -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
Percentage Current/design voltage -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
Percentage Current/full capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
Percentage Full/design capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
Design voltage -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
Present voltage -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx
=head1 MAGIC MARKERS
#%# family=power
#%# capabilities=autoconf
=head1 VERSION
1.0
=head1 BUGS
None known.
=head1 AUTHOR
Gorlow Maxim aka Sheridan <sheridan@sheridan-home.ru> - email and jabber
=head1 LICENSE
GPLv2
=cut
use strict;
use warnings;
use IO::Dir;
use Munin::Plugin;
need_multigraph();
my $proc_path = '/proc/acpi/battery';
my $sys_path = '/sys/class/power_supply';
my $proc_data_exists;
my $sys_data_exists;
my $batteryes_count;
sub trim
{
my($string)=@_;
for ($string)
{
s/^\s+//;
s/\s+$//;
}
return $string;
}
sub get_batteryes_count
{
my $path = $_[0];
return 0 unless (-e $path);
my $count = 0;
my $dir = IO::Dir->new($path);
if(defined $dir)
{
my $d;
while (defined ($d = $dir->read))
{
next unless $d =~ m/BAT\d+/;
$count++;
}
}
else { return 0; }
return $count;
}
sub init
{
my $proc_batt_count = get_batteryes_count($proc_path);
my $sys_batt_count = get_batteryes_count($sys_path );
#print "$proc_batt_count $sys_batt_count\n";
$proc_data_exists = $proc_batt_count > 0;
$sys_data_exists = $sys_batt_count > 0;
if ($proc_data_exists and $sys_data_exists and ($proc_batt_count != $sys_batt_count))
{
die "Something wrong, batteryes count from $proc_path and $sys_path not equal (proc: $proc_batt_count, sys: $sys_batt_count)!"
}
if ($proc_data_exists) { $batteryes_count = $proc_batt_count; }
elsif ($sys_data_exists) { $batteryes_count = $sys_batt_count; }
unless ($batteryes_count)
{
die "Batteryes not found."
}
}
sub read_proc_data
{
my ($batt_num, $file) = @_[0..1];
my ($var, $val, $result);
open(FH, '<', "${proc_path}/BAT${batt_num}/${file}") or die $!;
foreach my $line (<FH>)
{
chomp ($line);
($var, $val) = split(':', $line);
if ($val =~ m/^\s*$/ ) { $val = "nan"; }
elsif ($val =~ m/\w\s+\w/) { $val = (split(" " ,$val))[0]; }
$result->{$var} = trim($val);
#print "var $var - val $val\n";
}
close(FH);
return $result;
}
sub read_sys_data
{
my ($batt_num, $file) = @_[0..1];
my $file_content = "nan";
open(FH, '<', "${sys_path}/BAT${batt_num}/${file}") or die $!;
$file_content = <FH>;
close(FH);
chomp ($file_content);
if($file_content =~ m/^\s*$/) { return 'nan'; }
return $file_content;
}
sub percent
{
my ($full, $current) = @_[0..1];
return $current/($full/100);
}
sub read_info
{
my $info;
for (my $i = 0; $i < $batteryes_count; $i++)
{
if($sys_data_exists)
{
$info->{$i}{'manufacturer'} = read_sys_data($i, 'manufacturer');
$info->{$i}{'battery_type'} = read_sys_data($i, 'technology' );
$info->{$i}{'model_name'} = read_sys_data($i, 'model_name' );
$info->{$i}{'serial_number'} = read_sys_data($i, 'serial_number');
}
else
{
my $proc_info = read_proc_data($i, 'info');
$info->{$i}{'manufacturer'} = $proc_info->{'OEM info' };
$info->{$i}{'battery_type'} = $proc_info->{'battery type' };
$info->{$i}{'model_name'} = $proc_info->{'model number' };
$info->{$i}{'serial_number'} = $proc_info->{'serial number'};
}
}
return $info;
}
sub read_data
{
my $data;
for (my $i = 0; $i < $batteryes_count; $i++)
{
if($sys_data_exists)
{
my $divider = 1000000; # need for equvivalent sys and proc data
$data->{$i}{'design_capacity'} = read_sys_data($i, 'charge_full_design')/$divider;
$data->{$i}{'last_full_capacity'} = read_sys_data($i, 'charge_full') /$divider;
$data->{$i}{'remaining_capacity'} = read_sys_data($i, 'charge_now') /$divider;
$data->{$i}{'design_voltage'} = read_sys_data($i, 'voltage_min_design')/$divider;
$data->{$i}{'present_voltage'} = read_sys_data($i, 'voltage_now') /$divider;
$data->{$i}{'present_rate'} = read_sys_data($i, 'current_now') /$divider;
}
if($proc_data_exists)
{
my $divider = 1000; # need for equvivalent sys and proc data
my $proc_info = read_proc_data($i, 'info');
unless($sys_data_exists)
{
my $proc_state = read_proc_data($i, 'state');
$data->{$i}{'design_capacity'} = $proc_info ->{'design capacity'} /$divider;
$data->{$i}{'last_full_capacity'} = $proc_info ->{'last full capacity'}/$divider;
$data->{$i}{'remaining_capacity'} = $proc_state->{'remaining capacity'}/$divider;
$data->{$i}{'design_voltage'} = $proc_info ->{'design voltage'} /$divider;
$data->{$i}{'present_voltage'} = $proc_state->{'present voltage'} /$divider;
$data->{$i}{'present_rate'} = $proc_state->{'present rate'} /$divider;
}
$data->{$i}{'design_capacity_low'} = $proc_info ->{'design capacity low'} /$divider;
$data->{$i}{'design_capacity_warning'} = $proc_info ->{'design capacity warning'}/$divider;
$data->{$i}{'capacity_granularity_1'} = $proc_info ->{'capacity granularity 1'} /$divider;
$data->{$i}{'capacity_granularity_2'} = $proc_info ->{'capacity granularity 2'} /$divider;
}
$data->{$i}{'current_voltage_percent'} = percent($data->{$i}{'design_voltage'} , $data->{$i}{'present_voltage'});
$data->{$i}{'current_capacity_percent'} = percent($data->{$i}{'last_full_capacity'}, $data->{$i}{'remaining_capacity'});
$data->{$i}{'full_capacity_percent'} = percent($data->{$i}{'design_capacity'} , $data->{$i}{'last_full_capacity'});
}
return $data;
}
my $graphs =
{
'batteryes_capacity' => { 'vlabel' => 'Capacity, Ah', 'title' => '%s capacity', 'args' => '--base 1000',
'fields' => [qw/design_capacity last_full_capacity design_capacity_low design_capacity_warning capacity_granularity_1 capacity_granularity_2 remaining_capacity/] },
'batteryes_voltage' => { 'vlabel' => 'Voltage, V' , 'title' => '%s voltage' , 'args' => '--base 1000',
'fields' => [qw/design_voltage present_voltage/] },
'batteryes_percents' => { 'vlabel' => '%' , 'title' => '%s percents', 'args' => '--base 1000 --upper-limit 100 -l 0',
'fields' => [qw/current_voltage_percent current_capacity_percent full_capacity_percent/] },
'batteryes_current' => { 'vlabel' => 'Current, A' , 'title' => '%s current' , 'args' => '--base 1000',
'fields' => [qw/present_rate/] }
};
my $fields =
{
'design_capacity' => { 'source' => 'both', 'draw' => 'AREA' , 'label' => 'Design capacity' , 'info' => 'Battery design capacity' },
'last_full_capacity' => { 'source' => 'both', 'draw' => 'AREA' , 'label' => 'Last full capacity' , 'info' => 'Battery full charge capacity' },
'design_capacity_low' => { 'source' => 'proc', 'draw' => 'LINE2', 'label' => 'Design capacity low' , 'info' => 'Low battery level' },
'design_capacity_warning' => { 'source' => 'proc', 'draw' => 'LINE2', 'label' => 'Design capacity warning', 'info' => 'Warning battery level' },
'capacity_granularity_1' => { 'source' => 'proc', 'draw' => 'LINE2', 'label' => 'Capacity granularity 1' , 'info' => 'Capacity granularity 1' },
'capacity_granularity_2' => { 'source' => 'proc', 'draw' => 'LINE2', 'label' => 'Capacity granularity 2' , 'info' => 'Capacity granularity 2' },
'remaining_capacity' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Remaining capacity' , 'info' => 'Current battery charge' },
'present_rate' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Present rate' , 'info' => 'Current battery rate' },
'design_voltage' => { 'source' => 'both', 'draw' => 'AREA' , 'label' => 'Design voltage' , 'info' => 'Battery design voltage' },
'present_voltage' => { 'source' => 'both', 'draw' => 'AREA' , 'label' => 'Present voltage' , 'info' => 'Current battery voltage' },
'current_voltage_percent' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Current/design voltage' , 'info' => 'Current battery voltage / ( Battery design voltage / 100 )' },
'current_capacity_percent' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Current/full capacity' , 'info' => 'Current battery charge / ( Battery full charge capacity / 100 )' },
'full_capacity_percent' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Full/design capacity' , 'info' => 'Battery full charge capacity / ( Battery design capacity / 100 )' },
};
# ------------------------------------ start here -----------------------------------
if (defined($ARGV[0]) and ($ARGV[0] eq 'autoconf'))
{
printf("%s\n", (-e $proc_path or -e $sys_path) ? "yes" : "no ($proc_path and $sys_path not exists)");
exit (0);
}
init();
if ($ARGV[0] and $ARGV[0] eq "config")
{
my %config;
my $info = read_info();
foreach my $graph (keys %{$graphs})
{
my @order;
$config{$graph}{'graph'}{'title'} = sprintf($graphs->{$graph}{'title'}, 'Mean batteryes');
$config{$graph}{'graph'}{'args'} = $graphs->{$graph}{'args'};
$config{$graph}{'graph'}{'vlabel'} = $graphs->{$graph}{'vlabel'};
$config{$graph}{'graph'}{'category'} = 'power';
foreach my $field (@{$graphs->{$graph}{'fields'}})
{
if(($proc_data_exists and $fields->{$field}{'source'} eq 'proc') or $fields->{$field}{'source'} eq 'both')
{
$config{$graph}{'fields'}{$field}{'label'} = $fields->{$field}{'label'};
$config{$graph}{'fields'}{$field}{'info'} = $fields->{$field}{'info'};
$config{$graph}{'fields'}{$field}{'draw'} = $fields->{$field}{'draw'};
$config{$graph}{'fields'}{$field}{'type'} = 'GAUGE';
push(@order, $field);
}
}
$config{$graph}{'graph'}{'order'} = join(' ', @order);
for (my $i = 0; $i < $batteryes_count; $i++)
{
my @b_order;
my $battery_name = sprintf("BAT%s", $i);
my $graph_name = sprintf("%s.%s", $graph, $battery_name);
$config{$graph_name}{'graph'}{'title'} = sprintf($graphs->{$graph}{'title'}, $battery_name);
$config{$graph_name}{'graph'}{'info'} = sprintf("%s battery %s %s (sn: %s)", $info->{$i}{'battery_type'}, $info->{$i}{'manufacturer'}, $info->{$i}{'model_name'}, $info->{$i}{'serial_number'});
$config{$graph_name}{'graph'}{'args'} = '--base 1000';
$config{$graph_name}{'graph'}{'vlabel'} = $graphs->{$graph}{'vlabel'};
$config{$graph_name}{'graph'}{'category'} = 'power';
foreach my $field (@{$graphs->{$graph}{'fields'}})
{
if(($proc_data_exists and $fields->{$field}{'source'} eq 'proc') or $fields->{$field}{'source'} eq 'both')
{
$config{$graph_name}{'fields'}{$field}{'label'} = $fields->{$field}{'label'};
$config{$graph_name}{'fields'}{$field}{'info'} = $fields->{$field}{'info'};
$config{$graph_name}{'fields'}{$field}{'draw'} = $fields->{$field}{'draw'};
$config{$graph_name}{'fields'}{$field}{'type'} = 'GAUGE';
push(@b_order, $field);
}
}
$config{$graph_name}{'graph'}{'order'} = join(' ', @b_order);
}
}
# ---------------- print ------------------
foreach my $graph (sort keys %config)
{
printf("multigraph %s\n", $graph);
foreach my $g_option(sort keys %{$config{$graph}{'graph'}})
{
printf("graph_%s %s\n", $g_option, $config{$graph}{'graph'}{$g_option});
}
foreach my $field (sort keys %{$config{$graph}{'fields'}})
{
foreach my $f_option (sort keys %{$config{$graph}{'fields'}{$field}})
{
printf("%s.%s %s\n", $field, $f_option, $config{$graph}{'fields'}{$field}{$f_option});
}
}
print "\n";
}
exit (0);
}
# ----------------------------- values ---------------------------------------------
my $data = read_data();
foreach my $graph (sort keys %{$graphs})
{
printf ("multigraph %s\n", $graph);
foreach my $field (sort @{$graphs->{$graph}{'fields'}})
{
my $field_summ = 0;
if(($proc_data_exists and $fields->{$field}{'source'} eq 'proc') or $fields->{$field}{'source'} eq 'both')
{
for (my $i = 0; $i < $batteryes_count; $i++)
{
$field_summ += $data->{$i}{$field};
}
printf("%s.value %s\n", $field, $field_summ/$batteryes_count);
}
}
print "\n";
for (my $i = 0; $i < $batteryes_count; $i++)
{
my $graph_name = sprintf("%s.BAT%s", $graph, $i);
printf ("multigraph %s\n", $graph_name);
foreach my $field (sort @{$graphs->{$graph}{'fields'}})
{
if(($proc_data_exists and $fields->{$field}{'source'} eq 'proc') or $fields->{$field}{'source'} eq 'both')
{
printf("%s.value %s\n", $field, $data->{$i}{$field});
}
}
print "\n";
}
}