2010-09-03 18:26:59 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								#!/usr/bin/env php
 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-09 21:13:27 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								< ? php 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								$debug  =  " 1 " ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2010-02-27 14:44:38 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								include ( " includes/defaults.inc.php " ); 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-09 21:13:27 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								include ( " config.php " ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								include ( " includes/functions.php " ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								$iter  =  " 0 " ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								echo ( " Starting Polling Session ...  \n \n " ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								$bill_query  =  mysql_query ( " select * from bills " ); 
							 
						 
					
						
							
								
									
										
										
										
											2011-04-06 13:54:50 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								while  ( $bill_data  =  mysql_fetch_assoc ( $bill_query )) 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								{ 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  echo ( " Bill :  " . $bill_data [ 'bill_name' ] . " \n " ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  CollectData ( $bill_data [ 'bill_id' ]); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  $iter ++ ; 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-09 21:13:27 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								function  CollectData ( $bill_id ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								{ 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  $port_query  =  mysql_query ( " select * from bill_ports as P, ports as I, devices as D where P.bill_id=' $bill_id ' AND I.interface_id = P.port_id AND D.device_id = I.device_id " ); 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2011-04-06 13:54:50 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  while  ( $port_data  =  mysql_fetch_assoc ( $port_query )) 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $port_in_measurement ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $port_in_delta ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $last_port_in_measurement ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $last_port_in_delta ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $port_out_measurement ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $port_out_delta ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $last_port_out_measurement ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $last_port_out_delta ); 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    $port_id  =  $port_data [ 'port_id' ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $host     =  $port_data [ 'hostname' ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
										$port 	 =  $port_data [ 'port' ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    echo ( " \n Polling  " . $port_data [ 'ifDescr' ] . "  on  " . $port_data [ 'hostname' ] . " \n " ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $port_in_measurement  =  getValue ( $host ,  $port ,  $port_data [ 'ifIndex' ],  " In " ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $port_out_measurement  =  getValue ( $host ,  $port ,  $port_data [ 'ifIndex' ],  " Out " ); 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    $now  =  mysql_result ( mysql_query ( " SELECT NOW() " ),  0 ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $last_data  =  getLastPortCounter ( $port_id , in ); 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    if  ( $last_data [ state ]  ==  " ok " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    { 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      $last_port_in_measurement  =  $last_data [ counter ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      $last_port_in_delta  =  $last_data [ delta ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      if  ( $port_in_measurement  >  $last_port_in_measurement ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        $port_in_delta  =  $port_in_measurement  -  $last_port_in_measurement ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        $port_in_delta  =  $last_port_in_delta ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    }  else  { 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      $port_in_delta  =  '0' ; 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    } 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    $pim  =  " INSERT INTO port_in_measurements (port_id,timestamp,counter,delta) VALUES ( $port_id , ' $now ',  $port_in_measurement ,  $port_in_delta )  " ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    #echo("$pim \n");
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $pim_query  =  mysql_query ( $pim ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $last_data ,  $last_port_in_measurement ,  $last_port_in_delta ); 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    $last_data  =  getLastPortCounter ( $port_id , out ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    if  ( $last_data [ state ]  ==  " ok " ) 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    { 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      $last_port_out_measurement  =  $last_data [ counter ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      $last_port_out_delta  =  $last_data [ delta ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      if  ( $port_out_measurement  >  $last_port_out_measurement ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        $port_out_delta  =  $port_out_measurement  -  $last_port_out_measurement ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        $port_out_delta  =  $last_port_out_delta ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      $port_out_delta  =  '0' ; 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-11 18:03:49 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    } 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								    $pom  =  " INSERT INTO port_out_measurements (port_id,timestamp,counter,delta) VALUES ( $port_id , ' $now ',  $port_out_measurement ,  $port_out_delta )  " ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    #echo("$pom \n");
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $pom_query  =  mysql_query ( $pom ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $last_data ,  $last_port_in_measurement ,  $last_port_in_delta ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $delta  =  $delta  +  $port_in_delta  +  $port_out_delta ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $in_delta  =  $in_delta  +  $port_in_delta ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $out_delta  =  $out_delta  +  $port_out_delta ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    unset ( $port_in_delta , $port_out_delta , $prev_delta , $prev_timestamp , $period ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  $last_data  =  getLastMeasurement ( $bill_id ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  if  ( $last_data [ state ]  ==  " ok " ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $prev_delta      =  $last_data [ delta ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $prev_in_delta   =  $last_data [ in_delta ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $prev_out_delta  =  $last_data [ out_delta ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $prev_timestamp  =  $last_data [ timestamp ]; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $period  =  mysql_result ( mysql_query ( " SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) - UNIX_TIMESTAMP(' $prev_timestamp ') " ), 0 ); 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  }  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $prev_delta  =  '0' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $period    =  '0' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $prev_in_delta  =   '0' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $prev_out_delta  =   '0' ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2011-03-27 10:21:19 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  if  ( $delta  <  '0' ) 
							 
						 
					
						
							
								
									
										
										
										
											2011-03-23 09:54:56 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $delta  =  $prev_delta ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $in_delta  =  $prev_in_delta ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    $out_delta  =  $prev_out_delta ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  $insert_string  =  " INSERT INTO bill_data (bill_id,timestamp,period,delta,in_delta,out_delta) VALUES (' $bill_id ',' $now ',' $period ',' $delta ',' $in_delta ',' $out_delta ') " ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  $insert_measurement  =  mysql_query ( $insert_string ); 
							 
						 
					
						
							
								
									
										
										
										
											2008-03-09 21:13:27 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								if  ( $argv [ 1 ])  {  CollectData ( $argv [ 1 ]);  } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2011-03-22 12:09:25 +00:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								?>