View source
<?php
define("HEALTH_OKAY", 'okay');
define("HEALTH_WARNING", 'warning');
define("HEALTH_ERROR", 'error');
define("HEALTH_UNKNOWN", 'unknown');
function health_init() {
if (arg(1) == 'reports' && arg(2) == 'health') {
drupal_add_css(drupal_get_path('module', 'health') . '/css/health.css', array(
'type' => 'file',
));
}
}
function health_permission() {
return array(
'access health report' => array(
'title' => t('Access the system health report.'),
'description' => t('Allows a user to see all information in the health report'),
),
);
}
function health_menu() {
$items['admin/config/system/health'] = array(
'title' => 'Health settings',
'description' => 'Manage your Health page and API settings.',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'health_admin_settings_form',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'health.admin.inc',
);
$items['admin/reports/health/dashboard'] = array(
'title' => 'Health dashboard',
'description' => 'View the health of your site over the past few days.',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'health_admin_dashboard_form',
),
'access arguments' => array(
'access health report',
),
'file' => 'health.admin.inc',
);
$items['health/api/%'] = array(
'title' => 'Health API',
'description' => 'Health API callback.',
'type' => MENU_CALLBACK,
'page callback' => 'health_api_callback',
'page arguments' => array(
2,
),
'access callback' => 'health_api_access',
'file' => 'health.service.inc',
);
return $items;
}
function health_theme() {
return array(
'health_status_email' => array(
'template' => 'templates/health_status_email',
'variables' => array(),
),
'health_help_page' => array(
'template' => 'templates/health_help_page',
'variables' => array(),
),
);
}
function health_cron() {
$last_run = variable_get('health_cron_last', 0);
$frequency = variable_get('health_email_frequency');
if ($frequency !== "never" && ($frequency === "cron" || REQUEST_TIME - $frequency < $last_run)) {
if (health_email_send()) {
health_cron_email_send();
}
}
}
function health_email_send() {
$thresholds = array_keys(array_filter(variable_get('health_email_threshold', array())));
$data = health_get_data();
foreach ($data as $group => $results) {
foreach ($results as $monitor => $r) {
if (in_array($r['status'], $thresholds)) {
return TRUE;
}
}
}
return FALSE;
}
function health_cron_email_send() {
module_load_include('inc', 'health', 'health.admin');
$emails = explode(PHP_EOL, variable_get('health_email_users'));
$data = health_get_data();
$monitors = health_get_monitors();
$tables = array();
foreach ($data as $group => $results) {
$tables[$group] = theme('table', array(
'rows' => health_admin_dashboard_rows($monitors[$group], $results),
'header' => health_admin_dashboard_headers(),
));
}
$params = array(
'site_name' => variable_get('site_name'),
'data' => health_get_data(),
'tables' => $tables,
);
foreach ($emails as $to) {
drupal_mail('health', 'health_status', $to, language_default(), $params, variable_get('site_email'), TRUE);
}
}
function health_mail($key, &$message, $params) {
switch ($key) {
case "health_status":
$message['subject'] = variable_get('health_email_subject');
$message['body'][] = theme('health_status_email', $params);
$message['headers']['Content-Type'] = "text/html; charset=iso-8859-1";
$message['headers']['Mime-Version'] = "1.0";
break;
}
}
function health_get_data() {
$monitors = health_get_monitors();
foreach ($monitors as $group => $checks) {
foreach ($checks as $key => $check) {
$results[$group][$key] = module_invoke_all('health_monitor_' . $key, $check['args']);
}
}
drupal_alter('health_data', $results);
return (array) $results;
}
function health_get_monitors() {
$monitors = module_invoke_all('health_monitors');
$grouped = array();
foreach ($monitors as $key => $m) {
$grouped[$m['group']][$key] = $m;
}
drupal_alter('health_monitors', $grouped);
return $grouped;
}
function health_monitor_status($status, $message) {
return array(
'status' => $status,
'message' => $message,
);
}
function health_api_access() {
if (!isset($_POST['key']) || empty($_POST['key'])) {
return FALSE;
}
if (variable_get('health_api_require_https', TRUE) && $_SERVER['HTTPS'] !== "on") {
return FALSE;
}
$key = $_POST['key'];
if ($key !== variable_get('health_api_access_key', FALSE)) {
return FALSE;
}
return TRUE;
}
function health_help($path, $arg) {
switch ($path) {
case "admin/help#health":
return theme('health_help_page');
}
}