21 |
|
|
22 |
sub proc2hash { |
sub proc2hash { |
23 |
my $f = shift; |
my $f = shift; |
24 |
|
return unless -f $f; |
25 |
open(my $fh, '<', $f) || die "can't open $f: $!"; |
open(my $fh, '<', $f) || die "can't open $f: $!"; |
26 |
my $h; |
my $h; |
27 |
while(<$fh>) { |
while(<$fh>) { |
30 |
$value =~ s/ m[VW]h*$//; |
$value =~ s/ m[VW]h*$//; |
31 |
$h->{$key} = $value; |
$h->{$key} = $value; |
32 |
} |
} |
33 |
warn dump( $h ) if ( $debug ); |
warn "$f ",dump( $h ) if ( $debug ); |
34 |
return $h; |
return $h; |
35 |
} |
} |
36 |
|
|
37 |
|
my $proc_acpi_battery; |
38 |
|
sub find_proc_acpi { |
39 |
|
my ( $path, $check ) = @_; |
40 |
|
|
41 |
|
if ( -e "/proc/acpi/$path" ) { |
42 |
|
opendir(my $items, "/proc/acpi/$path") || die "can't open /proc/acpi/$path: $!"; |
43 |
|
foreach my $item ( readdir( $items ) ) { |
44 |
|
$check->( $item ) && last; |
45 |
|
} |
46 |
|
} |
47 |
|
} |
48 |
|
|
49 |
|
my $proc_acpi_thermal_zone_temperature; |
50 |
|
find_proc_acpi('thermal_zone', sub { |
51 |
|
my $thm = shift; |
52 |
|
return if ( ! -e "/proc/acpi/thermal_zone/$thm/temperature" ); |
53 |
|
warn "# using thermal zone $thm\n" if ( $debug ); |
54 |
|
$proc_acpi_thermal_zone_temperature = "/proc/acpi/thermal_zone/$thm/temperature"; |
55 |
|
}); |
56 |
|
|
57 |
|
find_proc_acpi('battery', sub { |
58 |
|
my $bat = shift; |
59 |
|
return unless $bat =~ /BAT/; |
60 |
|
|
61 |
|
warn "# testing battery $bat\n" if ( $debug ); |
62 |
|
|
63 |
|
if ( proc2hash( "/proc/acpi/battery/$bat/info" )->{present} eq 'yes' ) { |
64 |
|
$proc_acpi_battery = "/proc/acpi/battery/$bat"; |
65 |
|
warn "using $proc_acpi_battery to monitor battery\n"; |
66 |
|
} |
67 |
|
}); |
68 |
|
|
69 |
sub unit { |
sub unit { |
70 |
my $v = shift; |
my $v = shift; |
71 |
|
|
97 |
while ( 1 ) { |
while ( 1 ) { |
98 |
my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); |
my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); |
99 |
|
|
100 |
if ( $i % $acpi_every == 0 ) { |
if ( $i % $acpi_every == 0 && $proc_acpi_battery ) { |
101 |
|
|
102 |
my $state = proc2hash( '/proc/acpi/battery/BAT0/state' ); |
my $state = proc2hash( "$proc_acpi_battery/state" ); |
103 |
|
|
104 |
if ( $state->{'present rate'} != 0 ) { |
if ( $state->{'present rate'} != 0 ) { |
105 |
my $info = proc2hash( '/proc/acpi/battery/BAT0/info' ); |
my $info = proc2hash( "$proc_acpi_battery/info" ); |
106 |
|
|
107 |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
108 |
|
|
109 |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
110 |
|
$time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); |
111 |
|
|
112 |
warn "time = $time\n" if ($debug); |
warn "time = $time\n" if ($debug); |
113 |
|
|
133 |
chomp( $load ); |
chomp( $load ); |
134 |
$load =~ s!\s\d+/\d+.*!!; |
$load =~ s!\s\d+/\d+.*!!; |
135 |
|
|
136 |
my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature'); |
my $temp = ''; |
137 |
chomp( $temp ); |
if ( $proc_acpi_thermal_zone_temperature ) { |
138 |
$temp =~ s!^.*:\s+!!; |
$temp = read_file( $proc_acpi_thermal_zone_temperature ); |
139 |
|
chomp( $temp ); |
140 |
|
$temp =~ s!^.*:\s+!!; |
141 |
|
} |
142 |
|
|
143 |
my $net = read_file('/proc/net/dev'); |
my $net = read_file('/proc/net/dev'); |
144 |
my ( $rx, $tx ) = ( 0,0 ); |
my ( $rx, $tx ) = ( 0,0 ); |
145 |
|
|
146 |
foreach ( split(/\n/, $net) ) { |
foreach ( split(/\n/, $net) ) { |
147 |
s/^\s+//; |
s/^\s+//; |
148 |
|
s/:\s+/:/; |
149 |
my @n = split(/\s+/, $_, 17); |
my @n = split(/\s+/, $_, 17); |
150 |
next unless ( $n[0] =~ s!(eth\d|ath\d):!! ); |
next unless ( $n[0] =~ s!(eth\d|ath\d):!! ); |
151 |
|
|