root / plugins / network / tor-bandwidth-usage @ dd4afac8
Historique | Voir | Annoter | Télécharger (4,26 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
# |
| 3 |
# tor-bandwidth-usage - munin plugin to monitor Tor traffic |
| 4 |
# |
| 5 |
# To use this plugin you need the following: |
| 6 |
# o Enable accounting on torrc configuration file (even if you dont want to limit bandwidth usage, |
| 7 |
# just put a huge value for on AccountingMax) |
| 8 |
# example: |
| 9 |
# AccountingStart day 12:00 |
| 10 |
# AccountingMax 100 GB |
| 11 |
# o Enable CookieAuthentication (CookieAuthentication 1 in torrc) or define a HashedControlPassword |
| 12 |
# o Add something like the following to /etc/munin/plugin-conf.d/munin-node: |
| 13 |
# [tor-bandwidth-usage] |
| 14 |
# user debian-tor |
| 15 |
# env.cookiefile /var/run/tor/control.authcookie |
| 16 |
# |
| 17 |
# |
| 18 |
# tested with Tor releases: 0.2.1.28, 0.2.1.29, 0.2.2.35 |
| 19 |
# |
| 20 |
# Author: tazoi <dev AT tazoi DOT it>, based on a plugin by Ævar Arnfjörð Bjarmason <avarab@gmail.com> |
| 21 |
# |
| 22 |
# Parameters understood (defined in file /etc/munin/plugin-conf.d/munin-node or in environment) |
| 23 |
# host - Change which host to graph (default localhost) |
| 24 |
# port - Change which port to connect to (default 9051) |
| 25 |
# password - Plain-text control channel password (see torrc |
| 26 |
# HashedControlPassword parameter) |
| 27 |
# cookiefile - Name of the file containing the control channel cookie |
| 28 |
# (see torrc CookieAuthentication parameter) |
| 29 |
# |
| 30 |
# Using HashedControlPassword authentication has the problem that you |
| 31 |
# must include the plain-text password in the munin config file. To |
| 32 |
# have any effect, that file shouldn't be world-readable. |
| 33 |
# |
| 34 |
# If you're using CookieAuthentication, you should run this plugin as |
| 35 |
# a user which has read access to the tor datafiles. Also note that |
| 36 |
# bugs in versions upto and including 0.1.1.20 prevent |
| 37 |
# CookieAuthentication from working. |
| 38 |
# |
| 39 |
# Usage: place in /etc/munin/plugins (or link it there using ln -s) |
| 40 |
# |
| 41 |
#%# family=contrib |
| 42 |
#%# capabilities=autoconf |
| 43 |
|
| 44 |
use strict; |
| 45 |
use feature ':5.10'; |
| 46 |
use IO::Socket::INET; |
| 47 |
use Munin::Plugin; |
| 48 |
|
| 49 |
# Config |
| 50 |
my $address = $ENV{host} || "localhost";
|
| 51 |
my $port = $ENV{port} || 9051;
|
| 52 |
|
| 53 |
# Don't edit below this line |
| 54 |
|
| 55 |
sub Authenticate |
| 56 |
{
|
| 57 |
my ($socket) = @_; |
| 58 |
my $authline = "AUTHENTICATE"; |
| 59 |
if (defined($ENV{cookiefile})) {
|
| 60 |
if (open(COOKIE, "<$ENV{cookiefile}")) {
|
| 61 |
my $cookie; |
| 62 |
binmode COOKIE; |
| 63 |
read(COOKIE, $cookie, 32); |
| 64 |
close COOKIE; |
| 65 |
$authline .= ' "' . $cookie . '"'; |
| 66 |
} |
| 67 |
} elsif (defined($ENV{password})) {
|
| 68 |
$authline .= ' "' . $ENV{password} . '"';
|
| 69 |
} |
| 70 |
say $socket "$authline"; |
| 71 |
my $replyline = <$socket>; |
| 72 |
if (substr($replyline, 0, 1) != '2') {
|
| 73 |
$replyline =~ s/\s*$//; |
| 74 |
return "Failed to authenticate: $replyline"; |
| 75 |
} |
| 76 |
|
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ($ARGV[0] and $ARGV[0] eq "autoconf") {
|
| 81 |
# Try to connect to the daemon |
| 82 |
my $socket = IO::Socket::INET->new("$address:$port") or my $failed = 1;
|
| 83 |
|
| 84 |
if ($failed) {
|
| 85 |
say "no (failed to connect to $address port $port)"; |
| 86 |
exit 1; |
| 87 |
} |
| 88 |
|
| 89 |
my $msg = Authenticate($socket); |
| 90 |
if (defined($msg)) {
|
| 91 |
say $socket "QUIT"; |
| 92 |
close($socket); |
| 93 |
say "no ($msg)"; |
| 94 |
exit 1; |
| 95 |
} |
| 96 |
|
| 97 |
say $socket "QUIT"; |
| 98 |
close($socket); |
| 99 |
say "yes"; |
| 100 |
exit 0; |
| 101 |
} |
| 102 |
|
| 103 |
if ($ARGV[0] and $ARGV[0] eq "config") {
|
| 104 |
say "graph_order down up"; |
| 105 |
say "graph_title Tor traffic"; |
| 106 |
say "graph_args --base 1000"; |
| 107 |
say "graph_vlabel bits in (-) / out (+) per \${graph_period}";
|
| 108 |
say "graph_category network"; |
| 109 |
say "graph_info This graph shows the traffic through this Tor node."; |
| 110 |
say "down.label received"; |
| 111 |
say "down.type DERIVE"; |
| 112 |
say 'down.graph no'; |
| 113 |
say "down.cdef down,8,*"; |
| 114 |
say "down.min 0"; |
| 115 |
say "up.label b/s"; |
| 116 |
say "up.type DERIVE"; |
| 117 |
say "up.negative down"; |
| 118 |
say "up.cdef up,8,*"; |
| 119 |
say "up.min 0"; |
| 120 |
|
| 121 |
exit 0; |
| 122 |
} |
| 123 |
|
| 124 |
my $socket = IO::Socket::INET->new("$address:$port")
|
| 125 |
or die("Couldn't connect to $address port $port: $!");
|
| 126 |
|
| 127 |
my $msg = Authenticate($socket); |
| 128 |
if (defined($msg)) {
|
| 129 |
say $socket "QUIT"; |
| 130 |
close($socket); |
| 131 |
die "$msg\n"; |
| 132 |
} |
| 133 |
|
| 134 |
say $socket "GETINFO accounting/bytes"; |
| 135 |
my $down = 0; |
| 136 |
my $up = 0; |
| 137 |
my $replyline = <$socket>; |
| 138 |
chomp($replyline); |
| 139 |
if ($replyline =~ /^250-accounting\/bytes=(\d+)\s(\d+)/) {
|
| 140 |
$down = $1; |
| 141 |
$up = $2; |
| 142 |
} else {
|
| 143 |
die "Failed to get accounting info: $replyline\n"; |
| 144 |
} |
| 145 |
|
| 146 |
say $socket "QUIT"; |
| 147 |
close($socket); |
| 148 |
|
| 149 |
say "down.value $down"; |
| 150 |
say "up.value $up"; |
| 151 |
|
| 152 |
exit 0; |
