Location: PHPKode > projects > IPTables log analyzer > iptablelog/modules/TopHosts.php
<?php
/////////////////////////////////////////////////////////////////////////////////////
// IPTable log analyzer
// Copyright (C) 2002 Gerald GARCIA
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Plac<B2>e - Suite 330, Boston, MA  02111-1307, USA.
//
// Contact author : hide@address.com
/////////////////////////////////////////////////////////////////////////////////////

// $Id: TopHosts.php,v 1.4 2007/02/23 22:42:47 tarbuck Exp $

require_once("$file_base/utils/Cache.php");

Class TopHosts {
  
  function TopHosts() {
  }

  function generate() {
    global $ignored_list;
    global $chain;
    global $date;
    global $url_base;

    if ($chain != 'ALL') {
    	$chain_where = "AND u.oob_prefix='$chain'";
    }
    if ($date < 1000) {
    	$date_where = "AND u.local_time > UNIX_TIMESTAMP(ADDDATE(CURDATE(),INTERVAL -$date DAY))";
    }
    $query = "select INET_NTOA(u.ip_saddr) AS ip_src, IFNULL(shc.host,'Unresolved') AS name_src, count(id) AS nb FROM ulog u  LEFT OUTER JOIN host_cache shc ON u.ip_saddr = shc.ip WHERE 1=1 $date_where $chain_where GROUP BY ip_src ORDER BY nb DESC LIMIT 10";
    $result = db_query ($query);
    
    $out .= "<table width='100%'>\n";
    $out .= "<tr class='ModuleTabTitle'><td>Host</td><td>Nb</td></tr>\n";

    while($line = db_nextobject($result)){
      $out .= "<tr class='TopHostEntry'><td>";
      $out .= "<a href='$url_base/from_host.php?ip=$line->ip_src'>";
      if (strcmp($line->name_src,"Unresolved")==0) {
	$out .= $line->ip_src;
      } else {
	$out .= $line->name_src;
      }
      $out .= "</a>";
      $out .= "</td><td>$line->nb</td></tr>\n";
    }
    #$out .= "<tr class='TopHostEntry'><td>$query</td></tr>\n";
    $out .= "</table>\n";

    return $out;
  }

  function display() {
    global $cache_delay;

    $cache = new Cache($cache_delay);

    $str = $cache->get("TopHosts");
    if (strlen($str)==0) {
      print "<!-- The module is regenerated -->\n";
      $str = $this->generate();
      $cache->put("TopHosts",$str);
    }
    print $str;
    
  }
}

?>
Return current item: IPTables log analyzer