/[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 135 by dpavlin, Wed Mar 31 06:55:19 2010 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    my $awesome = 0;
20    my $dzen = 1;
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>) {
30                  chomp;                  chomp;
31                  my ( $key, $value ) = split(/:\s+/, $_, 2);                  my ( $key, $value ) = split(/:\s+/, $_, 2);
32                  $value =~ s/ m[VW]h*$//;                  $value =~ s/ m[AVW]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 =~ /\w+/;
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    my $unit_colors = {
72            'b' => '#ff0000',
73            'k' => '#ffff00',
74            'M' => '#00ff00',
75            'G' => '#0000ff',
76    };
77    
78  sub unit {  sub unit {
79          my $v = shift;          my ($v,$align) = @_;
80            $align ||= '';
81    
82          warn "unit( $v )\n" if ($debug);          warn "unit( $v )\n" if ($debug);
83    
84          my @units = qw/b k m g/;          my @units = qw/b k M G/;
85          my $o = 0;          my $o = 0;
86    
87          while ( ( $v / 1024 ) >= 1 ) {          while ( ( $v / 10000 ) >= 1 ) {
88                  $o++;                  $o++;
89                  $v /= 1024;                  $v /= 1024;
90          }          }
91    
92            my $unit = $units[$o];
93    
94            my $out = '';
95            $out .= '^fg(' . $unit_colors->{$unit} . ')';
96    
97          if ( $v >= 1 ) {          if ( $v >= 1 ) {
98                  return sprintf("%d%s/s", $v, $units[$o]);                  $out .= sprintf("%${align}5d",$v);
99            } elsif ( $v == 0 ) {
100                    $out = ' ' x 5;
101          } else {          } else {
102                  return sprintf("%.1f%s/s", $v, $units[$o]);                  $out .= sprintf("%${align}5.1f", $v);
103          }          }
104    
105            $unit = '^fg(#444444)' . $unit if $dzen;
106            $out =~ s/\s(\s*)$/$unit$1/ unless $out =~ m/\s{5}/;
107            $out .= '^fg(#ffffff)' if $dzen;
108    
109            return $out;
110  }  }
111    
112  my ( $lrx, $ltx ) = ( 0, 0 );  my ( $lrx, $ltx ) = ( 0, 0 );
113  my $bat;  my ( $ld_r, $ld_w ) = ( 0, 0 );
114    my $bat = '';
115    
116  my $i = 0;  my $i = 0;
117    my $sys_fs = '/sys/class/power_supply/BAT0';
118    
119  while ( 1 ) {  while ( 1 ) {
120          my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());          my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());
121    
122          if ( $i % $acpi_every == 0 ) {          if ( $i % $acpi_every == 0 ) {
123                    my $sysfs_path = glob "$sys_fs/*_full";
124                    if ( $sysfs_path ) {
125    
126                  $bat->{state} = proc2hash( '/proc/acpi/battery/BAT0/state' );                          my $full = read_file( $sysfs_path );
127                  $bat->{info} = proc2hash( '/proc/acpi/battery/BAT0/info' );                          $sysfs_path =~ s/_full/_now/;
128                            my $now = read_file( $sysfs_path );
129                            $bat = sprintf("%2d%%", $now * 100 / $full );
130    
131                  $bat->{pcnt} = $bat->{state}->{'remaining capacity'} / $bat->{info}->{'design capacity'};                  } elsif ( $proc_acpi_battery ) {
132    
133                  my $time = ( $bat->{info}->{'design capacity'} - $bat->{state}->{'remaining capacity'} ) / $bat->{state}->{'present rate'};                          my $state = proc2hash( "$proc_acpi_battery/state" );
134    
135                  warn "time = $time\n" if ($debug);                          if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) {
136                                    my $info = proc2hash( "$proc_acpi_battery/info" );
137    
138                  $bat->{hh} = int( $time );                                  my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};
                 $bat->{mm} = int( ( $time - $bat->{hh} ) * 60 );  
                 $bat->{ss} = ( $time * 3600 ) % 60;  
139    
140                  $bat->{new} = '!';                                  my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
141                                    $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' );
142    
143          } else {                                  warn "time = $time\n" if ($debug);
144                  $bat->{new} = ' ';  
145                                    my $hh = int( $time );
146                                    my $mm = int( ( $time - $hh ) * 60 );
147                                    my $ss = ( $time * 3600 ) % 60;
148    
149                                    $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!",
150                                            substr($state->{'charging state'},0,1),
151                                            $pcnt * 100, $hh, $mm, $ss,
152                                            $state->{'present rate'} / 1000
153                                    );
154                            }
155                    }
156          }          }
157          $i++;          $i++;
158    
# Line 85  while ( 1 ) { Line 160  while ( 1 ) {
160          chomp( $load );          chomp( $load );
161          $load =~ s!\s\d+/\d+.*!!;          $load =~ s!\s\d+/\d+.*!!;
162    
163          my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature');          my $temp = '';
164          chomp( $temp );          if ( $proc_acpi_thermal_zone_temperature ) {
165          $temp =~ s!^.*:\s+!!;                  $temp = read_file( $proc_acpi_thermal_zone_temperature );
166                    chomp( $temp );
167                    $temp =~ s!^.*:\s+!!;
168            }
169    
170          my $net = read_file('/proc/net/dev');          my $net = read_file('/proc/net/dev');
171          my ( $rx, $tx ) = ( 0,0 );          my ( $rx, $tx ) = ( 0,0 );
172    
173          foreach ( split(/\n/, $net) ) {          foreach ( split(/\n/, $net) ) {
174                  s/^\s+//;                  s/^\s+//;
175                    s/:\s*/ /;
176                  my @n = split(/\s+/, $_, 17);                  my @n = split(/\s+/, $_, 17);
177                  next unless ( $n[0] =~ m!(eth\d|ath\d):! );                  #next unless ( $n[0] =~ s!(eth\d|ath\d):!! );
178                    next unless $n[1] =~ m{^\d+$};
179    
180                  warn dump( @n ) if ($debug);                  warn dump( @n ) if ($debug);
181                  $rx += $n[1];                  $rx += $n[1];
# Line 103  while ( 1 ) { Line 183  while ( 1 ) {
183          }          }
184          warn "rx: $rx tx: $tx\n" if ($debug);          warn "rx: $rx tx: $tx\n" if ($debug);
185    
186          my $r = ( $rx - $lrx ) / $dt;          my $net_rx = ( $rx - $lrx ) / $dt;
187          my $t = ( $tx - $ltx ) / $dt;          my $net_tx = ( $tx - $ltx ) / $dt;
188          ( $lrx, $ltx ) = ( $rx, $tx );          ( $lrx, $ltx ) = ( $rx, $tx );
189    
190          printf "%s | %s |%6s >> %-6s| %s %2d%% %02d:%02d:%02d %3.1fW%s| %s\n",          my $disk = read_file('/proc/diskstats');
191                  $s,          my ( $d_r, $d_w ) = ( 0,0 );
192    
193            foreach ( split(/\n/, $disk) ) {
194                    s/^\s+//;
195                    my @d = split(/\s+/, $_, 17);
196                    next unless ( $d[2] =~ m/^[sh]d\w$/ );
197    
198                    warn dump( @d ) if ($debug);
199                    $d_r += $d[5] * $disk_blk_size;
200                    $d_w += $d[7] * $disk_blk_size;
201            }
202            warn "d_r: $d_r d_w: $d_w\n" if ($debug);
203    
204            my $d_read = ( $d_r - $ld_r ) / $dt;
205            my $d_write = ( $d_w - $ld_w ) / $dt;
206            ( $ld_r, $ld_w ) = ( $d_r, $d_w );
207    
208            my $out = join( $dzen ? ' ^fg(#333333)|^fg(#ffffff) ' : ' | ',
209                  $load,                  $load,
210                  unit( $r ), unit( $t ),                  unit( $d_read ) . ' D ' . unit( $d_write, '-' ),
211                  substr($bat->{state}->{'charging state'},0,1), $bat->{pcnt} * 100, $bat->{hh}, $bat->{mm}, $bat->{ss},                  unit( $net_rx ) . ' > ' . unit( $net_tx, '-' ),
212                          $bat->{state}->{'present rate'} / 1000, $bat->{new},                  $bat, $temp,
213                  $temp;                  $s,
214            );
215    
216    
217            print "$out\n";
218            if ( $awesome ) {
219                    open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!";
220                    print $fh
221                            $awesome == 3   ? qq{mytextbox.text="$out"\n}
222                                            : "0 widget_tell mystatusbar dwm-status text $out\n"
223                                            ;
224                    close($fh);
225            }
226    
227          sleep $dt;          sleep $dt;
228  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26