#!/usr/bin/perl -w
# -*- perl -*-

=head1 NAME

acpi_sys_batt_ Munin plugin to monitor the (note|net)book battery states through sysfs

=head1 APPLICABLE SYSTEMS

Notebooks and netbooks with avialable /sys/class/power_supply

=head1 CONFIGURATION

Configured change the name of symbolic link

acpi_sys_batt_X_capacity - chart of Charge full by design, Charge full and Charge now
acpi_sys_batt_X_current  - chart of battery current
acpi_sys_batt_X_percents - percentage chart of Current/design voltage, Full/current capacity, Design/full capacity
acpi_sys_batt_X_voltage  - chart of Design min voltage and Current voltage
Where X is the number of batteries from /sys/class/power_supply/BATX

=head1 INTERPRETATION

The plugin shows:
 Charge full by design
 Charge full
 Charge now
 Battery current
 Percentage Current/design voltage
 Percentage Full/current capacity
 Percentage Design/full capacity
 Design min voltage
 Current voltage

=head1 MAGIC MARKERS

#%# family=power

=head1 VERSION
=head1 BUGS

None known.

=head1 AUTHOR

Gorlow Maxim <sheridan@sheridan-home.ru>

=head1 LICENSE

GPLv2

=cut

use strict;
#use Data::Dumper;

my ($graph_type, $batt_num);
if ($0 =~ /^(?:|.*\/)acpi_sys_batt_([^_]+)_(.+)$/)
{
    $graph_type = $2;
    $batt_num = $1;
}
elsif (!defined($batt_num) or !defined($graph_type)) {
    die "# Error: couldn't understand what I'm supposed to monitor."; }

my $sys_path=sprintf("/sys/class/power_supply/BAT%s", $batt_num);

#print "$batt_num, $graph_type \n";

sub cat_file
{
	my $file = $_[0];
	my $fh;
	my $file_content = "--";
	open($fh, '<', "${sys_path}/${file}") or die $!;
	foreach my $line (<$fh>)
	{
		chomp ($line);
		$file_content = $line;
	}
	close($fh);
	return $file_content;
}

#print Dumper($batt_data);

if ($ARGV[0] and $ARGV[0] eq "config")
{

#manufacturer
#model_name
#serial_number
# ----------- status
#technology
#type


	my $batt_name = sprintf("%s %s %s %s (sn: %s)", cat_file('technology'), cat_file('type'), cat_file('manufacturer'), cat_file('model_name'), cat_file('serial_number'));
	print  ("graph_args --base 1000\n");
	printf ("graph_title Battery %s (%s) %s\n" , $batt_num, $batt_name, $graph_type);
	printf ("graph_info This graph shows battery %s (%s) %s\n" , $batt_num, $batt_name, $graph_type);
	print  ("graph_category sensors\n");
	if ($graph_type eq "capacity")
	{
		print  ("graph_vlabel Charge\n");
		print  ("cfd.label Charge full design\ncfd.type GAUGE\ncfd.draw AREA\n");
		print  ("cf.label Charge full\ncf.type GAUGE\ncf.draw AREA\n");
		print  ("cn.label Charge now\ncn.type GAUGE\ncn.draw AREA\n");
	}
	elsif ($graph_type eq "current")
	{
		print  ("graph_vlabel Current, mAh\n");
		print  ("currn.label Current\ncurrn.type GAUGE\ncurrn.draw AREA\n");
	}
	elsif ($graph_type eq "voltage")
	{
		print  ("graph_vlabel Voltage, mV\n");
		print  ("vmd.label Design min voltage\nvmd.type GAUGE\nvmd.draw AREA\n");
		print  ("vn.label Voltage now\nvn.type GAUGE\nvn.draw AREA\n");
	}
	elsif ($graph_type eq "percents")
	{
		print  ("graph_vlabel %\n");
		print  ("cv.label Current/design voltage\ncv.type GAUGE\ncv.draw LINE2\n");
		print  ("cc.label Full/current capacity\ncc.type GAUGE\ncc.draw LINE2\n");
		print  ("fc.label Design/full capacity\nfc.type GAUGE\nfc.draw LINE2\n");
	}
	exit 0;
}

#$batt_data->{'info'}{''}
sub percent
{
	my ($full, $current) = @_[0..1];
	return $current/($full/100);
}

#charge_full
#charge_full_design
#charge_now
#current_now
#voltage_min_design
#voltage_now

if ($graph_type eq "capacity")
{
	printf ("cfd.value %s\n", cat_file('charge_full_design'));
	printf ("cf.value %s\n", cat_file('charge_full'));
	printf ("cn.value %s\n", cat_file('charge_now'));
}
elsif ($graph_type eq "voltage")
{
	printf ("vmd.value %s\n", cat_file('voltage_min_design'));
	printf ("vn.value %s\n", cat_file('voltage_now'));
}
elsif ($graph_type eq "current")
{
	printf ("currn.value %s\n", cat_file('current_now'));
}
elsif ($graph_type eq "percents")
{
	printf ("cv.value %s\n", percent(cat_file('voltage_min_design'),cat_file('voltage_now')));
	printf ("cc.value %s\n", percent(cat_file('charge_full'),cat_file('charge_now')));
	printf ("fc.value %s\n", percent(cat_file('charge_full_design'),cat_file('charge_full')));
}


