root / plugins / network / tor-bandwidth-usage @ dd4afac8
Historique | Voir | Annoter | Télécharger (4,26 ko)
| 1 | 5ffa28e9 | tazoi | #!/usr/bin/perl -w |
|---|---|---|---|
| 2 | # |
||
| 3 | 8ea0ef82 | Kenyon Ralph | # tor-bandwidth-usage - munin plugin to monitor Tor traffic |
| 4 | 5ffa28e9 | tazoi | # |
| 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 | 8ea0ef82 | Kenyon Ralph | # 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 | 5ffa28e9 | tazoi | # |
| 17 | # |
||
| 18 | 8ea0ef82 | Kenyon Ralph | # 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 | 5ffa28e9 | tazoi | # |
| 22 | # Parameters understood (defined in file /etc/munin/plugin-conf.d/munin-node or in environment) |
||
| 23 | 8ea0ef82 | Kenyon Ralph | # host - Change which host to graph (default localhost) |
| 24 | # port - Change which port to connect to (default 9051) |
||
| 25 | 5ffa28e9 | tazoi | # password - Plain-text control channel password (see torrc |
| 26 | 8ea0ef82 | Kenyon Ralph | # HashedControlPassword parameter) |
| 27 | 5ffa28e9 | tazoi | # cookiefile - Name of the file containing the control channel cookie |
| 28 | 8ea0ef82 | Kenyon Ralph | # (see torrc CookieAuthentication parameter) |
| 29 | 5ffa28e9 | tazoi | # |
| 30 | 8ea0ef82 | Kenyon Ralph | # 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 | 5ffa28e9 | tazoi | # |
| 34 | 8ea0ef82 | Kenyon Ralph | # 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 | 5ffa28e9 | tazoi | # |
| 39 | 8ea0ef82 | Kenyon Ralph | # Usage: place in /etc/munin/plugins (or link it there using ln -s) |
| 40 | 5ffa28e9 | tazoi | # |
| 41 | #%# family=contrib |
||
| 42 | #%# capabilities=autoconf |
||
| 43 | |||
| 44 | use strict; |
||
| 45 | 8ea0ef82 | Kenyon Ralph | use feature ':5.10'; |
| 46 | 5ffa28e9 | tazoi | use IO::Socket::INET; |
| 47 | 8ea0ef82 | Kenyon Ralph | use Munin::Plugin; |
| 48 | 5ffa28e9 | tazoi | |
| 49 | # Config |
||
| 50 | 8ea0ef82 | Kenyon Ralph | my $address = $ENV{host} || "localhost";
|
| 51 | my $port = $ENV{port} || 9051;
|
||
| 52 | 5ffa28e9 | tazoi | |
| 53 | # Don't edit below this line |
||
| 54 | |||
| 55 | sub Authenticate |
||
| 56 | {
|
||
| 57 | 8ea0ef82 | Kenyon Ralph | 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 | 5ffa28e9 | tazoi | } |
| 79 | |||
| 80 | if ($ARGV[0] and $ARGV[0] eq "autoconf") {
|
||
| 81 | 8ea0ef82 | Kenyon Ralph | # 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 | 5ffa28e9 | tazoi | } |
| 102 | |||
| 103 | if ($ARGV[0] and $ARGV[0] eq "config") {
|
||
| 104 | 8ea0ef82 | Kenyon Ralph | 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 | 5ffa28e9 | tazoi | } |
| 123 | |||
| 124 | my $socket = IO::Socket::INET->new("$address:$port")
|
||
| 125 | 8ea0ef82 | Kenyon Ralph | or die("Couldn't connect to $address port $port: $!");
|
| 126 | 5ffa28e9 | tazoi | |
| 127 | my $msg = Authenticate($socket); |
||
| 128 | if (defined($msg)) {
|
||
| 129 | 8ea0ef82 | Kenyon Ralph | say $socket "QUIT"; |
| 130 | close($socket); |
||
| 131 | die "$msg\n"; |
||
| 132 | 5ffa28e9 | tazoi | } |
| 133 | |||
| 134 | 8ea0ef82 | Kenyon Ralph | say $socket "GETINFO accounting/bytes"; |
| 135 | 5ffa28e9 | tazoi | my $down = 0; |
| 136 | my $up = 0; |
||
| 137 | my $replyline = <$socket>; |
||
| 138 | chomp($replyline); |
||
| 139 | 8ea0ef82 | Kenyon Ralph | if ($replyline =~ /^250-accounting\/bytes=(\d+)\s(\d+)/) {
|
| 140 | $down = $1; |
||
| 141 | $up = $2; |
||
| 142 | 5ffa28e9 | tazoi | } else {
|
| 143 | 8ea0ef82 | Kenyon Ralph | die "Failed to get accounting info: $replyline\n"; |
| 144 | 5ffa28e9 | tazoi | } |
| 145 | |||
| 146 | 8ea0ef82 | Kenyon Ralph | say $socket "QUIT"; |
| 147 | 5ffa28e9 | tazoi | close($socket); |
| 148 | |||
| 149 | 8ea0ef82 | Kenyon Ralph | say "down.value $down"; |
| 150 | say "up.value $up"; |
||
| 151 | 5ffa28e9 | tazoi | |
| 152 | exit 0; |
