watchdog.module 7.07 KB
Newer Older
Dries Buytaert's avatar
   
Dries Buytaert committed
1
<?php
2
// $Id$
Dries Buytaert's avatar
 
Dries Buytaert committed
3

Dries Buytaert's avatar
   
Dries Buytaert committed
4
function watchdog_help($section = "admin/help#watchdog") {
Dries Buytaert's avatar
   
Dries Buytaert committed
5
  $output = "";
Dries Buytaert's avatar
   
Dries Buytaert committed
6
7

  switch ($section) {
Dries Buytaert's avatar
   
Dries Buytaert committed
8
    case 'admin/help#watchdog':
Dries Buytaert's avatar
   
Dries Buytaert committed
9
10
11
      $output .= 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/watchdog"), "%log-entry" => url("admin/system/modules/watchdog")));
Dries Buytaert's avatar
   
Dries Buytaert committed
12
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
13
14
   case 'admin/watchdog':
     $output = 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.");
Dries Buytaert's avatar
   
Dries Buytaert committed
15
     break;
Dries Buytaert's avatar
   
Dries Buytaert committed
16
17
  case 'admin/watchdog/user':
     $output = t("Watchdog events that have to do with users.");
Dries Buytaert's avatar
   
Dries Buytaert committed
18
     break;
Dries Buytaert's avatar
   
Dries Buytaert committed
19
20
  case 'admin/watchdog/regular':
    $output = t("Watchdog events that are \"normal\" and have no other classification.");
Dries Buytaert's avatar
   
Dries Buytaert committed
21
    break;
Dries Buytaert's avatar
   
Dries Buytaert committed
22
23
  case 'admin/watchdog/httpd':
    $output = t("Watchdog events that are from the web server.");
Dries Buytaert's avatar
   
Dries Buytaert committed
24
    break;
Dries Buytaert's avatar
   
Dries Buytaert committed
25
26
  case 'admin/watchdog/special':
    $output = t("Watchdog events about adding, changing, and moderating nodes and comments.");
Dries Buytaert's avatar
   
Dries Buytaert committed
27
    break;
Dries Buytaert's avatar
   
Dries Buytaert committed
28
29
30
  case 'admin/watchdog/search':
    $output = t("Watchdog events showing what users searched for.");
    break;
Dries Buytaert's avatar
   
Dries Buytaert committed
31
  case 'admin/watchdog/error':
Dries Buytaert's avatar
Dries Buytaert committed
32
    $output = t("Watchdog events about PHP and database errors.");
Dries Buytaert's avatar
   
Dries Buytaert committed
33
    break;
Dries Buytaert's avatar
   
Dries Buytaert committed
34
  case 'admin/watchdog/warning':
Dries Buytaert's avatar
   
Dries Buytaert committed
35
    $output = t("Watchdog warning events. These events don't stop Drupal from running, but are things you should know.");
Dries Buytaert's avatar
   
Dries Buytaert committed
36
37
38
39
40
    break;
  case 'admin/system/modules#description':
    $output = t("Logs and records system events.");
    break;
  case 'admin/system/modules/watchdog':
Dries Buytaert's avatar
   
Dries Buytaert committed
41
    $output = 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/watchdog")));
Dries Buytaert's avatar
   
Dries Buytaert committed
42
43
    break;
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
44

Dries Buytaert's avatar
   
Dries Buytaert committed
45
  return $output;
46
47
}

Dries Buytaert's avatar
   
Dries Buytaert committed
48
function watchdog_perm() {
Dries Buytaert's avatar
   
Dries Buytaert committed
49
  return array("administer watchdog");
Dries Buytaert's avatar
   
Dries Buytaert committed
50
51
}

Dries Buytaert's avatar
   
Dries Buytaert committed
52
function watchdog_link($type) {
Dries Buytaert's avatar
   
Dries Buytaert committed
53
54
  if ($type == "system") {
    if (user_access("administer watchdog")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
55
56
57
      if (arg(0) == 'admin') {  // avoid SQL queries if possible
        menu("admin/watchdog", t("logs"), "watchdog_admin", 7);
        menu("admin/watchdog/view", t("view details"), "watchdog_admin", 0, MENU_HIDE);
Dries Buytaert's avatar
   
Dries Buytaert committed
58

Dries Buytaert's avatar
   
Dries Buytaert committed
59
60
61
        foreach (_watchdog_get_message_types() as $type) {
          menu("admin/watchdog/$type", t($type), "watchdog_admin");
        }
Dries Buytaert's avatar
   
Dries Buytaert committed
62
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
63
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
64
65
66
  }
}

67
function watchdog_settings() {
Dries Buytaert's avatar
   
Dries Buytaert committed
68
69
  $period = array(3600 => format_interval(3600), 10800 => format_interval(10800), 21600 => format_interval(21600), 32400 => format_interval(32400), 43200 => format_interval(43200), 86400 => format_interval(86400), 172800 => format_interval(172800), 259200 => format_interval(259200), 604800 => format_interval(604800), 1209600 => format_interval(1209600), 2419200 => format_interval(2419200), 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."));
Dries Buytaert's avatar
   
Dries Buytaert committed
70
71
72
  return $output;
}

Dries Buytaert's avatar
   
Dries Buytaert committed
73
function watchdog_cron() {
Dries Buytaert's avatar
   
Dries Buytaert committed
74
  db_query("DELETE FROM {watchdog} WHERE ". time() ." - timestamp > ". variable_get("watchdog_clear", 604800));
Dries Buytaert's avatar
   
Dries Buytaert committed
75
}
Dries Buytaert's avatar
 
Dries Buytaert committed
76

Dries Buytaert's avatar
   
Dries Buytaert committed
77
function watchdog_overview($type) {
Dries Buytaert's avatar
   
Dries Buytaert committed
78
79
80
81
  foreach (_watchdog_get_message_types() as $key) {
    $query[$key] = "WHERE type = '". check_query($key) ."'";
  }
  $query['actions'] = "WHERE link != ''";
Dries Buytaert's avatar
 
Dries Buytaert committed
82

Dries Buytaert's avatar
   
Dries Buytaert committed
83
84
85
86
87
88
  $header = array(
    array("data" => t("date"), "field" => "w.timestamp", "sort" => "desc"),
    array("data" => t("event"), "field" => "w.message"),
    array("data" => t("user"), "field" => "u.name"),
    array("data" => t("operations"), "colspan" => "2")
  );
Dries Buytaert's avatar
   
Dries Buytaert committed
89
  $sql = "SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid ". ($type ? $query[$type] : "");
Dries Buytaert's avatar
   
Dries Buytaert committed
90
91
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);
Dries Buytaert's avatar
   
Dries Buytaert committed
92

Dries Buytaert's avatar
 
Dries Buytaert committed
93
  while ($watchdog = db_fetch_object($result)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
94
95
96
97
98
99
100
    $rows[] = array(
      array("data" => format_date($watchdog->timestamp, "small"), "class" => "watchdog-$watchdog->type"),
      array("data" => substr(strip_tags($watchdog->message), 0, 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/watchdog/view/$watchdog->wid"), "class" => "watchdog-$watchdog->type")
    );
Dries Buytaert's avatar
 
Dries Buytaert committed
101
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
102

Dries Buytaert's avatar
   
Dries Buytaert committed
103
  if (!$rows) {
Dries Buytaert's avatar
   
Dries Buytaert committed
104
    $rows[] = array(array("data" => t("No log messages available."), "colspan" => "5"));
Dries Buytaert's avatar
   
Dries Buytaert committed
105
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
106

Dries Buytaert's avatar
Dries Buytaert committed
107
  $pager = theme("pager", NULL, 50, 0, tablesort_pager());
Dries Buytaert's avatar
   
Dries Buytaert committed
108
109
110
  if (!empty($pager)) {
    $rows[] = array(array("data" => $pager, "colspan" => "5"));
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
111
  return theme("table", $header, $rows);
Dries Buytaert's avatar
 
Dries Buytaert committed
112
113
114
}

function watchdog_view($id) {
Dries Buytaert's avatar
   
Dries Buytaert committed
115

Dries Buytaert's avatar
   
Dries Buytaert committed
116
  $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);
Dries Buytaert's avatar
 
Dries Buytaert committed
117
118

  if ($watchdog = db_fetch_object($result)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
119
    $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
Dries Buytaert's avatar
   
Dries Buytaert committed
120
121
122
123
124
125
    $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>";
Dries Buytaert's avatar
   
Dries Buytaert committed
126
    $output .= "</table>";
Dries Buytaert's avatar
   
Dries Buytaert committed
127

Dries Buytaert's avatar
   
Dries Buytaert committed
128
129
    return $output;
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
130
131
}

Dries Buytaert's avatar
 
Dries Buytaert committed
132
function watchdog_admin() {
Dries Buytaert's avatar
   
Dries Buytaert committed
133

Dries Buytaert's avatar
   
Dries Buytaert committed
134
  if (user_access("administer watchdog")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
135
    switch (arg(2)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
136
      case "help":
Dries Buytaert's avatar
   
Dries Buytaert committed
137
        $output = watchdog_help();
Dries Buytaert's avatar
   
Dries Buytaert committed
138
139
        break;
      case "view":
Dries Buytaert's avatar
   
Dries Buytaert committed
140
        $output = watchdog_view(arg(3));
Dries Buytaert's avatar
   
Dries Buytaert committed
141
142
        break;
      default:
Dries Buytaert's avatar
   
Dries Buytaert committed
143
        $output = watchdog_overview(arg(2));
Dries Buytaert's avatar
   
Dries Buytaert committed
144
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
145
    print theme("page", $output);
Dries Buytaert's avatar
   
Dries Buytaert committed
146
147
  }
  else {
Dries Buytaert's avatar
   
Dries Buytaert committed
148
    print theme("page", message_access());
Dries Buytaert's avatar
 
Dries Buytaert committed
149
150
  }
}
Dries Buytaert's avatar
   
Dries Buytaert committed
151

Dries Buytaert's avatar
   
Dries Buytaert committed
152
153
154
155
156
157
158
159
160
161
162
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;
}

Dries Buytaert's avatar
   
Dries Buytaert committed
163
?>