watchdog.module 7.09 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
      $output .= "<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 %watchdog on a regular basis as it is often the only way to tell what is going on.</p>";
Dries Buytaert's avatar
   
Dries Buytaert committed
10
      $output .= "<p>To ease administration, the watchdog will automatically discard old log entries, %log-entry. Needs \"cron.php\" to discard the entries.</p>";
Dries Buytaert's avatar
   
Dries Buytaert committed
11
      $output = t($output, array("%watchdog" => l(t("check the watchdog report"), "admin/watchdog"), "%log-entry" => l(t("as configured"), "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
  case 'admin/watchdog/error':
    $output = t("Watchdog general error events, such as invalid login, permission denied, and database errors.");
Dries Buytaert's avatar
   
Dries Buytaert committed
30
    break;
Dries Buytaert's avatar
   
Dries Buytaert committed
31
  case 'admin/watchdog/warning':
Dries Buytaert's avatar
   
Dries Buytaert committed
32
    $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
33
34
35
36
37
    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
38
    $output = t("Watchdog logs your system events. To see these logs go to %watchdog. 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" => l(t("messages"), "admin/watchdog")));
Dries Buytaert's avatar
   
Dries Buytaert committed
39
40
    break;
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
41

Dries Buytaert's avatar
   
Dries Buytaert committed
42
  return $output;
43
44
}

Dries Buytaert's avatar
   
Dries Buytaert committed
45
function watchdog_perm() {
Dries Buytaert's avatar
   
Dries Buytaert committed
46
  return array("administer watchdog");
Dries Buytaert's avatar
   
Dries Buytaert committed
47
48
}

Dries Buytaert's avatar
   
Dries Buytaert committed
49
function watchdog_link($type) {
Dries Buytaert's avatar
   
Dries Buytaert committed
50
51
  if ($type == "system") {
    if (user_access("administer watchdog")) {
Dries Buytaert's avatar
   
Dries Buytaert committed
52
53
54
55
56
57
58
59
      menu("admin/watchdog", t("messages"), "watchdog_admin", 6);
      menu("admin/watchdog/user", t("user"), "watchdog_admin");
      menu("admin/watchdog/regular", t("regular"), "watchdog_admin");
      menu("admin/watchdog/special", t("special"), "watchdog_admin");
      menu("admin/watchdog/warning", t("warning"), "watchdog_admin");
      menu("admin/watchdog/error", t("error"), "watchdog_admin");
      menu("admin/watchdog/httpd", t("httpd"), "watchdog_admin");
      menu("admin/watchdog/view", t("view details"), "watchdog_admin", 0, 1); // hidden menu
Dries Buytaert's avatar
   
Dries Buytaert committed
60
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
61
62
63
  }
}

64
function watchdog_settings() {
Dries Buytaert's avatar
   
Dries Buytaert committed
65
66
  $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
67
68
69
  return $output;
}

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

Dries Buytaert's avatar
   
Dries Buytaert committed
74
function watchdog_overview($type) {
Dries Buytaert's avatar
   
Dries Buytaert committed
75
  $query = array("user" => "WHERE type = 'user'", "regular" => "WHERE type = 'message'", "special" => "WHERE type = 'special'", "warning" => "WHERE type = 'warning'", "error" => "WHERE type = 'error'", "httpd" => "WHERE type = 'httpd'", "actions" => "WHERE link != ''");
Dries Buytaert's avatar
 
Dries Buytaert committed
76

Dries Buytaert's avatar
   
Dries Buytaert committed
77
78
79
80
81
82
  $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
83
  $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
84
85
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);
Dries Buytaert's avatar
   
Dries Buytaert committed
86

Dries Buytaert's avatar
 
Dries Buytaert committed
87
  while ($watchdog = db_fetch_object($result)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
88
89
90
91
92
93
94
    $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
95
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
96

Dries Buytaert's avatar
   
Dries Buytaert committed
97
  if (!$rows) {
Dries Buytaert's avatar
   
Dries Buytaert committed
98
    $rows[] = array(array("data" => t("No log messages available."), "colspan" => "5"));
Dries Buytaert's avatar
   
Dries Buytaert committed
99
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
100

Dries Buytaert's avatar
   
Dries Buytaert committed
101
102
103
104
  $pager = pager_display(NULL, 50, 0, "admin", tablesort_pager());
  if (!empty($pager)) {
    $rows[] = array(array("data" => $pager, "colspan" => "5"));
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
105
  return theme("table", $header, $rows);
Dries Buytaert's avatar
 
Dries Buytaert committed
106
107
108
}

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

Dries Buytaert's avatar
   
Dries Buytaert committed
110
  $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
111
112

  if ($watchdog = db_fetch_object($result)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
113
    $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
Dries Buytaert's avatar
   
Dries Buytaert committed
114
115
116
117
118
119
    $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
120
    $output .= "</table>";
Dries Buytaert's avatar
   
Dries Buytaert committed
121

Dries Buytaert's avatar
   
Dries Buytaert committed
122
123
    return $output;
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
124
125
}

Dries Buytaert's avatar
 
Dries Buytaert committed
126
function watchdog_admin() {
Dries Buytaert's avatar
   
Dries Buytaert committed
127

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

Dries Buytaert's avatar
   
Dries Buytaert committed
146
?>