Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Admin tool "Opcache management" - Nagios monitoring check, part "cli"
19
 *
20
 * @package    tool_opcache
21
 * @copyright  2017 Alexander Bias, Ulm University <alexander.bias@uni-ulm.de>
22
 * @copyright  inspired by code by Mikanoshi, https://exchange.icinga.com/Mikanoshi/PHP+opcache+monitoring+plugin
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
define('CLI_SCRIPT', true);
27
 
28
require(__DIR__.'/../../../../config.php');
29
require_once($CFG->libdir.'/clilib.php');
30
require_once($CFG->libdir . '/filelib.php');
31
 
32
 
33
// Get cli options.
34
list($options, $unrecognized) = cli_get_params(['help' => false,
35
                                                    'url' => null,
36
                                                    'warning' => 80,
37
                                                    'critical' => 90, ],
38
                                               ['h' => 'help',
39
                                                    'u' => 'url',
40
                                                    'w' => 'warning',
41
                                                    'c' => 'critical', ]);
42
if ($unrecognized) {
43
    $unrecognized = implode("\n  ", $unrecognized);
44
    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
45
}
46
 
47
 
48
// Initialize return code as UNKNOWN.
49
$returnstatus = 3;
50
 
51
 
52
// CLI help.
53
if ($options['help'] || !($options['url'])) {
54
    $help = "Nagios check for PHP opcache
55
 
56
Options:
57
-u, --url       Full URL to check_opcache_web.php
58
-w, --warning   Threshold for warning (Default: 80)
59
-c, --critical  Threshold for critical (Default: 90)
60
-h, --help      Print out this help
61
 
62
Example:
63
\$ sudo -u www-data /usr/bin/php admin/tool/cli/check_opcache.php ".
64
        "--url=\"https://example.com/admin/tool/opcache/cli/check_opcache_web.php\" --warning=75 --critical=85
65
";
66
    cli_writeln($help);
67
    exit($returnstatus);
68
}
69
 
70
 
71
// Clean CLI parameters.
72
$options['url'] = clean_param($options['url'], PARAM_URL);
73
$options['warning'] = clean_param($options['warning'], PARAM_INT);
74
$options['critical'] = clean_param($options['critical'], PARAM_INT);
75
 
76
 
77
// Fetch Opcache figures from webserver.
78
$curl = new curl(['ignoresecurity' => true]); // The ignoresecurity option means that $CFG->curlsecurityblockedhosts is
79
                                                   // ignored by purpose. Otherwise, $CFG->curlsecurityblockedhosts might prevent
80
                                                   // that the web part of this CLI tool is fetched.
81
$curloptions = [
82
    'FRESH_CONNECT' => true,
83
    'RETURNTRANSFER' => true,
84
    'FORBID_REUSE' => true,
85
    'HEADER' => false,
86
    'CONNECTTIMEOUT' => 5,
87
];
88
$params = [];
89
if (isset($CFG->tool_opcache_check_secretkey)) {
90
    $params['secret'] = $CFG->tool_opcache_check_secretkey;
91
}
92
$curlret = $curl->get($options['url'], $params, $curloptions);
93
 
94
// Die if secret key was required but not correct (basically, this should not happen).
95
if ($curlret == 'FORBIDDEN') {
96
    $returnstatus = 2;
97
    $out = 'Secret key is required but is not correct.';
98
}
99
 
100
// Pick Opcache figures.
101
$figures = explode(PHP_EOL, $curlret);
102
$used = clean_param($figures[0], PARAM_INT);
103
$free = clean_param($figures[1], PARAM_INT);
104
$hitspct = clean_param($figures[2], PARAM_FLOAT);
105
$misspct = clean_param($figures[3], PARAM_FLOAT);
106
 
107
 
108
// Calculate used percentage value.
109
$usedpct = round($used / ($used + $free) * 100, 1);
110
// Calculate return status.
111
if ($usedpct >= $options['critical']) {
112
    $returnstatus = 2;
113
} else if ($usedpct >= $options['warning']) {
114
    $returnstatus = 1;
115
} else {
116
    $returnstatus = 0;
117
}
118
 
119
// Concatenate return string including performance data.
120
$out = $usedpct."% cache used | ".
121
        "used_pct=".$usedpct."%;".$options['warning'].";".$options['critical']." hit_pct=".$hitspct."%; miss_pct=".$misspct."%;";
122
 
123
// Echo return string.
124
if ($returnstatus == 0) {
125
    cli_writeln('OK - '.$out);
126
} else if ($returnstatus == 1) {
127
    cli_writeln('WARNING - '.$out);
128
} else {
129
    cli_writeln('CRITICAL - '.$out);
130
}
131
 
132
// Exit with calculated return status.
133
exit($returnstatus);