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

Annotation of /trunk/dwm-status.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (hide annotations)
Sun May 27 09:14:14 2007 UTC (15 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 2989 byte(s)
added disk stat

1 dpavlin 30 #!/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 dpavlin 31 my $acpi_every = 10;
15 dpavlin 30
16 dpavlin 35 my $disk_blk_size = 512;
17    
18 dpavlin 31 my $debug = shift @ARGV;
19 dpavlin 30
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 dump( $h ) if ( $debug );
33     return $h;
34     }
35    
36     sub unit {
37     my $v = shift;
38 dpavlin 31
39     warn "unit( $v )\n" if ($debug);
40    
41 dpavlin 35 my @units = qw/b k M G/;
42 dpavlin 30 my $o = 0;
43    
44     while ( ( $v / 1024 ) >= 1 ) {
45     $o++;
46     $v /= 1024;
47     }
48    
49 dpavlin 31 if ( $v >= 1 ) {
50 dpavlin 34 return sprintf("%d%s", $v, $units[$o]);
51 dpavlin 33 } elsif ( $v == 0 ) {
52     return '';
53 dpavlin 31 } else {
54 dpavlin 34 return sprintf("%.1f%s", $v, $units[$o]);
55 dpavlin 31 }
56 dpavlin 30 }
57    
58     my ( $lrx, $ltx ) = ( 0, 0 );
59 dpavlin 35 my ( $ld_r, $ld_w ) = ( 0, 0 );
60 dpavlin 30 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 dpavlin 32 my $state = proc2hash( '/proc/acpi/battery/BAT0/state' );
70 dpavlin 30
71 dpavlin 32 if ( $state->{'present rate'} != 0 ) {
72     my $info = proc2hash( '/proc/acpi/battery/BAT0/info' );
73 dpavlin 30
74 dpavlin 32 my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};
75 dpavlin 30
76 dpavlin 32 my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
77 dpavlin 31
78 dpavlin 32 warn "time = $time\n" if ($debug);
79 dpavlin 30
80 dpavlin 32 my $hh = int( $time );
81     my $mm = int( ( $time - $hh ) * 60 );
82     my $ss = ( $time * 3600 ) % 60;
83 dpavlin 30
84 dpavlin 32 $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ",
85     substr($state->{'charging state'},0,1),
86     $pcnt * 100, $hh, $mm, $ss,
87     $state->{'present rate'} / 1000
88     );
89    
90     } else {
91     $bat = '';
92     }
93 dpavlin 30 } else {
94 dpavlin 32 $bat =~ s/!(\|\s)$/ $1/;
95 dpavlin 30 }
96     $i++;
97    
98     my $load = read_file('/proc/loadavg');
99     chomp( $load );
100     $load =~ s!\s\d+/\d+.*!!;
101    
102     my $temp = read_file('/proc/acpi/thermal_zone/THM0/temperature');
103     chomp( $temp );
104     $temp =~ s!^.*:\s+!!;
105    
106     my $net = read_file('/proc/net/dev');
107     my ( $rx, $tx ) = ( 0,0 );
108    
109     foreach ( split(/\n/, $net) ) {
110     s/^\s+//;
111     my @n = split(/\s+/, $_, 17);
112     next unless ( $n[0] =~ m!(eth\d|ath\d):! );
113    
114     warn dump( @n ) if ($debug);
115     $rx += $n[1];
116     $tx += $n[9];
117     }
118     warn "rx: $rx tx: $tx\n" if ($debug);
119    
120 dpavlin 35 my $net_rx = ( $rx - $lrx ) / $dt;
121     my $net_tx = ( $tx - $ltx ) / $dt;
122 dpavlin 30 ( $lrx, $ltx ) = ( $rx, $tx );
123    
124 dpavlin 35 my $disk = read_file('/proc/diskstats');
125     my ( $d_r, $d_w ) = ( 0,0 );
126 dpavlin 30
127 dpavlin 35 foreach ( split(/\n/, $disk) ) {
128     s/^\s+//;
129     my @d = split(/\s+/, $_, 17);
130     next unless ( $d[2] =~ m/^[sh]d\w$/ );
131    
132     warn dump( @d ) if ($debug);
133     $d_r += $d[5] * $disk_blk_size;
134     $d_w += $d[7] * $disk_blk_size;
135     }
136     warn "d_r: $d_r d_w: $d_w\n" if ($debug);
137    
138     my $d_read = ( $d_r - $ld_r ) / $dt;
139     my $d_write = ( $d_w - $ld_w ) / $dt;
140     ( $ld_r, $ld_w ) = ( $d_r, $d_w );
141    
142     printf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n",
143     $s,
144     $load,
145     unit( $d_read ), unit( $d_write ),
146     unit( $net_rx ), unit( $net_tx ),
147     $bat, $temp,
148     );
149    
150 dpavlin 30 sleep $dt;
151     }
152    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26