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 = 3; |
15 |
|
16 |
my $debug = 0; |
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 |
my @units = qw/b k m g/; |
38 |
my $o = 0; |
39 |
|
40 |
while ( ( $v / 1024 ) >= 1 ) { |
41 |
$o++; |
42 |
$v /= 1024; |
43 |
} |
44 |
|
45 |
return sprintf("%d%s/s", $v, $units[$o]); |
46 |
} |
47 |
|
48 |
my ( $lrx, $ltx ) = ( 0, 0 ); |
49 |
my $bat; |
50 |
|
51 |
my $i = 0; |
52 |
|
53 |
while ( 1 ) { |
54 |
my $s = strftime("%Y-%m-%d %H:%M:%S", localtime()); |
55 |
|
56 |
if ( $i % $acpi_every == 0 ) { |
57 |
|
58 |
$bat->{state} = proc2hash( '/proc/acpi/battery/BAT0/state' ); |
59 |
$bat->{info} = proc2hash( '/proc/acpi/battery/BAT0/info' ); |
60 |
|
61 |
$bat->{pcnt} = $bat->{state}->{'remaining capacity'} / $bat->{info}->{'design capacity'}; |
62 |
|
63 |
my $time = ( $bat->{info}->{'design capacity'} - $bat->{state}->{'remaining capacity'} ) / $bat->{state}->{'present rate'}; |
64 |
|
65 |
$bat->{hh} = int( $time ); |
66 |
$bat->{mm} = int( ( $time - $bat->{hh} ) * 60 ); |
67 |
$bat->{ss} = ( $time * 3600 ) % 60; |
68 |
|
69 |
$bat->{new} = '!'; |
70 |
|
71 |
} else { |
72 |
$bat->{new} = ' '; |
73 |
} |
74 |
$i++; |
75 |
|
76 |
my $load = read_file('/proc/loadavg'); |
77 |
chomp( $load ); |
78 |
$load =~ s!\s\d+/\d+.*!!; |
79 |
|
80 |
my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature'); |
81 |
chomp( $temp ); |
82 |
$temp =~ s!^.*:\s+!!; |
83 |
|
84 |
my $net = read_file('/proc/net/dev'); |
85 |
my ( $rx, $tx ) = ( 0,0 ); |
86 |
|
87 |
foreach ( split(/\n/, $net) ) { |
88 |
s/^\s+//; |
89 |
my @n = split(/\s+/, $_, 17); |
90 |
next unless ( $n[0] =~ m!(eth\d|ath\d):! ); |
91 |
|
92 |
warn dump( @n ) if ($debug); |
93 |
$rx += $n[1]; |
94 |
$tx += $n[9]; |
95 |
} |
96 |
warn "rx: $rx tx: $tx\n" if ($debug); |
97 |
|
98 |
my $r = ( $rx - $lrx ) / $dt; |
99 |
my $t = ( $tx - $ltx ) / $dt; |
100 |
( $lrx, $ltx ) = ( $rx, $tx ); |
101 |
|
102 |
printf "%s | %s |%6s >> %-6s| %s %2d%% %02d:%02d:%02d%s| %s\n", |
103 |
$s, |
104 |
$load, |
105 |
unit( $r ), unit( $t ), |
106 |
substr($bat->{state}->{'charging state'},0,1), $bat->{pcnt} * 100, $bat->{hh}, $bat->{mm}, $bat->{ss}, $bat->{new}, |
107 |
$temp; |
108 |
|
109 |
sleep $dt; |
110 |
} |
111 |
|