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

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

  ViewVC Help
Powered by ViewVC 1.1.26