/[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

Diff of /trunk/dwm-status.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 30 by dpavlin, Sat May 26 23:18:05 2007 UTC revision 36 by dpavlin, Sun May 27 15:30:12 2007 UTC
# Line 11  use Time::HiRes; Line 11  use Time::HiRes;
11  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
12    
13  my $dt = 3;  my $dt = 3;
14  my $acpi_every = 3;  my $acpi_every = 10;
15    
16  my $debug = 0;  my $disk_blk_size = 512;
17    
18    my $debug = shift @ARGV;
19    
20  $|=1;  $|=1;
21    
# Line 33  sub proc2hash { Line 35  sub proc2hash {
35    
36  sub unit {  sub unit {
37          my $v = shift;          my $v = shift;
38            
39          my @units = qw/b k m g/;          warn "unit( $v )\n" if ($debug);
40    
41            my @units = qw/b k M G/;
42          my $o = 0;          my $o = 0;
43    
44          while ( ( $v / 1024 ) >= 1 ) {          while ( ( $v / 1024 ) >= 1 ) {
# Line 42  sub unit { Line 46  sub unit {
46                  $v /= 1024;                  $v /= 1024;
47          }          }
48    
49          return sprintf("%d%s/s", $v, $units[$o]);          if ( $v >= 1 ) {
50                    return sprintf("%d%s", $v, $units[$o]);
51            } elsif ( $v == 0 ) {
52                    return '';
53            } else {
54                    return sprintf("%.1f%s", $v, $units[$o]);
55            }
56  }  }
57    
58  my ( $lrx, $ltx ) = ( 0, 0 );  my ( $lrx, $ltx ) = ( 0, 0 );
59    my ( $ld_r, $ld_w ) = ( 0, 0 );
60  my $bat;  my $bat;
61    
62  my $i = 0;  my $i = 0;
# Line 55  while ( 1 ) { Line 66  while ( 1 ) {
66    
67          if ( $i % $acpi_every == 0 ) {          if ( $i % $acpi_every == 0 ) {
68    
69                  $bat->{state} = proc2hash( '/proc/acpi/battery/BAT0/state' );                  my $state = proc2hash( '/proc/acpi/battery/BAT0/state' );
70                  $bat->{info} = proc2hash( '/proc/acpi/battery/BAT0/info' );  
71                    if ( $state->{'present rate'} != 0 ) {
72                            my $info = proc2hash( '/proc/acpi/battery/BAT0/info' );
73    
74                            my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};
75    
76                  $bat->{pcnt} = $bat->{state}->{'remaining capacity'} / $bat->{info}->{'design capacity'};                          my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
77    
78                  my $time = ( $bat->{info}->{'design capacity'} - $bat->{state}->{'remaining capacity'} ) / $bat->{state}->{'present rate'};                          warn "time = $time\n" if ($debug);
79    
80                  $bat->{hh} = int( $time );                          my $hh = int( $time );
81                  $bat->{mm} = int( ( $time - $bat->{hh} ) * 60 );                          my $mm = int( ( $time - $hh ) * 60 );
82                  $bat->{ss} = ( $time * 3600 ) % 60;                          my $ss = ( $time * 3600 ) % 60;
83    
84                  $bat->{new} = '!';                          $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ",
85                                    substr($state->{'charging state'},0,1),
86                                    $pcnt * 100, $hh, $mm, $ss,
87                                    $state->{'present rate'} / 1000
88                            );
89    
90                            } else {
91                                    $bat = '';
92                            }
93          } else {          } else {
94                  $bat->{new} = ' ';                  $bat =~ s/!(\|\s)$/ $1/;
95          }          }
96          $i++;          $i++;
97    
# Line 87  while ( 1 ) { Line 109  while ( 1 ) {
109          foreach ( split(/\n/, $net) ) {          foreach ( split(/\n/, $net) ) {
110                  s/^\s+//;                  s/^\s+//;
111                  my @n = split(/\s+/, $_, 17);                  my @n = split(/\s+/, $_, 17);
112                  next unless ( $n[0] =~ m!(eth\d|ath\d):! );                  next unless ( $n[0] =~ s!(eth\d|ath\d):!! );
113    
114                  warn dump( @n ) if ($debug);                  warn dump( @n ) if ($debug);
115                  $rx += $n[1];                  $rx += $n[0];
116                  $tx += $n[9];                  $tx += $n[8];
117          }          }
118          warn "rx: $rx tx: $tx\n" if ($debug);          warn "rx: $rx tx: $tx\n" if ($debug);
119    
120          my $r = ( $rx - $lrx ) / $dt;          my $net_rx = ( $rx - $lrx ) / $dt;
121          my $t = ( $tx - $ltx ) / $dt;          my $net_tx = ( $tx - $ltx ) / $dt;
122          ( $lrx, $ltx ) = ( $rx, $tx );          ( $lrx, $ltx ) = ( $rx, $tx );
123    
124          printf "%s | %s |%6s >> %-6s| %s %2d%% %02d:%02d:%02d%s| %s\n",          my $disk = read_file('/proc/diskstats');
125            my ( $d_r, $d_w ) = ( 0,0 );
126    
127            foreach ( split(/\n/, $disk) ) {
128                    s/^\s+//;
129                    my @d = split(/\s+/, $_, 17);
130                    next unless ( $d[2] =~ m/^[sh]d\w$/ );
131    
132                    warn dump( @d ) if ($debug);
133                    $d_r += $d[5] * $disk_blk_size;
134                    $d_w += $d[7] * $disk_blk_size;
135            }
136            warn "d_r: $d_r d_w: $d_w\n" if ($debug);
137    
138            my $d_read = ( $d_r - $ld_r ) / $dt;
139            my $d_write = ( $d_w - $ld_w ) / $dt;
140            ( $ld_r, $ld_w ) = ( $d_r, $d_w );
141    
142            printf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n",
143                  $s,                  $s,
144                  $load,                  $load,
145                  unit( $r ), unit( $t ),                  unit( $d_read ), unit( $d_write ),
146                  substr($bat->{state}->{'charging state'},0,1), $bat->{pcnt} * 100, $bat->{hh}, $bat->{mm}, $bat->{ss}, $bat->{new},                  unit( $net_rx ), unit( $net_tx ),
147                  $temp;                  $bat, $temp,
148            );
149    
150          sleep $dt;          sleep $dt;
151  }  }

Legend:
Removed from v.30  
changed lines
  Added in v.36

  ViewVC Help
Powered by ViewVC 1.1.26