<?php/* logtailmail.php Copyright (C) 2007 Alexander Schaber All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, oR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*//* create some working data .. */$config["installedpackages"]["logtailmail"]["logfiles"]["system.log"] = "/var/log/system.log";$config["installedpackages"]["logtailmail"]["logfiles"]["filter.log"] = "/var/log/filter.log";$config["installedpackages"]["logtailmail"]["logfiles"]["dhcpd.log"] = "/var/log/dhcpd.log";$config["installedpackages"]["logtailmail"]["logfiles"]["portalauth.log"] = "/var/log/portalauth.log";$config["installedpackages"]["logtailmail"]["logfiles"]["ipsec.log"] = "/var/log/ipsec.log";$config["installedpackages"]["logtailmail"]["logfiles"]["vpn.log"] = "/var/log/vpn.log";$config["installedpackages"]["logtailmail"]["logfiles"]["slbd.log"] = "/var/log/slbd.log";$config["installedpackages"]["logtailmail"]["logfiles"]["ntpd.log"] = "/var/log/ntpd.log";$config["installedpackages"]["logtailmail"]["logfiles"]["openvpn.log"] = "/var/log/openvpn.log";$config["installedpackages"]["logtailmail"]["logfiles"]["userlog"] = "/var/log/userlog";$config["installedpackages"]["logtailmail"]["from"] = "pfsense@example.org";$config["installedpackages"]["logtailmail"]["to"] = "web@example.org";$config["installedpackages"]["logtailmail"]["remotesmtp"] = "example.org";$config["installedpackages"]["logtailmail"]["remotesmtpport"] = 25;/* for php5 emulation */function file_put_contents($n, $d, $flag = false) { $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w'; $f = @fopen($n, $mode); if ($f === false) { return 0; } else { if (is_array($d)) $d = implode($d); $bytes_written = fwrite($f, $d); fclose($f); return $bytes_written; }}class logtail { var $logs; var $logfile; var $newlogs = array(); function getnewlogs() { /* Check wether offset from previous check found */ if (file_exists("{$this->logfile}.loginfo")) { $loginfo = file("{$this->logfile}.loginfo"); $md5 = trim($loginfo[0]); $lastline = trim($loginfo[1]); echo "getnewlogs:".$lastline."\n"; $logsmd5 = array_map("md5", $this->logs); /* Check wether the last sent line is found */ $lastkey = array_search($md5, $logsmd5); if ($lastkey !== false) $this->newlogs = array_slice($this->logs, $lastkey+1); else $this->newlogs = $this->logs; } else { $this->newlogs = $this->logs; } } function writeoffset() { if (count($this->newlogs) >= 1) { /* Get md5 hash of last line */ $new_lastline = trim($this->newlogs[count($this->newlogs)-1]); $new_md5 = md5($new_lastline); /* Write offset and md5 to {$logfile}.loginfo */ $loginfo = $new_md5."\n"; $loginfo .= $new_lastline; file_put_contents("{$this->logfile}.loginfo", $loginfo); echo "writeoffset:".$loginfo."\n"; } else {// echo "nothing new"; } } function logtail($logfile) { $this->logfile = $logfile; /* Read logs */ exec ("clog {$logfile}", $this->logs); /* $this->logs = file($logfile); $this->logs = array_map ("trim", $this->logs); */ /* This will seek the handle to the position where to continue to read the logs .. If all fails, from the beginning */ $this->getnewlogs(); /* Read new log entries */ $this->writeoffset(); //$this->sendmail(); }}class logtailmail { var $body; function sendmail() { global $config; $mycnf = &$config["installedpackages"]["logtailmail"]; require_once ("class.smtp.php"); $smtp = new smtp_class; $smtp->host_name = $mycnf["remotesmtp"]; /* Change this variable to the address of the SMTP server to relay, like "smtp.myisp.com" */ $smtp->host_port = $mycnf["remotesmtpport"];; /* Change this variable to the port of the SMTP server to use, like 465 */ $smtp->direct_delivery = 1; /* Set to 1 to deliver directly to the recepient SMTP server */ $smtp->ssl = 0; /* Change this variable if the SMTP server requires an secure connection using SSL */ $smtp->debug = 1; /* Set to 1 to output the communication with the SMTP server */ $smtp->html_debug = 0; /* Set to 1 to format the debug output as HTML */ $from = $mycnf["from"]; $to = $mycnf["to"]; $headers = array( "From: $from", "To: $to",// "Subject: ".genhtmltitle('logtailmail'), "Subject: logtailmail", "Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z") ); if($smtp->SendMessage($from, array($to), $headers, $this->body)) echo "Message sent to $to OK.\n"; else echo "Cound not send the message to $to.\nError: ".$smtp->error."\n"; } function logtailmail ($logfiles) { if (!is_array($logfiles)) exit ("No array given"); foreach ($logfiles as $logfile) { $logtail = new logtail($logfile); $this->body .= "\n\n\n"; $this->body .= "{$logfile}:\n"; $this->body .= "________________________________________________________________________________\n"; $this->body .= "(".count($logtail->newlogs)." lines)\n"; $this->body .= implode ("\n", $logtail->newlogs); unset($logtail); } $this->sendmail(); //echo $this->body; }}new logtailmail($config['installedpackages']['logtailmail']['logfiles']);echo "\n";?>