File: 1.14.2a/server/bin/logwatch.php (View as HTML)

  1: <?php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: require("include.php");
 23: $NATS->Start();
 24: $min_delay=2;
 25: $first=true;
 26: $lastlog=0;
 27: $pull=20;
 28: $continue=false;
 29: $level=0;
 30: 
 31: for ($a=1; $a<$argc; $a++)
 32: 	{
 33: 	switch($argv[$a])
 34: 		{
 35: 		case "-f":
 36: 			$continue=true;
 37: 			break;
 38: 		case "-c":
 39: 			$pull=$argv[++$a];
 40: 			break;
 41: 		case "-l":
 42: 			$level=$argv[++$a];
 43: 			break;
 44: 		case "-d": case "-s":
 45: 			$min_delay=$argv[++$a];
 46: 			break;
 47: 		default:
 48: 
 49: echo "FreeNATS logwatch System Log Watcher Tool\n";
 50: echo "Usage: php logwatch.php [-f] [-c count] [-l level] [-d delay]\n";
 51: echo "\n";
 52: echo "Displays the last system event log items and optionally will\n";
 53: echo "continue to monitor the log for new events.\n";
 54: echo "\n";
 55: echo "Options:\n";
 56: echo " -f follow - continue to display output\n";
 57: echo " -c count - display this many previous entries initially\n";
 58: echo "            defaults to 20\n";
 59: echo " -l level - only displays log levels of this level or below\n";
 60: echo " -d delay - pause this many seconds between database fetches\n";
 61: echo "            when using -f (defaults to 2 seconds)\n\n";
 62:             exit();
 63:            break;
 64: 
 65: 		}
 66: 	}
 67: $loop=true;
 68: while ($loop)
 69: 	{
 70: 	$start=time();
 71: 	$q="SELECT * FROM fnlog";
 72: 	$wc="";
 73: 	if ($lastlog>0)
 74: 		{
 75: 		$wc.="logid>".$lastlog;
 76: 		}
 77: 	if ($level>0)
 78: 		{
 79: 		if ($lastlog>0) $wc.=" AND ";
 80: 		$wc.="loglevel<=".ss($level);
 81: 		}
 82: 	if ($wc!="") $q.=" WHERE ".$wc;
 83: 	$q.=" ORDER BY logid";
 84: 	if ($first)
 85: 		{
 86: 		$q.=" DESC LIMIT 0,".$pull;
 87: 		//$first=false;
 88: 		}
 89: 	else $q.=" ASC";
 90: 	$r=$NATS->DB->Query($q);
 91: 	if ($first) $s=array();
 92: 	while ($row=$NATS->DB->Fetch_Array($r))
 93: 		{
 94: 		$line=nicedt($row['postedx'])."\t".$row['loglevel']."\t".$row['modid'].":".$row['catid']."\t".$row['logevent']."\n";
 95: 		if ($first) $s[]=$line;
 96: 		else echo $line;
 97: 		if ($row['logid']>$lastlog) $lastlog=$row['logid'];
 98: 		}
 99: 	$NATS->DB->Free($r);
100: 	if ($first)
101: 		{
102: 		for ($a=count($s)-1; $a>=0; $a--)
103: 			{
104: 			echo $s[$a];
105: 			}
106: 		}
107: 	if (!$continue) $loop=false;
108: 	if ($first) $first=false;
109: 	if ($loop) while ( (time()-$start)<$min_delay ) sleep(1);
110: 	}
111: ?>