Skip to content

Commit

Permalink
logstream setting
Browse files Browse the repository at this point in the history
  • Loading branch information
srdjan-catalyst committed Mar 4, 2022
1 parent 6718d90 commit 7c5f2de
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 14 deletions.
20 changes: 20 additions & 0 deletions README-PERF-LOGSTREAM-TEST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Testing "Performance log stream"

## Workstation/server

Setting `tool_heartbeat | logstream` to both a file path, eg `/tmp/perf.log' or a syslog url, eg `syslog://local0/TEST` or just `syslog://local0/` should just work.

## Docker container

I test with standard `php:[Maj].[Min].-cli` base, running the ap as `php -S 0.0.0.0:80`. That should not matter, but just for the reference.

Whatever your way is, Setting `tool_heartbeat | logstream` to a file path should just work. Of course, if the destination is not mounted you'll need to get into the container and check the file.

### Syslog

To get syslog to work, I did following:

* Installer `rsyslog` in the image: `Dockerfile: apt-get install rsyslog`
* mounted host's `/dev/log`: `docker run ... -v /dev/log:/dev/log ...`

That makes container's syslog going straight to your workstation's syslog, so you can check your `/var/log/syslog`.
66 changes: 66 additions & 0 deletions classes/check/logcheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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/>.
/**
* Auth method health check.
*
* @package tool_heartbeat
* @author Srdjan Janković <[email protected]>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* This can be run either as a web api, or on the CLI. When run on the
* CLI it conforms to the Nagios plugin standard.
*
* See also:
* - http://nagios.sourceforge.net/docs/3_0/pluginapi.html
* - https://nagios-plugins.org/doc/guidelines.html#PLUGOUTPUT
*/

namespace tool_heartbeat\check;

use core\check\check;
use core\check\result;

use tool_heartbeat\logger;
use Exception;

defined('MOODLE_INTERNAL') || die();

class logcheck extends check {

public function get_action_link(): ?\action_link {
$url = new \moodle_url('/admin/tool/heartbeat/settings.php', ['section' => 'log']);
return new \action_link($url, get_string('logstream', 'tool_heartbeat'));
}

public function get_result() : result {
if ($logstream = get_config('tool_heartbeat', 'logstream')) {
try {
logger::log_to_stream(, ['check']);
$status = result::OK;
$summary = '';
} catch (Exception $e) {
$status = result::WARNING;
$summary = get_string('logstreamerror', 'tool_heartbeat', "$logstream ".$e->getMessage());
}
} else {
$status = result::WARNING;
$summary = get_string('logstreamnotset', 'tool_heartbeat');
}

return new result($status, $summary);
}
}
23 changes: 15 additions & 8 deletions classes/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,23 @@ public static function register_stat_callback(callable $cb, $stream = null): voi
*/
public static function log(): void {
$defaultstream = self::default_log_stream();
$statcallbacks = self::$statcallbacks;
if ($defaultstream) {
self::$statcallbacks[$defaultstream] = self::$statcallbacks[null];
self::$statcallbacks[$defaultstream][] = [self::$classname, 'default_stats'];
if (!isset($statcallbacks[$defaultstream])) {
$statcallbacks[$defaultstream] = [];
}
if (isset($statcallbacks[null])) {
$statcallbacks[$defaultstream] = array_merge(
$statcallbacks[$defaultstream],
$statcallbacks[null]
);
}
$statcallbacks[$defaultstream][] = [self::$classname, 'perf_stats'];
}
unset(self::$statcallbacks[null]);
unset($statcallbacks[null]);


foreach(self::$statcallbacks as $stream => $callbacks) {
foreach($statcallbacks as $stream => $callbacks) {
$lines = [];
foreach ($callbacks as $cb) {
try {
Expand Down Expand Up @@ -122,17 +131,15 @@ private static function default_log_stream(): ?string {
return null;
}

// XXX from config, currently supports 'syslog://<facility>/<log prefix>'
// defaults: facility: user, log prefix: shortname
return 'syslog://local5';
return get_config('tool_heartbeat', 'logstream');
}

/**
* Stats collector
*
* @return array
*/
public static function default_stats(): array {
public static function perf_stats(): array {
global $CFG, $PERF;

// XXX This is lame, same call is in request_shutdown()
Expand Down
4 changes: 4 additions & 0 deletions lang/en/tool_heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
$string['configuredauths'] = 'Check auth methods';
$string['configuredauthsdesc'] = 'Auth methods to check are enabled in the Check API. A warning will be emitted if they are not enabled.';
$string['checkauthcheck'] = 'Authentication methods';
$string['logstream'] = 'Performance log stream url';
$string['logstreamdesc'] = 'Url of the log stream for performance stats logging. currently supports standard php stream urls and "syslog://facility/[log prefix]", default log prefix: $SITE->shortname.';
$string['logstreamnotset'] = 'Performance log stream url not set';
$string['logstreamerror'] = 'Performance log stream error: {a}';
/*
* Privacy provider (GDPR)
*/
Expand Down
14 changes: 10 additions & 4 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@
defined('MOODLE_INTERNAL') || die();

function tool_heartbeat_status_checks() {
return [new \tool_heartbeat\check\authcheck()];
return [
new \tool_heartbeat\check\authcheck(),
new \tool_heartbeat\check\logcheck(),
];
}

stream_wrapper_register('syslog', 'tool_heartbeat\wrapper\syslog')
or throw new moodle_exception('Failed to register syslog protocol');
function tool_heartbeat_after_config() {
if(!stream_wrapper_register('syslog', 'tool_heartbeat\wrapper\syslog')) {
throw new moodle_exception('Failed to register syslog protocol');
}

tool_heartbeat\logger::register_shutdown_handler();
tool_heartbeat\logger::register_shutdown_handler();
}
4 changes: 4 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,9 @@
$settings->add(new admin_setting_configtext('tool_heartbeat/configuredauths',
get_string('configuredauths', 'tool_heartbeat'),
get_string('configuredauthsdesc', 'tool_heartbeat'), '', PARAM_TEXT));

$settings->add(new admin_setting_configtext('tool_heartbeat/logstream',
get_string('logstream', 'tool_heartbeat'),
get_string('logstreamdesc', 'tool_heartbeat'), '', PARAM_TEXT));
}
}
4 changes: 2 additions & 2 deletions tests/logger_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public function test_log() {
}
}

public function test_default_stats() {
public function test_perf_stats() {
$cnt = function_exists('memory_get_peak_usage') ? 4 : 3;

$stats = logger::default_stats();
$stats = logger::perf_stats();
$this->assertEquals($cnt, count($stats));
}
}
Expand Down

0 comments on commit 7c5f2de

Please sign in to comment.