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

Dries's avatar
 
Dries committed
3 4
$module = array("help" => "watchdog_help",
                "cron" => "watchdog_cron",
Dries's avatar
 
Dries committed
5 6
                "admin" => "watchdog_admin");

Dries's avatar
 
Dries committed
7 8 9 10 11 12 13
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>
  <P>To ease administration, the watchdog will automatically remove old logs.</P>
 <?
}

Dries's avatar
 
Dries committed
14 15 16
function watchdog_cron() {
  watchdog_clean();
}
Dries's avatar
 
Dries committed
17 18 19 20 21

function watchdog_display($order = "date") {
  $colors = array("#D8BFD8", "#6495ED", "#6A5ADF", "#FFFFFF", "#FFA500", "#FF3C3C");
  $fields = array("date" => "id DESC", "username" => "user", "location" => "location", "message" => "message DESC", "level" => "level DESC");

Dries's avatar
 
Dries committed
22
  // Perform query:
Dries's avatar
 
Dries committed
23
  $result = db_query("SELECT l.*, u.userid FROM watchdog l LEFT JOIN users u ON l.user = u.id ORDER BY l.$fields[$order]");
Dries's avatar
 
Dries committed
24

Dries's avatar
 
Dries committed
25
  // Generate output:
Dries's avatar
 
Dries committed
26
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
 
Dries committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  $output .= " <TR>\n";
  $output .= "  <TH ALIGN=\"right\" COLSPAN=\"4\">\n";
  $output .= "   <FORM ACTION=\"admin.php?mod=watchdog\" METHOD=\"post\">\n";
  $output .= "    <SELECT NAME=\"order\">\n";
  foreach ($fields as $key=>$value) {
    $output .= "     <OPTION VALUE=\"$key\"". ($key == $order ? " SELECTED" : "") .">Sort by $key</OPTION>\n";
  }
  $output .= "    </SELECT>\n";
  $output .= "    <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Update\">\n";
  $output .= "   </FORM>\n";
  $output .= "  </TH>\n";
  $output .= " </TR>\n";
  $output .= " <TR>\n";
  $output .= "  <TH>date</TH>\n";
  $output .= "  <TH>message</TH>\n";
  $output .= "  <TH>user</TH>\n";
  $output .= "  <TH>operations</TH>\n";
  $output .= " </TR>\n";

  while ($watchdog = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
47
    $output .= " <TR BGCOLOR=\"". $colors[$watchdog->level] ."\"><TD>". format_date($watchdog->timestamp) ."</TD><TD>". substr(check_output($watchdog->message), 0, 44) ."</TD><TD ALIGN=\"center\">". format_username($watchdog->userid) ."</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
48 49 50 51 52 53 54 55
  }

  $output .= "</TABLE>\n";

  print $output;
}

function watchdog_view($id) {
Dries's avatar
 
Dries committed
56
  $result = db_query("SELECT l.*, u.userid FROM watchdog l LEFT JOIN users u ON l.user = u.id WHERE l.id = '$id'");
Dries's avatar
 
Dries committed
57 58 59

  if ($watchdog = db_fetch_object($result)) {
    $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"3\" CELLSPACING=\"0\">\n";
Dries's avatar
 
Dries committed
60
    $output .= " <TR><TH>Level:</TH><TD>". check_output($watchdog->level) ."</TD></TR>\n";
Dries's avatar
 
Dries committed
61 62
    $output .= " <TR><TH>Date:</TH><TD>". format_date($watchdog->timestamp, "large") ."</TD></TR>\n";
    $output .= " <TR><TH>User:</TH><TD>". format_username($watchdog->userid) ."</TD></TR>\n";
Dries's avatar
 
Dries committed
63 64 65
    $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
66 67 68 69 70 71 72 73
    $output .= "</TABLE>\n";
    print $output;
  }
}

function watchdog_admin() {
  global $op, $id, $order;

Dries's avatar
 
Dries committed
74 75
  print "<SMALL><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
76
  switch ($op) {
Dries's avatar
 
Dries committed
77 78 79
    case "help":
      watchdog_help();
      break;
Dries's avatar
 
Dries committed
80
    case "view":
Dries's avatar
 
Dries committed
81
      watchdog_view(check_input($id));
Dries's avatar
 
Dries committed
82 83
      break;
    case "Update":
Dries's avatar
 
Dries committed
84
      watchdog_display(check_input($order));
Dries's avatar
 
Dries committed
85 86 87 88 89 90 91
      break;
    default:
      watchdog_display();
  }
}

?>