Projet

Général

Profil

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

root / plugins / rtorrent / rtom_allsessions_mem @ 51927e79

Historique | Voir | Annoter | Télécharger (3,58 ko)

1
#!/usr/bin/perl -w
2
#
3
# xmlrpc based munin plugin for monitoring rtorrent's memory usage
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
#       ip              rTorrent's ip address                   - using scgi_port       - needed, when "src" is NOT set to "socket"
28
#       port            rTorrent's scgi port (scgi_port)        - using scgi_port       - needed, when "src" is NOT set to "socket"
29
#       category        Change graph category
30
#       api             use "pre09" (pre 0.9.0) or "current" (0.9.0+) API calls
31
#
32
# Configuration example
33
#
34
#       [rtom_allsessions_*]
35
#       user username
36
#       env.src socket
37
#       env.socket /home/user/torrent/.socket/rpc.socket,/home/user/torrent/.socket/rpc.socket
38
#       env.category Category
39
#       env.api current
40
#
41
#       [rtom_allsessions_*]
42
#       user username
43
#       env.port 5000,5001,5002,5003
44
#       env.category Category
45
#
46
#%# family=auto
47

    
48

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

    
53
if ( $ARGV[0] and $ARGV[0] eq "config" ) {
54
  my $category = $ENV{"category"} || "";
55
  print "graph_title rTorrent memory usage\n";
56
  print "graph_args --base 1024 --lower-limit 0\n";
57
  print "graph_vlabel Bytes\n";
58
  print "graph_category filetransfer".${category}."\n";
59
  print "mem.label Memory usage\n";
60
  print "mem.info Memory usage of rTorrent\n";
61
  print "mem.type GAUGE\n";
62
  print "mem.draw LINE2\n";
63
  exit 0;
64
}
65

    
66
use IO::Socket;
67
my $src         = $ENV{"src"} || "";
68
my @sockets     = split /,/, $ENV{"socket"} || "";
69
my $ip          = $ENV{"ip"} || "127.0.0.1";
70
my @ports       = split /,/, $ENV{"port"} || "";
71
my $api         = $ENV{"api"} || "pre09";
72

    
73
my $mem         = 0;
74
my $pattern     = qr/<value><(int|i4|i8|ex\.i8)>(\d+)<\/(int|i4|i8|ex\.i8)><\/value>/;
75
my $line        = "";
76
if ($api =~ /pre09/) {
77
  $line         = "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>get_memory_usage</methodName></methodCall>";
78
} else {
79
  $line         = "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>pieces.memory.current</methodName></methodCall>";
80
}
81
my $llen        = length $line;
82
my $header      = "CONTENT_LENGTH\000${llen}\000SCGI\001\000";
83
my $hlen        = length $header;
84

    
85
if ( ( defined $src ) && ( $src eq "socket" ) ) {
86
  for $socket (@sockets)
87
  {
88
    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) or die;
89
    connect( SOCK, sockaddr_un( $socket ) ) or die $!;
90
    my $line = "${hlen}:${header},${line}";
91
    print SOCK $line;
92
    flush SOCK;
93
    while ( $line = <SOCK> ) {
94
      if ( $line =~ /$pattern/ ) {
95
        $mem = $mem + $2;
96
      }
97
    }
98
    close (SOCK);
99
  }
100
} else {
101
  for $port (@ports)
102
  {
103
    socket( SOCK, PF_INET, SOCK_STREAM, getprotobyname( "tcp" ) );
104
    connect( SOCK, sockaddr_in( $port, inet_aton( $ip ) ) );
105
    my $line = "${hlen}:${header},${line}";
106
    print SOCK $line;
107
    flush SOCK;
108
    while ( $line = <SOCK> ) {
109
      if ( $line =~ /$pattern/ ) {
110
        $mem = $mem + $2;
111
      }
112
    }
113
    close (SOCK);
114
  }
115
}
116

    
117
print "mem.value ${mem}\n";
118

    
119
exit;