summaryrefslogtreecommitdiff
path: root/modules/watchdog/watchdog.module
blob: a2be7242c795d9320b7ddd2fd76910830c61bf25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
// $Id$

/**
 * Implementation of hook_help().
 */
function watchdog_help($section = 'admin/help#watchdog') {
  switch ($section) {
    case 'admin/help#watchdog':
      return t('
      <p>Watchdog module monitors your web site, capturing system events in a log to be reviewed by an authorized individual at a later time.  The watchdog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information.  It is vital to <a href="%watchdog">check the watchdog report</a> on a regular basis as it is often the only way to tell what is going on.</p>
      <p>To ease administration, the watchdog will automatically discard old log entries, <a href="%log-entry">as configured</a>. Needs "cron.php" to discard the entries.</p>', array('%watchdog' => url('admin/logs'), '%log-entry' => url('admin/system/modules/watchdog')));
    case 'admin/logs':
      return t('The watchdog module monitors your web site, captures system events in a log and records them to be reviewed by an authorized individual at a later time.  The watchdog log is simply a list of events recorded during operation and contains usage data, performance data, errors, warnings and operational information. It is vital to check the watchdog report on a regular basis as it is often the only way to tell what is going on.');
    case 'admin/logs/user':
      return t('Watchdog events that have to do with users.');
    case 'admin/logs/regular':
      return t('Watchdog events that are "normal" and have no other classification.');
    case 'admin/logs/httpd':
      return t('Watchdog events that are from the web server.');
    case 'admin/logs/special':
      return t('Watchdog events about adding, changing, and moderating nodes and comments.');
    case 'admin/logs/search':
      return t('Watchdog events showing what users searched for.');
    case 'admin/logs/error':
      return t('Watchdog events about PHP and database errors.');
    case 'admin/logs/warning':
      return t('Watchdog warning events. These events don\'t stop Drupal from running, but are things you should know.');
    case 'admin/system/modules#description':
      return t('Logs and records system events.');
    case 'admin/system/modules/watchdog':
      return t('Watchdog logs your system events.  To see these events go to the <a href="%watchdog">logs</a>. Since these logs can grow out of control if kept around forever, below set how long an item should be kept in the log.  Note that to discard entries as set below you must run "cron.php" regularly.', array('%watchdog' => url('admin/logs')));
  }
}

/**
 * Implementation of hook_perm().
 */
function watchdog_perm() {
  return array('administer watchdog');
}

/**
 * Implementation of hook_link().
 */
function watchdog_link($type) {
  if ($type == 'system') {
    menu('admin/logs', t('logs'), user_access('administer watchdog') ? 'watchdog_overview' : MENU_DENIED, 7);
    menu('admin/logs/view', t('view details'), user_access('administer watchdog') ? 'watchdog_view' : MENU_DENIED, 0, MENU_HIDE, MENU_LOCKED);

    if (arg(1) == 'logs') {
      foreach (_watchdog_get_message_types() as $type) {
        menu("admin/logs/$type", t($type), MENU_FALLTHROUGH, 0, MENU_SHOW, MENU_LOCKED);
      }
    }
  }
}

/**
 * Implementation of hook_settings().
 */
function watchdog_settings() {
  $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
  $period[1000000000] = t('Never');
  $output .= form_select(t('Discard entries older than'), 'watchdog_clear', variable_get('watchdog_clear', 604800), $period, t('The time watchdog entries should be kept.  Older entries will be automatically discarded.  Requires crontab.'));
  return $output;
}

/**
 * Implementation of hook_cron().
 *
 * Remove expired log messages.
 */
function watchdog_cron() {
  db_query('DELETE FROM {watchdog} WHERE '. time() .' - timestamp > '. variable_get('watchdog_clear', 604800));
}

/**
 * Menu callback; displays a listing of log messages.
 */
function watchdog_overview($type = '') {
  foreach (_watchdog_get_message_types() as $key) {
    $query[$key] = "WHERE type = '". check_query($key) ."'";
  }
  $query['actions'] = "WHERE link != ''";

  $header = array(
    array('data' => t('date'), 'field' => 'w.timestamp', 'sort' => 'desc'),
    array('data' => t('message'), 'field' => 'w.message'),
    array('data' => t('user'), 'field' => 'u.name'),
    array('data' => t('operations'), 'colspan' => '2')
  );
  $sql = 'SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid '. ($type ? $query[$type] : '');
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);

  while ($watchdog = db_fetch_object($result)) {
    $rows[] = array(
      array('data' => format_date($watchdog->timestamp, 'small'), 'class' => "watchdog-$watchdog->type"),
      array('data' => truncate_utf8(strip_tags($watchdog->message), 64), 'class' => "watchdog-$watchdog->type"),
      array('data' => format_name($watchdog), 'class' => "watchdog-$watchdog->type"),
      array('data' => $watchdog->link, 'class' => "watchdog-$watchdog->type"),
      array('data' => l(t('view details'), "admin/logs/view/$watchdog->wid"), 'class' => "watchdog-$watchdog->type")
    );
  }

  if (!$rows) {
    $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => '5'));
  }

  $pager = theme('pager', NULL, 50, 0, tablesort_pager());
  if (!empty($pager)) {
    $rows[] = array(array('data' => $pager, 'colspan' => '5'));
  }
  print theme('page', theme('table', $header, $rows));
}

/**
 * Menu callback; displays details about a log message.
 */
function watchdog_view($id) {
  $output = '';
  $result = db_query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = %d', $id);
  if ($watchdog = db_fetch_object($result)) {
    $output .= '<table border="1" cellpadding="2" cellspacing="2">';
    $output .= ' <tr><th>'. t('Type') ."</th><td>$watchdog->type</td></tr>";
    $output .= ' <tr><th>'. t('Date') .'</th><td>'. format_date($watchdog->timestamp, 'large') .'</td></tr>';
    $output .= ' <tr><th>'. t('User') .'</th><td>'. format_name($watchdog) .'</td></tr>';
    $output .= ' <tr><th>'. t('Location') ."</th><td>$watchdog->location</td></tr>";
    $output .= ' <tr><th>'. t('Message') ."</th><td>$watchdog->message</td></tr>";
    $output .= ' <tr><th>'. t('Hostname') ."</th><td>$watchdog->hostname</td></tr>";
    $output .= '</table>';
  }
  print theme('page', $output);
}

function _watchdog_get_message_types() {
  $types = array();

  $result = db_query('SELECT DISTINCT(type) FROM {watchdog}');
  while ($object = db_fetch_object($result)) {
    $types[] = $object->type;
  }

  return $types;
}

?>