View source
<?php
define('NAGIOS_STATUS_OK', 0);
define('NAGIOS_STATUS_UNKNOWN', 1);
define('NAGIOS_STATUS_WARNING', 2);
define('NAGIOS_STATUS_CRITICAL', 3);
function nagios_status() {
return array(
NAGIOS_STATUS_OK => 'OK',
NAGIOS_STATUS_UNKNOWN => 'UNKNOWN',
NAGIOS_STATUS_WARNING => 'WARNING',
NAGIOS_STATUS_CRITICAL => 'CRITICAL',
);
}
function nagios_functions() {
return array(
'requirements',
'cron',
'session_anon',
'session_auth',
'nodes',
'users',
'modules',
'themes',
);
}
function nagios_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/nagios',
'type' => MENU_NORMAL_ITEM,
'title' => t('Nagios monitoring'),
'description' => t('Settings for Nagios monitoring'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'nagios_settings',
),
'access' => user_access('administer site configuration'),
);
$items[] = array(
'path' => 'nagios',
'type' => MENU_SUGGESTED_ITEM,
'title' => t('Nagios status page'),
'callback' => 'nagios_status_page',
'access' => TRUE,
);
}
return $items;
}
function nagios_settings() {
$group = 'modules';
$form['nagios_ua'] = array(
'#type' => 'textfield',
'#title' => t('Unique ID'),
'#default_value' => variable_get('nagios_ua', 'Nagios'),
'#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[$group] = array(
'#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] = array(
'#type' => 'checkbox',
'#title' => $data['name'] . ' (' . $module . ')',
'#default_value' => variable_get('nagios_enable_' . $module, TRUE),
);
}
foreach (nagios_invoke_all('nagios_settings') as $module => $module_settings) {
$form[$module] = array(
'#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() {
$skip_invoke = FALSE;
header("Pragma: no-cache");
header("Expires: 0");
$codes = nagios_status();
$ua = variable_get('nagios_ua', 'Nagios');
if ($_SERVER['HTTP_USER_AGENT'] != $ua) {
$skip_invoke = TRUE;
$nagios_data = array(
'nagios' => array(
'DRUPAL' => array(
'status' => NAGIOS_STATUS_UNKNOWN,
'type' => 'state',
'text' => t('Unauthorized'),
),
),
);
}
if (!$skip_invoke) {
$nagios_data = nagios_invoke_all('nagios');
}
$severity = NAGIOS_STATUS_OK;
foreach ($nagios_data as $module_name => $module_data) {
foreach ($module_data as $key => $value) {
$severity = max($severity, $value['status']);
}
}
$output = "\n" . 'nagios=' . $codes[$severity] . ', ';
$output_state = array();
$output_perf = array();
foreach ($nagios_data as $module_name => $module_data) {
foreach ($module_data as $key => $value) {
switch ($value['type']) {
case 'state':
$tmp_state = $key . ':' . $codes[$value['status']];
if (!empty($value['text'])) {
$tmp_state .= '=' . $value['text'];
}
$output_state[] = $tmp_state;
break;
case 'perf':
$output_perf[] = $key . '=' . $value['text'];
break;
}
}
}
$output .= implode(', ', $output_state) . ' | ' . implode(';', $output_perf) . "\n";
print theme('page', $output);
exit;
}
function nagios_invoke_all($hook = 'nagios') {
$return = array();
$args = func_get_args();
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
$result = call_user_func_array($function, $args);
$return[$module] = $result;
}
return $return;
}
function nagios_nagios_info() {
return array(
'name' => 'Nagios monitoring',
'id' => 'NAGIOS',
);
}
function nagios_nagios_settings() {
foreach (nagios_functions() as $function) {
$var = 'nagios_func_' . $function;
$form[$var] = array(
'#type' => 'checkbox',
'#title' => $function,
'#default_value' => variable_get($var, TRUE),
);
}
$group = 'thresholds';
$form[$group] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Thresholds'),
'#description' => t('Thresholds for reporting critical alerts to Nagios.'),
);
$form[$group]['nagios_cron_duration'] = array(
'#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.'),
);
return $form;
}
function nagios_nagios() {
$status = array();
foreach (nagios_functions() as $function) {
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 './includes/install.inc';
drupal_load_updates();
$reqs = module_invoke_all('requirements', 'runtime');
$severity = REQUIREMENT_OK;
foreach ($reqs as $key => $requirement) {
if (isset($requirement['severity'])) {
if ($requirement['severity'] > $severity) {
$severity = $requirement['severity'];
$desc = $requirement['title'];
}
}
}
switch ($severity) {
case REQUIREMENT_OK:
case REQUIREMENT_INFO:
$data = array(
'status' => NAGIOS_STATUS_OK,
'type' => 'state',
'text' => '',
);
break;
case REQUIREMENT_WARNING:
$data = array(
'status' => NAGIOS_STATUS_WARNING,
'type' => 'state',
'text' => t('@desc', array(
'@desc' => $desc,
)),
);
break;
case REQUIREMENT_ERROR:
$data = array(
'status' => NAGIOS_STATUS_CRITICAL,
'type' => 'state',
'text' => t('@desc', array(
'@desc' => $desc,
)),
);
break;
default:
$data = array(
'status' => NAGIOS_STATUS_UNKNOWN,
'type' => 'state',
'text' => t('severity is @severity', array(
'@severity' => $severity,
)),
);
break;
}
return array(
'key' => 'ADMIN',
'data' => $data,
);
}
function nagios_check_cron() {
$cron_last = variable_get('cron_last', 0);
$mins = variable_get('nagios_cron_duration', 60);
if (time() > $cron_last + $mins * 60) {
$data = array(
'status' => NAGIOS_STATUS_CRITICAL,
'type' => 'state',
'text' => t('cron not running @mins mins', array(
'@mins' => $mins,
)),
);
}
else {
$data = array(
'status' => NAGIOS_STATUS_OK,
'type' => 'state',
'text' => '',
);
}
return array(
'key' => 'CRON',
'data' => $data,
);
}
function nagios_check_session_anon() {
$interval = time() - 900;
$count = (int) sess_count($interval, TRUE);
$data = array(
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
);
return array(
'key' => 'SAN',
'data' => $data,
);
}
function nagios_check_session_auth() {
$interval = time() - 900;
$count = (int) sess_count($interval, FALSE);
$data = array(
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
);
return array(
'key' => 'SAU',
'data' => $data,
);
}
function nagios_check_nodes() {
$count = (int) db_result(db_query("SELECT COUNT(*) FROM {node} WHERE status = 1"));
$data = array(
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
);
return array(
'key' => 'NOD',
'data' => $data,
);
}
function nagios_check_users() {
$count = (int) db_result(db_query("SELECT COUNT(*) FROM {users} WHERE status = 1"));
$data = array(
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
);
return array(
'key' => 'USR',
'data' => $data,
);
}
function nagios_check_modules() {
$count = (int) db_result(db_query("SELECT COUNT(*) FROM {system} WHERE status = 1 AND type = 'module'"));
$data = array(
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
);
return array(
'key' => 'MOD',
'data' => $data,
);
}
function nagios_check_themes() {
$count = (int) db_result(db_query("SELECT COUNT(*) FROM {system} WHERE status = 1 AND type = 'theme'"));
$data = array(
'status' => NAGIOS_STATUS_OK,
'type' => 'perf',
'text' => $count,
);
return array(
'key' => 'THM',
'data' => $data,
);
}