Guest User

netflow php parser

a guest
Sep 5th, 2011
2,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.38 KB | None | 0 0
  1. <?php
  2. /**************************
  3. nfdump output parser script
  4. Use: nfcapd -l ... -D -x '/path_to_php/php /path_to_script/all_traf_stats.php %d/%f'
  5. Script collects data into array $stats
  6. (
  7.     [dst_cnt] => Array
  8.         (
  9.             [ip] => hits            
  10.         )
  11.     [src_cnt] => Array
  12.         (
  13.             [ip] => hits            
  14.         )
  15.     [dst_traf] => Array
  16.         (
  17.             [ip] => octets (bytes)            
  18.         )
  19.     [src_traf] => Array
  20.         (
  21.             [ip] => octets (bytes)
  22.         )
  23.     [proto] => Array
  24.         (
  25.             [proto:port] => bytes
  26.         )
  27. )
  28.  
  29. (c) Ross Vladislav, 2011
  30. ***************************/
  31.  
  32. /*  Settings    */
  33. define('STATFILE', 'stats.dat'); //Stats file name
  34. define('LOGFILE', 'all_traf_stats.log'); //Logfile name
  35.  
  36. //Local networks, "address/mask" format
  37. $localnets = array(
  38.     '192.168.0.0/255.255.255.0',
  39.     '192.168.4.0/255.255.254.0',
  40. );
  41.  
  42. //Do not gather statistics for these ports:
  43. $skipPorts = array(80, 21);
  44.  
  45.  
  46. error_reporting(E_ALL ^ E_NOTICE);
  47. chdir(dirname(__FILE__));
  48.  
  49. if(!isset($argv[1]) || !file_exists($argv[1])) exit(1);
  50. else $file = $argv[1];
  51.  
  52. if(@$_SERVER['REMOTE_ADDR']) exit;
  53.  
  54. $try = 0;
  55. while($try < 5 && !file_exists($file))
  56. {
  57.     $try++;
  58.     sleep(2);
  59. }
  60.  
  61. if($try == 5)
  62. {
  63.     file_put_contents(LOGFILE, date('d.m.Y H:i:s'). " " . $file . " not exists!\r\n", FILE_APPEND);
  64.     exit(1);
  65. }
  66.  
  67. file_put_contents(LOGFILE, date('d.m.Y H:i:s'). " " . $file . " started...\r\n", FILE_APPEND);
  68.    
  69. $prefixes = array(
  70.     '1048576' => 'M',
  71.     '1073741824' => 'G',
  72.     '1099511627776' => 'T'
  73. );
  74.    
  75. function is_local($ip)
  76. {
  77.     global $localnets;
  78.     list(, $ip) = unpack('l',pack('l', ip2long($ip)));
  79.     foreach($localnets as $localnet)
  80.     {
  81.         if($ip >= $localnet['start'] && $ip <= $localnet['end']) return true;
  82.     }
  83.     return false;
  84. }
  85.  
  86. foreach($localnets as $k => $net)
  87. {
  88.    
  89.     list($addr, $mask) = explode('/', $net);
  90.     list(, $addr) = unpack('l',pack('l', ip2long($addr)));
  91.     list(, $mask) = unpack('l',pack('l', ip2long($mask)));
  92.     $start = ($addr & $mask) + 1;
  93.     $end = $start + (~ $mask) - 1;
  94.     $localnets[$k] = array('start' => $start, 'end' => $end);
  95. }
  96.  
  97.  
  98. $traf_size = array();
  99. $traf_connections = array();
  100.  
  101. touch(STATFILE);
  102. $stats = unserialize(file_get_contents(STATFILE));
  103.  
  104. file_put_contents(LOGFILE, date('d.m.Y H:i:s'). " Executing nfdump\r\n", FILE_APPEND);
  105.  
  106. $fp = popen("nfdump -r $file -q", 'r');
  107. while(($row = fgets($fp)) !== false)
  108. {
  109.     $row = preg_split("/[\s]+/", $row);
  110.     $rc = count($row);
  111.     if($rc != 11 && $rc != 12)
  112.     {
  113.         file_put_contents(LOGFILE, date('d.m.Y H:i:s'). " Wrong nfdump output format!\r\n", FILE_APPEND);
  114.         print_r($row);
  115.         pclose($fp);
  116.         die("Wrong nfdump output format!\r\n");
  117.     }
  118.    
  119.     list($src_ip, $src_port) = explode(':', $row[3]);
  120.     list($dst_ip, $dst_port) = explode(':', $row[6]);
  121.    
  122.     if(is_local($src_ip) != is_local($dst_ip))
  123.     {
  124.         if(is_local($src_ip))
  125.         {
  126.             $local_ip = $src_ip;
  127.             $ext_ip = $dst_ip;
  128.             $port = $dst_port;
  129.         }
  130.         else
  131.         {
  132.             $local_ip = $dst_ip;
  133.             $ext_ip = $src_ip; 
  134.             $port = $src_port;         
  135.         }
  136.        
  137.            
  138.         $prefix_value = in_array($row[10], $prefixes);
  139.         if($prefix_value !== FALSE) $bytes = $prefix_value * $row[9];
  140.         else $bytes = $row[9];
  141.        
  142.         if(in_array($port, $skipPorts) === FALSE)
  143.         {
  144.             $traf_size[$local_ip] += $bytes;
  145.             $traf_connections[$local_ip]++;
  146.         }
  147.         $proto = strtolower($row[2]);
  148.        
  149.         $stats['dst_cnt'][$dst_ip]++;
  150.         $stats['src_cnt'][$src_ip]++;
  151.        
  152.         if($bytes > 0)
  153.         {
  154.             $stats['dst_traf'][$dst_ip] += $bytes;     
  155.             $stats['src_traf'][$src_ip] += $bytes;
  156.             if($proto == 'udp' || $proto == 'tcp') $stats['proto'][$proto . ":" . $dst_port] += $bytes;
  157.             else $stats['proto'][$proto] += $bytes;
  158.         }  
  159.        
  160.     }  
  161. }
  162.  
  163. pclose($fp);
  164.  
  165. /***** uncomment this block if you want to use nfdump with squid+sams *********
  166. if(!mysql_connect("localhost", "sams", "password")) exit(1);
  167. if(!mysql_select_db("squidctrl")) exit(1);
  168. foreach($traf_size as $ip => $traf)
  169. {
  170.     if($traf > 0)
  171.     {
  172.         mysql_query("UPDATE `squidctrl`.`squidusers` SET size=size+$traf, hit=hit+{$traf_connections[$ip]} WHERE `ip`='$ip' LIMIT 1");
  173.         $mar += mysql_affected_rows();
  174.         $sum += $traf;
  175.     }
  176. }
  177. *******************************************************************************/
  178.  
  179. file_put_contents(STATFILE, serialize($stats));
  180. file_put_contents(LOGFILE, date('d.m.Y H:i:s'). " " . $file . "finished! \r\n", FILE_APPEND);
  181. ?>
Advertisement
Add Comment
Please, Sign In to add comment