root / plugins / sensors / power / batteries @ e5ce7492
Historique | Voir | Annoter | Télécharger (14,1 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
# -*- perl -*- |
| 3 |
|
| 4 |
=head1 NAME |
| 5 |
|
| 6 |
batteries Munin plugin to monitor the battery states through procfs and sysfs |
| 7 |
|
| 8 |
=head1 APPLICABLE SYSTEMS |
| 9 |
|
| 10 |
Systems with avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 11 |
|
| 12 |
=head1 CONFIGURATION |
| 13 |
|
| 14 |
none |
| 15 |
|
| 16 |
=head1 INTERPRETATION |
| 17 |
|
| 18 |
The plugin shows: |
| 19 |
Design capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 20 |
Last full capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 21 |
Design capacity low -> avialable only if avialable /proc/acpi/battery/BATx |
| 22 |
Design capacity warning -> avialable only if avialable /proc/acpi/battery/BATx |
| 23 |
Capacity granularity 1 -> avialable only if avialable /proc/acpi/battery/BATx |
| 24 |
Capacity granularity 2 -> avialable only if avialable /proc/acpi/battery/BATx |
| 25 |
Remaining capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 26 |
Present rate -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 27 |
Percentage Current/design voltage -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 28 |
Percentage Current/full capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 29 |
Percentage Full/design capacity -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 30 |
Design voltage -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 31 |
Present voltage -> avialable if avialable /proc/acpi/battery/BATx or /sys/class/power_supply/BATx |
| 32 |
|
| 33 |
=head1 MAGIC MARKERS |
| 34 |
|
| 35 |
#%# family=power |
| 36 |
#%# capabilities=autoconf |
| 37 |
|
| 38 |
=head1 VERSION |
| 39 |
|
| 40 |
1.0 |
| 41 |
|
| 42 |
=head1 BUGS |
| 43 |
|
| 44 |
None known. |
| 45 |
|
| 46 |
=head1 AUTHOR |
| 47 |
|
| 48 |
Gorlow Maxim aka Sheridan <sheridan@sheridan-home.ru> - email and jabber |
| 49 |
|
| 50 |
=head1 LICENSE |
| 51 |
|
| 52 |
GPLv2 |
| 53 |
|
| 54 |
=cut |
| 55 |
|
| 56 |
|
| 57 |
use strict; |
| 58 |
use warnings; |
| 59 |
use IO::Dir; |
| 60 |
use Munin::Plugin; |
| 61 |
|
| 62 |
need_multigraph(); |
| 63 |
|
| 64 |
my $proc_path = '/proc/acpi/battery'; |
| 65 |
my $sys_path = '/sys/class/power_supply'; |
| 66 |
|
| 67 |
my $proc_data_exists; |
| 68 |
my $sys_data_exists; |
| 69 |
my $batteryes_count; |
| 70 |
|
| 71 |
sub trim |
| 72 |
{
|
| 73 |
my($string)=@_; |
| 74 |
for ($string) |
| 75 |
{
|
| 76 |
s/^\s+//; |
| 77 |
s/\s+$//; |
| 78 |
} |
| 79 |
return $string; |
| 80 |
} |
| 81 |
|
| 82 |
sub get_batteryes_count |
| 83 |
{
|
| 84 |
my $path = $_[0]; |
| 85 |
return 0 unless (-e $path); |
| 86 |
my $count = 0; |
| 87 |
my $dir = IO::Dir->new($path); |
| 88 |
if(defined $dir) |
| 89 |
{
|
| 90 |
my $d; |
| 91 |
while (defined ($d = $dir->read)) |
| 92 |
{
|
| 93 |
next unless $d =~ m/BAT\d+/; |
| 94 |
$count++; |
| 95 |
} |
| 96 |
} |
| 97 |
else { return 0; }
|
| 98 |
return $count; |
| 99 |
} |
| 100 |
|
| 101 |
sub init |
| 102 |
{
|
| 103 |
my $proc_batt_count = get_batteryes_count($proc_path); |
| 104 |
my $sys_batt_count = get_batteryes_count($sys_path ); |
| 105 |
#print "$proc_batt_count $sys_batt_count\n"; |
| 106 |
$proc_data_exists = $proc_batt_count > 0; |
| 107 |
$sys_data_exists = $sys_batt_count > 0; |
| 108 |
if ($proc_data_exists and $sys_data_exists and ($proc_batt_count != $sys_batt_count)) |
| 109 |
{
|
| 110 |
die "Something wrong, batteryes count from $proc_path and $sys_path not equal (proc: $proc_batt_count, sys: $sys_batt_count)!" |
| 111 |
} |
| 112 |
if ($proc_data_exists) { $batteryes_count = $proc_batt_count; }
|
| 113 |
elsif ($sys_data_exists) { $batteryes_count = $sys_batt_count; }
|
| 114 |
unless ($batteryes_count) |
| 115 |
{
|
| 116 |
die "Batteryes not found." |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
sub read_proc_data |
| 121 |
{
|
| 122 |
my ($batt_num, $file) = @_[0..1]; |
| 123 |
my ($var, $val, $result); |
| 124 |
open(FH, '<', "${proc_path}/BAT${batt_num}/${file}") or die $!;
|
| 125 |
foreach my $line (<FH>) |
| 126 |
{
|
| 127 |
chomp ($line); |
| 128 |
($var, $val) = split(':', $line);
|
| 129 |
if ($val =~ m/^\s*$/ ) { $val = "nan"; }
|
| 130 |
elsif ($val =~ m/\w\s+\w/) { $val = (split(" " ,$val))[0]; }
|
| 131 |
$result->{$var} = trim($val);
|
| 132 |
#print "var $var - val $val\n"; |
| 133 |
} |
| 134 |
close(FH); |
| 135 |
return $result; |
| 136 |
} |
| 137 |
|
| 138 |
sub read_sys_data |
| 139 |
{
|
| 140 |
my ($batt_num, $file) = @_[0..1]; |
| 141 |
my $file_content = "nan"; |
| 142 |
open(FH, '<', "${sys_path}/BAT${batt_num}/${file}") or die $!;
|
| 143 |
$file_content = <FH>; |
| 144 |
close(FH); |
| 145 |
chomp ($file_content); |
| 146 |
if($file_content =~ m/^\s*$/) { return 'nan'; }
|
| 147 |
return $file_content; |
| 148 |
} |
| 149 |
|
| 150 |
sub percent |
| 151 |
{
|
| 152 |
my ($full, $current) = @_[0..1]; |
| 153 |
return $current/($full/100); |
| 154 |
} |
| 155 |
|
| 156 |
sub read_info |
| 157 |
{
|
| 158 |
my $info; |
| 159 |
for (my $i = 0; $i < $batteryes_count; $i++) |
| 160 |
{
|
| 161 |
if($sys_data_exists) |
| 162 |
{
|
| 163 |
$info->{$i}{'manufacturer'} = read_sys_data($i, 'manufacturer');
|
| 164 |
$info->{$i}{'battery_type'} = read_sys_data($i, 'technology' );
|
| 165 |
$info->{$i}{'model_name'} = read_sys_data($i, 'model_name' );
|
| 166 |
$info->{$i}{'serial_number'} = read_sys_data($i, 'serial_number');
|
| 167 |
} |
| 168 |
else |
| 169 |
{
|
| 170 |
my $proc_info = read_proc_data($i, 'info'); |
| 171 |
$info->{$i}{'manufacturer'} = $proc_info->{'OEM info' };
|
| 172 |
$info->{$i}{'battery_type'} = $proc_info->{'battery type' };
|
| 173 |
$info->{$i}{'model_name'} = $proc_info->{'model number' };
|
| 174 |
$info->{$i}{'serial_number'} = $proc_info->{'serial number'};
|
| 175 |
} |
| 176 |
} |
| 177 |
return $info; |
| 178 |
} |
| 179 |
|
| 180 |
sub read_data |
| 181 |
{
|
| 182 |
my $data; |
| 183 |
for (my $i = 0; $i < $batteryes_count; $i++) |
| 184 |
{
|
| 185 |
if($sys_data_exists) |
| 186 |
{
|
| 187 |
my $divider = 1000000; # need for equvivalent sys and proc data |
| 188 |
$data->{$i}{'design_capacity'} = read_sys_data($i, 'charge_full_design')/$divider;
|
| 189 |
$data->{$i}{'last_full_capacity'} = read_sys_data($i, 'charge_full') /$divider;
|
| 190 |
$data->{$i}{'remaining_capacity'} = read_sys_data($i, 'charge_now') /$divider;
|
| 191 |
$data->{$i}{'design_voltage'} = read_sys_data($i, 'voltage_min_design')/$divider;
|
| 192 |
$data->{$i}{'present_voltage'} = read_sys_data($i, 'voltage_now') /$divider;
|
| 193 |
$data->{$i}{'present_rate'} = read_sys_data($i, 'current_now') /$divider;
|
| 194 |
} |
| 195 |
if($proc_data_exists) |
| 196 |
{
|
| 197 |
my $divider = 1000; # need for equvivalent sys and proc data |
| 198 |
my $proc_info = read_proc_data($i, 'info'); |
| 199 |
unless($sys_data_exists) |
| 200 |
{
|
| 201 |
my $proc_state = read_proc_data($i, 'state'); |
| 202 |
$data->{$i}{'design_capacity'} = $proc_info ->{'design capacity'} /$divider;
|
| 203 |
$data->{$i}{'last_full_capacity'} = $proc_info ->{'last full capacity'}/$divider;
|
| 204 |
$data->{$i}{'remaining_capacity'} = $proc_state->{'remaining capacity'}/$divider;
|
| 205 |
$data->{$i}{'design_voltage'} = $proc_info ->{'design voltage'} /$divider;
|
| 206 |
$data->{$i}{'present_voltage'} = $proc_state->{'present voltage'} /$divider;
|
| 207 |
$data->{$i}{'present_rate'} = $proc_state->{'present rate'} /$divider;
|
| 208 |
} |
| 209 |
$data->{$i}{'design_capacity_low'} = $proc_info ->{'design capacity low'} /$divider;
|
| 210 |
$data->{$i}{'design_capacity_warning'} = $proc_info ->{'design capacity warning'}/$divider;
|
| 211 |
$data->{$i}{'capacity_granularity_1'} = $proc_info ->{'capacity granularity 1'} /$divider;
|
| 212 |
$data->{$i}{'capacity_granularity_2'} = $proc_info ->{'capacity granularity 2'} /$divider;
|
| 213 |
} |
| 214 |
$data->{$i}{'current_voltage_percent'} = percent($data->{$i}{'design_voltage'} , $data->{$i}{'present_voltage'});
|
| 215 |
$data->{$i}{'current_capacity_percent'} = percent($data->{$i}{'last_full_capacity'}, $data->{$i}{'remaining_capacity'});
|
| 216 |
$data->{$i}{'full_capacity_percent'} = percent($data->{$i}{'design_capacity'} , $data->{$i}{'last_full_capacity'});
|
| 217 |
} |
| 218 |
return $data; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
my $graphs = |
| 223 |
{
|
| 224 |
'batteryes_capacity' => { 'vlabel' => 'Capacity, Ah', 'title' => '%s capacity', 'args' => '--base 1000',
|
| 225 |
'fields' => [qw/design_capacity last_full_capacity design_capacity_low design_capacity_warning capacity_granularity_1 capacity_granularity_2 remaining_capacity/] }, |
| 226 |
'batteryes_voltage' => { 'vlabel' => 'Voltage, V' , 'title' => '%s voltage' , 'args' => '--base 1000',
|
| 227 |
'fields' => [qw/design_voltage present_voltage/] }, |
| 228 |
'batteryes_percents' => { 'vlabel' => '%' , 'title' => '%s percents', 'args' => '--base 1000 --upper-limit 100 -l 0',
|
| 229 |
'fields' => [qw/current_voltage_percent current_capacity_percent full_capacity_percent/] }, |
| 230 |
'batteryes_current' => { 'vlabel' => 'Current, A' , 'title' => '%s current' , 'args' => '--base 1000',
|
| 231 |
'fields' => [qw/present_rate/] } |
| 232 |
}; |
| 233 |
|
| 234 |
my $fields = |
| 235 |
{
|
| 236 |
'design_capacity' => { 'source' => 'both', 'draw' => 'AREA' , 'label' => 'Design capacity' , 'info' => 'Battery design capacity' },
|
| 237 |
'last_full_capacity' => { 'source' => 'both', 'draw' => 'AREA' , 'label' => 'Last full capacity' , 'info' => 'Battery full charge capacity' },
|
| 238 |
'design_capacity_low' => { 'source' => 'proc', 'draw' => 'LINE2', 'label' => 'Design capacity low' , 'info' => 'Low battery level' },
|
| 239 |
'design_capacity_warning' => { 'source' => 'proc', 'draw' => 'LINE2', 'label' => 'Design capacity warning', 'info' => 'Warning battery level' },
|
| 240 |
'capacity_granularity_1' => { 'source' => 'proc', 'draw' => 'LINE2', 'label' => 'Capacity granularity 1' , 'info' => 'Capacity granularity 1' },
|
| 241 |
'capacity_granularity_2' => { 'source' => 'proc', 'draw' => 'LINE2', 'label' => 'Capacity granularity 2' , 'info' => 'Capacity granularity 2' },
|
| 242 |
'remaining_capacity' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Remaining capacity' , 'info' => 'Current battery charge' },
|
| 243 |
'present_rate' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Present rate' , 'info' => 'Current battery rate' },
|
| 244 |
'design_voltage' => { 'source' => 'both', 'draw' => 'AREA' , 'label' => 'Design voltage' , 'info' => 'Battery design voltage' },
|
| 245 |
'present_voltage' => { 'source' => 'both', 'draw' => 'AREA' , 'label' => 'Present voltage' , 'info' => 'Current battery voltage' },
|
| 246 |
'current_voltage_percent' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Current/design voltage' , 'info' => 'Current battery voltage / ( Battery design voltage / 100 )' },
|
| 247 |
'current_capacity_percent' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Current/full capacity' , 'info' => 'Current battery charge / ( Battery full charge capacity / 100 )' },
|
| 248 |
'full_capacity_percent' => { 'source' => 'both', 'draw' => 'LINE2', 'label' => 'Full/design capacity' , 'info' => 'Battery full charge capacity / ( Battery design capacity / 100 )' },
|
| 249 |
}; |
| 250 |
|
| 251 |
# ------------------------------------ start here ----------------------------------- |
| 252 |
|
| 253 |
if (defined($ARGV[0]) and ($ARGV[0] eq 'autoconf')) |
| 254 |
{
|
| 255 |
printf("%s\n", (-e $proc_path or -e $sys_path) ? "yes" : "no ($proc_path and $sys_path not exists)");
|
| 256 |
exit (0); |
| 257 |
} |
| 258 |
|
| 259 |
init(); |
| 260 |
|
| 261 |
if ($ARGV[0] and $ARGV[0] eq "config") |
| 262 |
{
|
| 263 |
my %config; |
| 264 |
my $info = read_info(); |
| 265 |
foreach my $graph (keys %{$graphs})
|
| 266 |
{
|
| 267 |
my @order; |
| 268 |
$config{$graph}{'graph'}{'title'} = sprintf($graphs->{$graph}{'title'}, 'Mean batteryes');
|
| 269 |
$config{$graph}{'graph'}{'args'} = $graphs->{$graph}{'args'};
|
| 270 |
$config{$graph}{'graph'}{'vlabel'} = $graphs->{$graph}{'vlabel'};
|
| 271 |
$config{$graph}{'graph'}{'category'} = 'power';
|
| 272 |
foreach my $field (@{$graphs->{$graph}{'fields'}})
|
| 273 |
{
|
| 274 |
if(($proc_data_exists and $fields->{$field}{'source'} eq 'proc') or $fields->{$field}{'source'} eq 'both')
|
| 275 |
{
|
| 276 |
$config{$graph}{'fields'}{$field}{'label'} = $fields->{$field}{'label'};
|
| 277 |
$config{$graph}{'fields'}{$field}{'info'} = $fields->{$field}{'info'};
|
| 278 |
$config{$graph}{'fields'}{$field}{'draw'} = $fields->{$field}{'draw'};
|
| 279 |
$config{$graph}{'fields'}{$field}{'type'} = 'GAUGE';
|
| 280 |
push(@order, $field); |
| 281 |
} |
| 282 |
} |
| 283 |
$config{$graph}{'graph'}{'order'} = join(' ', @order);
|
| 284 |
for (my $i = 0; $i < $batteryes_count; $i++) |
| 285 |
{
|
| 286 |
my @b_order; |
| 287 |
my $battery_name = sprintf("BAT%s", $i);
|
| 288 |
my $graph_name = sprintf("%s.%s", $graph, $battery_name);
|
| 289 |
$config{$graph_name}{'graph'}{'title'} = sprintf($graphs->{$graph}{'title'}, $battery_name);
|
| 290 |
$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'});
|
| 291 |
$config{$graph_name}{'graph'}{'args'} = '--base 1000';
|
| 292 |
$config{$graph_name}{'graph'}{'vlabel'} = $graphs->{$graph}{'vlabel'};
|
| 293 |
$config{$graph_name}{'graph'}{'category'} = 'power';
|
| 294 |
foreach my $field (@{$graphs->{$graph}{'fields'}})
|
| 295 |
{
|
| 296 |
if(($proc_data_exists and $fields->{$field}{'source'} eq 'proc') or $fields->{$field}{'source'} eq 'both')
|
| 297 |
{
|
| 298 |
$config{$graph_name}{'fields'}{$field}{'label'} = $fields->{$field}{'label'};
|
| 299 |
$config{$graph_name}{'fields'}{$field}{'info'} = $fields->{$field}{'info'};
|
| 300 |
$config{$graph_name}{'fields'}{$field}{'draw'} = $fields->{$field}{'draw'};
|
| 301 |
$config{$graph_name}{'fields'}{$field}{'type'} = 'GAUGE';
|
| 302 |
push(@b_order, $field); |
| 303 |
} |
| 304 |
} |
| 305 |
$config{$graph_name}{'graph'}{'order'} = join(' ', @b_order);
|
| 306 |
} |
| 307 |
} |
| 308 |
# ---------------- print ------------------ |
| 309 |
foreach my $graph (sort keys %config) |
| 310 |
{
|
| 311 |
printf("multigraph %s\n", $graph);
|
| 312 |
foreach my $g_option(sort keys %{$config{$graph}{'graph'}})
|
| 313 |
{
|
| 314 |
printf("graph_%s %s\n", $g_option, $config{$graph}{'graph'}{$g_option});
|
| 315 |
} |
| 316 |
foreach my $field (sort keys %{$config{$graph}{'fields'}})
|
| 317 |
{
|
| 318 |
foreach my $f_option (sort keys %{$config{$graph}{'fields'}{$field}})
|
| 319 |
{
|
| 320 |
printf("%s.%s %s\n", $field, $f_option, $config{$graph}{'fields'}{$field}{$f_option});
|
| 321 |
} |
| 322 |
} |
| 323 |
print "\n"; |
| 324 |
} |
| 325 |
exit (0); |
| 326 |
} |
| 327 |
|
| 328 |
# ----------------------------- values --------------------------------------------- |
| 329 |
my $data = read_data(); |
| 330 |
foreach my $graph (sort keys %{$graphs})
|
| 331 |
{
|
| 332 |
printf ("multigraph %s\n", $graph);
|
| 333 |
foreach my $field (sort @{$graphs->{$graph}{'fields'}})
|
| 334 |
{
|
| 335 |
my $field_summ = 0; |
| 336 |
if(($proc_data_exists and $fields->{$field}{'source'} eq 'proc') or $fields->{$field}{'source'} eq 'both')
|
| 337 |
{
|
| 338 |
for (my $i = 0; $i < $batteryes_count; $i++) |
| 339 |
{
|
| 340 |
$field_summ += $data->{$i}{$field};
|
| 341 |
} |
| 342 |
printf("%s.value %s\n", $field, $field_summ/$batteryes_count);
|
| 343 |
} |
| 344 |
} |
| 345 |
print "\n"; |
| 346 |
for (my $i = 0; $i < $batteryes_count; $i++) |
| 347 |
{
|
| 348 |
my $graph_name = sprintf("%s.BAT%s", $graph, $i);
|
| 349 |
printf ("multigraph %s\n", $graph_name);
|
| 350 |
foreach my $field (sort @{$graphs->{$graph}{'fields'}})
|
| 351 |
{
|
| 352 |
if(($proc_data_exists and $fields->{$field}{'source'} eq 'proc') or $fields->{$field}{'source'} eq 'both')
|
| 353 |
{
|
| 354 |
printf("%s.value %s\n", $field, $data->{$i}{$field});
|
| 355 |
} |
| 356 |
} |
| 357 |
print "\n"; |
| 358 |
} |
| 359 |
} |
