Projet

Général

Profil

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

root / plugins / other / opentracker_ @ 09b88141

Historique | Voir | Annoter | Télécharger (11,6 ko)

1
#!/usr/bin/perl
2
#
3
=head1 NAME
4

    
5
A Plugin to monitor OpenTracker Servers and their Performance
6

    
7
=head1 MUNIN CONFIGURATION
8

    
9
 [opentracker*]
10
 env.host 127.0.0.1      *default*
11
 env.port 6969           *default*
12
 env.uri /stats          *default*
13

    
14
=head2 MUNIN ENVIRONMENT CONFIGURATION EXPLANATION
15

    
16
 host = opentracker host to connect to
17
 port = opentracker http port to connect to
18
 uri = stats uri for appending requests for data
19

    
20
 I need this information so I can later build the full url which normally
21
 looks like the following example when put together:
22
    http://127.0.0.1:6969/stats?mode=conn
23

    
24
=head1 AUTHOR
25

    
26
Matt West < https://github.com/mhwest13/OpenTracker-Munin-Plugin >
27

    
28
=head1 LICENSE
29

    
30
GPLv2
31

    
32
=head1 MAGIC MARKERS
33

    
34
 #%# family=auto
35
 #%# capabilities=autoconf suggest
36

    
37
=cut
38

    
39
use strict;
40
use warnings;
41

    
42
use File::Basename;
43
use LWP::UserAgent;
44

    
45
if (basename($0) !~ /^opentracker_/) {
46
    print "This script needs to be named opentracker_ and have symlinks which start the same.\n";
47
    exit 1;
48
}
49

    
50
my $host = $ENV{host} || '127.0.0.1';
51
my $port = $ENV{port} || 6969;
52
my $uri = $ENV{uri} || '/stats';
53

    
54
=head1 Graph Declarations
55

    
56
    This block of code builds up all of the graph info for all root / sub graphs.
57

    
58
    %graphs   is a container for all of the graph definition information. In here is where you'll
59
           find the configuration information for munin's graphing procedure.
60
    Format:
61

    
62
    $graph{graph_name} => {
63
        config => {
64
            { key => value }, You'll find the main graph config stored here.
65
            { ... },
66
        },
67
        keys => [ 'Name', 'Name', 'Name', ... ], Used for building results set.
68
        datasrc => [
69
            # Name: name given to data value
70
            # Attr: Attribute for given value, attribute must be valid plugin argument
71
            { name => 'Name', info => 'info about graph' },
72
            { ... },
73
        ],
74
        results => {
75
            { key => value }, You'll find the results info from fetch_stats call stored here.
76
            { ... },
77
        },
78
    }
79

    
80
=cut
81

    
82
my %graphs;
83

    
84
# graph for connections
85
$graphs{conn} = {
86
    config => {
87
        args => '--lower-limit 0',
88
        vlabel => 'Connections',
89
        category => 'filetransfer',
90
        title => 'Current Connections',
91
        info => 'Current Connections to OpenTracker',
92
    },
93
    keys => [ 'Requests', 'Announces' ],
94
    datasrc => [
95
        { name => 'Requests', label => 'Requests', min => '0', type => 'COUNTER', info => 'number of Requests', draw => 'AREA' },
96
        { name => 'Announces', label => 'Announces', min => '0', type => 'COUNTER', info => 'number of Announces', draw => 'LINE2' },
97
    ],
98
};
99
# graph for peers
100
$graphs{peer} = {
101
    config => {
102
        args => '--lower-limit 0',
103
        vlabel => 'Peers',
104
        category => 'opentracker',
105
        title => 'Peers and Seeders',
106
        info => 'Current Peer and Seeder Connections',
107
    },
108
    keys => [ 'Peers', 'Seeders' ],
109
    datasrc => [
110
        { name => 'Peers', label => 'Peers', min => '0', type => 'GAUGE', info => 'current number of leechers & seeders (peers)', draw => 'AREA' },
111
        { name => 'Seeders', label => 'Seeders', min => '0', type => 'GAUGE', info => 'current number of seeders', draw => 'LINE2' },
112
    ],
113
};
114
# graph for scrapes
115
$graphs{scrp} = {
116
    config => {
117
        args => '--lower-limit 0',
118
        vlabel => 'Scrapes',
119
        category => 'opentracker',
120
        title => 'Scrapes',
121
        info => 'Number of Scrapes (TCP/UDP)',
122
    },
123
    keys => [ 'TCP', 'UDP' ],
124
    datasrc => [
125
        { name => 'TCP', label => 'TCP Requests', min => '0', type => 'COUNTER', info => 'number of scrapes requested via tcp', draw => 'AREASTACK' },
126
        { name => 'UDP', label => 'UDP Requests', min => '0', type => 'COUNTER', info => 'number of scrapes requested via udp', draw => 'AREA' },
127
    ],
128
};
129
# graph for livesyncs
130
$graphs{syncs} = {
131
    config => {
132
        args => '--lower-limit 0',
133
        vlabel => 'Syncs',
134
        category => 'opentracker',
135
        title => 'LiveSyncs',
136
        info => 'OpenTracker LiveSync Requests',
137
    },
138
    keys => [ 'Incoming', 'Outgoing' ],
139
    datasrc => [
140
        { name => 'Incoming', label => 'Incoming Syncs', min => '0', type => 'COUNTER', info => 'number of Incoming Syncs', draw => 'AREA' },
141
        { name => 'Outgoing', label => 'Outgoing Syncs', min => '0', type => 'COUNTER', info => 'number of Outgoing Syncs', draw => 'LINE2' },
142
    ],
143
};
144
# graph for tcp4 connections
145
$graphs{tcp4} = {
146
    config => {
147
        args => '--lower-limit 0',
148
        vlabel => 'TCP4 Requests',
149
        category => 'opentracker',
150
        title => 'TCP4 Requests',
151
        info => 'Current TCP4 Requests / Announces',
152
    },
153
    keys => [ 'Requests', 'Announces' ],
154
    datasrc => [
155
        { name => 'Requests', label => 'Requests', min => '0', type => 'COUNTER', info => 'number of tcp4 Requests', draw => 'AREA' },
156
        { name => 'Announces', label => 'Announces', min => '0', type => 'COUNTER', info => 'number of tcp4 Announces', draw => 'LINE2' },
157
    ],
158
};
159
# graph for torrents
160
$graphs{torr} = {
161
    config => {
162
        args => '--lower-limit 0',
163
        vlabel => '# of Torrents',
164
        category => 'opentracker',
165
        title => 'Torrents',
166
        info => 'Current number of Torrents',
167
    },
168
    keys => [ 'Torrents' ],
169
    datasrc => [
170
        { name => 'Torrents', label => 'Torrents', min => '0', type => 'GAUGE', info => 'number of torrents', draw => 'AREA' },
171
    ],
172
};
173
# graph for udp4 connections
174
$graphs{udp4} = {
175
    config => {
176
        args => '--lower-limit 0',
177
        vlabel => 'UDP4 Requests',
178
        category => 'opentracker',
179
        title => 'UDP4 Requests',
180
        info => 'Current UDP4 Requests / Announces',
181
    },
182
    keys => [ 'Requests', 'Announces' ],
183
    datasrc => [
184
        { name => 'Requests', label => 'Requests', min => '0', type => 'COUNTER', info => 'number of udp4 Requests', draw => 'AREA' },
185
        { name => 'Announces', label => 'Announces', min => '0', type => 'COUNTER', info => 'number of udp4 Announces', draw => 'LINE2' },
186
    ],
187
};
188

    
189
=head1 Munin Checks
190

    
191
    These checks look for config / autoconf / suggest params
192

    
193
=head2 Config Check
194

    
195
    This block of code looks at the argument that is possibly supplied,
196
    should it be config, it then checks to make sure the plugin
197
    specified exists, assuming it does, it will run the do_config
198
    subroutine for the plugin specified, otherwise it dies complaining
199
    about an unknown plugin.
200

    
201
=cut
202

    
203
if (defined $ARGV[0] && $ARGV[0] eq 'config') {
204
    # Lets take the plugin from the execution name.
205
    $0 =~ /opentracker_(.+)*/;
206
    my $plugin = $1;
207
    # And lets make sure we have a plugin called that.
208
    die 'Unknown Plugin Specified: ' . ($plugin ? $plugin : '') unless $graphs{$plugin};
209
    # Now lets go ahead and print out our config.
210
    print_config($plugin);
211
	exit 0;
212
}
213

    
214
=head2 Autoconf Check
215

    
216
    This block of code looks at the argument that is possibly supplied,
217
    should it be autoconf, we are going to print yes at this point since
218
    we've already tested for our binary to exist and be executable, the
219
    process will then exit.
220

    
221
=cut
222

    
223
if (defined $ARGV[0] && $ARGV[0] eq 'autoconf') {
224
    # well we can execute the binary, so lets make sure we can curl opentracker
225
    my $url = "http://".$host.":".$port.$uri."\?mode=version";
226
    my $ua = LWP::UserAgent->new;
227
    $ua->timeout(15);
228
    my $response = $ua->get($url);
229
    if ($response->is_success) {
230
        print "yes\n";
231
    } else {
232
        print "no: unable to connect to url: $url\n";
233
    }
234
    exit 0;
235
}
236

    
237
=head2 Suggest Check
238

    
239
    This block of code looks at the argument that is possibly supplied,
240
    should it be suggest, we are going to print the possible plugins
241
    which can be specified.
242

    
243
=cut
244

    
245
if (defined $ARGV[0] && $ARGV[0] eq 'suggest') {
246
    # well we can execute the binary, so print possible plugin names
247
    my @rootplugins = ('conn','peer','scrp','syncs','tcp4','torr','udp4');
248
    foreach my $plugin (@rootplugins) {
249
        print "$plugin\n";
250
    }
251
    exit 0;
252
}
253

    
254
=head1 Subroutines
255

    
256
    Begin Subroutine calls to output data / config information
257

    
258
=head2 fetch_output
259

    
260
    This subroutine is the main call for printing data for the plugin.
261
    No parameters are taken as this is the default call if no arguments
262
    are supplied from the command line.
263

    
264
=cut
265

    
266
fetch_output();
267

    
268
sub fetch_output {
269
    # Lets figure out what plugin they want to run, and check that it exists
270
    $0 =~ /opentracker_(.+)*/;
271
    my $plugin = $1;
272
    die 'Unknown Plugin Specified: ' . ($plugin ? $plugin : '') unless $graphs{$plugin};
273
    # Lets print out the data for our plugin
274
    print_output($plugin);
275
    return;
276
}
277

    
278
=head2 print_output
279

    
280
    This block of code prints out the return values for our graphs. It takes
281
    one parameter $plugin. Returns when completed
282

    
283
        $plugin;    graph we are calling up to print data values for
284

    
285
    Example: print_output($plugin);
286

    
287
=cut
288

    
289
sub print_output {
290
    # Lets get our plugin, set our graph information, and print for Munin to process
291
    my ($plugin) = (@_);
292
    my $graph = $graphs{$plugin};
293
    print "graph opentracker_$plugin\n";
294
    # Getting keys to pass to fetch_stats for data retrieval
295
    # call up fetch_stats with the keys we just got.
296
    my @keys = @{$graph->{keys}};
297
    fetch_stats($plugin,@keys);
298
    # print the results for the keys with the name for Munin to process
299
    foreach my $dsrc (@{$graph->{datasrc}}) {
300
        my $output = 0;
301
        my %datasrc = %$dsrc;
302
        while ( my ($key, $value) = each(%datasrc)) {
303
            next if ($key ne 'name');
304
            print "$dsrc->{name}.value $graph->{results}->{$value}\n";
305
        }
306
    }
307
    return;
308
}
309

    
310
=head2 print_config
311

    
312
    This subroutine prints out the main config information for all of the graphs.
313
    It takes one parameters, $plugin
314

    
315
        $plugin;    graph being called up to print config for
316

    
317
    Example:  print_config($plugin);
318

    
319
=cut
320

    
321
sub print_config {
322
    # Lets get our plugin and graph, after that print for Munin to process it.
323
    my ($plugin) = (@_);
324
    my $graph = $graphs{$plugin};
325
    print "graph opentracker_$plugin\n";
326
    # Lets print out graph's main config info.
327
    my %graphconf = %{$graph->{config}};
328
    while ( my ($key, $value) = each(%graphconf)) {
329
        print "graph_$key $value\n";
330
    }
331
    # Lets print our graphs per graph config info.
332
    foreach my $dsrc (@{$graph->{datasrc}}) {
333
        my %datasrc = %$dsrc;
334
        while ( my ($key, $value) = each(%datasrc)) {
335
            next if ($key eq 'name');
336
            print "$dsrc->{name}.$key $value\n";
337
        }
338
    }
339
    return;
340
}
341

    
342
=head2 fetch_stats
343

    
344
    This subroutine actually fetches data from opentracker with the plugin specified
345
    It will then parse the data using the keys assigned in an array.
346
    Two parameters are passed, $plugin and @keys, and it will return when complete.
347

    
348
        $plugin;    graph we are calling up, we use this to store the results in the hash
349
                    for easy recall later.
350
        @keys;      keys we want the values for from opentracker stats url.
351

    
352
    Example: fetch_stats($plugin,@keys);
353

    
354
=cut
355

    
356
sub fetch_stats {
357
    # Lets get our current plugin and list of keys we want info for, as well as reference our graph
358
    my ($plugin,@keys) = (@_);
359
    my $graph = $graphs{$plugin};
360
    # Lets create our url to fetch
361
    my $url = "http://".$host.":".$port.$uri."\?mode=".$plugin;
362
    my $ua = LWP::UserAgent->new;
363
    $ua->timeout(15);
364
    my $response = $ua->get($url);
365
    # Lets print some info since we got back some info
366
    if ($response->is_success) {
367
        my @tmparray = split("\n",$response->content);
368
        foreach my $key (@keys) {
369
            my $value = shift(@tmparray);
370
            $graph->{results}->{$key} = $value;
371
        }
372
    } else {
373
        print "Unable to Fetch data from URL: $url\n";
374
        exit 1;
375
    }
376
    return;
377
}