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

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

  switch ($section) {
Dries's avatar
 
Dries committed
8 9 10 11 12
    case 'admin/help':
    case 'admin/watchdog/help':
      $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>";
      $output .= "<p>To ease administration, the watchdog will automatically discard old log entries, %log-entry. Needs \"cron.php\" to discard the entries.</p>";
      $output = t($output, array("%watchdog" => l(t("check the Watchdog report"), "admin/watchdog"), "%log-entry" => l(t("as configured"), "admin/system/modules/watchdog")));
Dries's avatar
 
Dries committed
13
      break;
Dries's avatar
 
Dries committed
14 15
   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's avatar
 
Dries committed
16
     break;
Dries's avatar
 
Dries committed
17 18
  case 'admin/watchdog/user':
     $output = t("Watchdog events that have to do with users.");
Dries's avatar
 
Dries committed
19
     break;
Dries's avatar
 
Dries committed
20 21
  case 'admin/watchdog/regular':
    $output = t("Watchdog events that are \"normal\" and have no other classification.");
Dries's avatar
 
Dries committed
22
    break;
Dries's avatar
 
Dries committed
23 24
  case 'admin/watchdog/httpd':
    $output = t("Watchdog events that are from the web server.");
Dries's avatar
 
Dries committed
25
    break;
Dries's avatar
 
Dries committed
26 27
  case 'admin/watchdog/special':
    $output = t("Watchdog events about adding, changing, and moderating nodes and comments.");
Dries's avatar
 
Dries committed
28
    break;
Dries's avatar
 
Dries committed
29 30
  case 'admin/watchdog/error':
    $output = t("Watchdog general error events, such as invalid login, permission denied, and database errors.");
Dries's avatar
 
Dries committed
31
    break;
Dries's avatar
 
Dries committed
32
  case 'admin/watchdog/warning':
Dries's avatar
 
Dries committed
33
    $output = t("Watchdog warning events. These events don't stop Drupal from running, but are things you should know.");
Dries's avatar
 
Dries committed
34 35 36 37 38 39
    break;
  case 'admin/system/modules#description':
    $output = t("Logs and records system events.");
    break;
  case 'admin/system/modules/watchdog':
    $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.<br />Note:<ul><li>To discard entries as set below you must run \"cron.php\" regularly.</li></ul>", array("%watchdog" => l(t("Site monitoring"), "admin/watchdog")));
Dries's avatar
 
Dries committed
40 41
    break;
  }
Dries's avatar
 
Dries committed
42

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

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

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

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

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

Dries's avatar
 
Dries committed
75
function watchdog_overview($type) {
Dries's avatar
 
Dries committed
76 77
  $color = array("user" => "#FFEEAA", "message" => "#FFFFFF", "special" => "#A49FFF", "warning" => "#FFAA22", "httpd" => "#99DD99", "error" => "#EE4C4C");
  $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's avatar
 
Dries committed
78

Dries's avatar
 
Dries committed
79 80 81 82 83 84
  $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's avatar
 
Dries committed
85
  $sql = "SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid ". ($type ? $query[$type] : "");
Dries's avatar
 
Dries committed
86 87
  $sql .= tablesort_sql($header);
  $result = pager_query($sql, 50);
Dries's avatar
 
Dries committed
88

Dries's avatar
 
Dries committed
89
  while ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
90
    if ($background = $color[$watchdog->type]) {
Dries's avatar
 
Dries committed
91 92 93 94 95 96 97
      $rows[] = array(
        array("data" => format_date($watchdog->timestamp, "small"), "style" => "background-color: $background"),
        array("data" => substr(strip_tags($watchdog->message), 0, 64), "style" => "background-color: $background"),
        array("data" => format_name($watchdog), "style" => "background-color: $background"),
        array("data" => $watchdog->link, "style" => "background-color: $background"),
        array("data" => l(t("view details"), "admin/watchdog/view/$watchdog->wid"), "style" => "background-color: $background")
      );
Dries's avatar
 
Dries committed
98
    }
Dries's avatar
 
Dries committed
99
  }
Dries's avatar
 
Dries committed
100

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

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

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

Dries's avatar
 
Dries committed
114
  $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's avatar
 
Dries committed
115 116

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

Dries's avatar
 
Dries committed
126 127
    return $output;
  }
Dries's avatar
 
Dries committed
128 129
}

Dries's avatar
 
Dries committed
130
function watchdog_admin() {
Dries's avatar
 
Dries committed
131

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

Dries's avatar
 
Dries committed
150
?>