-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_live_exam_users_munin.php
executable file
·177 lines (156 loc) · 6.47 KB
/
get_live_exam_users_munin.php
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
#!/usr/bin/php
<?php
$doc = <<<CUT
=head1 NAME
chamilo_live_exams - Munin plugin to monitor the number of live users taking an exam on Chamilo portals.
=head1 APPLICABLE SYSTEMS
Chamilo 1.* web applications.
=head1 CONFIGURATION
The plugin needs access to the root web directory /var/www (or any other given
path) in order to scan for Chamilo installations. It also offers a way to
define subdirs if your Chamilo portals are generally stored under two folder
levels inside /var/www/. The plugin also needs the possibility to execute PHP
on the command line (php-cli package) and to connect to a MySQL database
(php5-mysql package).
=head1 INTERPRETATION
The plugin shows the number of users giving a live exam at any given time
for any Chamilo portal, indicating the URL of the corresponding portal.
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf
=head1 BUGS
None known of
=head1 VERSION
0.2
=head1 AUTHOR
Yannick Warnier <yannick.warnier@beeznest.com>
=head1 LICENSE
AGPLv3
=cut
CUT;
ini_set('error_reporting', 'E_ALL & ~E_WARNING & ~E_NOTICE');
$bd = '/var/www';
$sub = '/www';
$last_connect_minutes = 5;
$connections = get_connections($bd, $sub, $last_connect_minutes);
$output = '';
if (!empty($argv[1]) && $argv[1] == 'config') {
// Global Munin attr., see http://munin-monitoring.org/wiki/protocol-config
$output .= "graph_title Chamilo live exam users v2\n";
$output .= "graph_args --lower-limit 0\n";
$output .= "graph_category chamilo\n";
$output .= "graph_info This graph shows the number of users taking a live exam on Chamilo portals over time.\n";
$output .= "graph_vlabel Users in last $last_connect_minutes min\n";
$output .= "graph_scale off\n";
$total = 0;
foreach ($connections as $portal => $num) {
$total += $num;
}
$connections['Total'] = $total;
foreach ($connections as $portal => $num) {
$output .= "portal$portal.label Host $portal\n";
// echo "portal$portal.type DERIVE\n";
//echo "portal$portal.max 500\n";
// echo "portal$portal.min 0\n";
// echo "portal$portal.max 500\n";
$output .= "portal$portal.warning 8000\n";
$output .= "portal$portal.critical 12500\n";
$output .= "portal$portal.draw LINE2\n";
}
file_put_contents('/tmp/get_live_exam_users_config', $output);
exit;
}
if (is_array($connections) && count($connections) > 0) {
$total = 0;
foreach ($connections as $portal => $num) {
$total += $num;
}
$connections['Total'] = $total;
foreach ($connections as $portal => $num) {
$output .= "portal$portal.value $num\n";
}
}
file_put_contents('/tmp/get_live_exam_users', $output);
function get_connections($bd, $sub, $last_connect_minutes)
{
$match_count = 0;
$connections = array();
$exclusions = array();
$exclusionsFile = __DIR__.'/exclusions.conf';
if (is_file($exclusionsFile) && is_readable($exclusionsFile)) {
$exclusions = file($exclusionsFile, FILE_SKIP_EMPTY_LINES);
foreach ($exclusions as $i => $exclusion) {
$exclusions[$i] = trim($exclusion);
}
}
$list = scandir($bd);
foreach ($list as $dir) {
//skip system directories
if (substr($dir, 0, 1) == '.' or $dir == 'lost+found') {
continue;
}
//skip directories that are in the exclusions file
if (in_array(trim($dir), $exclusions)) {
continue;
}
//check the existence of configuration.php
$config_file = '';
if (is_file($bd.'/'.$dir.$sub.'/app/config/configuration.php')) {
// Chamilo 1.10+
$config_file = $bd.'/'.$dir.$sub.'/app/config/configuration.php';
} elseif (is_file($bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php')) {
// Chamilo 1.9
$config_file = $bd.'/'.$dir.$sub.'/main/inc/conf/configuration.php';
}
if (!empty($config_file) && is_file($config_file) && is_readable($config_file)) {
$_configuration = [];
// Virtual Chamilo plugin not supported yet, skip such portals
$configRaw = file_get_contents($config_file);
if (preg_match('/Virtual::/', $configRaw)) {
continue;
}
$inc = include_once($config_file);
if (!empty($_configuration['db_user']) && !empty($_configuration['main_database'])) {
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
try {
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
} catch (PDOException $e) {
error_log('Failed to connect to database '.$_configuration['main_database'].': '.$e->getMessage().' in '.$bd.'/'.$dir.$sub);
continue;
}
if ($inc !== false && $dbh !== false) {
$sql = "SELECT CONCAT(UTC_DATE(),' ',UTC_TIME())";
$res = $dbh->query($sql);
$row = $res->fetch();
$current_date = $row[0];
$track_online_table = 'track_e_exercices';
// Make sure the right table is selected (exercices in 1.9.* and exercises after)
$vc = version_compare($_configuration['system_version'], '1.10.0');
if ($vc >= 0) {
$track_online_table = 'track_e_exercises';
}
//$current_date=date('Y-m-d H:i:s',time());
$query = "SELECT count(distinct(exe_user_id)) ".
" FROM ".$track_online_table.
" WHERE DATE_ADD(exe_date, ".
"INTERVAL $last_connect_minutes MINUTE) >= '".$current_date."' ";
//"INTERVAL $last_connect_minutes MINUTE) >= NOW() ";
$res = $dbh->query($query);
if ($res === false) {
$num = 0;
} else {
$row = $res->fetch();
$num = $row[0];
}
$cut_point = 7;
if (substr($_configuration['root_web'], 0, 5) == 'https') {
$cut_point = 8;
}
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
$match_count += $num;
}
}
}
}
return $connections;
}