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 |
$bat->{state} = proc2hash( '/proc/acpi/battery/BAT0/state' ); |
65 |
$bat->{info} = proc2hash( '/proc/acpi/battery/BAT0/info' ); |
66 |
|
67 |
$bat->{pcnt} = $bat->{state}->{'remaining capacity'} / $bat->{info}->{'design capacity'}; |
68 |
|
69 |
my $time = ( $bat->{info}->{'design capacity'} - $bat->{state}->{'remaining capacity'} ) / $bat->{state}->{'present rate'}; |
70 |
|
71 |
warn "time = $time\n" if ($debug); |
72 |
|
73 |
$bat->{hh} = int( $time ); |
74 |
$bat->{mm} = int( ( $time - $bat->{hh} ) * 60 ); |
75 |
$bat->{ss} = ( $time * 3600 ) % 60; |
76 |
|
77 |
$bat->{new} = '!'; |
78 |
|
79 |
} else { |
80 |
$bat->{new} = ' '; |
81 |
} |
82 |
$i++; |
83 |
|
84 |
my $load = read_file('/proc/loadavg'); |
85 |
chomp( $load ); |
86 |
$load =~ s!\s\d+/\d+.*!!; |
87 |
|
88 |
my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature'); |
89 |
chomp( $temp ); |
90 |
$temp =~ s!^.*:\s+!!; |
91 |
|
92 |
my $net = read_file('/proc/net/dev'); |
93 |
my ( $rx, $tx ) = ( 0,0 ); |
94 |
|
95 |
foreach ( split(/\n/, $net) ) { |
96 |
s/^\s+//; |
97 |
my @n = split(/\s+/, $_, 17); |
98 |
next unless ( $n[0] =~ m!(eth\d|ath\d):! ); |
99 |
|
100 |
warn dump( @n ) if ($debug); |
101 |
$rx += $n[1]; |
102 |
$tx += $n[9]; |
103 |
} |
104 |
warn "rx: $rx tx: $tx\n" if ($debug); |
105 |
|
106 |
my $r = ( $rx - $lrx ) / $dt; |
107 |
my $t = ( $tx - $ltx ) / $dt; |
108 |
( $lrx, $ltx ) = ( $rx, $tx ); |
109 |
|
110 |
printf "%s | %s |%6s >> %-6s| %s %2d%% %02d:%02d:%02d %3.1fW%s| %s\n", |
111 |
$s, |
112 |
$load, |
113 |
unit( $r ), unit( $t ), |
114 |
substr($bat->{state}->{'charging state'},0,1), $bat->{pcnt} * 100, $bat->{hh}, $bat->{mm}, $bat->{ss}, |
115 |
$bat->{state}->{'present rate'} / 1000, $bat->{new}, |
116 |
$temp; |
117 |
|
118 |
sleep $dt; |
119 |
} |
120 |
|