-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathnagios.php
151 lines (132 loc) · 4.69 KB
/
nagios.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
<?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
*
* Returns a Nagios status code, used for automated monitoring
*
* Numeric value: | Status | Description
* 0 | OK | The plugin was able to check the service and
* | it appeared to be functioning properly.
* 1 | Warning | The plugin was able to check the service, but
* | it appeared to be above some "warning"
* | threshold or did not appear to be working properly.
* 2 | Critical | The plugin detected that either the service was
* | not running or it was above some "critical" threshold.
* 3 | Unknown | The plugin was unable to determine the status of the service.
*
* @package tool_heartbeat
* @copyright 2019 Peter Burnett <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// @codingStandardsIgnoreStart
// Ignore required to skip codechecker error for no config.php load in class.
require_once(__DIR__ . '/../../../config.php');
// @codingStandardsIgnoreEnd
$format = '%b %d %H:%M:%S';
$now = userdate(time(), $format);
/**
* Sends a good Nagios response, with message.
*
* @param string $msg the message to append the Nagios response.
* @param string $more aditional message
*/
function send_good($msg, $more = '') {
send(0, $msg, $more);
}
/**
* Sends a warning Nagios response, with message.
*
* @param string $msg the message to append the Nagios response.
* @param string $more aditional message
*/
function send_warning($msg, $more = '') {
send(1, $msg, $more);
}
/**
* Sends a critical Nagios response, with message.
*
* @param string $msg the message to append the Nagios response.
* @param string $more aditional message
*/
function send_critical($msg, $more = '') {
send(2, $msg, $more);
}
/**
* Sends an unknown Nagios response, with message.
*
* @param string $msg the message to append the Nagios response.
* @param string $more aditional message
*/
function send_unknown($msg, $more = '') {
send(3, $msg, $more);
}
/**
* Sends a Nagios response, with message.
* @param int $level Status code level
* @param string $msg the message to append the Nagios response.
* @param string $more additional message
*/
function send($level, $msg, $more = '') {
global $now;
$buffercontents = check_buffer();
// If buffer was outputted, and is currently OK or UNKNOWN level, upgrade to WARNING level.
if (!empty($buffercontents) && ($level == 0 || $level == 3)) {
$level = 1;
}
$prefixes = [
0 => "OK",
1 => "WARNING",
2 => "CRITICAL",
3 => "UNKNOWN",
];
// Add any buffer contents message to the msg, ensuring the details are on the first line.
$msglines = explode("\n", $msg);
$msglines[0] .= $buffercontents;
$msg = implode("\n", $msglines);
printf("{$prefixes[$level]}: $msg (Checked {$now})\n$more");
exit($level);
}
/**
* Finishes output buffering, and returns a message if the buffer contained anything.
* Ideally, the buffer should always be empty, but debugging messages often are outputted to it.
* @return string
*/
function check_buffer() {
$contents = ob_get_clean();
// All good - no output.
if ($contents == false) {
return '';
}
// There was output, return it.
return ", but there was unexpected output:\n {$contents}\n";
}
/**
* This converts a check to the nagios web format
*
* @param \core\check\check $check
* @return void
*/
function send_check(\core\check\check $check): void {
$result = $check->get_result();
if ($result->status == \core\check\result::OK) {
send_good($result->get_summary(), $result->get_details());
}
if ($result->status == \core\check\result::WARNING) {
send_warning($result->get_summary(), $result->get_details());
}
send_critical($result->get_summary(), $result->get_details());
}