Script: volumeter.pl

Show volume of data on network interface in a bar.
Author: nils_2 — Version: 0.2 — License: GPL-3.0-or-later
For WeeChat ≥ 0.3.0.
Tags: info, item
Added: 2009-10-15

Download GitHub Repository

  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
# Copyright (c) 2009 by Nils Görs <weechatter@arcor.de>
#
# based on the irssi script by Riku Voipio <riku.voipio@iki.fi>
# and rewritten for weechat after a request from wishbone in irc.freenode.net/#weechat
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# v0.2:	- settings will be set on startup if not exists (suggested by xt)
#	- TiB (TeraByte) support added (suggested by xt)
#	- /proc/net/dev will be used instead of mrtg-ip-acct (suggested by xt)
# v0.1: first release.

# configuration:
# to make volumeter visible you have to add ",[volumeter]" (without "")
# in your weechat.bar.status.items

# /set plugins.var.perl.volumeter.device_name "wlan0"
# /set plugins.var.perl.volumeter.refresh_rate "1000"
# /set plugins.var.perl.volumeter.display_char "Mb"
# possible display_chars are:
# "TiB" = TeraByte
# "MiB" = MegaByte
# "KiB" = KiloByte
# "Byt" = Byte
# after changing your settings you have to "/perl reload" (without "") the script.

use strict;
my $program_name = "volumeter";
my $version = "0.2";
my $description = "shows volume usage in statusbar";
my $device = "eth0";					# standard device of mrtg-ip-acct
my $device_name = "device_name";			# name for device-setting
my $refresh_rate = "5000";
my $refresh_rate_name = "refresh_rate";			# name for refresh-setting
my $display_char = "MiB";				# TiB (Tera), GiB (Giga), MiB (Mega) KiB (Kilo) and byt (byte)
my $display_char_name = "display_char";			# name for char-setting
my $bar_output = "";
my ($last_in, $last_out) = (0.0,0.0);
my @localstats;
my $procnetdev =  "/proc/net/dev";			# name of file

# first function called by a WeeChat-script
weechat::register($program_name, "Nils Görs <weechatter\@arcor.de>", $version,
                  "GPL3", $description, "", "");

  unless (-e $procnetdev){
    weechat::print("",$procnetdev . "does not exists.");
return weechat::WEECHAT_RC_ERROR;
 
 }
  get_config();

  weechat::bar_item_new($program_name, "refresh_stats","");
  weechat::hook_timer($refresh_rate,1,0,"volumeter_update","");

return weechat::WEECHAT_RC_OK;

### sub-routines
sub get_stats
{
  my $tmp = `cat "$procnetdev" | grep "$device"`;
  $last_out = (($tmp =~ m/(\d+)/g)[9]);			# get last_out
  $last_in = (($tmp =~ m/(\d+)/g)[1]);			# get last_in
  return;
}

sub refresh_stats{
  get_stats();
  calculate_value($last_in,$last_out);
  $bar_output = $device . ": " . $bar_output;
 return $bar_output;
}
sub volumeter_update{
      weechat::bar_item_update('volumeter');
        return weechat::WEECHAT_RC_OK
}
sub calculate_value{
  my ($in_value, $out_value) = ($_[0], $_[1]);				# get argument

  if ($display_char eq "KiB"){
    $in_value = ($in_value / (1024));
    $in_value = sprintf("%.2fKiB" , $in_value);
    $out_value = ($out_value / (1024));
    $out_value = sprintf("%.2fKiB" , $out_value);
    $bar_output = "i:" . $in_value . " o:" . $out_value;
    return;
  }
  if ($display_char eq "MiB"){
    $in_value = ($in_value / (1024*1024));
    $in_value = sprintf("%.2fMiB" , $in_value);
    $out_value = ($out_value / (1024*1024));
    $out_value = sprintf("%.2fMiB" , $out_value);
    $bar_output = "i:" . $in_value . " o:" . $out_value;
    return;
  }
  if ($display_char eq "GiB"){
    $in_value = ($in_value / (1024*1024*1024));
    $in_value = sprintf("%.3fGiB" , $in_value);
    $out_value = ($out_value / (1024*1024*1024));
    $out_value = sprintf("%.3fGiB" , $out_value);
    $bar_output = "i:" . $in_value . " o:" . $out_value;
    return;
  }
  if ($display_char eq "TiB"){
    $in_value = ($in_value / (1024*1024*1024*1024));
    $in_value = sprintf("%.3fTiB" , $in_value);
    $out_value = ($out_value / (1024*1024*1024*1024));
    $out_value = sprintf("%.3fTiB" , $out_value);
    $bar_output = "i:" . $in_value . " o:" . $out_value;
    return;
  }
    $in_value = sprintf("%.0fByt" , $in_value);				# Byte value
    $out_value = sprintf("%.0fByt" , $out_value);
    $bar_output = "i:" . $in_value . " o:" . $out_value;
}

sub get_config{
	my $result_conf = weechat::config_get_plugin($refresh_rate_name);
	  if ($result_conf ne ""){
	      $refresh_rate = $result_conf;
	  }
	  else{
	      weechat::config_set_plugin($refresh_rate_name, $refresh_rate);
	  }
	$result_conf = weechat::config_get_plugin($device_name);
	  if ($result_conf ne ""){
	      $device = $result_conf;
	  }
	  else{
	      weechat::config_set_plugin($device_name, $device);
	  }
	$result_conf = weechat::config_get_plugin($display_char_name);
	  if ($result_conf ne ""){
	      $display_char = $result_conf;
	  }
	  else{
	      weechat::config_set_plugin($display_char_name, $display_char);
	  }
}