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