NagiosCommands.php in Nagios Monitoring 8
File
src/Commands/NagiosCommands.php
View source
<?php
namespace Drupal\nagios\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\nagios\Controller\StatuspageController;
use Drush\Commands\DrushCommands;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
class NagiosCommands extends DrushCommands {
public function nagios($check = '') {
if ($check) {
$moduleHandler = \Drupal::moduleHandler();
if (array_key_exists($check, nagios_functions())) {
$func = 'nagios_check_' . $check;
$result = $func();
$nagios_data['nagios'][$result['key']] = $result['data'];
}
elseif ($moduleHandler
->moduleExists($check)) {
$result = $moduleHandler
->invoke($check, 'nagios');
$nagios_data[$check] = $result;
}
else {
$logger = $this
->logger();
$logger
->error($check . ' is not a valid nagios check.');
$logger
->error(dt('Run `drush nagios-list` for valid checks.'));
return 1;
}
}
else {
$nagios_data = nagios_invoke_all('nagios');
}
[
$output,
$severity,
] = (new StatuspageController())
->getStringFromNagiosData($nagios_data);
$users = array_unique(_nagios_update_os_user());
if (count($users) > 1 && $severity != NAGIOS_STATUS_OK) {
$warning = dt('Warning') . ': ';
$warning .= dt('All nagios checks should be executed as the same user as the web page.') . "\n";
$warning .= dt('This is important when modules confirm file system permissions are correct.') . "\n";
$warning .= dt('You can use `sudo -u` to run drush under a different user.') . "\n";
$warning .= dt('Use `:command` to delete the state.', [
':command' => 'drush state:delete nagios.os_user',
]) . "\n";
$warning .= 'User' . print_r($users, TRUE);
$this
->logger()
->warning($warning);
}
$this->output
->writeln($output);
return $severity;
}
public function nagios_list() {
$valid_checks = nagios_functions();
$rows = [];
foreach ($valid_checks as $check => $description) {
$rows[$check] = [
'check' => $check,
'description' => $description,
'module' => 'nagios',
];
}
$moduleHandler = \Drupal::moduleHandler();
$module_names = $moduleHandler
->getImplementations('nagios');
foreach ($module_names as $name) {
$info = $moduleHandler
->invoke($name, 'nagios_info');
$description = !empty($info['name']) && is_string($info['name']) ? $info['name'] : '';
$rows[$name] = [
'check' => $name,
'description' => $description,
'module' => $name,
];
}
ksort($rows);
return new RowsOfFields($rows);
}
public function check_updates() {
$logger = $this->logger;
try {
$update_data = nagios_check_requirements()['data'];
$severity = $update_data['status'];
} catch (ServiceNotFoundException $e) {
$logger
->error(dt('This Drush command is only available if Core’s update module is enabled.'));
$logger
->error(dt('Run `drush en update` to enable it.'));
return NAGIOS_STATUS_UNKNOWN;
}
if ($severity == NAGIOS_STATUS_CRITICAL) {
$logger
->error($update_data['text']);
}
elseif ($severity == NAGIOS_STATUS_WARNING) {
$logger
->warning($update_data['text']);
}
else {
$logger
->notice($update_data['text']);
}
return $severity;
}
}