forked from catalyst/moodle-tool_heartbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
162 lines (136 loc) · 4.96 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
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
<?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
*/
// Make sure varnish doesn't cache this. But it still might so go check it!
header('Pragma: no-cache');
header('Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate');
header('Expires: Tue, 04 Sep 2012 05:32:29 GMT');
// 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);
/**
* Checks if the command line maintenance mode has been enabled. Skip the config bootstrapping.
*
* @param string $configfile The relative path for config.php
* @return bool True if climaintenance.html is found.
*/
function check_climaintenance($configfile) {
$content = file_get_contents($configfile);
$content = preg_replace("#[^!:]//#", "\n//", $content); // Set comments to be on newlines, replace '//' with '\n//', where // does not start with :
$content = preg_replace("/;/", ";\n", $content); // Split up statements, replace ';' with ';\n'
$content = preg_replace("/^[\s]+/m", "", $content); // Removes all initial whitespace and newlines.
$re = '/^\$CFG->dataroot\s+=\s+["\'](.*?)["\'];/m'; // Lines starting with $CFG->dataroot
preg_match($re, $content, $matches);
if (!empty($matches)) {
$climaintenance = $matches[count($matches) - 1] . '/climaintenance.html';
if (file_exists($climaintenance)) {
return true;
}
}
return false;
}
if (check_climaintenance(__DIR__ . '/../../../config.php') === true) {
print "Server is in MAINTENANCE<br>\n";
exit;
}
require_once(__DIR__ . '/../../../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');
$savepath = property_exists($CFG, 'session_memcached_save_path');
if ($sessionhandler && $savepath) {
require_once($CFG->libdir . '/classes/session/util.php');
$servers = \core\session\util::connection_string_to_memcache_servers($CFG->session_memcached_save_path);
try {
$memcached = new \Memcached();
$memcached->addServers($servers);
$stats = $memcached->getStats();
$memcached->quit();
$addr = $servers[0][0];
$port = $servers[0][1];
if ($stats[$addr . ':' . $port]['uptime'] > 0) {
$status .= "session memcached OK<br>\n";
} else {
failed('sessions memcached');
}
} catch (Exception $e) {
failed('sessions memcached');
} catch (Throwable $e) {
failed('sessions memcached');
}
}
// 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');
} catch (Throwable $e) {
failed('database error');
}
}
print "Server is ALIVE<br>\n";
print $status;