/[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 81 by dpavlin, Tue Jul 22 01:16:52 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          }          }
82    
83          if ( $v >= 1 ) {          if ( $v >= 1 ) {
84                  return sprintf("%d%s/s", $v, $units[$o]);                  return sprintf("%d%s", $v, $units[$o]);
85            } elsif ( $v == 0 ) {
86                    return '';
87          } else {          } else {
88                  return sprintf("%.1f%s/s", $v, $units[$o]);                  return sprintf("%.1f%s", $v, $units[$o]);
89          }          }
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  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 ) {
103                    if ( $proc_acpi_battery ) {
104    
105                  $bat->{state} = proc2hash( '/proc/acpi/battery/BAT0/state' );                          my $state = proc2hash( "$proc_acpi_battery/state" );
                 $bat->{info} = proc2hash( '/proc/acpi/battery/BAT0/info' );  
   
                 $bat->{pcnt} = $bat->{state}->{'remaining capacity'} / $bat->{info}->{'design capacity'};  
   
                 my $time = ( $bat->{info}->{'design capacity'} - $bat->{state}->{'remaining capacity'} ) / $bat->{state}->{'present rate'};  
   
                 warn "time = $time\n" if ($debug);  
106    
107                  $bat->{hh} = int( $time );                          if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) {
108                  $bat->{mm} = int( ( $time - $bat->{hh} ) * 60 );                                  my $info = proc2hash( "$proc_acpi_battery/info" );
                 $bat->{ss} = ( $time * 3600 ) % 60;  
109    
110                  $bat->{new} = '!';                                  my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};
111    
112                                    my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
113                                    $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' );
114    
115                                    warn "time = $time\n" if ($debug);
116    
117                                    my $hh = int( $time );
118                                    my $mm = int( ( $time - $hh ) * 60 );
119                                    my $ss = ( $time * 3600 ) % 60;
120    
121                                    $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ",
122                                            substr($state->{'charging state'},0,1),
123                                            $pcnt * 100, $hh, $mm, $ss,
124                                            $state->{'present rate'} / 1000
125                                    );
126                            }
127                    } elsif ( -e $sys_fs ) {
128                            my $full = read_file( "$sys_fs/charge_full" );
129                            my $now = read_file( "$sys_fs/charge_now" );
130                            $bat = sprintf("%2d%% | ", $now * 100 / $full );
131                    }
132          } else {          } else {
133                  $bat->{new} = ' ';                  $bat =~ s/!(\|\s)$/ $1/;
134          }          }
135          $i++;          $i++;
136    
# Line 85  while ( 1 ) { Line 138  while ( 1 ) {
138          chomp( $load );          chomp( $load );
139          $load =~ s!\s\d+/\d+.*!!;          $load =~ s!\s\d+/\d+.*!!;
140    
141          my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature');          my $temp = '';
142          chomp( $temp );          if ( $proc_acpi_thermal_zone_temperature ) {
143          $temp =~ s!^.*:\s+!!;                  $temp = read_file( $proc_acpi_thermal_zone_temperature );
144                    chomp( $temp );
145                    $temp =~ s!^.*:\s+!!;
146            }
147    
148          my $net = read_file('/proc/net/dev');          my $net = read_file('/proc/net/dev');
149          my ( $rx, $tx ) = ( 0,0 );          my ( $rx, $tx ) = ( 0,0 );
150    
151          foreach ( split(/\n/, $net) ) {          foreach ( split(/\n/, $net) ) {
152                  s/^\s+//;                  s/^\s+//;
153                    s/:\s+/:/;
154                  my @n = split(/\s+/, $_, 17);                  my @n = split(/\s+/, $_, 17);
155                  next unless ( $n[0] =~ m!(eth\d|ath\d):! );                  next unless ( $n[0] =~ s!(eth\d|ath\d):!! );
156    
157                  warn dump( @n ) if ($debug);                  warn dump( @n ) if ($debug);
158                  $rx += $n[1];                  $rx += $n[0];
159                  $tx += $n[9];                  $tx += $n[8];
160          }          }
161          warn "rx: $rx tx: $tx\n" if ($debug);          warn "rx: $rx tx: $tx\n" if ($debug);
162    
163          my $r = ( $rx - $lrx ) / $dt;          my $net_rx = ( $rx - $lrx ) / $dt;
164          my $t = ( $tx - $ltx ) / $dt;          my $net_tx = ( $tx - $ltx ) / $dt;
165          ( $lrx, $ltx ) = ( $rx, $tx );          ( $lrx, $ltx ) = ( $rx, $tx );
166    
167          printf "%s | %s |%6s >> %-6s| %s %2d%% %02d:%02d:%02d %3.1fW%s| %s\n",          my $disk = read_file('/proc/diskstats');
168            my ( $d_r, $d_w ) = ( 0,0 );
169    
170            foreach ( split(/\n/, $disk) ) {
171                    s/^\s+//;
172                    my @d = split(/\s+/, $_, 17);
173                    next unless ( $d[2] =~ m/^[sh]d\w$/ );
174    
175                    warn dump( @d ) if ($debug);
176                    $d_r += $d[5] * $disk_blk_size;
177                    $d_w += $d[7] * $disk_blk_size;
178            }
179            warn "d_r: $d_r d_w: $d_w\n" if ($debug);
180    
181            my $d_read = ( $d_r - $ld_r ) / $dt;
182            my $d_write = ( $d_w - $ld_w ) / $dt;
183            ( $ld_r, $ld_w ) = ( $d_r, $d_w );
184    
185            my $out = sprintf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n",
186                  $s,                  $s,
187                  $load,                  $load,
188                  unit( $r ), unit( $t ),                  unit( $d_read ), unit( $d_write ),
189                  substr($bat->{state}->{'charging state'},0,1), $bat->{pcnt} * 100, $bat->{hh}, $bat->{mm}, $bat->{ss},                  unit( $net_rx ), unit( $net_tx ),
190                          $bat->{state}->{'present rate'} / 1000, $bat->{new},                  $bat, $temp,
191                  $temp;          );
192    
193            print $out;
194            if ( $awesome ) {
195                    open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!";
196                    print $fh "0 widget_tell mystatusbar dwm-status text $out\n";
197                    close($fh);
198            }
199    
200          sleep $dt;          sleep $dt;
201  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26