/[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 39 - (hide annotations)
Thu Jun 14 11:56:26 2007 UTC (15 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 4141 byte(s)
wrote /proc/acpi/battery/ and /proc/acpi/thermal_zone/ auto-detection

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26