watchdog.module 4.64 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
Dries's avatar
 
Dries committed
2

Dries's avatar
 
Dries committed
3 4 5
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
6
  <P>To ease administration, the watchdog will automatically discard old log entries.</P>
Dries's avatar
 
Dries committed
7
 <?php
Dries's avatar
 
Dries committed
8 9
}

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

Dries's avatar
 
Dries committed
14
function watchdog_link($type) {
Dries's avatar
 
Dries committed
15
  if ($type == "admin" && user_access("administer watchdog")) {
Dries's avatar
 
Dries committed
16 17 18 19 20 21
    $links[] = "<a href=\"admin.php?mod=watchdog\">watchdog</a>";
  }

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

Dries's avatar
CHANGES  
Dries committed
22
function watchdog_conf_options() {
Dries's avatar
 
Dries committed
23 24
  $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
25 26 27
  return $output;
}

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

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

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

Dries's avatar
 
Dries committed
38
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
 
Dries committed
39
  $output .= " <TR><TH>date</TH><TH>message</TH><TH>user</TH><TH>operations</TH></TR>\n";
Dries's avatar
 
Dries committed
40
  while ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
41
    if ($background = $color[$watchdog->type]) {
Dries's avatar
 
Dries committed
42
      $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\"><A HREF=\"admin.php?mod=watchdog&op=view&id=$watchdog->id\">details</A></TD></TR>\n";
Dries's avatar
 
Dries committed
43
    }
Dries's avatar
 
Dries committed
44 45 46
  }
  $output .= "</TABLE>\n";

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

function watchdog_view($id) {
Dries's avatar
 
Dries committed
51
  $result = db_query("SELECT l.*, u.name, u.uid FROM watchdog l LEFT JOIN user u ON l.user = u.uid WHERE l.id = '$id'");
Dries's avatar
 
Dries committed
52 53 54

  if ($watchdog = db_fetch_object($result)) {
    $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"3\" CELLSPACING=\"0\">\n";
Dries's avatar
 
Dries committed
55
    $output .= " <TR><TH>Type:</TH><TD>". check_output($watchdog->type) ."</TD></TR>\n";
Dries's avatar
 
Dries committed
56
    $output .= " <TR><TH>Date:</TH><TD>". format_date($watchdog->timestamp, "large") ."</TD></TR>\n";
Dries's avatar
 
Dries committed
57
    $output .= " <TR><TH>User:</TH><TD>". format_name($watchdog) ."</TD></TR>\n";
Dries's avatar
 
Dries committed
58 59 60
    $output .= " <TR><TH>Location:</TH><TD>". check_output($watchdog->location). "</TD></TR>\n";
    $output .= " <TR><TH>Message:</TH><TD>". check_output($watchdog->message) ."</TD></TR>\n";
    $output .= " <TR><TH>Hostname:</TH><TD>". check_output($watchdog->hostname) ."</TD></TR>\n";
Dries's avatar
 
Dries committed
61
    $output .= "</TABLE>\n";
Dries's avatar
 
Dries committed
62

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

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

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

Dries's avatar
 
Dries committed
72
    print "<SMALL><A HREF=\"admin.php?mod=watchdog&type=user\">user messages</A> | <A HREF=\"admin.php?mod=watchdog&type=regular\">regular messages</A> | <A HREF=\"admin.php?mod=watchdog&type=special\">special messages</A> | <A HREF=\"admin.php?mod=watchdog&type=warning\">warning messages</A> | <A HREF=\"admin.php?mod=watchdog&type=error\">error messages</A> | <A HREF=\"admin.php?mod=watchdog&type=httpd\">httpd messages</A> | <A HREF=\"admin.php?mod=watchdog\">overview</A> | <A HREF=\"admin.php?mod=watchdog&op=help\">help</A></SMALL><HR>\n";
Dries's avatar
 
Dries committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86

    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
87 88 89 90
  }
}

?>