8 |
use POSIX qw/strftime/; |
use POSIX qw/strftime/; |
9 |
use File::Slurp; |
use File::Slurp; |
10 |
use Time::HiRes; |
use Time::HiRes; |
11 |
use Data::Dump qw/dump/; |
eval "use Data::Dump qw/dump/;"; |
12 |
|
|
13 |
my $dt = 3; |
my $dt = 3; |
14 |
my $acpi_every = 10; |
my $acpi_every = 10; |
17 |
|
|
18 |
my $debug = shift @ARGV; |
my $debug = shift @ARGV; |
19 |
|
|
20 |
|
my $awesome = 0; |
21 |
|
|
22 |
$|=1; |
$|=1; |
23 |
|
|
24 |
sub proc2hash { |
sub proc2hash { |
25 |
my $f = shift; |
my $f = shift; |
26 |
|
return unless -f $f; |
27 |
open(my $fh, '<', $f) || die "can't open $f: $!"; |
open(my $fh, '<', $f) || die "can't open $f: $!"; |
28 |
my $h; |
my $h; |
29 |
while(<$fh>) { |
while(<$fh>) { |
32 |
$value =~ s/ m[VW]h*$//; |
$value =~ s/ m[VW]h*$//; |
33 |
$h->{$key} = $value; |
$h->{$key} = $value; |
34 |
} |
} |
35 |
warn dump( $h ) if ( $debug ); |
warn "$f ",dump( $h ) if ( $debug ); |
36 |
return $h; |
return $h; |
37 |
} |
} |
38 |
|
|
39 |
|
my $proc_acpi_battery; |
40 |
|
sub find_proc_acpi { |
41 |
|
my ( $path, $check ) = @_; |
42 |
|
|
43 |
|
if ( -e "/proc/acpi/$path" ) { |
44 |
|
opendir(my $items, "/proc/acpi/$path") || die "can't open /proc/acpi/$path: $!"; |
45 |
|
foreach my $item ( readdir( $items ) ) { |
46 |
|
$check->( $item ) && last; |
47 |
|
} |
48 |
|
} |
49 |
|
} |
50 |
|
|
51 |
|
my $proc_acpi_thermal_zone_temperature; |
52 |
|
find_proc_acpi('thermal_zone', sub { |
53 |
|
my $thm = shift; |
54 |
|
return if ( ! -e "/proc/acpi/thermal_zone/$thm/temperature" ); |
55 |
|
warn "# using thermal zone $thm\n" if ( $debug ); |
56 |
|
$proc_acpi_thermal_zone_temperature = "/proc/acpi/thermal_zone/$thm/temperature"; |
57 |
|
}); |
58 |
|
|
59 |
|
find_proc_acpi('battery', sub { |
60 |
|
my $bat = shift; |
61 |
|
return unless $bat =~ /\w+/; |
62 |
|
|
63 |
|
warn "# testing battery $bat\n" if ( $debug ); |
64 |
|
|
65 |
|
if ( proc2hash( "/proc/acpi/battery/$bat/info" )->{present} eq 'yes' ) { |
66 |
|
$proc_acpi_battery = "/proc/acpi/battery/$bat"; |
67 |
|
warn "using $proc_acpi_battery to monitor battery\n"; |
68 |
|
} |
69 |
|
}); |
70 |
|
|
71 |
sub unit { |
sub unit { |
72 |
my $v = shift; |
my $v = shift; |
73 |
|
|
76 |
my @units = qw/b k M G/; |
my @units = qw/b k M G/; |
77 |
my $o = 0; |
my $o = 0; |
78 |
|
|
79 |
while ( ( $v / 1024 ) >= 1 ) { |
while ( ( $v / 10000 ) >= 1 ) { |
80 |
$o++; |
$o++; |
81 |
$v /= 1024; |
$v /= 1024; |
82 |
} |
} |
96 |
|
|
97 |
my $i = 0; |
my $i = 0; |
98 |
|
|
99 |
|
if ( $awesome ) { |
100 |
|
undef $awesome; |
101 |
|
open( $awesome, '|-', 'awesome-client' ) || die "can't open awesome-client: $!"; |
102 |
|
} |
103 |
|
|
104 |
while ( 1 ) { |
while ( 1 ) { |
105 |
my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); |
my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); |
106 |
|
|
107 |
if ( $i % $acpi_every == 0 ) { |
if ( $i % $acpi_every == 0 && $proc_acpi_battery ) { |
108 |
|
|
109 |
my $state = proc2hash( '/proc/acpi/battery/BAT0/state' ); |
my $state = proc2hash( "$proc_acpi_battery/state" ); |
110 |
|
|
111 |
if ( $state->{'present rate'} != 0 ) { |
if ( $state->{'present rate'} != 0 ) { |
112 |
my $info = proc2hash( '/proc/acpi/battery/BAT0/info' ); |
my $info = proc2hash( "$proc_acpi_battery/info" ); |
113 |
|
|
114 |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
115 |
|
|
116 |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
117 |
|
$time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); |
118 |
|
|
119 |
warn "time = $time\n" if ($debug); |
warn "time = $time\n" if ($debug); |
120 |
|
|
140 |
chomp( $load ); |
chomp( $load ); |
141 |
$load =~ s!\s\d+/\d+.*!!; |
$load =~ s!\s\d+/\d+.*!!; |
142 |
|
|
143 |
my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature'); |
my $temp = ''; |
144 |
chomp( $temp ); |
if ( $proc_acpi_thermal_zone_temperature ) { |
145 |
$temp =~ s!^.*:\s+!!; |
$temp = read_file( $proc_acpi_thermal_zone_temperature ); |
146 |
|
chomp( $temp ); |
147 |
|
$temp =~ s!^.*:\s+!!; |
148 |
|
} |
149 |
|
|
150 |
my $net = read_file('/proc/net/dev'); |
my $net = read_file('/proc/net/dev'); |
151 |
my ( $rx, $tx ) = ( 0,0 ); |
my ( $rx, $tx ) = ( 0,0 ); |
152 |
|
|
153 |
foreach ( split(/\n/, $net) ) { |
foreach ( split(/\n/, $net) ) { |
154 |
s/^\s+//; |
s/^\s+//; |
155 |
|
s/:\s+/:/; |
156 |
my @n = split(/\s+/, $_, 17); |
my @n = split(/\s+/, $_, 17); |
157 |
next unless ( $n[0] =~ s!(eth\d|ath\d):!! ); |
next unless ( $n[0] =~ s!(eth\d|ath\d):!! ); |
158 |
|
|
184 |
my $d_write = ( $d_w - $ld_w ) / $dt; |
my $d_write = ( $d_w - $ld_w ) / $dt; |
185 |
( $ld_r, $ld_w ) = ( $d_r, $d_w ); |
( $ld_r, $ld_w ) = ( $d_r, $d_w ); |
186 |
|
|
187 |
printf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n", |
my $out = sprintf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n", |
188 |
$s, |
$s, |
189 |
$load, |
$load, |
190 |
unit( $d_read ), unit( $d_write ), |
unit( $d_read ), unit( $d_write ), |
192 |
$bat, $temp, |
$bat, $temp, |
193 |
); |
); |
194 |
|
|
195 |
|
if ( $awesome ) { |
196 |
|
print $awesome "0 statusbar_set_text $out"; |
197 |
|
} else { |
198 |
|
print $out; |
199 |
|
} |
200 |
|
|
201 |
sleep $dt; |
sleep $dt; |
202 |
} |
} |
203 |
|
|