1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# dwm-status.pl |
4 |
# |
5 |
# 05/26/07 23:08:31 CEST Dobrica Pavlinusic <dpavlin@rot13.org> |
6 |
|
7 |
use strict; |
8 |
use POSIX qw/strftime/; |
9 |
use File::Slurp; |
10 |
use Time::HiRes; |
11 |
use Data::Dump qw/dump/; |
12 |
|
13 |
my $dt = 3; |
14 |
my $acpi_every = 10; |
15 |
|
16 |
my $debug = shift @ARGV; |
17 |
|
18 |
$|=1; |
19 |
|
20 |
sub proc2hash { |
21 |
my $f = shift; |
22 |
open(my $fh, '<', $f) || die "can't open $f: $!"; |
23 |
my $h; |
24 |
while(<$fh>) { |
25 |
chomp; |
26 |
my ( $key, $value ) = split(/:\s+/, $_, 2); |
27 |
$value =~ s/ m[VW]h*$//; |
28 |
$h->{$key} = $value; |
29 |
} |
30 |
warn dump( $h ) if ( $debug ); |
31 |
return $h; |
32 |
} |
33 |
|
34 |
sub unit { |
35 |
my $v = shift; |
36 |
|
37 |
warn "unit( $v )\n" if ($debug); |
38 |
|
39 |
my @units = qw/b k m g/; |
40 |
my $o = 0; |
41 |
|
42 |
while ( ( $v / 1024 ) >= 1 ) { |
43 |
$o++; |
44 |
$v /= 1024; |
45 |
} |
46 |
|
47 |
if ( $v >= 1 ) { |
48 |
return sprintf("%d%s/s", $v, $units[$o]); |
49 |
} else { |
50 |
return sprintf("%.1f%s/s", $v, $units[$o]); |
51 |
} |
52 |
} |
53 |
|
54 |
my ( $lrx, $ltx ) = ( 0, 0 ); |
55 |
my $bat; |
56 |
|
57 |
my $i = 0; |
58 |
|
59 |
while ( 1 ) { |
60 |
my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); |
61 |
|
62 |
if ( $i % $acpi_every == 0 ) { |
63 |
|
64 |
my $state = proc2hash( '/proc/acpi/battery/BAT0/state' ); |
65 |
|
66 |
if ( $state->{'present rate'} != 0 ) { |
67 |
my $info = proc2hash( '/proc/acpi/battery/BAT0/info' ); |
68 |
|
69 |
my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'}; |
70 |
|
71 |
my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} ); |
72 |
|
73 |
warn "time = $time\n" if ($debug); |
74 |
|
75 |
my $hh = int( $time ); |
76 |
my $mm = int( ( $time - $hh ) * 60 ); |
77 |
my $ss = ( $time * 3600 ) % 60; |
78 |
|
79 |
$bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ", |
80 |
substr($state->{'charging state'},0,1), |
81 |
$pcnt * 100, $hh, $mm, $ss, |
82 |
$state->{'present rate'} / 1000 |
83 |
); |
84 |
|
85 |
} else { |
86 |
$bat = ''; |
87 |
} |
88 |
} else { |
89 |
$bat =~ s/!(\|\s)$/ $1/; |
90 |
} |
91 |
$i++; |
92 |
|
93 |
my $load = read_file('/proc/loadavg'); |
94 |
chomp( $load ); |
95 |
$load =~ s!\s\d+/\d+.*!!; |
96 |
|
97 |
my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature'); |
98 |
chomp( $temp ); |
99 |
$temp =~ s!^.*:\s+!!; |
100 |
|
101 |
my $net = read_file('/proc/net/dev'); |
102 |
my ( $rx, $tx ) = ( 0,0 ); |
103 |
|
104 |
foreach ( split(/\n/, $net) ) { |
105 |
s/^\s+//; |
106 |
my @n = split(/\s+/, $_, 17); |
107 |
next unless ( $n[0] =~ m!(eth\d|ath\d):! ); |
108 |
|
109 |
warn dump( @n ) if ($debug); |
110 |
$rx += $n[1]; |
111 |
$tx += $n[9]; |
112 |
} |
113 |
warn "rx: $rx tx: $tx\n" if ($debug); |
114 |
|
115 |
my $r = ( $rx - $lrx ) / $dt; |
116 |
my $t = ( $tx - $ltx ) / $dt; |
117 |
( $lrx, $ltx ) = ( $rx, $tx ); |
118 |
|
119 |
printf "%s | %s |%6s >> %-6s| %s%s\n", $s, $load, unit( $r ), unit( $t ), $bat, $temp; |
120 |
|
121 |
sleep $dt; |
122 |
} |
123 |
|