You are here

function nagios_check_watchdog in Nagios Monitoring 7

Same name and namespace in other branches
  1. 8 nagios.module \nagios_check_watchdog()
  2. 6 nagios.module \nagios_check_watchdog()

Check Drupal {watchdog} table recent entries.

Return value

array

File

./nagios.module, line 841

Code

function nagios_check_watchdog() {

  //@TODO Allow LIMIT and OFFSET to be passed in or configurable, or use existing Drupal settings

  //@TODO Manually count the rows on admin/reports/dblog and match that if nothing else

  //@TODO Do this more the Drupal Way, whatever that might be

  //@TODO Allow multi-value 'type' and/or 'severity' inputs for filtering

  //@TODO Allow datetime ranges

  // Construct base select query.
  $query = db_select('watchdog', 'w');
  $query
    ->fields('w', [
    'wid',
    'uid',
    'type',
    'severity',
    'message',
    'variables',
    'link',
    'location',
    'hostname',
    'timestamp',
  ]);
  $query
    ->orderBy('timestamp', 'DESC');

  // Get watchdog result limit.
  $limit = variable_get('limit_watchdog_results', 50);
  if (!empty($limit)) {

    // Get range.
    $offset = 0;

    // Set range.
    $query
      ->range($offset, $limit);
  }

  // Check if we are limiting to only new logs since last check.
  $limit_watchdog = variable_get('limit_watchdog_display', FALSE);
  if (!empty($limit_watchdog)) {

    // Get timestamp of the last watchdog entry retrieved.
    $limit_watchdog_timestamp = variable_get('nagios_limit_watchdog_timestamp', FALSE);
    if ($limit_watchdog_timestamp !== FALSE) {

      // Ensure we only get entries that are greater then the timestamp.
      $query
        ->condition('timestamp', $limit_watchdog_timestamp, '>');
    }
  }

  // Execute query.
  $result = $query
    ->execute();
  if (!$result) {
    return [
      'status' => NAGIOS_STATUS_UNKNOWN,
      'type' => 'state',
      'text' => t('Unable to SELECT FROM {watchdog}'),
    ];
  }

  //RFC3164/Watchdog has 8 levels. Nagios module has 3 (plus UNKNOWN). This maps one to the other.
  $severity_translation = [
    //watchdog => nagios
    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;

  //max this across the result set
  $min_severity = variable_get('nagios_min_report_severity', NAGIOS_STATUS_WARNING);
  $messages = [];
  $descriptions = [];
  $count = 0;
  while ($row = $result
    ->fetchAssoc()) {

    // Set timestamp of the first watchdog error for use when restricting logs to only new entries.
    if ($count == 0) {
      variable_set('nagios_limit_watchdog_timestamp', $row['timestamp']);
      $count++;
    }

    // Get severity of log entry.
    $nagios_severity = $severity_translation[$row['severity']];

    // Only continue if severity is greater then the min severity set.
    if ($nagios_severity < $min_severity) {
      continue;
    }

    // If the severity is greater then our current severity level, set it it to new level.
    if ($nagios_severity > $severity) {
      $severity = $nagios_severity;
    }

    // Create error message.
    $message = "{$row['type']} {$row['message']}";

    //@TODO Untangle l(truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE), 'admin/reports/event/'. $dblog->wid, array('html' => TRUE)) and use it here
    $variables = unserialize($row['variables']);
    if (is_array($variables)) {
      foreach ($variables as $key => $value) {
        $message = str_replace($key, $value, $message);
      }
    }

    // Add message to messages array only if there isn't already an identical entry.
    if (!in_array($message, $messages, TRUE)) {
      $messages[] = $message;
    }
    else {

      // We only want to show each message once so continue.
      continue;
    }

    // Prepend the timestamp onto the front of the message.
    $message = date('Y-m-d H:i:s', $row['timestamp']) . " " . $message;

    // Add message to descriptions array.
    $descriptions[] = $message;
  }

  // Join all descriptions together into a string.
  $desc = join(', ', $descriptions);

  //why is an extra array wrapped around here?
  return [
    'key' => 'WATCHDOG',
    'data' => [
      'status' => $severity,
      'type' => 'state',
      'text' => t('@desc', [
        '@desc' => $desc,
      ]),
    ],
  ];
}