root / plugins / newznab / nn_ @ 91710873
Historique | Voir | Annoter | Télécharger (8,44 ko)
| 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' => $env{'mysqlconnection'} || 'dbi:mysql:newznab',
|
| 50 |
'user' => $env{'mysqluser'} || 'test',
|
| 51 |
'password' => $env{'mysqlpassword'} || 'test',
|
| 52 |
); |
| 53 |
|
| 54 |
my %defaults = ( |
| 55 |
global_attrs => {
|
| 56 |
args => '--base 1000 -l 0', |
| 57 |
scale => 'no', |
| 58 |
}, |
| 59 |
data_source_attrs => {
|
| 60 |
draw => 'AREA', |
| 61 |
}, |
| 62 |
); |
| 63 |
|
| 64 |
my %graphs = (); |
| 65 |
|
| 66 |
$graphs{releases} = {
|
| 67 |
config => {
|
| 68 |
global_attrs => {
|
| 69 |
title => 'Releases', |
| 70 |
vlabel => 'Releases', |
| 71 |
}, |
| 72 |
data_source_attrs => {
|
| 73 |
min => '0', |
| 74 |
|
| 75 |
}, |
| 76 |
}, |
| 77 |
data_sources => [ |
| 78 |
{name => 'releases', label => 'Releases'},
|
| 79 |
], |
| 80 |
}; |
| 81 |
$graphs{category} = {
|
| 82 |
config => {
|
| 83 |
global_attrs => {
|
| 84 |
title => 'Releases by category', |
| 85 |
vlabel => 'Releases by category', |
| 86 |
}, |
| 87 |
data_source_attrs => {
|
| 88 |
min => '0', |
| 89 |
draw => 'LINE2', |
| 90 |
type => 'GAUGE', |
| 91 |
}, |
| 92 |
}, |
| 93 |
data_sources => [ |
| 94 |
{name => '1000', label => 'Console'},
|
| 95 |
{name => '1010', label => 'NDS'},
|
| 96 |
{name => '1020', label => 'PSP'},
|
| 97 |
{name => '1030', label => 'Wii'},
|
| 98 |
{name => '1040', label => 'Xbox'},
|
| 99 |
{name => '1050', label => 'Xbox 360'},
|
| 100 |
{name => '1060', label => 'WiiWare/VC'},
|
| 101 |
{name => '1070', label => 'XBOX 360 DLC'},
|
| 102 |
{name => '1080', label => 'PS3'},
|
| 103 |
{name => '2000', label => 'Movies'},
|
| 104 |
{name => '2010', label => 'Foreign'},
|
| 105 |
{name => '2020', label => 'Other'},
|
| 106 |
{name => '2030', label => 'SD'},
|
| 107 |
{name => '2040', label => 'HD'},
|
| 108 |
{name => '2050', label => 'BluRay'},
|
| 109 |
{name => '2060', label => '3D'},
|
| 110 |
{name => '3000', label => 'Audio'},
|
| 111 |
{name => '3010', label => 'MP3'},
|
| 112 |
{name => '3020', label => 'Video'},
|
| 113 |
{name => '3030', label => 'Audiobook'},
|
| 114 |
{name => '3040', label => 'Lossless'},
|
| 115 |
{name => '4000', label => 'PC'},
|
| 116 |
{name => '4010', label => '0day'},
|
| 117 |
{name => '4020', label => 'ISO'},
|
| 118 |
{name => '4030', label => 'Mac'},
|
| 119 |
{name => '4040', label => 'Mobile-Other'},
|
| 120 |
{name => '4050', label => 'Games'},
|
| 121 |
{name => '4060', label => 'Mobile-iOS'},
|
| 122 |
{name => '4070', label => 'Mobile-Android'},
|
| 123 |
{name => '5000', label => 'TV'},
|
| 124 |
{name => '5020', label => 'Foreign'},
|
| 125 |
{name => '5030', label => 'SD'},
|
| 126 |
{name => '5040', label => 'HD'},
|
| 127 |
{name => '5050', label => 'Other'},
|
| 128 |
{name => '5060', label => 'Sport'},
|
| 129 |
{name => '5070', label => 'Anime'},
|
| 130 |
{name => '5080', label => 'Documentary'},
|
| 131 |
{name => '6000', label => 'XXX'},
|
| 132 |
{name => '6010', label => 'DVD'},
|
| 133 |
{name => '6020', label => 'WMV'},
|
| 134 |
{name => '6030', label => 'XviD'},
|
| 135 |
{name => '6040', label => 'x264'},
|
| 136 |
{name => '6050', label => 'Pack'},
|
| 137 |
{name => '6060', label => 'ImgSet'},
|
| 138 |
{name => '7000', label => 'Other'},
|
| 139 |
{name => '7010', label => 'Misc'},
|
| 140 |
{name => '7020', label => 'Ebook'},
|
| 141 |
{name => '7030', label => 'Comics'},
|
| 142 |
], |
| 143 |
}; |
| 144 |
|
| 145 |
$graphs{api} = {
|
| 146 |
config => {
|
| 147 |
global_attrs => {
|
| 148 |
title => 'API Requests / Downloads', |
| 149 |
vlabel => 'API / DOWNLOAD', |
| 150 |
}, |
| 151 |
data_source_attrs => {
|
| 152 |
min => '0', |
| 153 |
draw => 'LINE1', |
| 154 |
type => 'GAUGE', |
| 155 |
}, |
| 156 |
}, |
| 157 |
data_sources => [ |
| 158 |
{name => 'request', label => 'API Requests'},
|
| 159 |
{name => 'download', label => 'Downloads'},
|
| 160 |
], |
| 161 |
}; |
| 162 |
|
| 163 |
$graphs{users} = {
|
| 164 |
config => {
|
| 165 |
global_attrs => {
|
| 166 |
title => 'Registered Users', |
| 167 |
vlabel => 'Usercount', |
| 168 |
}, |
| 169 |
data_source_attrs => {
|
| 170 |
min => '0', |
| 171 |
}, |
| 172 |
}, |
| 173 |
data_sources => [ |
| 174 |
{name => 'users', label => 'Users'},
|
| 175 |
], |
| 176 |
}; |
| 177 |
our $data; |
| 178 |
sub config {
|
| 179 |
my $graph_name = shift; |
| 180 |
die 'Unknown graph ' . ($graph_name ? $graph_name : '') |
| 181 |
unless $graphs{$graph_name};
|
| 182 |
|
| 183 |
my $graph = $graphs{$graph_name};
|
| 184 |
|
| 185 |
my %conf = (%{$defaults{global_attrs}}, %{$graph->{config}{global_attrs}});
|
| 186 |
while (my ($k, $v) = each %conf) {
|
| 187 |
print "graph_$k $v\n"; |
| 188 |
} |
| 189 |
print "graph_category newznab\n"; |
| 190 |
|
| 191 |
my $i = 0; |
| 192 |
for my $ds (@{$graph->{data_sources}}) {
|
| 193 |
my %ds_spec = ( |
| 194 |
%{$defaults{data_source_attrs}},
|
| 195 |
%{$graph->{config}{data_source_attrs}},
|
| 196 |
%$ds, |
| 197 |
); |
| 198 |
while (my ($k, $v) = each %ds_spec) {
|
| 199 |
# 'name' is only used internally in this script, not |
| 200 |
# understood by munin. |
| 201 |
next if ($k eq 'name'); |
| 202 |
|
| 203 |
if ($k eq 'draw' && $v eq 'AREASTACK') {
|
| 204 |
printf("%s.%s %s\n",
|
| 205 |
clean_fieldname($ds->{name}), $k, ($i ? 'STACK' : 'AREA'));
|
| 206 |
} |
| 207 |
else {
|
| 208 |
printf("%s.%s %s\n", clean_fieldname($ds->{name}), $k, $v);
|
| 209 |
} |
| 210 |
$i++; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
sub main {
|
| 218 |
my $graph = substr(basename($0), length('nn_'));
|
| 219 |
my $command = $ARGV[0] || 'show'; |
| 220 |
|
| 221 |
my %commands = ( |
| 222 |
'config' => \&config, |
| 223 |
'show' => \&show, |
| 224 |
); |
| 225 |
|
| 226 |
die "Unknown command: $command" unless exists $commands{$command};
|
| 227 |
return $commands{$command}->($graph);
|
| 228 |
} |
| 229 |
|
| 230 |
sub show {
|
| 231 |
my $graph_name = shift; |
| 232 |
die 'Unknown graph ' . ($graph_name ? $graph_name : '') |
| 233 |
unless $graphs{$graph_name};
|
| 234 |
|
| 235 |
my $graph = $graphs{$graph_name};
|
| 236 |
run_queries($graph_name); |
| 237 |
for my $ds (@{$graph->{data_sources}}) {
|
| 238 |
printf "%s.value %s\n", clean_fieldname($ds->{name}), ($data->{$ds->{name}} ? $data->{$ds->{name}} : '0');
|
| 239 |
} |
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
sub db_connect {
|
| 244 |
my $dsn = "$config{dsn};mysql_connect_timeout=5";
|
| 245 |
|
| 246 |
return DBI->connect($dsn, $config{user}, $config{password}, {
|
| 247 |
RaiseError => 1, |
| 248 |
PrintError => 0, |
| 249 |
FetchHashKeyName => 'NAME_lc', |
| 250 |
}); |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
sub run_queries {
|
| 255 |
my $updater = shift; |
| 256 |
$data = {};
|
| 257 |
my $dbh = db_connect(); |
| 258 |
my %updaters = ( |
| 259 |
'releases' => \&update_releases, |
| 260 |
'users' => \&update_users, |
| 261 |
'api' => \&update_requests, |
| 262 |
'category' => \&update_category, |
| 263 |
); |
| 264 |
die "Unknown updater: $updater" unless exists $updaters{$updater};
|
| 265 |
$updaters{$updater}->($dbh);
|
| 266 |
} |
| 267 |
|
| 268 |
sub update_requests {
|
| 269 |
my ($dbh) = @_; |
| 270 |
my %queries = ( |
| 271 |
request => 'select count(*) as requests from userrequests where timestamp > now() - INTERVAL 5 MINUTE;', |
| 272 |
download => 'select count(*) as downloads from userdownloads where timestamp > now() - INTERVAL 5 MINUTE;', |
| 273 |
); |
| 274 |
for my $name ( qw(request download) ) {
|
| 275 |
my $query = $queries{$name};
|
| 276 |
my $sth = $dbh->prepare($query); |
| 277 |
$sth->execute(); |
| 278 |
while (my $row = $sth->fetch) {
|
| 279 |
$data->{$name} = $row->[0];
|
| 280 |
} |
| 281 |
$sth->finish(); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
sub update_category {
|
| 286 |
my ($dbh) = @_; |
| 287 |
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');
|
| 288 |
$sth->execute(); |
| 289 |
if ($@) { die $@; }
|
| 290 |
while (my $row = $sth->fetch) {
|
| 291 |
$data->{$row->[2]} = $row->[0];
|
| 292 |
} |
| 293 |
|
| 294 |
} |
| 295 |
sub update_releases {
|
| 296 |
my ($dbh) = @_; |
| 297 |
my $sth = $dbh->prepare('SELECT count(*) as releases from releases');
|
| 298 |
eval {
|
| 299 |
$sth->execute(); |
| 300 |
}; |
| 301 |
if ($@) { die $@; }
|
| 302 |
my $row = $sth->fetchrow_hashref(); |
| 303 |
$data->{releases} = $row->{'releases'};
|
| 304 |
$sth->finish(); |
| 305 |
} |
| 306 |
|
| 307 |
sub update_users {
|
| 308 |
my ($dbh) = @_; |
| 309 |
my $sth = $dbh->prepare('SELECT count(*) as users from users');
|
| 310 |
eval {
|
| 311 |
$sth->execute(); |
| 312 |
}; |
| 313 |
if ($@) { die $@; }
|
| 314 |
my $row = $sth->fetchrow_hashref(); |
| 315 |
$data->{users} = $row->{'users'};
|
| 316 |
$sth->finish(); |
| 317 |
} |
| 318 |
|
| 319 |
sub clean_fieldname {
|
| 320 |
my $name = shift; |
| 321 |
$name =~ s/^[^A-Za-z0-9_]+/_/; |
| 322 |
$name =~ s/^[0-9]+$/a$name/; |
| 323 |
$name = substr($name,-19); |
| 324 |
return $name; |
| 325 |
} |
| 326 |
|
| 327 |
exit main() unless caller; |
| 328 |
|
| 329 |
|
| 330 |
1; |
