/[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 31 by dpavlin, Sat May 26 23:47:35 2007 UTC revision 55 by dpavlin, Tue Oct 2 16:10:08 2007 UTC
# Line 8  use strict; Line 8  use strict;
8  use POSIX qw/strftime/;  use POSIX qw/strftime/;
9  use File::Slurp;  use File::Slurp;
10  use Time::HiRes;  use Time::HiRes;
11  use Data::Dump qw/dump/;  eval {
12            use Data::Dump qw/dump/;
13    };
14    
15  my $dt = 3;  my $dt = 3;
16  my $acpi_every = 10;  my $acpi_every = 10;
17    
18    my $disk_blk_size = 512;
19    
20  my $debug = shift @ARGV;  my $debug = shift @ARGV;
21    
22  $|=1;  $|=1;
23    
24  sub proc2hash {  sub proc2hash {
25          my $f = shift;          my $f = shift;
26            return unless -f $f;
27          open(my $fh, '<', $f) || die "can't open $f: $!";          open(my $fh, '<', $f) || die "can't open $f: $!";
28          my $h;          my $h;
29          while(<$fh>) {          while(<$fh>) {
# Line 27  sub proc2hash { Line 32  sub proc2hash {
32                  $value =~ s/ m[VW]h*$//;                  $value =~ s/ m[VW]h*$//;
33                  $h->{$key} = $value;                  $h->{$key} = $value;
34          }          }
35          warn dump( $h ) if ( $debug );          warn "$f ",dump( $h ) if ( $debug );
36          return $h;          return $h;
37  }  }
38    
39    my $proc_acpi_battery;
40    sub find_proc_acpi {
41            my ( $path, $check ) = @_;
42    
43            if ( -e "/proc/acpi/$path" ) {
44                    opendir(my $items, "/proc/acpi/$path") || die "can't open /proc/acpi/$path: $!";
45                    foreach my $item ( readdir( $items ) ) {
46                            $check->( $item ) && last;
47                    }
48            }
49    }
50    
51    my $proc_acpi_thermal_zone_temperature;
52    find_proc_acpi('thermal_zone', sub {
53            my $thm = shift;
54            return if ( ! -e "/proc/acpi/thermal_zone/$thm/temperature" );
55            warn "# using thermal zone $thm\n" if ( $debug );
56            $proc_acpi_thermal_zone_temperature = "/proc/acpi/thermal_zone/$thm/temperature";
57    });
58    
59    find_proc_acpi('battery', sub {
60            my $bat = shift;
61            return unless $bat =~ /BAT/;
62    
63            warn "# testing battery $bat\n" if ( $debug );
64    
65            if ( proc2hash( "/proc/acpi/battery/$bat/info" )->{present} eq 'yes' ) {
66                    $proc_acpi_battery = "/proc/acpi/battery/$bat";
67                    warn "using $proc_acpi_battery to monitor battery\n";
68            }
69    });
70    
71  sub unit {  sub unit {
72          my $v = shift;          my $v = shift;
73    
74          warn "unit( $v )\n" if ($debug);          warn "unit( $v )\n" if ($debug);
75    
76          my @units = qw/b k m g/;          my @units = qw/b k M G/;
77          my $o = 0;          my $o = 0;
78    
79          while ( ( $v / 1024 ) >= 1 ) {          while ( ( $v / 10000 ) >= 1 ) {
80                  $o++;                  $o++;
81                  $v /= 1024;                  $v /= 1024;
82          }          }
83    
84          if ( $v >= 1 ) {          if ( $v >= 1 ) {
85                  return sprintf("%d%s/s", $v, $units[$o]);                  return sprintf("%d%s", $v, $units[$o]);
86            } elsif ( $v == 0 ) {
87                    return '';
88          } else {          } else {
89                  return sprintf("%.1f%s/s", $v, $units[$o]);                  return sprintf("%.1f%s", $v, $units[$o]);
90          }          }
91  }  }
92    
93  my ( $lrx, $ltx ) = ( 0, 0 );  my ( $lrx, $ltx ) = ( 0, 0 );
94    my ( $ld_r, $ld_w ) = ( 0, 0 );
95  my $bat;  my $bat;
96    
97  my $i = 0;  my $i = 0;
# Line 59  my $i = 0; Line 99  my $i = 0;
99  while ( 1 ) {  while ( 1 ) {
100          my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());          my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());
101    
102          if ( $i % $acpi_every == 0 ) {          if ( $i % $acpi_every == 0 && $proc_acpi_battery ) {
103    
104                    my $state = proc2hash( "$proc_acpi_battery/state" );
105    
106                  $bat->{state} = proc2hash( '/proc/acpi/battery/BAT0/state' );                  if ( $state->{'present rate'} != 0 ) {
107                  $bat->{info} = proc2hash( '/proc/acpi/battery/BAT0/info' );                          my $info = proc2hash( "$proc_acpi_battery/info" );
108    
109                  $bat->{pcnt} = $bat->{state}->{'remaining capacity'} / $bat->{info}->{'design capacity'};                          my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};
110    
111                  my $time = ( $bat->{info}->{'design capacity'} - $bat->{state}->{'remaining capacity'} ) / $bat->{state}->{'present rate'};                          my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
112                            $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' );
113    
114                  warn "time = $time\n" if ($debug);                          warn "time = $time\n" if ($debug);
115    
116                  $bat->{hh} = int( $time );                          my $hh = int( $time );
117                  $bat->{mm} = int( ( $time - $bat->{hh} ) * 60 );                          my $mm = int( ( $time - $hh ) * 60 );
118                  $bat->{ss} = ( $time * 3600 ) % 60;                          my $ss = ( $time * 3600 ) % 60;
119    
120                  $bat->{new} = '!';                          $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ",
121                                    substr($state->{'charging state'},0,1),
122                                    $pcnt * 100, $hh, $mm, $ss,
123                                    $state->{'present rate'} / 1000
124                            );
125    
126                            } else {
127                                    $bat = '';
128                            }
129          } else {          } else {
130                  $bat->{new} = ' ';                  $bat =~ s/!(\|\s)$/ $1/;
131          }          }
132          $i++;          $i++;
133    
# Line 85  while ( 1 ) { Line 135  while ( 1 ) {
135          chomp( $load );          chomp( $load );
136          $load =~ s!\s\d+/\d+.*!!;          $load =~ s!\s\d+/\d+.*!!;
137    
138          my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature');          my $temp = '';
139          chomp( $temp );          if ( $proc_acpi_thermal_zone_temperature ) {
140          $temp =~ s!^.*:\s+!!;                  $temp = read_file( $proc_acpi_thermal_zone_temperature );
141                    chomp( $temp );
142                    $temp =~ s!^.*:\s+!!;
143            }
144    
145          my $net = read_file('/proc/net/dev');          my $net = read_file('/proc/net/dev');
146          my ( $rx, $tx ) = ( 0,0 );          my ( $rx, $tx ) = ( 0,0 );
147    
148          foreach ( split(/\n/, $net) ) {          foreach ( split(/\n/, $net) ) {
149                  s/^\s+//;                  s/^\s+//;
150                    s/:\s+/:/;
151                  my @n = split(/\s+/, $_, 17);                  my @n = split(/\s+/, $_, 17);
152                  next unless ( $n[0] =~ m!(eth\d|ath\d):! );                  next unless ( $n[0] =~ s!(eth\d|ath\d):!! );
153    
154                  warn dump( @n ) if ($debug);                  warn dump( @n ) if ($debug);
155                  $rx += $n[1];                  $rx += $n[0];
156                  $tx += $n[9];                  $tx += $n[8];
157          }          }
158          warn "rx: $rx tx: $tx\n" if ($debug);          warn "rx: $rx tx: $tx\n" if ($debug);
159    
160          my $r = ( $rx - $lrx ) / $dt;          my $net_rx = ( $rx - $lrx ) / $dt;
161          my $t = ( $tx - $ltx ) / $dt;          my $net_tx = ( $tx - $ltx ) / $dt;
162          ( $lrx, $ltx ) = ( $rx, $tx );          ( $lrx, $ltx ) = ( $rx, $tx );
163    
164          printf "%s | %s |%6s >> %-6s| %s %2d%% %02d:%02d:%02d %3.1fW%s| %s\n",          my $disk = read_file('/proc/diskstats');
165            my ( $d_r, $d_w ) = ( 0,0 );
166    
167            foreach ( split(/\n/, $disk) ) {
168                    s/^\s+//;
169                    my @d = split(/\s+/, $_, 17);
170                    next unless ( $d[2] =~ m/^[sh]d\w$/ );
171    
172                    warn dump( @d ) if ($debug);
173                    $d_r += $d[5] * $disk_blk_size;
174                    $d_w += $d[7] * $disk_blk_size;
175            }
176            warn "d_r: $d_r d_w: $d_w\n" if ($debug);
177    
178            my $d_read = ( $d_r - $ld_r ) / $dt;
179            my $d_write = ( $d_w - $ld_w ) / $dt;
180            ( $ld_r, $ld_w ) = ( $d_r, $d_w );
181    
182            printf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n",
183                  $s,                  $s,
184                  $load,                  $load,
185                  unit( $r ), unit( $t ),                  unit( $d_read ), unit( $d_write ),
186                  substr($bat->{state}->{'charging state'},0,1), $bat->{pcnt} * 100, $bat->{hh}, $bat->{mm}, $bat->{ss},                  unit( $net_rx ), unit( $net_tx ),
187                          $bat->{state}->{'present rate'} / 1000, $bat->{new},                  $bat, $temp,
188                  $temp;          );
189    
190          sleep $dt;          sleep $dt;
191  }  }

Legend:
Removed from v.31  
changed lines
  Added in v.55

  ViewVC Help
Powered by ViewVC 1.1.26