Projet

Général

Profil

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

root / plugins / moodle / moodle_files @ eaf6c2d7

Historique | Voir | Annoter | Télécharger (4,02 ko)

1
#!/usr/bin/php
2
<?php
3
/**
4
 * Moodle files
5
 * Munin plugin to count users files number and size
6
 *
7
 * It's required to define a container entry for this plugin in your
8
 * /etc/munin/plugin-conf.d/moodle.conf
9
 *
10
 * @example Example entry for configuration:
11
 * [moodle*]
12
 * env.type mysql
13
 * env.db moodle
14
 * env.user mysql_user
15
 * env.pass mysql_pass
16
 * env.host localhost
17
 * env.port 3306
18
 * env.table_prefix mdl_
19
 *
20
 * @author Arnaud Trouvé <ak4t0sh@free.fr>
21
 * @version 1.0 2014
22
 *
23
 */
24
$dbh = null;
25
$db = getenv('db');
26
$type = getenv('type');
27
$host = getenv('host');
28
$user = getenv('user');
29
$pass = getenv('pass');
30
$table_prefix = getenv('table_prefix');
31
$port = getenv('port');
32
if (!$port)
33
    $port = 3306;
34

    
35

    
36
try {
37
    $dsn = $type . ':host=' . $host . ';port=' . $port . ';dbname=' . $db;
38
    $dbh = new PDO($dsn, $user, $pass);
39
} catch (Exception $e) {
40
    echo "Connection failed\n";
41
    exit(1);
42
}
43

    
44

    
45
$data = array();
46
if (($stmt = $dbh->query("SELECT COUNT(id) as nbfiles, mimetype, SUM(filesize) as totalfilesize FROM {$table_prefix}files  WHERE filesize > 0 GROUP BY mimetype")) !== false) {
47
    foreach($stmt->fetchAll(PDO::FETCH_OBJ) as $entry) {
48
        $mimetype = preg_split('#/#', $entry->mimetype);
49
        if (count($mimetype) > 0) {
50
            if (!array_key_exists($mimetype[0], $data)) {
51
                $data[$mimetype[0]] = new stdClass();
52
                $data[$mimetype[0]]->totalfiles = 0;
53
                $data[$mimetype[0]]->filesize = 0;
54
            }
55
            $data[$mimetype[0]]->totalfiles += $entry->nbfiles;
56
            $data[$mimetype[0]]->filesize += $entry->totalfilesize;
57
        }
58
    }
59
    ksort($data);
60
}
61

    
62
if (count($argv) === 2 && $argv[1] === 'config') {
63
    echo "multigraph file_number\n";
64
    echo "graph_title Moodle Files Number\n";
65
    echo "graph_args --base 1000 --lower-limit 0\n";
66
    echo "graph_vlabel files\n";
67
    echo "graph_category cms\n";
68
    echo "graph_scale no\n";
69
    echo "graph_total total\n";
70
    echo "graph_info Displays the total number of moodle users files and repartition by type\n";
71

    
72
    $draw = "AREA";
73
    foreach (array_keys($data) as $mimetype) {
74
        echo $mimetype."_number.label $mimetype\n";
75
        echo $mimetype."_number.min 0\n";
76
        echo $mimetype."_number.draw $draw\n";
77
        $draw = "STACK";
78
    }
79
    echo "multigraph file_size\n";
80
    echo "graph_title Moodle Files Size\n";
81
    echo "graph_args --base 1024 --lower-limit 0\n";
82
    echo "graph_vlabel size\n";
83
    echo "graph_category cms\n";
84
    echo "graph_total total\n";
85
    echo "graph_info Displays the total size of moodle users files and repartition by type\n";
86

    
87
    $draw = "AREA";
88
    foreach (array_keys($data) as $mimetype) {
89
        echo $mimetype."_size.label $mimetype\n";
90
        echo $mimetype."_size.min 0\n";
91
        echo $mimetype."_size.draw $draw\n";
92
        $draw = "STACK";
93
    }
94
    exit(0);
95
}
96

    
97
echo "multigraph file_number\n";
98
echo "graph_title Moodle Files Number\n";
99
echo "graph_args --base 1000 --lower-limit 0\n";
100
echo "graph_vlabel files\n";
101
echo "graph_category cms\n";
102
echo "graph_scale no\n";
103
echo "graph_total total\n";
104
echo "graph_info Displays the total number of moodle users files and repartition by type\n";
105

    
106
$draw = "AREA";
107
foreach (array_keys($data) as $mimetype) {
108
    echo $mimetype."_number.label $mimetype\n";
109
    echo $mimetype."_number.min 0\n";
110
    echo $mimetype."_number.draw $draw\n";
111
    $draw = "STACK";
112
}
113
foreach ($data as $mimetype => $entry) {
114
    echo $mimetype."_number.value ".$entry->totalfiles."\n";
115
}
116
echo "multigraph file_size\n";
117
echo "graph_title Moodle Files Size\n";
118
echo "graph_args --base 1024 --lower-limit 0\n";
119
echo "graph_vlabel size\n";
120
echo "graph_category cms\n";
121
echo "graph_total total\n";
122
echo "graph_info Displays the total size of moodle users files and repartition by type\n";
123

    
124
$draw = "AREA";
125
foreach (array_keys($data) as $mimetype) {
126
    echo $mimetype."_size.label $mimetype\n";
127
    echo $mimetype."_size.min 0\n";
128
    echo $mimetype."_size.draw $draw\n";
129
    $draw = "STACK";
130
}
131
foreach ($data as $mimetype => $entry) {
132
    echo $mimetype."_size.value ".$entry->filesize."\n";
133
}