watchdog.module 4.82 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
}

Dries's avatar
 
Dries committed
11
function watchdog_perm() {
Dries's avatar
 
Dries committed
12
  return array("administer watchdog");
Dries's avatar
 
Dries committed
13 14
}

Dries's avatar
 
Dries committed
15
function watchdog_link($type) {
Dries's avatar
 
Dries committed
16
  if ($type == "admin" && user_access("administer watchdog")) {
Dries's avatar
 
Dries committed
17
    $links[] = la(t("watchdog"), array("mod" => "watchdog"));
Dries's avatar
 
Dries committed
18 19 20 21 22
  }

  return $links ? $links : array();
}

Dries's avatar
CHANGES  
Dries committed
23
function watchdog_conf_options() {
Dries's avatar
 
Dries committed
24 25
  $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 => "Never");
  $output .= form_select("Discard entries older than", "watchdog_clear", variable_get("watchdog_clear", 604800), $period, "The time watchdog entries should be kept.  Older entries will be automatically discarded.  Requires crontab.");
Dries's avatar
 
Dries committed
26 27 28
  return $output;
}

Dries's avatar
 
Dries committed
29
function watchdog_cron() {
Dries's avatar
 
Dries committed
30
  db_query("DELETE FROM watchdog WHERE ". time() ." - timestamp > ". variable_get("watchdog_clear", 604800));
Dries's avatar
 
Dries committed
31
}
Dries's avatar
 
Dries committed
32

Dries's avatar
 
Dries committed
33
function watchdog_overview($type) {
Dries's avatar
 
Dries committed
34 35
  $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
36

Dries's avatar
 
Dries committed
37
  $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
38

Dries's avatar
 
Dries committed
39 40
  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
  $output .= " <tr><th>date</th><th>message</th><th>user</th><th>operations</th></tr>";
Dries's avatar
 
Dries committed
41
  while ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
42
    if ($background = $color[$watchdog->type]) {
Kjartan's avatar
Kjartan committed
43
      $output .= " <tr bgcolor=\"$background\"><td>". format_date($watchdog->timestamp, "small") ."</td><td>". substr(check_output($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
44
    }
Dries's avatar
 
Dries committed
45
  }
Dries's avatar
 
Dries committed
46
  $output .= "</table>";
Dries's avatar
 
Dries committed
47

Dries's avatar
 
Dries committed
48
  return $output;
Dries's avatar
 
Dries committed
49 50 51
}

function watchdog_view($id) {
Dries's avatar
 
Dries committed
52
  $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
53 54

  if ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
55 56 57 58 59 60 61 62
    $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
    $output .= " <tr><th>Type:</th><td>". check_output($watchdog->type) ."</td></tr>";
    $output .= " <tr><th>Date:</th><td>". format_date($watchdog->timestamp, "large") ."</td></tr>";
    $output .= " <tr><th>User:</th><td>". format_name($watchdog) ."</td></tr>";
    $output .= " <tr><th>Location:</th><td>". check_output($watchdog->location) ."</td></tr>";
    $output .= " <tr><th>Message:</th><td>". check_output($watchdog->message) ."</td></tr>";
    $output .= " <tr><th>Hostname:</th><td>". check_output($watchdog->hostname) ."</td></tr>";
    $output .= "</table>";
Dries's avatar
 
Dries committed
63

Dries's avatar
 
Dries committed
64 65
    return $output;
  }
Dries's avatar
 
Dries committed
66 67
}

Dries's avatar
 
Dries committed
68
function watchdog_admin() {
Dries's avatar
 
Dries committed
69
  global $op, $id, $type, $order;
Dries's avatar
 
Dries committed
70

Dries's avatar
 
Dries committed
71
  if (user_access("administer watchdog")) {
Dries's avatar
 
Dries committed
72

Dries's avatar
 
Dries committed
73 74 75 76 77 78 79 80 81
    $links[] = la(t("user messages"), array("mod" => "watchdog", "type" => "user"));
    $links[] = la(t("regular messages"), array("mod" => "watchdog", "type" => "regular"));
    $links[] = la(t("special messages"), array("mod" => "watchdog", "type" => "special"));
    $links[] = la(t("warning messages"), array("mod" => "watchdog", "type" => "warning"));
    $links[] = la(t("error messages"), array("mod" => "watchdog", "type" => "error"));
    $links[] = la(t("httpd messages"), array("mod" => "watchdog", "type" => "httpd"));
    $links[] = la(t("overview"), array("mod" => "watchdog"));
    $links[] = la(t("help"), array("mod" => "watchdog", "op" => "help"));

Kjartan's avatar
Kjartan committed
82
    print "<small>". implode(" | ", $links) ."</small><hr />";
Dries's avatar
 
Dries committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96

    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
97 98
  }
}
Dries's avatar
 
Dries committed
99

Dries's avatar
 
Dries committed
100
?>