13 |
my $dt = 3; |
my $dt = 3; |
14 |
my $acpi_every = 10; |
my $acpi_every = 10; |
15 |
|
|
16 |
|
my $disk_blk_size = 512; |
17 |
|
|
18 |
my $debug = shift @ARGV; |
my $debug = shift @ARGV; |
19 |
|
my $awesome = 3; |
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>) { |
29 |
chomp; |
chomp; |
30 |
my ( $key, $value ) = split(/:\s+/, $_, 2); |
my ( $key, $value ) = split(/:\s+/, $_, 2); |
31 |
$value =~ s/ m[VW]h*$//; |
$value =~ s/ m[AVW]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 |
|
|
73 |
warn "unit( $v )\n" if ($debug); |
warn "unit( $v )\n" if ($debug); |
74 |
|
|
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 |
} |
} |
90 |
} |
} |
91 |
|
|
92 |
my ( $lrx, $ltx ) = ( 0, 0 ); |
my ( $lrx, $ltx ) = ( 0, 0 ); |
93 |
my $bat; |
my ( $ld_r, $ld_w ) = ( 0, 0 ); |
94 |
|
my $bat = ''; |
95 |
|
|
96 |
my $i = 0; |
my $i = 0; |
97 |
|
my $sys_fs = '/sys/class/power_supply/BAT0'; |
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 ) { |
103 |
|
my $sysfs_path = glob "$sys_fs/*_full"; |
104 |
|
if ( $sysfs_path ) { |
105 |
|
|
106 |
|
my $full = read_file( $sysfs_path ); |
107 |
|
$sysfs_path =~ s/_full/_now/; |
108 |
|
my $now = read_file( $sysfs_path ); |
109 |
|
$bat = sprintf("%2d%% | ", $now * 100 / $full ); |
110 |
|
|
111 |
my $state = proc2hash( '/proc/acpi/battery/BAT0/state' ); |
} elsif ( $proc_acpi_battery ) { |
112 |
|
|
113 |
if ( $state->{'present rate'} != 0 ) { |
my $state = proc2hash( "$proc_acpi_battery/state" ); |
|
my $info = proc2hash( '/proc/acpi/battery/BAT0/info' ); |
|
114 |
|
|
115 |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) { |
116 |
|
my $info = proc2hash( "$proc_acpi_battery/info" ); |
117 |
|
|
118 |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
119 |
|
|
120 |
warn "time = $time\n" if ($debug); |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
121 |
|
$time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' ); |
122 |
|
|
123 |
my $hh = int( $time ); |
warn "time = $time\n" if ($debug); |
|
my $mm = int( ( $time - $hh ) * 60 ); |
|
|
my $ss = ( $time * 3600 ) % 60; |
|
124 |
|
|
125 |
$bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", |
my $hh = int( $time ); |
126 |
substr($state->{'charging state'},0,1), |
my $mm = int( ( $time - $hh ) * 60 ); |
127 |
$pcnt * 100, $hh, $mm, $ss, |
my $ss = ( $time * 3600 ) % 60; |
|
$state->{'present rate'} / 1000 |
|
|
); |
|
128 |
|
|
129 |
} else { |
$bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", |
130 |
$bat = ''; |
substr($state->{'charging state'},0,1), |
131 |
|
$pcnt * 100, $hh, $mm, $ss, |
132 |
|
$state->{'present rate'} / 1000 |
133 |
|
); |
134 |
} |
} |
135 |
|
} |
136 |
} else { |
} else { |
137 |
$bat =~ s/!(\|\s)$/ $1/; |
$bat =~ s/!(\|\s)$/ $1/; |
138 |
} |
} |
142 |
chomp( $load ); |
chomp( $load ); |
143 |
$load =~ s!\s\d+/\d+.*!!; |
$load =~ s!\s\d+/\d+.*!!; |
144 |
|
|
145 |
my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature'); |
my $temp = ''; |
146 |
chomp( $temp ); |
if ( $proc_acpi_thermal_zone_temperature ) { |
147 |
$temp =~ s!^.*:\s+!!; |
$temp = read_file( $proc_acpi_thermal_zone_temperature ); |
148 |
|
chomp( $temp ); |
149 |
|
$temp =~ s!^.*:\s+!!; |
150 |
|
} |
151 |
|
|
152 |
my $net = read_file('/proc/net/dev'); |
my $net = read_file('/proc/net/dev'); |
153 |
my ( $rx, $tx ) = ( 0,0 ); |
my ( $rx, $tx ) = ( 0,0 ); |
154 |
|
|
155 |
foreach ( split(/\n/, $net) ) { |
foreach ( split(/\n/, $net) ) { |
156 |
s/^\s+//; |
s/^\s+//; |
157 |
|
s/:\s+/:/; |
158 |
my @n = split(/\s+/, $_, 17); |
my @n = split(/\s+/, $_, 17); |
159 |
next unless ( $n[0] =~ m!(eth\d|ath\d):! ); |
#next unless ( $n[0] =~ s!(eth\d|ath\d):!! ); |
160 |
|
next unless $n[1] =~ m{^\d+$}; |
161 |
|
|
162 |
warn dump( @n ) if ($debug); |
warn dump( @n ) if ($debug); |
163 |
$rx += $n[1]; |
$rx += $n[1]; |
164 |
$tx += $n[9]; |
$tx += $n[8]; |
165 |
} |
} |
166 |
warn "rx: $rx tx: $tx\n" if ($debug); |
warn "rx: $rx tx: $tx\n" if ($debug); |
167 |
|
|
168 |
my $r = ( $rx - $lrx ) / $dt; |
my $net_rx = ( $rx - $lrx ) / $dt; |
169 |
my $t = ( $tx - $ltx ) / $dt; |
my $net_tx = ( $tx - $ltx ) / $dt; |
170 |
( $lrx, $ltx ) = ( $rx, $tx ); |
( $lrx, $ltx ) = ( $rx, $tx ); |
171 |
|
|
172 |
printf "%s | %s |%5s > %-5s| %s%s\n", $s, $load, unit( $r ), unit( $t ), $bat, $temp; |
my $disk = read_file('/proc/diskstats'); |
173 |
|
my ( $d_r, $d_w ) = ( 0,0 ); |
174 |
|
|
175 |
|
foreach ( split(/\n/, $disk) ) { |
176 |
|
s/^\s+//; |
177 |
|
my @d = split(/\s+/, $_, 17); |
178 |
|
next unless ( $d[2] =~ m/^[sh]d\w$/ ); |
179 |
|
|
180 |
|
warn dump( @d ) if ($debug); |
181 |
|
$d_r += $d[5] * $disk_blk_size; |
182 |
|
$d_w += $d[7] * $disk_blk_size; |
183 |
|
} |
184 |
|
warn "d_r: $d_r d_w: $d_w\n" if ($debug); |
185 |
|
|
186 |
|
my $d_read = ( $d_r - $ld_r ) / $dt; |
187 |
|
my $d_write = ( $d_w - $ld_w ) / $dt; |
188 |
|
( $ld_r, $ld_w ) = ( $d_r, $d_w ); |
189 |
|
|
190 |
|
my $out = sprintf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s", |
191 |
|
$s, |
192 |
|
$load, |
193 |
|
unit( $d_read ), unit( $d_write ), |
194 |
|
unit( $net_rx ), unit( $net_tx ), |
195 |
|
$bat, $temp, |
196 |
|
); |
197 |
|
|
198 |
|
print "$out\n"; |
199 |
|
if ( $awesome ) { |
200 |
|
open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!"; |
201 |
|
print $fh |
202 |
|
$awesome == 3 ? qq{mytextbox.text="$out"\n} |
203 |
|
: "0 widget_tell mystatusbar dwm-status text $out\n" |
204 |
|
; |
205 |
|
close($fh); |
206 |
|
} |
207 |
|
|
208 |
sleep $dt; |
sleep $dt; |
209 |
} |
} |