/[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 89 - (show annotations)
Tue Sep 16 18:10:06 2008 UTC (15 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 4634 byte(s)
move /sysfs before depricated /proc/acpi

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 my $awesome = 1;
20
21 $|=1;
22
23 sub proc2hash {
24 my $f = shift;
25 return unless -f $f;
26 open(my $fh, '<', $f) || die "can't open $f: $!";
27 my $h;
28 while(<$fh>) {
29 chomp;
30 my ( $key, $value ) = split(/:\s+/, $_, 2);
31 $value =~ s/ m[VW]h*$//;
32 $h->{$key} = $value;
33 }
34 warn "$f ",dump( $h ) if ( $debug );
35 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 {
71 my $v = shift;
72
73 warn "unit( $v )\n" if ($debug);
74
75 my @units = qw/b k M G/;
76 my $o = 0;
77
78 while ( ( $v / 10000 ) >= 1 ) {
79 $o++;
80 $v /= 1024;
81 }
82
83 if ( $v >= 1 ) {
84 return sprintf("%d%s", $v, $units[$o]);
85 } elsif ( $v == 0 ) {
86 return '';
87 } else {
88 return sprintf("%.1f%s", $v, $units[$o]);
89 }
90 }
91
92 my ( $lrx, $ltx ) = ( 0, 0 );
93 my ( $ld_r, $ld_w ) = ( 0, 0 );
94 my $bat = '';
95
96 my $i = 0;
97 my $sys_fs = '/sys/class/power_supply/BAT0';
98
99 while ( 1 ) {
100 my $s = strftime("%Y-%m-%d %H:%M:%S", localtime());
101
102 if ( $i % $acpi_every == 0 ) {
103 if ( -e $sys_fs ) {
104
105 my $full = read_file( "$sys_fs/charge_full" );
106 my $now = read_file( "$sys_fs/charge_now" );
107 $bat = sprintf("%2d%% | ", $now * 100 / $full );
108
109 } elsif ( $proc_acpi_battery ) {
110
111 my $state = proc2hash( "$proc_acpi_battery/state" );
112
113 if ( $state->{'present rate'} =~ m/^\d+$/ && $state->{'present rate'} != 0 ) {
114 my $info = proc2hash( "$proc_acpi_battery/info" );
115
116 my $pcnt = $state->{'remaining capacity'} / $info->{'design capacity'};
117
118 my $time = $state->{'remaining capacity'} / ( $state->{'present rate'} );
119 $time = ( $info->{'design capacity'} - $state->{'remaining capacity'} ) / $state->{'present rate'} if ( $state->{'charging state'} eq 'charging' );
120
121 warn "time = $time\n" if ($debug);
122
123 my $hh = int( $time );
124 my $mm = int( ( $time - $hh ) * 60 );
125 my $ss = ( $time * 3600 ) % 60;
126
127 $bat = sprintf("%s %2d%% %02d:%02d:%02d %3.1fW!| ",
128 substr($state->{'charging state'},0,1),
129 $pcnt * 100, $hh, $mm, $ss,
130 $state->{'present rate'} / 1000
131 );
132 }
133 }
134 } else {
135 $bat =~ s/!(\|\s)$/ $1/;
136 }
137 $i++;
138
139 my $load = read_file('/proc/loadavg');
140 chomp( $load );
141 $load =~ s!\s\d+/\d+.*!!;
142
143 my $temp = '';
144 if ( $proc_acpi_thermal_zone_temperature ) {
145 $temp = read_file( $proc_acpi_thermal_zone_temperature );
146 chomp( $temp );
147 $temp =~ s!^.*:\s+!!;
148 }
149
150 my $net = read_file('/proc/net/dev');
151 my ( $rx, $tx ) = ( 0,0 );
152
153 foreach ( split(/\n/, $net) ) {
154 s/^\s+//;
155 s/:\s+/:/;
156 my @n = split(/\s+/, $_, 17);
157 next unless ( $n[0] =~ s!(eth\d|ath\d):!! );
158
159 warn dump( @n ) if ($debug);
160 $rx += $n[0];
161 $tx += $n[8];
162 }
163 warn "rx: $rx tx: $tx\n" if ($debug);
164
165 my $net_rx = ( $rx - $lrx ) / $dt;
166 my $net_tx = ( $tx - $ltx ) / $dt;
167 ( $lrx, $ltx ) = ( $rx, $tx );
168
169 my $disk = read_file('/proc/diskstats');
170 my ( $d_r, $d_w ) = ( 0,0 );
171
172 foreach ( split(/\n/, $disk) ) {
173 s/^\s+//;
174 my @d = split(/\s+/, $_, 17);
175 next unless ( $d[2] =~ m/^[sh]d\w$/ );
176
177 warn dump( @d ) if ($debug);
178 $d_r += $d[5] * $disk_blk_size;
179 $d_w += $d[7] * $disk_blk_size;
180 }
181 warn "d_r: $d_r d_w: $d_w\n" if ($debug);
182
183 my $d_read = ( $d_r - $ld_r ) / $dt;
184 my $d_write = ( $d_w - $ld_w ) / $dt;
185 ( $ld_r, $ld_w ) = ( $d_r, $d_w );
186
187 my $out = sprintf("%s | %s |%5s D %-5s|%5s > %-5s| %s%s\n",
188 $s,
189 $load,
190 unit( $d_read ), unit( $d_write ),
191 unit( $net_rx ), unit( $net_tx ),
192 $bat, $temp,
193 );
194
195 print $out;
196 if ( $awesome ) {
197 open(my $fh, '|-', 'awesome-client') || die "can't pipe to awesome-client: $!";
198 print $fh "0 widget_tell mystatusbar dwm-status text $out\n";
199 close($fh);
200 }
201
202 sleep $dt;
203 }
204

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26