Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / rtorrent / rtom_allsessions_peers @ 51927e79

Historique | Voir | Annoter | Télécharger (4,92 ko)

1
#!/usr/bin/perl -w
2
#
3
# xmlrpc based munin plugin for monitoring rtorrent's peer count
4
# prerequisites:
5
#  - rtorrent 0.7.5 or newer compiled with --with-xmlrpc-c
6
# check http://libtorrent.rakshasa.no/wiki/RTorrentXMLRPCGuide for further information
7
#
8
# written by Gabor Hudiczius
9
# web: http://projects.cyla.homeip.net/rtwi/wiki/rTorrentOMeter
10
# email: ghudiczius@gmail.com
11
#
12
# 0.2.0 - 080619
13
#  support for scgi_port and scgi_local
14
#  configurable via munin env variables
15
#  initial release
16
#
17
#
18
# Parameters:
19
#
20
#  config    required
21
#
22
#
23
# Configurable variables
24
#
25
#  src           "socket" when using scgi_socket, or anything else when using scgi_port
26
#  socket        rTorrent's rpc socket (scgi_local)  - using scgi_local  - needed, when "src" is set to "socket"
27
#  category      Change graph category
28
#  api           use "pre09" (pre 0.9.0) or "current" (0.9.0+) API calls
29
#
30
# Configuration example
31
#
32
#       [rtom_allsessions_*]
33
#       user username
34
#       env.src socket
35
#       env.socket /home/user/torrent/.socket/rpc.socket,/home/user/torrent/.socket/rpc.socket
36
#       env.category Category
37
#       env.api current
38
#
39
#       [rtom_allsessions_*]
40
#       user username
41
#       env.port 5000,5001,5002,5003
42
#       env.category Category
43
#
44
#%# family=auto
45

    
46

    
47
if ( $ARGV[0] and $ARGV[0] eq "autoconf" ) {
48
  exit 1;
49
}
50

    
51
if ( $ARGV[0] and $ARGV[0] eq "config" ) {
52
  my $category = $ENV{"category"} || "";
53
  print "graph_title rTorrent peer statistics\n";
54
  print "graph_args --base 1000 --lower-limit 0\n";
55
  print "graph_vlabel peers\n";
56
  print "graph_category filetransfer".${category}."\n";
57
  print "outgoing.label outgoing\n";
58
  print "outgoing.draw AREA\n";
59
  print "outgoing.info number of outgoing connections\n";
60
  print "incoming.label incoming\n";
61
  print "incoming.draw STACK\n";
62
  print "incoming.info number of incoming connections\n";
63
  print "plain.label plain text\n";
64
  print "plain.draw LINE2\n";
65
  print "plain.info number of plain text connections\n";
66
  print "encrypted.label encrypted\n";
67
  print "encrypted.draw LINE2\n";
68
  print "encrypted.info number of encrypted connections\n";
69
  print "total.label total\n";
70
  print "total.draw LINE2\n";
71
  print "total.info total number of connections\n";
72
  exit 0;
73
}
74

    
75
use IO::Socket;
76

    
77
my $src         = $ENV{"src"} || "";
78
my @sockets     = split /,/, $ENV{"socket"} || "";
79
my $ip          = $ENV{"ip"} || "127.0.0.1";
80
my @ports       = split /,/, $ENV{"port"} || "";
81
my $api         = $ENV{"api"} || "pre09";
82

    
83
my $pattern  = qr/<value><(int|i4|i8|ex\.i8)>(\d+)<\/(int|i4|i8|ex\.i8)><\/value>/;
84
my $tpattern  = qr/[0-9A-F]{20}/;
85

    
86
my $line        = "";
87
if ($api =~ /pre09/) {
88
  $line         = "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>d.multicall</methodName><params><param><value><string>main</string></value></param><param><value><string>d.get_hash=</string></value></param><param><value><string>p.multicall=,p.is_encrypted=,p.is_incoming=</string></value></param></params></methodCall>";
89
} else {
90
  $line         = "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>d.multicall2</methodName><params><param><value><string></string></value></param><param><value><string>main</string></value></param><param><value><string>d.hash=</string></value></param><param><value><string>p.multicall=,p.is_encrypted=,p.is_incoming=</string></value></param></params></methodCall>";
91
}
92
my $llen  = length $line;
93
my $header  = "CONTENT_LENGTH\000${llen}\000SCGI\001\000";
94
my $hlen  = length $header;
95

    
96
my $tor = 0;
97
my $tot = 0;
98
my $enc = 0;
99
my $inc = 0;
100
my $pline = "";
101
my $ppline = "";
102
my $out = 0;
103
my $pla = 0;
104

    
105

    
106
if ( ( defined $src ) && ( $src eq "socket" ) ) {
107
  for $socket (@sockets)
108
  {
109
    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) or die;
110
    connect( SOCK, sockaddr_un( $socket ) ) or die $!;
111
    my $line = "${hlen}:${header},${line}";
112
    print SOCK $line;
113
    flush SOCK;
114
    while ( $line = <SOCK> ) {
115
      if ( $line =~ /$tpattern/ ) {
116
        $tor += 1;
117
      } elsif ( $line =~ /$pattern/ ) {
118
        $tot += 1;
119
        $enc += $2;
120
        $line = <SOCK>;
121
        $line =~ /$pattern/;
122
        $inc += $2;
123
      }
124
      $ppline = $pline;
125
      $pline = $line;
126
    }
127
    close (SOCK);
128
    $out = $out + $tot - $inc;
129
    $pla = $pla + $tot - $enc;
130
  }
131
} else {
132
  for $port (@ports)
133
  {
134
    socket( SOCK, PF_INET, SOCK_STREAM, getprotobyname( "tcp" ) );
135
    connect( SOCK, sockaddr_in( $port, inet_aton( $ip ) ) );
136
    my $line = "${hlen}:${header},${line}";
137
    print SOCK $line;
138
    flush SOCK;
139
    while ( $line = <SOCK> ) {
140
      if ( $line =~ /$tpattern/ ) {
141
        $tor += 1;
142
      } elsif ( $line =~ /$pattern/ ) {
143
        $tot += 1;
144
        $enc += $2;
145
        $line = <SOCK>;
146
        $line =~ /$pattern/;
147
        $inc += $2;
148
      }
149
      $ppline = $pline;
150
      $pline = $line;
151
    }
152
    close (SOCK);
153
    $out = $out + $tot - $inc;
154
    $pla = $pla + $tot - $enc;
155
  }
156
}
157

    
158

    
159
print "torrents.value ${tor}\ntotal.value ${tot}\nencrypted.value ${enc}\nplain.value ${pla}\nincoming.value ${inc}\noutgoing.value ${out}\n";
160

    
161
exit;