initial import from onelab svn codebase
[plewww.git] / modules / watchdog.module
1 <?php
2 // $Id: watchdog.module 144 2007-03-28 07:52:20Z thierry $
3
4 /**
5  * @file
6  * System monitoring and logging for administrators.
7  *
8  * The watchdog module monitors your site and keeps a list of
9  * recorded events containing usage and performance data, errors,
10  * warnings, and similar operational information.
11  *
12  * @see watchdog().
13  */
14
15 /**
16  * Implementation of hook_help().
17  */
18 function watchdog_help($section) {
19   switch ($section) {
20     case 'admin/help#watchdog':
21       $output = '<p>'. t('The watchdog module monitors your system, capturing system events in a log to be reviewed by an authorized individual at a later time.  This is useful for site administrators who want a quick overview of activities on their site. The logs also record the sequence of events, so it can be useful for debugging site errors.') .'</p>';
22       $output .= '<p>'. t('The watchdog log is simply a list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the watchdog report on a regular basis to ensure their site is working properly.') .'</p>';
23       $output .= t('<p>You can</p>
24 <ul>
25 <li>view watchdog logs at <a href="%admin-watchdog">administer &gt;&gt; watchdog</a>.</li>
26 <li>view watchdog event logs at <a href="%admin-watchdog-events">administer &gt;&gt; watchdog &gt;&gt; events</a>.</li>
27 </ul>
28 ', array('%admin-watchdog' => url('admin/watchdog'), '%admin-watchdog-events' => url('admin/watchdog/events')));
29       $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%watchdog">Watchdog page</a>.', array('%watchdog' => 'http://drupal.org/handbook/modules/watchdog/')) .'</p>';
30       return $output;
31     case 'admin/modules#description':
32       return t('Logs and records system events.');
33     case 'admin/logs':
34       return t('<p>The 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 check the watchdog report on a regular basis as it is often the only way to tell what is going on.</p>');
35   }
36 }
37
38 /**
39  * Implementation of hook_menu().
40  */
41 function watchdog_menu($may_cache) {
42   $items = array();
43
44   if ($may_cache) {
45     $items[] = array('path' => 'admin/logs', 'title' => t('logs'),
46       'callback' => 'watchdog_overview');
47     $items[] = array('path' => 'admin/logs/event', 'title' => t('details'),
48       'callback' => 'watchdog_event',
49       'type' => MENU_CALLBACK);
50   }
51   return $items;
52 }
53
54 /**
55  * Implementation of hook_cron().
56  *
57  * Remove expired log messages and flood control events.
58  */
59 function watchdog_cron() {
60   db_query('DELETE FROM {watchdog} WHERE timestamp < %d', time() - variable_get('watchdog_clear', 604800));
61   db_query('DELETE FROM {flood} WHERE timestamp < %d', time() - 3600);
62 }
63
64 /**
65  * Implementation of hook_user().
66  */
67 function watchdog_user($op, &$edit, &$user) {
68   if ($op == 'delete') {
69     db_query('UPDATE {watchdog} SET uid = 0 WHERE uid = %d', $user->uid);
70   }
71 }
72
73 /**
74  * Menu callback; displays a listing of log messages.
75  */
76 function watchdog_overview() {
77   $icons = array(WATCHDOG_NOTICE  => '',
78                  WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
79                  WATCHDOG_ERROR   => theme('image', 'misc/watchdog-error.png', t('error'), t('error')));
80   $classes = array(WATCHDOG_NOTICE => 'watchdog-notice', WATCHDOG_WARNING => 'watchdog-warning', WATCHDOG_ERROR => 'watchdog-error');
81
82   $names['all'] = t('all messages');
83   foreach (_watchdog_get_message_types() as $type) {
84     $names[$type] = t('%type messages', array('%type' => t($type)));
85   }
86
87   if (empty($_SESSION['watchdog_overview_filter'])) {
88     $_SESSION['watchdog_overview_filter'] = 'all';
89   }
90
91   $form['filter'] = array(
92     '#type' => 'select',
93     '#title' => t('Filter by message type'),
94     '#options' => $names,
95     '#default_value' => $_SESSION['watchdog_overview_filter']
96   );
97   $form['#action'] = url('admin/logs');
98
99   $form['submit'] = array('#type' => 'submit', '#value' =>t('Filter'));
100   $output = drupal_get_form('watchdog_form_overview', $form);
101
102   $header = array(
103     ' ',
104     array('data' => t('Type'), 'field' => 'w.type'),
105     array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
106     array('data' => t('Message'), 'field' => 'w.message'),
107     array('data' => t('User'), 'field' => 'u.name'),
108     array('data' => t('Operations'))
109   );
110
111   $sql = "SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
112   $tablesort = tablesort_sql($header);
113   $type = $_SESSION['watchdog_overview_filter'];
114   if ($type != 'all') {
115     $result = pager_query($sql ." WHERE w.type = '%s'". $tablesort, 50, 0, NULL, $type);
116   }
117   else {
118     $result = pager_query($sql . $tablesort, 50);
119   }
120
121   while ($watchdog = db_fetch_object($result)) {
122     $rows[] = array('data' =>
123       array(
124         // Cells
125         $icons[$watchdog->severity],
126         t($watchdog->type),
127         format_date($watchdog->timestamp, 'small'),
128         l(truncate_utf8($watchdog->message, 56, TRUE, TRUE), 'admin/logs/event/'. $watchdog->wid, array(), NULL, NULL, FALSE, TRUE),
129         theme('username', $watchdog),
130         $watchdog->link,
131       ),
132       // Attributes for tr
133       'class' => "watchdog-". preg_replace('/[^a-z]/i', '-', $watchdog->type) .' '. $classes[$watchdog->severity]
134     );
135   }
136
137   if (!$rows) {
138     $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
139   }
140
141   $output .= theme('table', $header, $rows);
142   $output .= theme('pager', NULL, 50, 0);
143
144   return $output;
145 }
146
147 function theme_watchdog_form_overview($form) {
148   return '<div class="container-inline">'. form_render($form) .'</div>';
149 }
150
151 function watchdog_form_overview_submit($form_id, $form) {
152   global $form_values;
153   $_SESSION['watchdog_overview_filter'] = $form_values['filter'];
154 }
155
156 /**
157  * Menu callback; displays details about a log message.
158  */
159 function watchdog_event($id) {
160   $severity = array(WATCHDOG_NOTICE => t('notice'), WATCHDOG_WARNING => t('warning'), WATCHDOG_ERROR => t('error'));
161   $output = '';
162   $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);
163   if ($watchdog = db_fetch_object($result)) {
164     $header = array(t('Type'), t('Date'), t('User'), t('Location'), t('Referrer'), t('Message'), t('Severity'), t('Hostname'));
165     $data = array(t($watchdog->type), format_date($watchdog->timestamp, 'large'), theme('username', $watchdog), l($watchdog->location, $watchdog->location), l($watchdog->referer, $watchdog->referer), $watchdog->message, $severity[$watchdog->severity], $watchdog->hostname);
166     $output = theme('watchdog_event', $header, $data);
167   }
168   return $output;
169 }
170
171 function theme_watchdog_event($header, $data) {
172   $output = '';
173   $output .= '<table class="watchdog-event">';
174
175   $n = count($header);
176   for ($i = 0; $i < $n; $i++) {
177     $output .= '<tr class="' . ($i % 2 == 0 ? 'even' : 'odd') . '"><th>' . $header[$i] . '</th><td>' . $data[$i] . '</td></tr>';
178   }
179
180   $output .= '</table>';
181
182   return $output;
183 }
184
185 function _watchdog_get_message_types() {
186   $types = array();
187
188   $result = db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type');
189   while ($object = db_fetch_object($result)) {
190     $types[] = $object->type;
191   }
192
193   return $types;
194 }