-
Notifications
You must be signed in to change notification settings - Fork 1
/
opentracker_
executable file
·378 lines (315 loc) · 11.6 KB
/
opentracker_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/perl
#
=head1 OPENTRACKER PLUGIN
A Plugin to monitor OpenTracker Servers and their Performance
=head1 MUNIN CONFIGURATION
[opentracker*]
env.host 127.0.0.1 *default*
env.port 6969 *default*
env.uri /stats *default*
=head2 MUNIN ENVIRONMENT CONFIGURATION EXPLANATION
host = opentracker host to connect to
port = opentracker http port to connect to
uri = stats uri for appending requests for data
I need this information so I can later build the full url which normally
looks like the following example when put together:
http://127.0.0.1:6969/stats?mode=conn
=head1 AUTHOR
Matt West < https://github.com/mhwest13/OpenTracker-Munin-Plugin >
=head1 LICENSE
GPLv2
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf suggest
=cut
use strict;
use warnings;
use File::Basename;
use LWP::UserAgent;
if (basename($0) !~ /^opentracker_/) {
print "This script needs to be named opentracker_ and have symlinks which start the same.\n";
exit 1;
}
my $host = $ENV{host} || '127.0.0.1';
my $port = $ENV{port} || 6969;
my $uri = $ENV{uri} || '/stats';
=head1 Graph Declarations
This block of code builds up all of the graph info for all root / sub graphs.
%graphs is a container for all of the graph definition information. In here is where you'll
find the configuration information for munin's graphing procedure.
Format:
$graph{graph_name} => {
config => {
{ key => value }, You'll find the main graph config stored here.
{ ... },
},
keys => [ 'Name', 'Name', 'Name', ... ], Used for building results set.
datasrc => [
# Name: name given to data value
# Attr: Attribute for given value, attribute must be valid plugin argument
{ name => 'Name', info => 'info about graph' },
{ ... },
],
results => {
{ key => value }, You'll find the results info from fetch_stats call stored here.
{ ... },
},
}
=cut
my %graphs;
# graph for connections
$graphs{conn} = {
config => {
args => '--lower-limit 0',
vlabel => 'Connections',
category => 'opentracker',
title => 'Current Connections',
info => 'Current Connections to OpenTracker',
},
keys => [ 'Requests', 'Announces' ],
datasrc => [
{ name => 'Requests', label => 'Requests', min => '0', type => 'COUNTER', info => 'number of Requests', draw => 'AREA' },
{ name => 'Announces', label => 'Announces', min => '0', type => 'COUNTER', info => 'number of Announces', draw => 'LINE2' },
],
};
# graph for peers
$graphs{peer} = {
config => {
args => '--lower-limit 0',
vlabel => 'Peers',
category => 'opentracker',
title => 'Peers and Seeders',
info => 'Current Peer and Seeder Connections',
},
keys => [ 'Peers', 'Seeders' ],
datasrc => [
{ name => 'Peers', label => 'Peers', min => '0', type => 'GAUGE', info => 'current number of leechers & seeders (peers)', draw => 'AREA' },
{ name => 'Seeders', label => 'Seeders', min => '0', type => 'GAUGE', info => 'current number of seeders', draw => 'LINE2' },
],
};
# graph for scrapes
$graphs{scrp} = {
config => {
args => '--lower-limit 0',
vlabel => 'Scrapes',
category => 'opentracker',
title => 'Scrapes',
info => 'Number of Scrapes (TCP/UDP)',
},
keys => [ 'TCP', 'UDP' ],
datasrc => [
{ name => 'TCP', label => 'TCP Requests', min => '0', type => 'COUNTER', info => 'number of scrapes requested via tcp', draw => 'AREASTACK' },
{ name => 'UDP', label => 'UDP Requests', min => '0', type => 'COUNTER', info => 'number of scrapes requested via udp', draw => 'AREA' },
],
};
# graph for livesyncs
$graphs{syncs} = {
config => {
args => '--lower-limit 0',
vlabel => 'Syncs',
category => 'opentracker',
title => 'LiveSyncs',
info => 'OpenTracker LiveSync Requests',
},
keys => [ 'Incoming', 'Outgoing' ],
datasrc => [
{ name => 'Incoming', label => 'Incoming Syncs', min => '0', type => 'COUNTER', info => 'number of Incoming Syncs', draw => 'AREA' },
{ name => 'Outgoing', label => 'Outgoing Syncs', min => '0', type => 'COUNTER', info => 'number of Outgoing Syncs', draw => 'LINE2' },
],
};
# graph for tcp4 connections
$graphs{tcp4} = {
config => {
args => '--lower-limit 0',
vlabel => 'TCP4 Requests',
category => 'opentracker',
title => 'TCP4 Requests',
info => 'Current TCP4 Requests / Announces',
},
keys => [ 'Requests', 'Announces' ],
datasrc => [
{ name => 'Requests', label => 'Requests', min => '0', type => 'COUNTER', info => 'number of tcp4 Requests', draw => 'AREA' },
{ name => 'Announces', label => 'Announces', min => '0', type => 'COUNTER', info => 'number of tcp4 Announces', draw => 'LINE2' },
],
};
# graph for torrents
$graphs{torr} = {
config => {
args => '--lower-limit 0',
vlabel => '# of Torrents',
category => 'opentracker',
title => 'Torrents',
info => 'Current number of Torrents',
},
keys => [ 'Torrents' ],
datasrc => [
{ name => 'Torrents', label => 'Torrents', min => '0', type => 'GAUGE', info => 'number of torrents', draw => 'AREA' },
],
};
# graph for udp4 connections
$graphs{udp4} = {
config => {
args => '--lower-limit 0',
vlabel => 'UDP4 Requests',
category => 'opentracker',
title => 'UDP4 Requests',
info => 'Current UDP4 Requests / Announces',
},
keys => [ 'Requests', 'Announces' ],
datasrc => [
{ name => 'Requests', label => 'Requests', min => '0', type => 'COUNTER', info => 'number of udp4 Requests', draw => 'AREA' },
{ name => 'Announces', label => 'Announces', min => '0', type => 'COUNTER', info => 'number of udp4 Announces', draw => 'LINE2' },
],
};
=head1 Munin Checks
These checks look for config / autoconf / suggest params
=head2 Config Check
This block of code looks at the argument that is possibly supplied,
should it be config, it then checks to make sure the plugin
specified exists, assuming it does, it will run the do_config
subroutine for the plugin specified, otherwise it dies complaining
about an unknown plugin.
=cut
if (defined $ARGV[0] && $ARGV[0] eq 'config') {
# Lets take the plugin from the execution name.
$0 =~ /opentracker_(.+)*/;
my $plugin = $1;
# And lets make sure we have a plugin called that.
die 'Unknown Plugin Specified: ' . ($plugin ? $plugin : '') unless $graphs{$plugin};
# Now lets go ahead and print out our config.
print_config($plugin);
exit 0;
}
=head2 Autoconf Check
This block of code looks at the argument that is possibly supplied,
should it be autoconf, we are going to print yes at this point since
we've already tested for our binary to exist and be executable, the
process will then exit.
=cut
if (defined $ARGV[0] && $ARGV[0] eq 'autoconf') {
# well we can execute the binary, so lets make sure we can curl opentracker
my $url = "http://".$host.":".$port.$uri."\?mode=version";
my $ua = LWP::UserAgent->new;
$ua->timeout(15);
my $response = $ua->get($url);
if ($response->is_success) {
print "yes\n";
exit 0;
} else {
print "no: unable to connect to url: $url\n";
exit 1;
}
}
=head2 Suggest Check
This block of code looks at the argument that is possibly supplied,
should it be suggest, we are going to print the possible plugins
which can be specified.
=cut
if (defined $ARGV[0] && $ARGV[0] eq 'suggest') {
# well we can execute the binary, so print possible plugin names
my @rootplugins = ('conn','peer','scrp','syncs','tcp4','torr','udp4');
foreach my $plugin (@rootplugins) {
print "$plugin\n";
}
exit 0;
}
=head1 Subroutines
Begin Subroutine calls to output data / config information
=head2 fetch_output
This subroutine is the main call for printing data for the plugin.
No parameters are taken as this is the default call if no arguments
are supplied from the command line.
=cut
fetch_output();
sub fetch_output {
# Lets figure out what plugin they want to run, and check that it exists
$0 =~ /opentracker_(.+)*/;
my $plugin = $1;
die 'Unknown Plugin Specified: ' . ($plugin ? $plugin : '') unless $graphs{$plugin};
# Lets print out the data for our plugin
print_output($plugin);
return;
}
=head2 print_output
This block of code prints out the return values for our graphs. It takes
one parameter $plugin. Returns when completed
$plugin; graph we are calling up to print data values for
Example: print_output($plugin);
=cut
sub print_output {
# Lets get our plugin, set our graph information, and print for Munin to process
my ($plugin) = (@_);
my $graph = $graphs{$plugin};
print "graph opentracker_$plugin\n";
# Getting keys to pass to fetch_stats for data retrieval
# call up fetch_stats with the keys we just got.
my @keys = @{$graph->{keys}};
fetch_stats($plugin,@keys);
# print the results for the keys with the name for Munin to process
foreach my $dsrc (@{$graph->{datasrc}}) {
my $output = 0;
my %datasrc = %$dsrc;
while ( my ($key, $value) = each(%datasrc)) {
next if ($key ne 'name');
print "$dsrc->{name}.value $graph->{results}->{$value}\n";
}
}
return;
}
=head2 print_config
This subroutine prints out the main config information for all of the graphs.
It takes one parameters, $plugin
$plugin; graph being called up to print config for
Example: print_config($plugin);
=cut
sub print_config {
# Lets get our plugin and graph, after that print for Munin to process it.
my ($plugin) = (@_);
my $graph = $graphs{$plugin};
print "graph opentracker_$plugin\n";
# Lets print out graph's main config info.
my %graphconf = %{$graph->{config}};
while ( my ($key, $value) = each(%graphconf)) {
print "graph_$key $value\n";
}
# Lets print our graphs per graph config info.
foreach my $dsrc (@{$graph->{datasrc}}) {
my %datasrc = %$dsrc;
while ( my ($key, $value) = each(%datasrc)) {
next if ($key eq 'name');
print "$dsrc->{name}.$key $value\n";
}
}
return;
}
=head2 fetch_stats
This subroutine actually fetches data from opentracker with the plugin specified
It will then parse the data using the keys assigned in an array.
Two parameters are passed, $plugin and @keys, and it will return when complete.
$plugin; graph we are calling up, we use this to store the results in the hash
for easy recall later.
@keys; keys we want the values for from opentracker stats url.
Example: fetch_stats($plugin,@keys);
=cut
sub fetch_stats {
# Lets get our current plugin and list of keys we want info for, as well as reference our graph
my ($plugin,@keys) = (@_);
my $graph = $graphs{$plugin};
# Lets create our url to fetch
my $url = "http://".$host.":".$port.$uri."\?mode=".$plugin;
my $ua = LWP::UserAgent->new;
$ua->timeout(15);
my $response = $ua->get($url);
# Lets print some info since we got back some info
if ($response->is_success) {
my @tmparray = split("\n",$response->content);
foreach my $key (@keys) {
my $value = shift(@tmparray);
$graph->{results}->{$key} = $value;
}
} else {
print "Unable to Fetch data from URL: $url\n";
exit 1;
}
return;
}