forked from catalyst/moodle-tool_heartbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
111 lines (93 loc) · 3.01 KB
/
index.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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Are you Ok? heartbeat for load balancers
*
* @package tool_heartbeat
* @copyright 2014 Brendan Heywood <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Set this manually to true as needed.
if (false) {
print "Server is in MAINTENANCE";
exit;
}
$fullcheck = false;
if (isset($argv) && $argv[0]) {
define('CLI_SCRIPT', true);
$fullcheck = count($argv) > 1 && $argv[1] === 'fullcheck';
} else {
define('NO_MOODLE_COOKIES', true);
$fullcheck = isset($_GET['fullcheck']);
}
define('NO_UPGRADE_CHECK', true);
define('ABORT_AFTER_CONFIG', true);
require_once('../../../config.php');
global $CFG;
$status = "";
/**
* Return an error that ELB will pick up
*
* @param string $reason
*/
function failed($reason) {
// Status for ELB, will cause ELB to remove instance.
header("HTTP/1.0 503 Service unavailable: failed $reason check");
// Status for the humans.
print "Server is DOWN<br>\n";
echo "Failed: $reason";
exit;
}
$testfile = $CFG->dataroot . "/tool_heartbeat.test";
$size = file_put_contents($testfile, '1');
if ($size !== 1) {
failed('sitedata not writable');
}
if (file_exists($testfile)) {
$status .= "sitedata OK<br>\n";
} else {
failed('sitedata not readable');
}
$sessionhandler = (property_exists($CFG, 'session_handler_class') && $CFG->session_handler_class == '\core\session\memcached');
if ($sessionhandler) {
$memcache = explode(':', $CFG->session_memcached_save_path );
try {
$md = new Memcached();
$md->addServers(array(array($memcache[0],$memcache[1])));
$status .= "session memcache OK<br>\n";
} catch (Exception $e) {
failed('sessions memcache');
}
}
// Optionally check database configuration and access (slower).
if ($fullcheck) {
try {
define('ABORT_AFTER_CONFIG_CANCEL', true);
require($CFG->dirroot . '/lib/setup.php');
global $DB;
// Try to get the first record from the user table.
$user = $DB->get_record_sql('SELECT id FROM {user} WHERE 0 < id ', null, IGNORE_MULTIPLE);
if ($user) {
$status .= "database OK<br>\n";
} else {
failed('no users in database');
}
} catch (Exception $e) {
failed('database error');
}
}
print "Server is ALIVE<br>\n";
print $status;