All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
5 use vars qw($VERSION %IRSSI);
10 contact=> 'bd@bc-bd.org',
12 description=> 'Adds an item which displays the current network activity. Needs /proc/net/dev.',
13 license=> 'GPL v2 or later',
14 url=> 'https://bc-bd.org/svn/repos/irssi/nact',
21 # Currently running on Linux, OpenBsd and FreeBsd.
23 # Type this to add the item:
25 # /statusbar window add nact
31 # for more help on how to custimize your statusbar.
32 # Add something like this to your theme file to customize to look of the item
34 # nact_display = "$0%R>%n%_$1%_%G>%n$2";
36 # where $0 is the input, $1 the device and $2 the output. To customize the
37 # outout of the /bw command add something like
39 # nact_command = "$0)in:$1:out($2";
47 # This is the complete list of parameters passed to the format:
50 # $1: name of the device, eg. eth0
52 # $3: total bytes received
53 # $4: total bytes sent
54 # $5: sum of $4 and $5
60 # Make this script work on other unices, For that i need some infos on where
61 # to find the total amount of sent bytes on other systems. You may be so kind
62 # as to send me the name of such a file and a sample output of it.
66 # you can be so kind as to send me a patch. Fort that _please_ check that you
67 # do have the latest nact.pl and use these diff switches:
69 # diff -uN nact.pl.new nact.pl.old
75 # /set nact_command <command>
76 # command: command to execute on /bw. example:
78 # /set nact_command /say
80 # /set nact_devices <devices>
81 # devices: space seperated list of devices to display
83 # /set nact_interval <n>
84 # n: number of mili-seconds to wait before an update of the item
86 # /set nact_format <format>
87 # format: a format string like the one with sprintf. examples:
89 # /set nact_format %d no digits after the point at all
90 # /set nact_format %.3f 3 digits after the point
91 # /set nact_format %.5f 5 digits after the point
94 # n: set the unit to KiB, MiB, or GiB. examples:
96 # /set nact_unit 0 calculate dynamically
97 # /set nact_unit 1 set to KiB/s
98 # /set nact_unit 2 set to MiB/s
99 # /set nact_unit 3 set to GiB/s
104 my $outString = "nact...";
105 my $outCmd = "nact...";
106 my (%in,%out,$timeout,$getBytes);
108 sub getBytesLinux() {
112 open(FID, "/proc/net/dev");
121 $line =~ s/[\s:]/ /g;
122 @list = split(" ", $line);
123 $in{$list[0]} = $list[1];
124 $out{$list[0]} = $list[9];
133 open(FID, "/usr/bin/netstat -nib|");
137 @list = split(" ", $line);
138 $in{$list[0]} = $list[4];
139 $out{$list[0]} = $list[5];
149 open(FID, "/usr/bin/netstat -nib|");
152 @list = split(" ", $line);
153 next if $list[0] eq $olddev;
154 $in{$list[0]} = $list[6];
155 $out{$list[0]} = $list[9];
163 my ($what,$format,$unit) = @_;
166 # determine the effective unit, either from forcing, or from dynamically
167 # checking the size of the value
169 if ($what >= 1024*1024*1024) {
171 } elsif ($what >= 1024*1024) {
173 } elsif ($what >= 1024) {
182 if ($effective >= 3) {
183 return sprintf($format."%s", $what/(1024*1024*1024), "G");
184 } elsif ($effective == 2) {
185 return sprintf($format."%s", $what/(1024*1024), "M");
186 } elsif ($effective == 1) {
187 return sprintf($format."%s", $what/(1024), "K");
189 return sprintf($format, $what);
194 my ($item, $get_size_only) = @_;
196 $item->default_handler($get_size_only, "{sb $outString}", undef, 1);
201 my $slice = Irssi::settings_get_int('nact_interval');
202 my $format = Irssi::settings_get_str('nact_format');
203 my $unit = Irssi::settings_get_int('nact_unit');
204 my $theme = Irssi::current_theme();
213 foreach (split(" ", Irssi::settings_get_str('nact_devices'))) {
215 my $b_out = $out{$_};
216 my $deltaIn = make_kilo(($b_in -$oldIn{$_})*1000/$slice,$format,$unit);
217 my $deltaOut = make_kilo(($b_out -$oldOut{$_})*1000/$slice,$format,$unit);
218 my $i = make_kilo($b_in,$format,$unit);
219 my $o = make_kilo($b_out,$format,$unit);
220 my $s = make_kilo($b_in +$b_out,$format,$unit);
222 $out .= Irssi::current_theme->format_expand(
223 "{nact_display $deltaIn $_ $deltaOut $i $o $s}",Irssi::EXPAND_FLAG_IGNORE_REPLACES);
225 $outCmd .= Irssi::current_theme->format_expand(
226 "{nact_command $deltaIn $_ $deltaOut $i $o $s}",Irssi::EXPAND_FLAG_IGNORE_REPLACES);
229 # perhaps this usage of $out as temp variable does fix those nasty
232 Irssi::statusbar_items_redraw('nact');
236 my $slice = Irssi::settings_get_int('nact_interval');
238 Irssi::timeout_remove($timeout);
241 Irssi::print("nact.pl, ERROR nact_interval must be greater than 10");
245 $timeout = Irssi::timeout_add($slice, 'timeout_nact' , undef);
249 my ($data, $server, $witem) = @_;
251 if ($witem && ($witem->{type} eq "CHANNEL" || $witem->{type} eq "QUERY")) {
252 $witem->command(Irssi::settings_get_str('nact_command')." ".$outCmd);
254 Irssi::print("nact: command needs window of type channel or query.");
258 Irssi::command_bind('bw','cmd_bw');
260 Irssi::signal_add('setup changed','nact_setup');
263 Irssi::statusbar_item_register('nact', undef, 'sb_nact');
265 # register our os independant settings
266 Irssi::settings_add_int('misc', 'nact_interval', 10000);
267 Irssi::settings_add_str('misc', 'nact_format', '%.0f');
268 Irssi::settings_add_int('misc', 'nact_unit', 0);
269 Irssi::settings_add_str('misc', 'nact_command', 'me looks at the gauges:');
273 if ($os =~ /Linux/) {
274 Irssi::print("nact.pl, running on Linux, using /proc/net/dev");
275 $getBytes = \&getBytesLinux;
276 Irssi::settings_add_str('misc', 'nact_devices', "eth0 lo");
277 } elsif ($os =~ /OpenBSD/) {
278 Irssi::print("nact.pl, running on OpenBSD, using netstat -nbi");
279 $getBytes = \&getBytesOBSD;
280 Irssi::settings_add_str('misc', 'nact_devices', "tun0");
281 } elsif ($os =~ /FreeBSD/) {
282 Irssi::print("nact.pl, running on FreeBSD, using netstat -nbi");
283 $getBytes = \&getBytesFBSD;
284 Irssi::settings_add_str('misc', 'nact_devices', "rl0");
286 Irssi::print("nact.pl, sorry no support for OS:$os");
287 Irssi::print("nact.pl, If you know how to collect the needed data on your OS, mail me :)");
301 # - added nact_command
305 # - added FreeBSD support (by senneth)
308 # - stray ' ' in the item (reported by darix). Add a " " at the end of your
309 # nact_display if you have more than one interface listed.
312 # - added missing use Irssi::TextUI (reported by darix)
313 # - small parameter switch bug (reported by darix)
316 # - added total number of bytes sent/received
319 # - runs now from autorun/ on openbsd
320 # - changed nact_interval to mili-seconds
321 # - added nact_format, nact_unit
324 # - small typo in the docs
327 # - introduced multiple os support
328 # - added a theme thingie to make sascha happy ;)