Skip to content

Commit 945ba9f

Browse files
committed
Avoid useless cron reports by adding additional check on configuration.php variables before trying a database connection
1 parent de66723 commit 945ba9f

6 files changed

+171
-159
lines changed

get_connected_users_5_munin.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,36 +114,38 @@ function get_connections($bd, $sub, $last_connect_minutes)
114114
continue;
115115
}
116116
$inc = include_once($config_file);
117-
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
118-
try {
119-
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
120-
} catch (PDOException $e) {
121-
error_log('Failed to connect to database '.$_configuration['main_database'].': '.$e->getMessage());
122-
continue;
123-
}
124-
if ($inc !== false && $dbh !== false) {
125-
$sql = "SELECT CONCAT(UTC_DATE(),' ',UTC_TIME())";
126-
$res = $dbh->query($sql);
127-
$row = $res->fetch();
128-
$current_date = $row[0];
129-
$track_online_table = 'track_e_online';
130-
$query = "SELECT count(login_user_id) ".
131-
" FROM ".$track_online_table.
132-
" WHERE DATE_ADD(login_date, ".
133-
"INTERVAL $last_connect_minutes MINUTE) >= '".$current_date."' ";
134-
$res = $dbh->query($query);
135-
if ($res === false) {
136-
$num = 0;
137-
} else {
138-
$row = $res->fetch();
139-
$num = $row[0];
117+
if (!empty($_configuration['user']) && !empty($_configuration['main_database'])) {
118+
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
119+
try {
120+
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
121+
} catch (PDOException $e) {
122+
error_log('Failed to connect to database '.$_configuration['main_database'].': '.$e->getMessage());
123+
continue;
140124
}
141-
$cut_point = 7;
142-
if (substr($_configuration['root_web'], 0, 5) == 'https') {
143-
$cut_point = 8;
125+
if ($inc !== false && $dbh !== false) {
126+
$sql = "SELECT CONCAT(UTC_DATE(),' ',UTC_TIME())";
127+
$res = $dbh->query($sql);
128+
$row = $res->fetch();
129+
$current_date = $row[0];
130+
$track_online_table = 'track_e_online';
131+
$query = "SELECT count(login_user_id) ".
132+
" FROM ".$track_online_table.
133+
" WHERE DATE_ADD(login_date, ".
134+
"INTERVAL $last_connect_minutes MINUTE) >= '".$current_date."' ";
135+
$res = $dbh->query($query);
136+
if ($res === false) {
137+
$num = 0;
138+
} else {
139+
$row = $res->fetch();
140+
$num = $row[0];
141+
}
142+
$cut_point = 7;
143+
if (substr($_configuration['root_web'], 0, 5) == 'https') {
144+
$cut_point = 8;
145+
}
146+
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
147+
$match_count += $num;
144148
}
145-
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
146-
$match_count += $num;
147149
}
148150
}
149151
}

get_live_answers_munin.php

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -118,38 +118,40 @@ function get_connections($bd, $sub, $last_connect_minutes)
118118
continue;
119119
}
120120
$inc = include_once($config_file);
121-
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
122-
try {
123-
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
124-
} catch (PDOException $e) {
125-
error_log('Failed to connect to database: '.$e->getMessage());
126-
continue;
127-
}
128-
if ($inc !== false && $dbh !== false) {
129-
$sql = "SELECT CONCAT(UTC_DATE(),' ',UTC_TIME())";
130-
$res = $dbh->query($sql);
131-
$row = $res->fetch();
132-
$current_date = $row[0];
133-
//$current_date=date('Y-m-d H:i:s',time());
134-
$track_table = 'track_e_attempt';
135-
$query = "SELECT count(distinct(question_id)) ".
136-
" FROM ".$track_table.
137-
" WHERE DATE_ADD(tms, ".
138-
"INTERVAL $last_connect_minutes MINUTE) >= '".$current_date."' ";
139-
//"INTERVAL $last_connect_minutes MINUTE) >= NOW() ";
140-
$res = $dbh->query($query);
141-
if ($res === false) {
142-
$num = 0;
143-
} else {
144-
$row = $res->fetch();
145-
$num = $row[0];
121+
if (!empty($_configuration['user']) && !empty($_configuration['main_database'])) {
122+
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
123+
try {
124+
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
125+
} catch (PDOException $e) {
126+
error_log('Failed to connect to database: '.$e->getMessage());
127+
continue;
146128
}
147-
$cut_point = 7;
148-
if (substr($_configuration['root_web'], 0, 5) == 'https') {
149-
$cut_point = 8;
129+
if ($inc !== false && $dbh !== false) {
130+
$sql = "SELECT CONCAT(UTC_DATE(),' ',UTC_TIME())";
131+
$res = $dbh->query($sql);
132+
$row = $res->fetch();
133+
$current_date = $row[0];
134+
//$current_date=date('Y-m-d H:i:s',time());
135+
$track_table = 'track_e_attempt';
136+
$query = "SELECT count(distinct(question_id)) ".
137+
" FROM ".$track_table.
138+
" WHERE DATE_ADD(tms, ".
139+
"INTERVAL $last_connect_minutes MINUTE) >= '".$current_date."' ";
140+
//"INTERVAL $last_connect_minutes MINUTE) >= NOW() ";
141+
$res = $dbh->query($query);
142+
if ($res === false) {
143+
$num = 0;
144+
} else {
145+
$row = $res->fetch();
146+
$num = $row[0];
147+
}
148+
$cut_point = 7;
149+
if (substr($_configuration['root_web'], 0, 5) == 'https') {
150+
$cut_point = 8;
151+
}
152+
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
153+
$match_count += $num;
150154
}
151-
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
152-
$match_count += $num;
153155
}
154156
}
155157
}

get_live_exam_users_munin.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -118,43 +118,45 @@ function get_connections($bd, $sub, $last_connect_minutes)
118118
continue;
119119
}
120120
$inc = include_once($config_file);
121-
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
122-
try {
123-
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
124-
} catch (PDOException $e) {
125-
error_log('Failed to connect to database: '.$e->getMessage());
126-
continue;
127-
}
128-
if ($inc !== false && $dbh !== false) {
129-
$sql = "SELECT CONCAT(UTC_DATE(),' ',UTC_TIME())";
130-
$res = $dbh->query($sql);
131-
$row = $res->fetch();
132-
$current_date = $row[0];
133-
$track_online_table = 'track_e_exercices';
134-
// Make sure the right table is selected (exercices in 1.9.* and exercises after)
135-
$vc = version_compare($_configuration['system_version'], '1.10.0');
136-
if ($vc >= 0) {
137-
$track_online_table = 'track_e_exercises';
121+
if (!empty($_configuration['user']) && !empty($_configuration['main_database'])) {
122+
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
123+
try {
124+
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
125+
} catch (PDOException $e) {
126+
error_log('Failed to connect to database: '.$e->getMessage());
127+
continue;
138128
}
139-
//$current_date=date('Y-m-d H:i:s',time());
140-
$query = "SELECT count(distinct(exe_user_id)) ".
141-
" FROM ".$track_online_table.
142-
" WHERE DATE_ADD(exe_date, ".
143-
"INTERVAL $last_connect_minutes MINUTE) >= '".$current_date."' ";
144-
//"INTERVAL $last_connect_minutes MINUTE) >= NOW() ";
145-
$res = $dbh->query($query);
146-
if ($res === false) {
147-
$num = 0;
148-
} else {
129+
if ($inc !== false && $dbh !== false) {
130+
$sql = "SELECT CONCAT(UTC_DATE(),' ',UTC_TIME())";
131+
$res = $dbh->query($sql);
149132
$row = $res->fetch();
150-
$num = $row[0];
151-
}
152-
$cut_point = 7;
153-
if (substr($_configuration['root_web'], 0, 5) == 'https') {
154-
$cut_point = 8;
133+
$current_date = $row[0];
134+
$track_online_table = 'track_e_exercices';
135+
// Make sure the right table is selected (exercices in 1.9.* and exercises after)
136+
$vc = version_compare($_configuration['system_version'], '1.10.0');
137+
if ($vc >= 0) {
138+
$track_online_table = 'track_e_exercises';
139+
}
140+
//$current_date=date('Y-m-d H:i:s',time());
141+
$query = "SELECT count(distinct(exe_user_id)) ".
142+
" FROM ".$track_online_table.
143+
" WHERE DATE_ADD(exe_date, ".
144+
"INTERVAL $last_connect_minutes MINUTE) >= '".$current_date."' ";
145+
//"INTERVAL $last_connect_minutes MINUTE) >= NOW() ";
146+
$res = $dbh->query($query);
147+
if ($res === false) {
148+
$num = 0;
149+
} else {
150+
$row = $res->fetch();
151+
$num = $row[0];
152+
}
153+
$cut_point = 7;
154+
if (substr($_configuration['root_web'], 0, 5) == 'https') {
155+
$cut_point = 8;
156+
}
157+
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
158+
$match_count += $num;
155159
}
156-
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
157-
$match_count += $num;
158160
}
159161
}
160162
}

get_registered_courses_munin.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,30 +110,32 @@ function get_courses($bd, $sub)
110110
continue;
111111
}
112112
$inc = include_once($config_file);
113-
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
114-
try {
115-
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
116-
} catch (PDOException $e) {
117-
error_log('Failed to connect to database: '.$e->getMessage());
118-
continue;
119-
}
120-
if ($inc !== false && $dbh !== false) {
121-
$user_table = 'course';
122-
$query = "SELECT count(code) ".
123-
" FROM ".$user_table;
124-
$res = $dbh->query($query);
125-
if ($res === false) {
126-
$num = 0;
127-
} else {
128-
$row = $res->fetch();
129-
$num = $row[0];
113+
if (!empty($_configuration['user']) && !empty($_configuration['main_database'])) {
114+
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
115+
try {
116+
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
117+
} catch (PDOException $e) {
118+
error_log('Failed to connect to database: '.$e->getMessage());
119+
continue;
130120
}
131-
$cut_point = 7;
132-
if (substr($_configuration['root_web'], 0, 5) == 'https') {
133-
$cut_point = 8;
121+
if ($inc !== false && $dbh !== false) {
122+
$user_table = 'course';
123+
$query = "SELECT count(code) ".
124+
" FROM ".$user_table;
125+
$res = $dbh->query($query);
126+
if ($res === false) {
127+
$num = 0;
128+
} else {
129+
$row = $res->fetch();
130+
$num = $row[0];
131+
}
132+
$cut_point = 7;
133+
if (substr($_configuration['root_web'], 0, 5) == 'https') {
134+
$cut_point = 8;
135+
}
136+
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
137+
$match_count += $num;
134138
}
135-
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
136-
$match_count += $num;
137139
}
138140
}
139141
}

get_registered_sessions_munin.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,33 +110,35 @@ function get_sessions($bd, $sub)
110110
continue;
111111
}
112112
$inc = include_once($config_file);
113-
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
114-
try {
115-
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
116-
} catch (PDOException $e) {
117-
error_log('Failed to connect to database: '.$e->getMessage());
118-
continue;
119-
}
120-
if ($inc !== false && $dbh !== false) {
121-
$user_table = 'session';
122-
$query = "SELECT count(id) ".
123-
" FROM ".$user_table;
124-
$res = $dbh->query($query);
125-
if ($res === false) {
126-
$num = 0;
127-
//echo " There was a query error for the following portal\n";
128-
} else {
129-
$row = $res->fetch();
130-
$num = $row[0];
113+
if (!empty($_configuration['user']) && !empty($_configuration['main_database'])) {
114+
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
115+
try {
116+
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
117+
} catch (PDOException $e) {
118+
error_log('Failed to connect to database: '.$e->getMessage());
119+
continue;
131120
}
132-
$cut_point = 7;
133-
if (substr($_configuration['root_web'], 0, 5) == 'https') {
134-
$cut_point = 8;
121+
if ($inc !== false && $dbh !== false) {
122+
$user_table = 'session';
123+
$query = "SELECT count(id) ".
124+
" FROM ".$user_table;
125+
$res = $dbh->query($query);
126+
if ($res === false) {
127+
$num = 0;
128+
//echo " There was a query error for the following portal\n";
129+
} else {
130+
$row = $res->fetch();
131+
$num = $row[0];
132+
}
133+
$cut_point = 7;
134+
if (substr($_configuration['root_web'], 0, 5) == 'https') {
135+
$cut_point = 8;
136+
}
137+
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
138+
$match_count += $num;
139+
} else {
140+
//echo "$bd/$dir$sub:could not open configuration.php or database:\n";
135141
}
136-
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
137-
$match_count += $num;
138-
} else {
139-
//echo "$bd/$dir$sub:could not open configuration.php or database:\n";
140142
}
141143
}
142144
}

get_registered_users_munin.php

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,30 @@ function get_registrations($bd, $sub)
110110
continue;
111111
}
112112
$inc = include_once($config_file);
113-
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
114-
try {
115-
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
116-
} catch (PDOException $e) {
117-
error_log('Failed to connect to database: '.$e->getMessage());
118-
continue;
119-
}
120-
if ($inc !== false && $dbh !== false) {
121-
$query = "SELECT count(user_id) FROM user";
122-
$res = $dbh->query($query);
123-
if ($res === false) {
124-
$num = 0;
125-
} else {
126-
$row = $res->fetch();
127-
$num = $row[0];
113+
if (!empty($_configuration['user']) && !empty($_configuration['main_database'])) {
114+
$dsn = 'mysql:dbname='.$_configuration['main_database'].';host='.$_configuration['db_host'];
115+
try {
116+
$dbh = new PDO($dsn, $_configuration['db_user'], $_configuration['db_password']);
117+
} catch (PDOException $e) {
118+
error_log('Failed to connect to database: '.$e->getMessage());
119+
continue;
128120
}
129-
$cut_point = 7;
130-
if (substr($_configuration['root_web'], 0, 5) == 'https') {
131-
$cut_point = 8;
121+
if ($inc !== false && $dbh !== false) {
122+
$query = "SELECT count(user_id) FROM user";
123+
$res = $dbh->query($query);
124+
if ($res === false) {
125+
$num = 0;
126+
} else {
127+
$row = $res->fetch();
128+
$num = $row[0];
129+
}
130+
$cut_point = 7;
131+
if (substr($_configuration['root_web'], 0, 5) == 'https') {
132+
$cut_point = 8;
133+
}
134+
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
135+
$match_count += $num;
132136
}
133-
$connections[str_replace('.', '_', substr($_configuration['root_web'], $cut_point, -1))] = $num;
134-
$match_count += $num;
135137
}
136138
}
137139
}

0 commit comments

Comments
 (0)