summaryrefslogtreecommitdiffstats
path: root/scripts/bandwidth.pl
blob: 8c0d0fb74e36efeb5eafbbb4900e3713692319f4 (plain)
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
# Mrtg-compatible any statistic loader
#  /SET status_min_in - The minimum load to show
#  /SET status_min_in - The minimum load to show
#  /SET status_refresh - How often the loadavg is refreshed
#
#  takes output from mrtg compatible scripts, 
#  see the mrtg-contrib and mrtgutils package for scripts to load
#
#  this one requires /usr/bin/mrtg-ip-acct from mrtgutils package
#
#  TODO ; add support for more than one stat at the same time
#  TODO : negative amounts?
  
use Irssi 20011113;
use Irssi::TextUI;

use strict;
use 5.6.0;

use vars qw($VERSION %IRSSI);
    
# header begins here

$VERSION = "1.0";
%IRSSI = (
	authors     => "Riku Voipio",
	contact     => "riku.voipio\@iki.fi",
	name        => "bandwidth",
	description => "shows bandwidth usage in statusbar",
	sbitems     => "stats",
        license     => "GPLv2",
        url         => "http://nchip.ukkosenjyly.mine.nu/irssiscripts/",
    );

my ($refresh, $last_refresh, $refresh_tag) = (10);
my ($last_in, $last_out) = (0.0,0.0);
my ($min_in, $min_out) = (1.0,1.0);
my ($cur_in, $cur_out, $first_run) = (0.0,0.0,1);
my $command =  '/usr/bin/mrtg-ip-acct';


sub get_stats 
{
  my ($old_in, $old_out) = ($last_in, $last_out);

  my @localstats;
  if (open my $fh, q{-|}, $command) 
  {
     @localstats = <$fh>;
    close $fh;
  } else {
    Irssi::print("Failed to execute $command: $!", MSGLEVEL_CLIENTERROR);
  }
  
  for(@localstats[0..1]) {
    return unless defined;
    return unless /^\d+$/;
  }
  $last_in=$localstats[0];
  $last_out=$localstats[1];

  if ($old_out==0){return;}

  $cur_out=($last_out-$old_out) / ($refresh*1024);
  $cur_in=($last_in-$old_in) / ($refresh*1024);
}

sub stats {
  my ($item, $get_size_only) = @_;
  #get_stats();
  
  $min_out = Irssi::settings_get_int('stats_min_out');
  $min_in = Irssi::settings_get_int('stats_min_in');
  $min_in = 0 if $min_in < 0;
  $min_out = 0 if $min_out < 0;
  
  
  if ($cur_in < $min_in and $cur_out <$min_out){
	  #dont print
    if ($get_size_only) {
      $item->{min_size} = $item->{max_size} = 0;
    }
  } else { 
      $item->default_handler($get_size_only, undef, sprintf("i:%.2f o:%.2f",$cur_in, $cur_out ), 1 );
  }
}

sub refresh_stats {
  get_stats();
  Irssi::statusbar_items_redraw('stats');
}

sub read_settings {
  $refresh = Irssi::settings_get_int('stats_refresh');
  $command = Irssi::settings_get_str('stats_commandline');
  $refresh = 1 if $refresh < 1;
  return if $refresh == $last_refresh;
  $last_refresh = $refresh;

  Irssi::timeout_remove($refresh_tag) if $refresh_tag;
  $refresh_tag = Irssi::timeout_add($refresh*1000, 'refresh_stats', undef);
}

Irssi::settings_add_int('misc', 'stats_min_in', $min_in);
Irssi::settings_add_int('misc', 'stats_min_out', $min_out);
Irssi::settings_add_int('misc', 'stats_refresh', $refresh);
Irssi::settings_add_str('misc', 'stats_commandline', $command);

Irssi::statusbar_item_register('stats', '{sb S: $0-}', 'stats');
Irssi::statusbars_recreate_items();

read_settings();
Irssi::signal_add('setup changed', 'read_settings');