View source
<?php
define('NAGIOS_STATUS_OK', variable_get('nagios_status_ok_value', 0));
define('NAGIOS_STATUS_WARNING', variable_get('nagios_status_warning_value', 1));
define('NAGIOS_STATUS_CRITICAL', variable_get('nagios_status_critical_value', 2));
define('NAGIOS_STATUS_UNKNOWN', variable_get('nagios_status_unknown_value', 3));
function nagios_status() {
return [
NAGIOS_STATUS_OK => 'OK',
NAGIOS_STATUS_UNKNOWN => 'UNKNOWN',
NAGIOS_STATUS_WARNING => 'WARNING',
NAGIOS_STATUS_CRITICAL => 'CRITICAL',
];
}
function nagios_functions() {
$functions = [
'requirements' => t('Checking of hook_requirements. This includes the following: module updates, database schema, files directory writability, update.php protected, Lots of other good stuff ...'),
'watchdog' => t('Check recent watchdog entries'),
'cron' => t('Check whether cron has been running regularly'),
'session_anon' => t('Check the number of anonymous sessions for nagios performance data'),
'session_auth' => t('Check the number of authenticated sessions for nagios performance data'),
'nodes' => t('Check the number of nodes for nagios performance data'),
'users' => t('Check the number of users for nagios performance data'),
'modules' => t('Check the number of modules for nagios performance data'),
'themes' => t('Check the number of themes for nagios performance data'),
];
if (module_exists('elysia_cron')) {
$functions['elysia_cron'] = t('Check whether elysia cron has been running regularly');
}
return $functions;
}
function nagios_menu() {
$items = [];
$items['admin/config/system/nagios'] = [
'type' => MENU_NORMAL_ITEM,
'title' => 'Nagios Monitoring',
'description' => 'Settings for Nagios Monitoring',
'page callback' => 'drupal_get_form',
'page arguments' => [
'nagios_settings',
],
'access arguments' => [
'administer site configuration',
],
];
$path = variable_get('nagios_page_path', 'nagios-status');
$callback = variable_get('nagios_page_callback', 'nagios_status_page');
$items[$path] = [
'type' => MENU_SUGGESTED_ITEM,
'title' => 'Nagios status page',
'page callback' => $callback,
'access callback' => 'variable_get',
'access arguments' => [
'nagios_enable_status_page',
FALSE,
],
];
return $items;
}
function nagios_menu_site_status_alter(&$menu_site_status, $path) {
if ($menu_site_status == MENU_SITE_OFFLINE && $path == variable_get('nagios_page_path', 'nagios-status')) {
$menu_site_status = MENU_SITE_ONLINE;
}
}
function nagios_permission() {
$permission['administer nagios ignore'] = [
'title' => t('Administer nagios ignore'),
'description' => t('Select modules to be ignored by nagios.'),
];
return $permission;
}
function _nagios_update_os_user() {
$os_user = variable_get('nagios_os_user', []);
$current_user = posix_getpwuid(posix_geteuid())['name'];
if (isset($os_user[PHP_SAPI]) && $os_user[PHP_SAPI] == $current_user) {
return $os_user;
}
$os_user[PHP_SAPI] = $current_user;
variable_set('nagios_os_user', $os_user);
return $os_user;
}
function nagios_settings($form) {
$group = 'modules';
_nagios_update_os_user();
$form['nagios_ua'] = [
'#type' => 'textfield',
'#title' => t('Unique ID'),
'#default_value' => variable_get('nagios_ua', ''),
'#description' => t('Restrict sending information to requests identified by this Unique ID. You should change this to some unique string for your organization, and configure Nagios accordingly. This makes Nagios data less accessible to curious users. See the README.txt for more details.'),
];
$form['nagios_show_outdated_names'] = [
'#type' => 'checkbox',
'#title' => t('Show outdated module/theme name?'),
'#default_value' => variable_get('nagios_show_outdated_names', TRUE),
];
$form['nagios_status_page'] = [
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Status page settings'),
'#description' => t('Control the availability and location of the HTTP status page. NOTE: you must clear the menu cache for changes to these settings to register.'),
];
$form['nagios_status_page']['nagios_enable_status_page'] = [
'#type' => 'checkbox',
'#title' => t('Enable status page?'),
'#default_value' => variable_get('nagios_enable_status_page', FALSE),
];
$form['nagios_status_page']['nagios_page_path'] = [
'#type' => 'textfield',
'#title' => t('Nagios page path'),
'#description' => t('Enter the path for the Nagios HTTP status page. It must be a valid Drupal path.'),
'#default_value' => variable_get('nagios_page_path', 'nagios'),
];
$form['nagios_status_page']['nagios_page_callback'] = [
'#type' => 'textfield',
'#title' => t('Nagios page callback'),
'#description' => t('Enter the name of the callback function to be used by the Nagios status page. Take care and be sure this function exists before clearing the menu cache!'),
'#default_value' => variable_get('nagios_page_callback', 'nagios_status_page'),
];
$form['nagios_status_page']['nagios_enable_status_page_get'] = [
'#type' => 'checkbox',
'#title' => t('Enable Unique ID checking via URL on status page?'),
'#default_value' => variable_get('nagios_enable_status_page_get', FALSE),
'#description' => t('If enabled the $_GET variable "unique_id" is used for checking the correct Unique ID instead of "User Agent" ($_SERVER[\'HTTP_USER_AGENT\']). This alternative checking is only working if the URL is containing the value like "/nagios?unique_id=*****". This feature is useful to avoid webserver stats with the Unique ID as "User Agent" and helpful for human testing.'),
];
$form['nagios_error_levels'] = [
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Error levels'),
'#description' => t('Set the values to be used for error levels when reporting to Nagios.'),
];
$form['nagios_error_levels']['nagios_status_ok_value'] = [
'#type' => 'textfield',
'#title' => t('Status OK'),
'#description' => t('The value to send to Nagios for a Status OK message.'),
'#default_value' => variable_get('nagios_status_ok_value', 0),
];
$form['nagios_error_levels']['nagios_status_warning_value'] = [
'#type' => 'textfield',
'#title' => t('Warning'),
'#description' => t('The value to send to Nagios for a Warning message.'),
'#default_value' => variable_get('nagios_status_warning_value', 1),
];
$form['nagios_error_levels']['nagios_status_critical_value'] = [
'#type' => 'textfield',
'#title' => t('Critical'),
'#description' => t('The value to send to Nagios for a Critical message.'),
'#default_value' => variable_get('nagios_status_critical_value', 2),
];
$form['nagios_error_levels']['nagios_status_unknown_value'] = [
'#type' => 'textfield',
'#title' => t('Unknown'),
'#description' => t('The value to send to Nagios for an Unknown message.'),
'#default_value' => variable_get('nagios_status_unknown_value', 3),
];
$form[$group] = [
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Modules'),
'#description' => t('Select the modules that should report their data into Nagios.'),
];
foreach (nagios_invoke_all('nagios_info') as $module => $data) {
$form[$group]['nagios_enable_' . $module] = [
'#type' => 'checkbox',
'#title' => $data['name'] . ' (' . $module . ')',
'#default_value' => variable_get('nagios_enable_' . $module, TRUE),
];
}
$form['watchdog'] = [
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Watchdog Settings'),
'#description' => t('Controls how watchdog messages are retrieved and displayed when watchdog checking is set.'),
];
$form['watchdog']['limit_watchdog_display'] = [
'#type' => 'checkbox',
'#title' => 'Limit watchdog display',
'#default_value' => variable_get('limit_watchdog_display', FALSE),
'#description' => t('Limit watchdog messages to only those that are new since the last check.'),
];
$form['watchdog']['limit_watchdog_results'] = [
'#type' => 'textfield',
'#title' => 'Limit watchdog logs',
'#default_value' => variable_get('limit_watchdog_results', 50),
'#description' => t('Limit the number of watchdog logs that are checked. E.G. 50 will only check the newest 50 logs.'),
];
foreach (nagios_invoke_all('nagios_settings') as $module => $module_settings) {
$form[$module] = [
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => $module,
];
foreach ($module_settings as $element => $data) {
$form[$module][$element] = $data;
}
}
return system_settings_form($form);
}
function nagios_status_page() {
drupal_page_is_cacheable(FALSE);
_nagios_update_os_user();
$args = func_get_args();
$module = array_shift($args);
$id = array_shift($args);
header("Pragma: no-cache");
header("Expires: 0");
$codes = nagios_status();
$ua = variable_get('nagios_ua', '');
$request_code = $_SERVER['HTTP_USER_AGENT'];
if (isset($_GET['unique_id'])) {
if (variable_get('nagios_enable_status_page_get', FALSE) == TRUE) {
$request_code = $_GET['unique_id'];
}
}
if ($request_code == $ua || user_access('administer site configuration')) {
if ($module) {
$nagios_data = [];
$nagios_data[$module] = module_invoke($module, 'nagios', $id);
}
else {
$nagios_data = nagios_invoke_all('nagios');
}
}
else {
$nagios_data = [
'nagios' => [
'DRUPAL' => [
'status' => NAGIOS_STATUS_UNKNOWN,
'type' => 'state',
'text' => t('Unauthorized'),
],
],
];
}
$severity = NAGIOS_STATUS_OK;
$min_severity = variable_get('nagios_min_report_severity', NAGIOS_STATUS_WARNING);
foreach ($nagios_data as $module_name => $module_data) {
foreach ($module_data as $key => $value) {
if (is_array($value) && array_key_exists('status', $value) && $value['status'] >= $min_severity) {
$severity = max($severity, $value['status']);
}
}
}
$output = "\n" . 'nagios=' . $codes[$severity] . ', ';
$output_state = [];
$output_perf = [];
foreach ($nagios_data as $module_name => $module_data) {
foreach ($module_data as $key => $value) {
switch ($value['type']) {
case 'state':
if ($value['status'] >= $min_severity) {
$tmp_state = $key . ':' . $codes[$value['status']];
}
else {
$tmp_state = $key . ':' . $codes[NAGIOS_STATUS_OK];
}
if (!empty($value['text'])) {
$tmp_state .= '=' . $value['text'];
}
if ($key == 'ADMIN' && $value['text'] == 'Module and theme update status' && variable_get('nagios_show_outdated_names', TRUE)) {
if (db_table_exists('cache_update')) {
$tmp_projects = update_calculate_project_data(_nagios_update_get_projects());
$nagios_ignored_modules = variable_get('nagios_ignored_modules', []);
$nagios_ignored_themes = variable_get('nagios_ignored_themes', []);
$nagios_ignored_projects = $nagios_ignored_modules + $nagios_ignored_themes;
$outdated_count = 0;
$tmp_modules = '';
foreach ($tmp_projects as $project_key => $project_val) {
if (!isset($nagios_ignored_projects[$project_key])) {
if ($project_val['status'] < UPDATE_CURRENT && $project_val['status'] >= UPDATE_NOT_SECURE) {
switch ($project_val['status']) {
case UPDATE_NOT_SECURE:
$tmp_project_status = t('NOT SECURE');
break;
case UPDATE_REVOKED:
$tmp_project_status = t('REVOKED');
break;
case UPDATE_NOT_SUPPORTED:
$tmp_project_status = t('NOT SUPPORTED');
break;
case UPDATE_NOT_CURRENT:
$tmp_project_status = t('NOT CURRENT');
break;
default:
$tmp_project_status = $project_val['status'];
}
$tmp_modules .= ' ' . $project_key . ':' . $tmp_project_status;
$outdated_count++;
}
}
}
if ($outdated_count > 0) {
$tmp_modules = trim($tmp_modules);
$tmp_state .= " ({$tmp_modules})";
}
}
else {
watchdog('nagios', t('The core update module was never installed so we cannot use update check features.'));
}
}
$output_state[] = $tmp_state;
break;
case 'perf':
$output_perf[] = $key . '=' . $value['text'];
break;
}
}
}
$output .= implode(', ', $output_state) . ' | ' . implode('; ', $output_perf) . "\n";
echo $output;
drupal_exit();
}
function nagios_invoke_all($hook = 'nagios') {
$return = [];
foreach (module_implements($hook) as $module) {
if ($hook == 'nagios' && !variable_get('nagios_enable_' . $module, 0)) {
continue;
}
$function = $module . '_' . $hook;
$result = $function();
$return[$module] = $result;
}
return $return;
}
function nagios_nagios_info() {
return [
'name' => 'Nagios Monitoring',
'id' => 'NAGIOS',
];
}
function nagios_nagios_settings() {
foreach (nagios_functions() as $function => $description) {
$var = 'nagios_func_' . $function;
$form[$var] = [
'#type' => 'checkbox',
'#title' => $function,
'#default_value' => variable_get($var, TRUE),
'#description' => $description,
];
}
$group = 'thresholds';
$form[$group] = [
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Thresholds'),
'#description' => t('Thresholds for reporting critical alerts to Nagios.'),
];
$form[$group]['nagios_cron_duration'] = [
'#type' => 'textfield',
'#title' => t('Cron duration'),
'#default_value' => variable_get('nagios_cron_duration', 60),
'#description' => t('Issue a critical alert when cron has not been running for this duration (in minutes). Default is 60 minutes.'),
];
if (module_exists('elysia_cron')) {
$form[$group]['nagios_elysia_cron_duration'] = [
'#type' => 'textfield',
'#title' => t('Elysia cron duration'),
'#default_value' => variable_get('nagios_elysia_cron_duration', 60),
'#description' => t('Issue a critical alert when elysia cron has not been running for this duration (in minutes). Default is 60 minutes.'),
];
}
$states_without_ok = array_diff_key(nagios_status(), [
NAGIOS_STATUS_OK => 'OK',
]);
$form[$group]['nagios_min_report_severity'] = [
'#type' => 'select',
'#title' => t('Minimum report severity'),
'#default_value' => variable_get('nagios_min_report_severity', NAGIOS_STATUS_WARNING),
'#options' => $states_without_ok,
'#description' => t('Issue an alert only for this minimum severity, not for lower severities.'),
];
return $form;
}
function nagios_form_system_modules_alter(&$form) {
if (isset($form['confirm']) || !user_access('administer nagios ignore')) {
return;
}
if (!db_table_exists('cache_update')) {
watchdog('nagios', t('The core update module was never installed so we cannot use update check features.'));
return;
}
$nagios_ignored_modules = variable_get('nagios_ignored_modules', []);
$projects_data = _nagios_update_get_projects();
foreach ($form['modules'] as $package_name => &$package) {
if ($package_name[0] != '#') {
foreach ($package as $module_name => &$module) {
if ($module_name[0] != '#') {
if (array_key_exists($module_name, $projects_data) && !empty($module['enable']['#default_value'])) {
$tooltip = [
'title' => t('If checked, this module’s update status and hook_requirements() implementation is ignored.'),
];
$module['links']['nagios_ignore'] = [
'#type' => 'checkbox',
'#title' => t('Ignore from Nagios'),
'#default_value' => !empty($nagios_ignored_modules[$module_name]),
'#attributes' => $tooltip,
];
if (!module_exists('coder_review')) {
$package['#theme'] = 'nagios_modules_fieldset';
}
}
}
}
unset($module);
}
}
unset($package);
$form['#submit'][] = 'nagios_system_modules_form_submit';
}
function nagios_system_modules_form_submit(&$form, &$form_state) {
$nagios_ignored_modules = [];
foreach ($form_state['values']['modules'] as $package_name => $package) {
if ($package_name[0] != '#') {
foreach ($package as $module_name => $module) {
if ($module_name[0] != '#') {
if (!empty($module['links']['nagios_ignore'])) {
$nagios_ignored_modules[$module_name] = TRUE;
}
}
}
}
}
variable_set('nagios_ignored_modules', $nagios_ignored_modules);
}
function nagios_form_system_theme_settings_alter(&$form, &$form_state) {
if (isset($form['confirm']) || !user_access('administer nagios ignore')) {
return;
}
if (!db_table_exists('cache_update')) {
watchdog('nagios', t('The core update module was never installed so we cannot use update check features.'));
return;
}
if (isset($form_state['build_info']['args'][0]) && !empty($form_state['build_info']['args'][0])) {
$theme_name = check_plain($form_state['build_info']['args'][0]);
$nagios_ignored_themes = variable_get('nagios_ignored_themes', []);
$projects_data = _nagios_update_get_projects();
if (array_key_exists($theme_name, $projects_data)) {
$form['nagios'] = [
'#type' => 'fieldset',
'#title' => t('Nagios Monitoring'),
'nagios_ignore' => [
'#type' => 'checkbox',
'#title' => t('Ignore from Nagios'),
'#weight' => 200,
'#default_value' => !empty($nagios_ignored_themes[$theme_name]),
],
];
$form['#submit'][] = 'nagios_system_theme_settings_form_submit';
}
}
}
function nagios_system_theme_settings_form_submit(&$form, &$form_state) {
if (isset($form_state['build_info']['args'][0]) && !empty($form_state['build_info']['args'][0])) {
$theme_name = check_plain($form_state['build_info']['args'][0]);
$nagios_ignored_themes = variable_get('nagios_ignored_themes', []);
if (!empty($form_state['values']['nagios_ignore'])) {
$nagios_ignored_themes[$theme_name] = TRUE;
}
else {
unset($nagios_ignored_themes[$theme_name]);
}
variable_set('nagios_ignored_themes', $nagios_ignored_themes);
}
}
function nagios_nagios($id = '') {
$status = [];
if (!empty($id) && variable_get('nagios_func_' . $id, FALSE)) {
$func = 'nagios_check_' . $id;
$result = $func();
$status[$result['key']] = $result['data'];
}
else {
foreach (nagios_functions() as $function => $description) {
if (variable_get('nagios_func_' . $function, TRUE)) {
$func = 'nagios_check_' . $function;
$result = $func();
$status[$result['key']] = $result['data'];
}
}
}
return $status;
}
function nagios_check_requirements() {
include_once DRUPAL_ROOT . '/' . './includes/install.inc';
module_load_include('inc', 'update', 'update.compare');
drupal_load_updates();
$project_data = _nagios_update_get_projects();
$nagios_ignored_modules = variable_get('nagios_ignored_modules', []);
$nagios_ignored_themes = variable_get('nagios_ignored_themes', []);
$nagios_ignored_projects = $nagios_ignored_modules + $nagios_ignored_themes;
$enabled_modules = [];
foreach ($project_data as $project) {
foreach ($project['includes'] as $key => $val) {
if (!isset($nagios_ignored_projects[$key])) {
$enabled_modules[] = $key;
}
}
}
$data = [];
if ($available = _nagios_update_get_available(TRUE)) {
$data = _nagios_update_calculate_project_data($available);
}
foreach ($nagios_ignored_projects as $key => $value) {
unset($data[$key]);
}
$reqs = [];
$module_data = [];
foreach ($enabled_modules as $module_name_outer) {
$requirements_data = module_invoke($module_name_outer, 'requirements', 'runtime');
if (is_array($requirements_data) && count($requirements_data)) {
if ($module_name_outer == 'update' && !empty($data)) {
unset($requirements_data['update_contrib']);
foreach ($enabled_modules as $module_name) {
if (!array_key_exists($module_name, $project_data['drupal']['includes'])) {
if (isset($data[$module_name]['status']) && is_numeric($data[$module_name]['status'])) {
$contrib_req = _update_requirement_check($data[$module_name], 'contrib');
$contrib_req['name'] = $module_name;
if (!isset($contrib_req['severity'])) {
$contrib_req['severity'] = -1;
}
if ($contrib_req) {
$module_data[] = $contrib_req;
}
}
}
}
usort($module_data, '_nagios_updates_sort_by_severity');
$requirements_data['update_contrib'] = array_pop($module_data);
}
$reqs += $requirements_data;
}
}
$descriptions = [];
$severity = REQUIREMENT_OK;
$min_severity = variable_get('nagios_min_report_severity', NAGIOS_STATUS_WARNING);
foreach ($reqs as $key => $requirement) {
if (isset($requirement['severity'])) {
if ($key == 'update_core' || $key == 'update_contrib') {
$grace_time = FALSE;
if ($requirement['severity'] == REQUIREMENT_WARNING && $requirement['reason'] == UPDATE_UNKNOWN) {
$grace_seconds = 60 * variable_get('nagios_cron_duration', 60);
$expire = db_query("SELECT expire FROM {cache_update} WHERE cid = :cid", [
':cid' => 'update_available_releases',
])
->fetchField();
if ($expire && time() < $expire + $grace_seconds) {
$grace_time = TRUE;
}
}
if ($grace_time) {
$requirement = cache_get('nagios_update_core_requirement');
}
else {
cache_set('nagios_update_core_requirement', $requirement);
}
}
if ($requirement['severity'] >= $min_severity) {
if ($requirement['severity'] > $severity) {
$severity = $requirement['severity'];
}
$descriptions[] = $requirement['title'];
}
}
}
if (empty($descriptions)) {
$desc = t('No information.');
}
else {
$desc = join(', ', $descriptions);
}
switch ($severity) {
case REQUIREMENT_OK:
case REQUIREMENT_INFO:
$data = [
'status' => NAGIOS_STATUS_OK,
'type' => 'state',
'text' => t('No known issues at this time.'),
];
break;
case REQUIREMENT_WARNING:
$data = [
'status' => NAGIOS_STATUS_WARNING,
'type' => 'state',
'text' => t('@desc', [
'@desc' => $desc,
]),
];
break;
case REQUIREMENT_ERROR:
$data = [
'status' => NAGIOS_STATUS_CRITICAL,
'type' => 'state',
'text' => t('@desc', [
'@desc' => $desc,
]),
];
break;
default:
$data = [
'status' => NAGIOS_STATUS_UNKNOWN,
'type' => 'state',
'text' => t('severity is @severity', [
'@severity' => $severity,
]),
];
break;
}
return [
'key' => 'ADMIN',
'data' => $data,
];
}
function nagios_check_watchdog() {
$query = db_select('watchdog', 'w');
$query
->fields('w', [
'wid',
'uid',
'type',
'severity',
'message',
'variables',
'link',
'location',
'hostname',
'timestamp',
]);
$query
->orderBy('timestamp', 'DESC');
$limit = variable_get('limit_watchdog_results', 50);
if (!empty($limit)) {
$offset = 0;
$query
->range($offset, $limit);
}
$limit_watchdog = variable_get('limit_watchdog_display', FALSE);
if (!empty($limit_watchdog)) {
$limit_watchdog_timestamp = variable_get('nagios_limit_watchdog_timestamp', FALSE);
if ($limit_watchdog_timestamp !== FALSE) {
$query
->condition('timestamp', $limit_watchdog_timestamp, '>');
}
}
$result = $query
->execute();
if (!$result) {
return [
'status' => NAGIOS_STATUS_UNKNOWN,
'type' => 'state',
'text' => t('Unable to SELECT FROM {watchdog}'),
];
}
$severity_translation = [
WATCHDOG_DEBUG => NAGIOS_STATUS_OK,
WATCHDOG_INFO => NAGIOS_STATUS_OK,
WATCHDOG_NOTICE => NAGIOS_STATUS_OK,
WATCHDOG_WARNING => NAGIOS_STATUS_WARNING,
WATCHDOG_ERROR => NAGIOS_STATUS_CRITICAL,
WATCHDOG_CRITICAL => NAGIOS_STATUS_CRITICAL,
WATCHDOG_ALERT => NAGIOS_STATUS_CRITICAL,
WATCHDOG_EMERGENCY => NAGIOS_STATUS_CRITICAL,
];
$severity = NAGIOS_STATUS_OK;
$min_severity = variable_get('nagios_min_report_severity', NAGIOS_STATUS_WARNING);
$messages = [];
$descriptions = [];
$count = 0;
while ($row = $result
->fetchAssoc()) {
if ($count == 0) {
variable_set('nagios_limit_watchdog_timestamp', $row['timestamp']);
$count++;
}
$nagios_severity = $severity_translation[$row['severity']];
if ($nagios_severity < $min_severity) {
continue;
}
if ($nagios_severity > $severity) {
$severity = $nagios_severity;
}
$message = "{$row['type']} {$row['message']}";
$variables = unserialize($row['variables']);
if (is_array($variables)) {
foreach ($variables as $key => $value) {
$message = str_replace($key, $value, $message);
}
}
if (!in_array($message, $messages, TRUE)) {
$messages[] = $message;
}
else {
continue;
}
$message = date('Y-m-d H:i:s', $row['timestamp']) . " " . $message;
$descriptions[] = $message;
}
$desc = join(', ', $descriptions);
return [
'key' => 'WATCHDOG',
'data' => [
'status' => $severity,
'type' => 'state',
'text' => t('@desc', [
'@desc' => $desc,
]),
],
];
}
function nagios_check_cron() {
$cron_last = variable_get('cron_last', 0);
$minutes = variable_get('nagios_cron_duration', 60);
if (REQUEST_TIME > $cron_last + $minutes * 60) {
$data = [
'status' => NAGIOS_STATUS_CRITICAL,
'type' => 'state',
'text' => t('cron did not run within last @minutes minutes', [
'@minutes' => $minutes,
]),
];
}
else {
$data = [
'status' => NAGIOS_STATUS_OK,
'type' => 'state',
'text' => '',
];
}
return [
'key' => 'CRON',
'data' => $data,
];
}
function nagios_check_elysia_cron() {
$result = db_query('select * from elysia_cron where last_aborted <> 0');
$err_crons = [];
foreach ($result as $blocked_cron) {
$err_crons[] = $blocked_cron;
}
$text = '';
foreach ($err_crons as $cron) {
$text .= t('Elysia cron ":cron" last aborted on ":abort"' . PHP_EOL, [
':cron' => $cron->name,
':abort' => $cron->last_abort_function,
]);
}
if (!empty($err_crons)) {
$data = [
'status' => NAGIOS_STATUS_CRITICAL,
'type' => 'state',
'text' => $text,
];
}
else {
$data = [
'status' => NAGIOS_STATUS_OK,
'type' => 'state',
'text' => '',
];
}
return [
'key' => 'ELYSIA_CRON',
'data' => $data,
];
}
function nagios_check_session_anon() {
$interval = REQUEST_TIME - 900;
$count = (int) db_query("SELECT COUNT(*) FROM {sessions} WHERE timestamp >= :timestamp AND uid = :uid", [
':timestamp' => $interval,
':uid' => 0,
])
->fetchField();
$data = [
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
];
return [
'key' => 'SAN',
'data' => $data,
];
}
function nagios_check_session_auth() {
$interval = REQUEST_TIME - 900;
$count = (int) db_query("SELECT COUNT(*) FROM {sessions} WHERE timestamp >= :timestamp AND uid > :uid", [
':timestamp' => $interval,
':uid' => 0,
])
->fetchField();
$data = [
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
];
return [
'key' => 'SAU',
'data' => $data,
];
}
function nagios_check_nodes() {
$count = (int) db_query("SELECT COUNT(*) FROM {node} WHERE status = :status", [
':status' => 1,
])
->fetchField();
$data = [
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
];
return [
'key' => 'NOD',
'data' => $data,
];
}
function nagios_check_users() {
$count = (int) db_query("SELECT COUNT(*) FROM {users} WHERE status = :status", [
':status' => 1,
])
->fetchField();
$data = [
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
];
return [
'key' => 'USR',
'data' => $data,
];
}
function nagios_check_modules() {
$count = (int) db_query("SELECT COUNT(*) FROM {system} WHERE status = :status AND type = :type", [
':status' => 1,
':type' => 'module',
])
->fetchField();
$data = [
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
];
return [
'key' => 'MOD',
'data' => $data,
];
}
function nagios_nagios_checks() {
return nagios_functions();
}
function nagios_nagios_check($function) {
$func = 'nagios_check_' . $function;
$result = $func();
$status[$result['key']] = $result['data'];
return $status;
}
function nagios_check_themes() {
$count = (int) db_query("SELECT COUNT(*) FROM {system} WHERE status = :status AND type = :type", [
':status' => 1,
':type' => 'theme',
])
->fetchField();
$data = [
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
];
return [
'key' => 'THM',
'data' => $data,
];
}
function nagios_theme() {
return [
'nagios_modules_fieldset' => [
'render element' => 'form',
],
];
}
function theme_nagios_modules_fieldset($variables) {
$form = $variables['form'];
$rows = [];
$children = element_children($form);
$operations = drupal_map_assoc([
'help',
'permissions',
'configure',
]);
foreach ($children as $key) {
$links = array_filter(array_keys($form[$key]['links']), function ($var) {
return $var && $var[0] != '#';
});
if ($links) {
$operations += drupal_map_assoc($links);
}
}
foreach ($children as $key) {
$module = $form[$key];
$row = [];
unset($module['enable']['#title']);
$row[] = [
'class' => [
'checkbox',
],
'data' => drupal_render($module['enable']),
];
$label = '<label';
if (isset($module['enable']['#id'])) {
$label .= ' for="' . $module['enable']['#id'] . '"';
}
$row[] = $label . '><strong>' . drupal_render($module['name']) . '</strong></label>';
$row[] = drupal_render($module['version']);
$description = drupal_render($module['description']);
if ($module['#requires']) {
$description .= '<div class="admin-requirements">' . t('Requires: !module-list', [
'!module-list' => implode(', ', $module['#requires']),
]) . '</div>';
}
if ($module['#required_by']) {
$description .= '<div class="admin-requirements">' . t('Required by: !module-list', [
'!module-list' => implode(', ', $module['#required_by']),
]) . '</div>';
}
$row[] = [
'data' => $description,
'class' => [
'description',
],
];
foreach ($operations as $key_inner) {
$row[] = [
'data' => drupal_render($module['links'][$key_inner]),
'class' => [
$key_inner,
],
];
}
$form['#header'][4]['colspan'] = count($operations);
$rows[] = $row;
}
return theme('table', [
'header' => $form['#header'],
'rows' => $rows,
]);
}
function _nagios_updates_sort_by_severity($a, $b) {
if (isset($a['severity'], $b['severity'])) {
if ($a['severity'] == $b['severity']) {
return 0;
}
return $a['severity'] < $b['severity'] ? -1 : 1;
}
return 0;
}
function _nagios_update_get_projects() {
if (module_exists('update')) {
return update_get_projects();
}
module_load_include('inc', 'update', 'update.compare');
$projects =& drupal_static(__FUNCTION__, []);
if (empty($projects)) {
$module_data = system_rebuild_module_data();
$theme_data = system_rebuild_theme_data();
_update_process_info_list($projects, $module_data, 'module', TRUE);
_update_process_info_list($projects, $theme_data, 'theme', TRUE);
if (variable_get('update_check_disabled', FALSE)) {
_update_process_info_list($projects, $module_data, 'module', FALSE);
_update_process_info_list($projects, $theme_data, 'theme', FALSE);
}
drupal_alter('update_projects', $projects);
}
return $projects;
}
function _nagios_update_get_available($refresh = FALSE) {
if (!module_exists('update')) {
if (db_table_exists('cache_update')) {
drupal_load('module', 'update');
return update_get_available($refresh);
}
watchdog('nagios', t('The core update module was never installed so we cannot use update check features.'));
}
else {
return update_get_available($refresh);
}
return [];
}
function _nagios_update_calculate_project_data($available = []) {
if (!module_exists('update')) {
if (db_table_exists('cache_update')) {
module_load_include('inc', 'update', 'update.compare');
return update_calculate_project_data($available);
}
watchdog('nagios', t('The core update module was never installed so we cannot use update check features.'));
}
else {
return update_calculate_project_data($available);
}
return [];
}