/[scripts]/trunk/dwm-status.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/dwm-status.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations)
Sun May 27 08:41:53 2007 UTC (16 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 2403 byte(s)
report no networking as empty string

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26