Projet

Général

Profil

Révision 14bfe7f2

ID14bfe7f243a72550737c84cd61e24ba79204d17f
Parent e8d5b844
Enfant 91710873

Ajouté par Jan Astrup il y a environ 13 ans

Added newznab plugin

Voir les différences:

plugins/newznab/nn_
1
#!/usr/bin/perl
2

  
3
=encoding utf8
4

  
5
=head1 NAME
6

  
7
nn_ - Munin plugin to display misc newznab stats.
8

  
9
=head1 CONFIGURATION
10

  
11
This script is used to generate data for several graphs. To generate
12
data for one specific graph, you need to create a symbolic link with a
13
name like nn_<GRAPH> to this script.
14

  
15
To get a graph over numbers of users use nn_users
16

  
17
=head1 LICENSE
18

  
19
Copyright (C) 2012 Jan Astrup (cryzeck@synIRC)
20

  
21
This program is free software; you can redistribute it and/or modify
22
it under the terms of the GNU General Public License as published by
23
the Free Software Foundation; version 2 dated June, 1991.
24

  
25
This program is distributed in the hope that it will be useful, but
26
WITHOUT ANY WARRANTY; without even the implied warranty of
27
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28
General Public License for more details.
29

  
30
You should have received a copy of the GNU General Public License
31
along with this program; if not, write to the Free Software
32
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
33
USA.
34

  
35
=cut
36

  
37
use warnings;
38
use strict;
39
use utf8;
40

  
41
use DBI;
42
use Data::Dumper;
43
use feature 'say';
44
use File::Basename;
45

  
46
#-- CONFIG --#
47

  
48
my %config = (
49
    'dsn'        => 'dbi:mysql:newznab',
50
    'user'       => 'test',
51
    'password'   => 'test',
52
);
53
#my %config = (
54
#    'dsn'        => $env{'mysqlconnection'} || 'dbi:mysql:newznab',
55
#    'user'       => $env{'mysqluser'}       || 'root',
56
#    'password'   => $env{'mysqlpassword'}   || '*****',
57
#);
58

  
59
my %defaults = (
60
	global_attrs => {
61
		args   => '--base 1000 -l 0',
62
		scale  => 'no',
63
	},
64
	data_source_attrs => {
65
		draw  => 'AREA',
66
	},
67
);
68

  
69
my %graphs = ();
70

  
71
$graphs{releases} = {
72
	config => {
73
		global_attrs => {
74
			title 	=> 'Releases',
75
			vlabel	=> 'Releases',
76
		},
77
		data_source_attrs => {
78
			min   => '0',
79
			
80
		},
81
	},
82
	data_sources => [
83
		{name => 'releases',		label => 'Releases'},
84
	],
85
};
86
$graphs{category} = {
87
	config => {
88
		global_attrs => {
89
			title 	=> 'Releases by category',
90
			vlabel	=> 'Releases by category',
91
		},
92
		data_source_attrs => {
93
			min   => '0',
94
			draw  => 'LINE2',
95
			type  => 'GAUGE',
96
		},
97
	},
98
	data_sources => [
99
		{name => '1000', label => 'Console'},
100
		{name => '1010', label => 'NDS'},
101
		{name => '1020', label => 'PSP'},
102
		{name => '1030', label => 'Wii'},
103
		{name => '1040', label => 'Xbox'},
104
		{name => '1050', label => 'Xbox 360'},
105
		{name => '1060', label => 'WiiWare/VC'},
106
		{name => '1070', label => 'XBOX 360 DLC'},
107
		{name => '1080', label => 'PS3'},
108
		{name => '2000', label => 'Movies'},
109
		{name => '2010', label => 'Foreign'},
110
		{name => '2020', label => 'Other'},
111
		{name => '2030', label => 'SD'},
112
		{name => '2040', label => 'HD'},
113
		{name => '2050', label => 'BluRay'},
114
		{name => '2060', label => '3D'},
115
		{name => '3000', label => 'Audio'},
116
		{name => '3010', label => 'MP3'},
117
		{name => '3020', label => 'Video'},
118
		{name => '3030', label => 'Audiobook'},
119
		{name => '3040', label => 'Lossless'},
120
		{name => '4000', label => 'PC'},
121
		{name => '4010', label => '0day'},
122
		{name => '4020', label => 'ISO'},
123
		{name => '4030', label => 'Mac'},
124
		{name => '4040', label => 'Mobile-Other'},
125
		{name => '4050', label => 'Games'},
126
		{name => '4060', label => 'Mobile-iOS'},
127
		{name => '4070', label => 'Mobile-Android'},
128
		{name => '5000', label => 'TV'},
129
		{name => '5020', label => 'Foreign'},
130
		{name => '5030', label => 'SD'},
131
		{name => '5040', label => 'HD'},
132
		{name => '5050', label => 'Other'},
133
		{name => '5060', label => 'Sport'},
134
		{name => '5070', label => 'Anime'},
135
		{name => '5080', label => 'Documentary'},
136
		{name => '6000', label => 'XXX'},
137
		{name => '6010', label => 'DVD'},
138
		{name => '6020', label => 'WMV'},
139
		{name => '6030', label => 'XviD'},
140
		{name => '6040', label => 'x264'},
141
		{name => '6050', label => 'Pack'},
142
		{name => '6060', label => 'ImgSet'},
143
		{name => '7000', label => 'Other'},
144
		{name => '7010', label => 'Misc'},
145
		{name => '7020', label => 'Ebook'},
146
		{name => '7030', label => 'Comics'},	
147
	],
148
};
149

  
150
$graphs{api} = {
151
	config => {
152
		global_attrs => {
153
			title 	=> 'API Requests / Downloads',
154
			vlabel	=> 'API / DOWNLOAD',
155
		},
156
		data_source_attrs => {
157
			min   	=> '0',
158
			draw	=> 'LINE1',
159
			type	=> 'GAUGE',
160
		},
161
	},
162
	data_sources => [
163
		{name => 'request',		label => 'API Requests'},
164
		{name => 'download',		label => 'Downloads'},
165
	],
166
};
167

  
168
$graphs{users} = {
169
	config => {
170
		global_attrs => {
171
			title 	=> 'Registered Users',
172
			vlabel	=> 'Usercount',
173
		},
174
		data_source_attrs => {
175
			min   => '0',
176
		},
177
	},
178
	data_sources => [
179
		{name => 'users',		label => 'Users'},
180
	],
181
};
182
our $data;
183
sub config {
184
    my $graph_name = shift;
185
    die 'Unknown graph ' . ($graph_name ? $graph_name : '')
186
	unless $graphs{$graph_name};
187

  
188
    my $graph = $graphs{$graph_name};
189

  
190
    my %conf = (%{$defaults{global_attrs}}, %{$graph->{config}{global_attrs}});
191
    while (my ($k, $v) = each %conf) {
192
	print "graph_$k $v\n";
193
    }
194
    print "graph_category newznab\n";
195

  
196
    my $i = 0;
197
    for my $ds (@{$graph->{data_sources}}) {
198
	my %ds_spec = (
199
	    %{$defaults{data_source_attrs}},
200
	    %{$graph->{config}{data_source_attrs}},
201
	    %$ds,
202
	);
203
	while (my ($k, $v) = each %ds_spec) {
204
	    # 'name' is only used internally in this script, not
205
	    # understood by munin.
206
	    next if ($k eq 'name');
207

  
208
	    if ($k eq 'draw' && $v eq 'AREASTACK') {
209
		printf("%s.%s %s\n",
210
		       clean_fieldname($ds->{name}), $k, ($i ? 'STACK' : 'AREA'));
211
	    }
212
	    else {
213
		printf("%s.%s %s\n", clean_fieldname($ds->{name}), $k, $v);
214
	    }
215
	    $i++;
216
	}
217
    }
218

  
219
    return 0;
220
}
221

  
222
sub main {
223
	my $graph = substr(basename($0), length('nn_'));
224
	my $command = $ARGV[0] || 'show';
225

  
226
	my %commands = (
227
		'config'	=>	\&config,
228
		'show'		=>	\&show,
229
	);
230
	
231
	die "Unknown command: $command" unless exists $commands{$command};
232
	return $commands{$command}->($graph);
233
}
234

  
235
sub show {
236
	my $graph_name = shift;
237
	die 'Unknown graph ' . ($graph_name ? $graph_name : '')
238
		unless $graphs{$graph_name};
239

  
240
	my $graph = $graphs{$graph_name};
241
	run_queries($graph_name);	
242
	for my $ds (@{$graph->{data_sources}}) {
243
		printf "%s.value %s\n", clean_fieldname($ds->{name}), ($data->{$ds->{name}} ? $data->{$ds->{name}} : '0');
244
	}
245
	return 0;
246
}
247

  
248
sub db_connect {
249
	my $dsn = "$config{dsn};mysql_connect_timeout=5";
250

  
251
	return DBI->connect($dsn, $config{user}, $config{password}, {
252
		RaiseError       => 1,
253
		PrintError       => 0,
254
		FetchHashKeyName => 'NAME_lc',
255
	});
256

  
257
}
258

  
259
sub run_queries {
260
	my $updater = shift;
261
	$data = {};
262
	my $dbh = db_connect();
263
	my %updaters = (
264
		'releases'			=> \&update_releases,
265
		'users'				=> \&update_users,
266
		'api'				=> \&update_requests,
267
		'category'			=> \&update_category,
268
	);
269
	die "Unknown updater: $updater" unless exists $updaters{$updater};
270
	$updaters{$updater}->($dbh);
271
}
272

  
273
sub update_requests {
274
	my ($dbh) = @_;                                                                                         
275
	my %queries = (
276
		request => 'select count(*) as requests from userrequests where timestamp > now() - INTERVAL 5 MINUTE;',                 
277
		download => 'select count(*) as downloads from userdownloads where timestamp > now() - INTERVAL 5 MINUTE;',                                              
278
	); 
279
	for my $name ( qw(request download) ) {
280
		my $query = $queries{$name};                                                                              
281
		my $sth = $dbh->prepare($query);                                                                
282
		$sth->execute();                                                                                
283
		while (my $row = $sth->fetch) {                                                                 
284
			$data->{$name} = $row->[0];
285
		}
286
		$sth->finish();                                                                                 
287
	}       
288
}
289
      
290
sub update_category {
291
	my ($dbh) = @_;
292
	my $sth = $dbh->prepare('select count(*) as releases, category.title, category.id from releases LEFT JOIN category ON releases.categoryID = category.ID group by releases.categoryID');
293
	$sth->execute();
294
	if ($@) { die $@; }
295
	while (my $row = $sth->fetch) {
296
	$data->{$row->[2]} = $row->[0];
297
	}
298
	
299
}
300
sub update_releases {
301
	my ($dbh) = @_;
302
	my $sth = $dbh->prepare('SELECT count(*) as releases from releases');
303
	eval {
304
		$sth->execute();
305
	};
306
	if ($@) { die $@; }
307
	my $row = $sth->fetchrow_hashref();	
308
	$data->{releases} = $row->{'releases'};
309
	$sth->finish();
310
}
311

  
312
sub update_users {
313
	my ($dbh) = @_;
314
	my $sth = $dbh->prepare('SELECT count(*) as users from users');
315
	eval {
316
		$sth->execute();
317
	};
318
	if ($@) { die $@; }
319
	my $row = $sth->fetchrow_hashref();	
320
	$data->{users} = $row->{'users'};
321
	$sth->finish();
322
}
323

  
324
sub clean_fieldname {
325
	my $name = shift;
326
	$name =~ s/^[^A-Za-z0-9_]+/_/;
327
	$name =~ s/^[0-9]+$/a$name/;
328
	$name = substr($name,-19);
329
	return $name;
330
}
331

  
332
exit main() unless caller;
333

  
334

  
335
1;

Formats disponibles : Unified diff