1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#!/usr/bin/env perl
#
# Dependencies: iproute2 iw volctl
#
################################################################################
use strict;
use warnings;
use feature 'signatures';
use Time::HiRes qw<gettimeofday>;
# Turn off buffering
$| = 1;
################################################################################
# Formatting
sub pango_warning($txt, $enabled) {
return $enabled ? "<span background='red'>$txt</span>" : $txt;
}
sub pango_onoff($txt, $on) {
return $on ? $txt : "<span strikethrough='true'>$txt</span>";
}
sub txt_onoff($txt, $on) {
return $on ? $txt : "${txt}X";
}
sub render_iface_linux($is_wireless, $name, $status, $addr) {
my $extra = $addr ? " $addr" : '';
if ($is_wireless and $status eq 'UP') {
my ($ssid) = `iw dev $name link` =~ /SSID: ([^\n]*).*/;
my ($dbm) = `iw dev $name link` =~ /signal: ([0-9-]+ dBm)/;
$extra .= ' (' . $ssid . ', ' . $dbm . ')';
}
return pango_onoff($is_wireless ? '๐' : '๐', $status eq 'UP') . $extra;
}
sub render_iface_openbsd($ifaces, $ifname) {
my $iface = $ifaces->{$ifname};
my $extra = $iface->{'ip'} ? " $iface->{'ip'}" : '';
my $is_wireless = exists $iface->{'wifi'};
my $is_up = $iface->{'up'};
if ($is_wireless and $is_up) {
$extra .= ' (' . $iface->{'wifi_ssid'} . ', ' . $iface->{'wifi_signal'} . ')';
}
return txt_onoff($is_wireless ? '๐ถ' : '๐', $is_up) . $extra;
}
################################################################################
# Profiling
sub show_time_delta($section, $time0, $computed) {
my (undef, $time) = gettimeofday();
my $diff = ($time - $time0) / 1000;
my $extra = $computed ? ' (COMPUTED)' : '';
printf STDERR "Spent $diff milliseconds @ $section$extra\n";
}
################################################################################
# Modules
# open(my $loadf, '<', '/proc/loadavg');
# my %mod_cpu_linux = (
# name => 'CPU',
# period => 5,
# compute => sub {
# read($loadf, my $loads, 14);
# seek($loadf, 0, 0);
# return sprintf ("CPU %s/%s/%s", split ' ', $loads);
# }
# );
my %mod_cpu_openbsd = (
name => 'CPU',
period => 5,
compute => sub {
my $loads = `sysctl -n vm.loadavg`;
chomp $loads;
return sprintf ("CPU %s/%s/%s", split ' ', $loads);
}
);
# my %mod_mem_linux = (
# name => 'MEMORY',
# period => 5,
# compute => sub {
# my $mused = `free | awk '/^Mem:/ {printf("%d", 100 * (\$3/\$2))}'`;
# return pango_warning(sprintf("MEM %s%%", $mused), $mused > 90);
# }
# );
my %mod_mem_openbsd = (
name => 'MEMORY',
period => 5,
compute => sub {
my $uvmexp = `vmstat -s`;
my ($mtotal) = $uvmexp =~ /([0-9]+) pages managed/;
my ($mused) = $uvmexp =~ /([0-9]+) pages active/;
$mused = 100 * $mused / $mtotal;
return pango_warning(sprintf("MEM %d%%", $mused), $mused > 90);
}
);
# my %mod_net_linux = (
# name => 'NETWORK',
# period => 4,
# compute => sub {
# my @wired = ();
# my @wireless = ();
# for (split("\n", `ip --br a`)) {
# next if substr($_, 0, 2) eq 'lo'; # prevent regex
# my ($if, $status, $addr) = $_ =~ /^(\S+)\s+(\S+)(?:\s+(\S+))?/;
# push @wired, [0, $if, $status, $addr] if (substr($if, 0, 2) eq 'en');
# push @wireless, [1, $if, $status, $addr] if (substr($if, 0, 2) eq 'wl');
# }
# return join ' | ', map { render_iface_linux(@$_) } @wired, @wireless;
# }
# );
my %mod_net_openbsd = (
name => 'NETWORK',
period => 4,
compute => sub {
my @wired = ();
my @wireless = ();
my %ifaces;
my $ifname;
for (`ifconfig`) {
if (/^([a-z]+[0-9]+):/) {
$ifname = $1;
$ifaces{$ifname} = {};
$ifaces{$ifname}{'up'} = /UP/;
} elsif (/media: (.*)/) {
$ifaces{$ifname}{'media'} = $1;
} elsif (/ieee80211:.* join "([^"]*)".* ([0-9]+%)/) {
$ifaces{$ifname}{'wifi'} = 1;
$ifaces{$ifname}{'wifi_ssid'} = $1;
$ifaces{$ifname}{'wifi_signal'} = $2;
} elsif (/inet ([0-9.]+)/) {
$ifaces{$ifname}{'ip'} = $1;
}
}
foreach my $ifname (keys %ifaces) {
next if $ifname =~ /^lo[0-9]+/;
my $iface = $ifaces{$ifname};
next unless $iface->{'ip'} || $iface->{'media'};
if ($iface->{'wifi'}) {
push @wireless, $ifname;
} else {
push @wired, $ifname;
}
}
# use Data::Dumper;
# print Dumper(\%ifaces);
# print Dumper(\@wired);
# print Dumper(\@wireless);
return join ' ยท ', map { render_iface_openbsd(\%ifaces, $_) } sort(@wired), @wireless;
}
);
# my $BATDIR = glob '/sys/class/power_supply/BAT?';
# open(my $bcapf, '<', "$BATDIR/capacity");
# open(my $bstatf, '<', "$BATDIR/status");
# my %mod_bat_linux = (
# name => 'BATTERY',
# period => 3,
# compute => sub {
# my $batlevel = <$bcapf>;
# chomp $batlevel;
# seek($bcapf, 0, 0);
# my $batcharging = <$bstatf> eq "Charging\n" ? 'โก' : '';
# seek($bstatf, 0, 0);
# return pango_warning(sprintf("๐%s%s", $batlevel, $batcharging), $batlevel < 10);
# }
# );
sub obsd_get_sysctl($mib) {
my @parts = split(' ', `sysctl -n $mib`);
return $parts[0];
}
my %mod_bat_openbsd = (
name => 'BATTERY',
period => 3,
compute => sub {
my $batcharging = obsd_get_sysctl('hw.sensors.acpibat0.raw0') eq "0" ? 'โก' : '';
my $batmax = obsd_get_sysctl('hw.sensors.acpibat0.watthour0');
my $batcur = obsd_get_sysctl('hw.sensors.acpibat0.watthour3');
my $batlevel = 100 * $batcur / $batmax;
return pango_warning(sprintf("๐%d%%%s", $batlevel, $batcharging), $batlevel < 10);
}
);
# my %mod_audio_linux = (
# name => 'AUDIO',
# period => 2,
# compute => sub {
# return sprintf "%s%s",
# pango_onoff('๐', do {`volctl vol-on`; $? == 0}),
# pango_onoff('๐ค', do {`volctl mic-on`; $? == 0});
# }
# );
my %mod_audio_openbsd = (
name => 'AUDIO',
period => 2,
compute => sub {
my $audioinfo = `sndioctl`;
my ($muted) = $audioinfo =~ /output\.mute=([01])/;
my ($micmuted) = $audioinfo =~ /input\.mute=([01])/;
return sprintf "%s %s",
txt_onoff('๐', $muted == 0),
txt_onoff('๐ค', $micmuted == 0);
}
);
my %mod_date = (
name => 'DATE',
period => 2,
compute => sub {
my $date = `date +'%Y-%m-%d %H:%M'`;
chomp $date;
return $date;
}
);
################################################################################
# Main
my @mods = (\%mod_cpu_openbsd, \%mod_mem_openbsd, \%mod_net_openbsd, \%mod_bat_openbsd, \%mod_audio_openbsd, \%mod_date);
my %cache;
my $profile = 0;
foreach my $arg (@ARGV) {
if ($arg eq '--profile') {
$profile = 1;
}
}
my $counter = 0;
while (1) {
my @sections;
my (undef, $time0) = gettimeofday() if $profile;
foreach my $mod (@mods) {
my (undef, $time) = gettimeofday() if $profile;
my $result;
if ($counter % $mod->{period} == 0) {
$result = &{$mod->{compute}};
$cache{$mod->{name}} = $result;
} else {
$result = $cache{$mod->{name}};
}
push(@sections, $result);
show_time_delta($mod->{name}, $time, $counter % $mod->{period} == 0) if $profile;
}
show_time_delta('TOTAL', $time0, 0) if $profile;
printf("%s\n", join(' ยท ', @sections));
$counter += 1;
sleep 1;
}
# vim: set sw=4
|