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

Legend:
Removed from v.34  
changed lines
  Added in v.80

  ViewVC Help
Powered by ViewVC 1.1.26