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

Dries's avatar
 
Dries committed
4 5 6
function watchdog_help() {
 ?>
  <P>The watchdog module monitors your website, 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.</P>
Dries's avatar
 
Dries committed
7
  <P>To ease administration, the watchdog will automatically discard old log entries.</P>
Dries's avatar
 
Dries committed
8
 <?php
Dries's avatar
 
Dries committed
9 10
}

11 12 13 14 15
function watchdog_system($field){
  $system["description"] = t("Logs and records system events.");
  return $system[$field];
}

Dries's avatar
 
Dries committed
16
function watchdog_perm() {
Dries's avatar
 
Dries committed
17
  return array("administer watchdog");
Dries's avatar
 
Dries committed
18 19
}

Dries's avatar
 
Dries committed
20
function watchdog_link($type) {
Dries's avatar
 
Dries committed
21
  if ($type == "admin" && user_access("administer watchdog")) {
Dries's avatar
 
Dries committed
22 23 24 25 26 27 28 29 30
    $help = "The watchdog module monitors your website, 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.";

    menu_add("watchdog", "admin.php?mod=watchdog", "Display system events.", $help, "site monitoring", 1, 1);
     menu_add("user messages", "admin.php?mod=watchdog&type=user", "Display system events", $help, "watchdog");
     menu_add("regular messages", "admin.php?mod=watchdog&type=regular", "Display system events", $help, "watchdog");
     menu_add("special messages", "admin.php?mod=watchdog&type=special", "Display system events", $help, "watchdog");
     menu_add("warning messages", "admin.php?mod=watchdog&type=warning", "Display system events", $help, "watchdog");
     menu_add("error messages", "admin.php?mod=watchdog&type=error", "Display system events", $help, "watchdog");
     menu_add("httpd messages", "admin.php?mod=watchdog&type=httpd", "Display system events", $help, "watchdog");
Dries's avatar
 
Dries committed
31 32 33
  }
}

Dries's avatar
CHANGES  
Dries committed
34
function watchdog_conf_options() {
Dries's avatar
 
Dries committed
35 36
  $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
37 38 39
  return $output;
}

Dries's avatar
 
Dries committed
40
function watchdog_cron() {
Dries's avatar
 
Dries committed
41
  db_query("DELETE FROM watchdog WHERE ". time() ." - timestamp > ". variable_get("watchdog_clear", 604800));
Dries's avatar
 
Dries committed
42
}
Dries's avatar
 
Dries committed
43

Dries's avatar
 
Dries committed
44
function watchdog_overview($type) {
Dries's avatar
 
Dries committed
45 46
  $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'");
Dries's avatar
 
Dries committed
47

Dries's avatar
 
Dries committed
48
  $result = db_query("SELECT w.*, u.name, u.uid FROM watchdog w LEFT JOIN users u ON w.uid = u.uid ". ($type ? $query[$type] : "") ." ORDER BY timestamp DESC LIMIT 1000");
Dries's avatar
 
Dries committed
49

Dries's avatar
 
Dries committed
50
  $output .= "<table>";
Dries's avatar
 
Dries committed
51
  $output .= " <tr><th>" . t("date") . "</th><th>" . t("message") . "</th><th>" . t("user") . "</th><th>" . t("operations") . "</th></tr>";
Dries's avatar
 
Dries committed
52
  while ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
53
    if ($background = $color[$watchdog->type]) {
Dries's avatar
 
Dries committed
54
      $output .= " <tr bgcolor=\"$background\"><td nowrap=\"nowrap\">". format_date($watchdog->timestamp, "small") ."</td><td>". substr(strip_tags($watchdog->message), 0, 64) ."</td><td align=\"center\">". format_name($watchdog) ."</a></td><td align=\"center\">". la(t("details"), array("mod" => "watchdog", "op" => "view", "id" => $watchdog->wid)) ."</td></tr>";
Dries's avatar
 
Dries committed
55
    }
Dries's avatar
 
Dries committed
56
  }
Dries's avatar
 
Dries committed
57
  $output .= "</table>";
Dries's avatar
 
Dries committed
58

Dries's avatar
 
Dries committed
59
  return $output;
Dries's avatar
 
Dries committed
60 61 62
}

function watchdog_view($id) {
Dries's avatar
 
Dries committed
63
  $result = db_query("SELECT w.*, u.name, u.uid FROM watchdog w LEFT JOIN users u ON w.uid = u.uid WHERE w.wid = '$id'");
Dries's avatar
 
Dries committed
64 65

  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
66
    $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
Dries's avatar
 
Dries committed
67 68 69 70 71 72
    $output .= " <tr><th>" . t("Type") . ":" . "</th><td>". check_output($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>". check_output($watchdog->location) ."</td></tr>";
    $output .= " <tr><th>" . t("Message") . ":" . "</th><td>". check_output($watchdog->message) ."</td></tr>";
    $output .= " <tr><th>" . t("Hostname") . ":" . "</th><td>". check_output($watchdog->hostname) ."</td></tr>";
Dries's avatar
 
Dries committed
73
    $output .= "</table>";
Dries's avatar
 
Dries committed
74

Dries's avatar
 
Dries committed
75 76
    return $output;
  }
Dries's avatar
 
Dries committed
77 78
}

Dries's avatar
 
Dries committed
79
function watchdog_admin() {
Dries's avatar
 
Dries committed
80
  global $op, $id, $type, $order;
Dries's avatar
 
Dries committed
81

Dries's avatar
 
Dries committed
82
  if (user_access("administer watchdog")) {
Dries's avatar
 
Dries committed
83 84 85 86 87 88 89 90 91 92 93 94 95
    switch ($op) {
      case "help":
        watchdog_help();
        break;
      case "view":
        print watchdog_view(check_input($id));
        break;
      default:
        print watchdog_overview($type);
    }
  }
  else {
    print message_access();
Dries's avatar
 
Dries committed
96 97
  }
}
Dries's avatar
 
Dries committed
98

Dries's avatar
 
Dries committed
99
?>