/[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

Contents of /trunk/dwm-status.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 38 - (show annotations)
Tue May 29 11:44:46 2007 UTC (16 years, 10 months ago) by dpavlin
File MIME type: text/plain
File size: 3160 byte(s)
fix charging estimate (but lust design capacity instead of last full
capacity so numbers will be bigger than reported by acpi, but hopefully more
correct ;-)

1 #!/usr/bin/perl -w
2
3 # dwm-status.pl
4 #
5 # 05/26/07 23:08:31 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
6
7 use strict;
8 use POSIX qw/strftime/;
9 use File::Slurp;
10 use Time::HiRes;
11 use Data::Dump qw/dump/;
12
13 my $dt = 3;
14 my $acpi_every = 10;
15
16 my $disk_blk_size = 512;
17
18 my $debug = shift @ARGV;
19
20 $|=1;
21
22 sub proc2hash {
23 my $f = shift;
24 open(my $fh, '<', $f) || die "can't open $f: $!";
25 my $h;
26 while(<$fh>) {
27 chomp;
28 my ( $key, $value ) = split(/:\s+/, $_, 2);
29 $value =~ s/ m[VW]h*$//;
30 $h->{$key} = $value;
31 }
32 warn "$f ",dump( $h ) if ( $debug );
33 return $h;
34 }
35
36 sub unit {
37 my $v = shift;
38
39 warn "unit( $v )\n" if ($debug);
40
41 my @units = qw/b k M G/;
42 my $o = 0;
43
44 while ( ( $v / 1024 ) >= 1 ) {
45 $o++;
46 $v /= 1024;
47 }
48
49 if ( $v >= 1 ) {
50 return sprintf("%d%s", $v, $units[$o]);
51 } elsif ( $v == 0 ) {
52 return '';
53 } else {
54 return sprintf("%.1f%s", $v, $units[$o]);
55 }
56 }
57
58 my ( $lrx, $ltx ) = ( 0, 0 );
59 my ( $ld_r, $ld_w ) = ( 0, 0 );
60 my $bat;
61
62 my $i = 0;
63
64 while ( 1 ) {
65 my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());
66
67 if ( $i % $acpi_every == 0 ) {
68
69 my $state = proc2hash( '/proc/acpi/battery/BAT0/state' );
70
71 if ( $state->{'present rate'} != 0 ) {
72 my $info = proc2hash( '/proc/acpi/battery/BAT0/info' );
73
74 my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};
75
76 my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
77 $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' );
78
79 warn "time = $time\n" if ($debug);
80
81 my $hh = int( $time );
82 my $mm = int( ( $time - $hh ) * 60 );
83 my $ss = ( $time * 3600 ) % 60;
84
85 $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ",
86 substr($state->{'charging state'},0,1),
87 $pcnt * 100, $hh, $mm, $ss,
88 $state->{'present rate'} / 1000
89 );
90
91 } else {
92 $bat = '';
93 }
94 } else {
95 $bat =~ s/!(\|\s)$/ $1/;
96 }
97 $i++;
98
99 my $load = read_file('/proc/loadavg');
100 chomp( $load );
101 $load =~ s!\s\d+/\d+.*!!;
102
103 my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature');
104 chomp( $temp );
105 $temp =~ s!^.*:\s+!!;
106
107 my $net = read_file('/proc/net/dev');
108 my ( $rx, $tx ) = ( 0,0 );
109
110 foreach ( split(/\n/, $net) ) {
111 s/^\s+//;
112 s/:\s+/:/;
113 my @n = split(/\s+/, $_, 17);
114 next unless ( $n[0] =~ s!(eth\d|ath\d):!! );
115
116 warn dump( @n ) if ($debug);
117 $rx += $n[0];
118 $tx += $n[8];
119 }
120 warn "rx: $rx tx: $tx\n" if ($debug);
121
122 my $net_rx = ( $rx - $lrx ) / $dt;
123 my $net_tx = ( $tx - $ltx ) / $dt;
124 ( $lrx, $ltx ) = ( $rx, $tx );
125
126 my $disk = read_file('/proc/diskstats');
127 my ( $d_r, $d_w ) = ( 0,0 );
128
129 foreach ( split(/\n/, $disk) ) {
130 s/^\s+//;
131 my @d = split(/\s+/, $_, 17);
132 next unless ( $d[2] =~ m/^[sh]d\w$/ );
133
134 warn dump( @d ) if ($debug);
135 $d_r += $d[5] * $disk_blk_size;
136 $d_w += $d[7] * $disk_blk_size;
137 }
138 warn "d_r: $d_r d_w: $d_w\n" if ($debug);
139
140 my $d_read = ( $d_r - $ld_r ) / $dt;
141 my $d_write = ( $d_w - $ld_w ) / $dt;
142 ( $ld_r, $ld_w ) = ( $d_r, $d_w );
143
144 printf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n",
145 $s,
146 $load,
147 unit( $d_read ), unit( $d_write ),
148 unit( $net_rx ), unit( $net_tx ),
149 $bat, $temp,
150 );
151
152 sleep $dt;
153 }
154

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26