Projet

Général

Profil

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

root / plugins / spotweb / spotweb_catsize_average @ 8589c6df

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

1
#!/usr/bin/perl
2
#
3
# Munin plugin for the average filesize of the spots by categories in a MySQL database
4
#
5
# Copyright (C) 2011 - Rowdy Schwachofer (http://rowdy.nl)
6
#
7
# Spotweb       : http://github.com/spotweb/spotweb
8
#
9
# This program is free software: you can redistribute it and/or modify 
10
# it under the terms of the GNU General Public License as published by
11
# the Free Software Foundation, either version 3 of the License, or
12
# (at your option) any later version.
13
#
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
# GNU General Public License for more details.
18
#
19
# You should have received a copy of the GNU General Public License
20
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
#
22
#
23
# Configuration example
24
#
25
# [spotweb*]
26
# env.mysql /usr/bin/mysql                               # MySQL binary (optional)
27
# env.mysqlopts -u <MYSQL_USERNAME> -p<MYSQL_PASSWORD>   # How to connect to the database (optional if no password is set)
28
# env.database spotweb                                   # Spotweb database (optional)
29
#
30
# [spotweb_catsize_average]
31
# env.period 1800                                        # The period over which to check in seconds, default to 30 minutes (1800 seconds)
32
# env.countporn 'no'                                     # Also count porn? Default is 'yes'
33
#
34

    
35
use strict;
36

    
37

    
38
# Environment variables
39
my $MYSQL      = $ENV{mysql}      || "/usr/bin/mysql";
40
my $MYSQLOPTS  = $ENV{mysqlopts}  || "";
41
my $DATABASE   = $ENV{database}   || "spotweb";
42
my $PERIOD     = $ENV{period}     || 1800;
43
my $COUNTPORN 	= $ENV{countporn} || "yes";
44

    
45

    
46
# Local variables
47
my $FROMSTAMP  = time - $PERIOD;
48
my $TITLE      = "Spotweb filesize by Category (".($PERIOD / 60)." minute average)";
49

    
50

    
51
# Output for config
52
if(defined $ARGV[0] && $ARGV[0] eq 'config') {
53
    print <<EOC
54
graph_title $TITLE
55
graph_vlabel Size
56
graph_category other
57
cat0.label Videos/images
58
cat0.colour 008edf
59
cat1.label Music
60
cat1.colour e9a213
61
cat2.label Games
62
cat2.colour 00de00
63
cat3.label Applications
64
cat3.colour de2500
65
EOC
66
;
67
    exit 0;
68
}
69

    
70
# Output of values. We just call a subroutine here to prevent much duplicate code
71
if($COUNTPORN eq 'yes') { 
72
    &print_value(0);
73
}
74
else { 
75
# 11-04-2011 - New pr0n filters
76
#   &print_value(0, " AND subcatd NOT LIKE '%23%' AND subcatd NOT LIKE '%24%' AND subcatd NOT LIKE '%25%' AND subcatd NOT LIKE '%26%'");
77
    &print_value(0, " AND (subcatz IS NULL OR subcatz NOT LIKE '%4%') AND subcatd NOT LIKE '%23%' AND subcatd NOT LIKE '%24%' AND subcatd NOT LIKE '%25%' AND subcatd NOT LIKE '%26%' AND subcatd NOT LIKE '%72%' AND subcatd NOT LIKE '%73%' AND subcatd NOT LIKE '%74%' AND subcatd NOT LIKE '%75%' AND subcatd NOT LIKE '%76%' AND subcatd NOT LIKE '%77%' AND subcatd NOT LIKE '%78%' AND subcatd NOT LIKE '%79%' AND subcatd NOT LIKE '%80%' AND subcatd NOT LIKE '%81%' AND subcatd NOT LIKE '%82%' AND subcatd NOT LIKE '%83%' AND subcatd NOT LIKE '%84%' AND subcatd NOT LIKE '%85%' AND subcatd NOT LIKE '%86%' AND subcatd NOT LIKE '%87%' AND subcatd NOT LIKE '%88%' AND subcatd NOT LIKE '%88%' AND subcatd NOT LIKE '%89%'"); 
78
}
79
&print_value(1);
80
&print_value(2);
81
&print_value(3);
82

    
83

    
84
# Subroutine to print the values
85
sub print_value {
86
    my $spots = `$MYSQL $MYSQLOPTS -e "SELECT sum(filesize) AS size_mb FROM $DATABASE.spots WHERE stamp > $FROMSTAMP AND category = $_[0]$_[1];"`;
87
    $spots =~ /(\d+)/;
88
    if($1 eq '') {
89
        print "cat".$_[0].".value 0\n";
90
    }
91
    else {
92
        print "cat".$_[0].".value ".$1."\n"; 
93
    }
94
}