watchdog.module 7.47 KB
Newer Older
1
<?php
2
// $Id$
Dries's avatar
Dries committed
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
function watchdog_help($section = "admin/help") {

  switch ($section) {
    case "admin/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 ". l("check the Watchdog report", "admin/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, ". l("as configured", "admin/system/modules/watchdog") .". Needs \"cron.php\" to discard the entries.</p>";
      break;
   case "admin/watchdog":
     $output = "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.";
     break;
  case "admin/watchdog/user":
     $output = "Watchdog events that have to do with users.";
     break;
  case "admin/watchdog/regular":
    $output = "Watchdog events that are \"normal\" and have no other classification.";
    break;
  case "admin/watchdog/httpd":
    $output = "Watchdog events that are from the web server.";
    break;
  case "admin/watchdog/special":
    $output = "Watchdog events about adding, changing, and moderating nodes and comments.";
    break;
  case "admin/watchdog/error":
    $output = "Watchdog general error events, such as invalid login, permission denied, and database errors.";
    break;
  case "admin/watchdog/warning":
    $output = "Watchdog warning events. These events don't stop Drupal from running, but are things you should no to correct.";
    break;
  }
33 34

  return t($output);
Dries's avatar
Dries committed
35 36
}

37 38
function watchdog_system($field){
  $system["description"] = t("Logs and records system events.");
39
  $system["admin_help"] = t("Watchdog logs your system events. Too see these logs go to <a href=\"%watchdog\">Site monitoring</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.<br />Note:<ul><li>To discard entries as set below you must run \"cron.php\" regularly.</li></ul>", array("%watchdog" => url("admin/watchdog")));
40 41 42
  return $system[$field];
}

43
function watchdog_perm() {
44
  return array("administer watchdog");
45 46
}

Dries's avatar
Dries committed
47
function watchdog_link($type) {
48
  if ($type == "admin" && user_access("administer watchdog")) {
49 50 51 52 53 54 55 56
    menu("admin/watchdog", "site monitoring", "watchdog_admin", watchdog_help("admin/watchdog"), 6);
    menu("admin/watchdog/user", "user messages", "watchdog_admin", watchdog_help("admin/watchdog/user"));
    menu("admin/watchdog/regular", "regular messages", "watchdog_admin", watchdog_help("admin/watchdog/regular"));
    menu("admin/watchdog/special", "special messages", "watchdog_admin", watchdog_help("admin/watchdog/special"));
    menu("admin/watchdog/warning", "warning messages", "watchdog_admin", watchdog_help("admin/watchdog/warning"));
    menu("admin/watchdog/error", "error messages", "watchdog_admin", watchdog_help("admin/watchdog/error"));
    menu("admin/watchdog/httpd", "httpd messages", "watchdog_admin", watchdog_help("admin/watchdog/httpd"));
    menu("admin/watchdog/view", "view details", "watchdog_admin", NULL, 0, 1); // hidden menu
Dries's avatar
Dries committed
57 58 59
  }
}

60
function watchdog_settings() {
61 62
  $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
63 64 65
  return $output;
}

66
function watchdog_cron() {
67
  db_query("DELETE FROM {watchdog} WHERE ". time() ." - timestamp > ". variable_get("watchdog_clear", 604800));
68
}
Dries's avatar
Dries committed
69

70
function watchdog_overview($type) {
71 72
  $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
73

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

Dries's avatar
Dries committed
84
  while ($watchdog = db_fetch_object($result)) {
85
    if ($background = $color[$watchdog->type]) {
86 87 88 89 90 91 92
      $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")
      );
93
    }
Dries's avatar
Dries committed
94
  }
95

96 97 98 99 100 101 102 103
  if (!$rows) {
    $rows[] = array(array("data" => t("No <i>%s</i> messages currently available.", array("%s" => $type)), "colspan" => "5"));
  }
  $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
104 105 106
}

function watchdog_view($id) {
107

108
  $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
109 110

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

120 121
    return $output;
  }
Dries's avatar
Dries committed
122 123
}

Dries's avatar
Dries committed
124
function watchdog_admin() {
125

126
  if (user_access("administer watchdog")) {
Dries's avatar
Dries committed
127
    switch (arg(2)) {
128 129 130 131
      case "help":
        watchdog_help();
        break;
      case "view":
132
        print watchdog_view(arg(3));
133 134
        break;
      default:
135
        print watchdog_overview(arg(2));
136 137 138 139
    }
  }
  else {
    print message_access();
Dries's avatar
Dries committed
140 141
  }
}
142

143
?>