/** */ public static function dataTransform($trArr) { global $savePath, $webUrl; /* echo '<textarea>'; print_r($trArr); echo '</textarea>';*/ $date = md5(date('d-m-o_G-i-s')); $myLogFile = $savePath . "/" . "_log_" . $date . ".csv"; $myLogFileWeb = $webUrl . '/gm-temp/_log_' . $date . ".csv"; //$fhLog = fopen($myLogFile, 'w') or die("can't open file"); $dist_from_origin = 0; $latOrigin = 0; $longOrigin = 0; $originAsn = 0; $imp_dist = 0; $imp_dist_txt = '"Trid";"Hop";"Country";"City";"ASN";"IP";"Latency";"Time SoL";"Distance From Origin (KM)";"gl_override";"Origin Lat";"Origin Long"; "Origin ASN" '; //fwrite($fhLog, $imp_dist_txt); $time_light_will_do = 0; $trData = array(); // distance speed of light in KM per 1 milsec $SL = 200; //$SL = 86; for ($i = 0; $i < count($trArr); $i++) { $dist_from_origin = 0; //$latOrigin = 0; //$longOrigin = 0; $imp_dist = 0; $imp_dist_txt = ''; $time_light_will_do = 0; $trId = $trArr[$i]['id']; $hop = $trArr[$i]['hop']; $ip = $trArr[$i]['ip_addr']; /* $lat = $trArr[$i]['mm_lat']; $long = $trArr[$i]['mm_long']; */ $lat = $trArr[$i]['lat']; $long = $trArr[$i]['long']; $num = $trArr[$i]['num']; $name = $trArr[$i]['name']; $rtt_ms = $trArr[$i]['rtt_ms']; // calclulate first hop for first hop available: note this is not 100% acurate //if($i==0){ if ($hop == 1) { $latOrigin = $lat; $longOrigin = $long; $originAsn = $num; } else { // calculate distance from origin $dist_from_origin = Traceroute::distance($latOrigin, $longOrigin, $lat, $long, false); $time_light_will_do = $dist_from_origin / $SL; $time_light_will_do *= 2; // is it an imposible time? distance? if ($rtt_ms < $time_light_will_do) { $imp_dist = 1; //$imp_dist_txt = '<b>YES!</b>'; } } $trData[$trId][] = array($ip, $hop, $lat, $long, $trId, $num, $name, $trArr[$i]['dest'], $trArr[$i]['dest_ip'], $trArr[$i]['submitter'], $trArr[$i]['mm_city'], $trArr[$i]['mm_country'], $trArr[$i]['sub_time'], $trArr[$i]['rtt_ms'], $trArr[$i]['gl_override'], $dist_from_origin, $imp_dist, $time_light_will_do, $latOrigin, $longOrigin); // write impossible distances to a file: this method seems to be more secure that jQuery if ($imp_dist == 1) { $impDistanceLog = '' . $trId . ';' . $hop . ';"' . $trArr[$i]['mm_country'] . '";"' . $trArr[$i]['mm_city'] . '";' . $num . ';"' . $ip . '";' . $trArr[$i]['rtt_ms'] . ';' . $time_light_will_do . ';"' . $dist_from_origin . '";' . $trArr[$i]['gl_override'] . ';"' . $latOrigin . '";"' . $longOrigin . '";"' . $originAsn . '" '; //echo '<br/>'.$imp_dist_txt.$impDistanceLog; //fwrite($fhLog, $impDistanceLog); } } // end for //fclose($fhLog); //echo '<br/>Impossible Distances log saved at <a href="'.$myLogFileWeb.'">_log_'.$date.'.csv</a>'; unset($trArr); return $trData; //unset($trData); /* echo '<hr/><textarea>'; print_r($trData); echo '</textarea>'; */ }
/** Transform basic tr results array and gather new data for advanced analysis. i.e SoL calculations */ public static function dataTransform($trArr) { global $savePath, $webUrl; /* echo '<textarea>'; print_r($trArr); echo '</textarea>'; */ print_r($trArr); $date = md5(date('d-m-o_G-i-s')); $myLogFile = $savePath . "/" . "_log_" . $date . ".csv"; $myLogFileWeb = $webUrl . '/gm-temp/_log_' . $date . ".csv"; //$fhLog = fopen($myLogFile, 'w') or die("can't open file"); $dist_from_origin = 0; $latOrigin = 0; $longOrigin = 0; $originAsn = 0; $imp_dist = 0; $imp_dist_txt = '"Trid";"Hop";"Country";"City";"ASN";"IP";"Latency";"Time SoL";"Distance From Origin (KM)";"gl_override";"Origin Lat";"Origin Long"; "Origin ASN" '; //fwrite($fhLog, $imp_dist_txt); $time_light_will_do = 0; $trData = array(); // distance speed of light in KM per 1 milsec $SL = 200; //$SL = 86; // get tr data for all attempts only once $activeTrId = $trArr[0]['id']; $trDetailsAllData = Traceroute::getTraceRouteAll($activeTrId); // exit funcion if not data is returned if (count($trDetailsAllData) == 0) { return array(); } /* echo '<textarea>'; print_r($trArr); echo '</textarea>'; */ // analyze min latency for origin // calculate if the min latency of the following hop is less than the min latency of the origin, // if so assign that min latency to orign and this also applies to following hop; where current hop != from origin and != from last hop. // origin data // last hop data // analyze here all the hops in between first and last // assess geocorrection. Based on this analysis we could indicate which IP should be used to replace wrong coordinates of any hop, based on the following logic: /* a) N-1 and N+1 for currentHop, when currentHop != first and != last hop b) N+1 for currentHop, when currentHop = first hop c) N-1 for currentHop, when currentHop = last hop */ $totHopsData = count($trDetailsAllData); /*FIXME: why is not set? It is not set because the hop has no ip a*/ //print_r($trDetailsAllData); $lastHop = $trDetailsAllData[$totHopsData - 1]['hop']; $firstHop = $trDetailsAllData[0]['hop']; $latenciesArray = array(); foreach ($trDetailsAllData as $trDetail => $TrDetailData) { $currentHop = $TrDetailData['hop']; // collect latencies and exclude values = -1 and = 0 if ($TrDetailData['rtt_ms'] != -1 && $TrDetailData['rtt_ms'] != 0) { // this approach actually works better. Capture all here, then analyze the array. $latenciesArray[$TrDetailData['hop']][] = $TrDetailData['rtt_ms']; //$latenciesArray[$TrDetailData['hop']][$TrDetailData['rtt_ms']]=0; } } // end for collecting latencies //$ar2 = array(1, 3, 2, 4); //array_multisort($latenciesArray,$ar2); /* This approach to calculate speed impossible distance is put on standby for now.. will come back to it laters ;) It's way to unstable still. */ ////////////////////////// /* $minOriginLatency = sort($latenciesArray[1]); $minOriginLatency = $latenciesArray[1][0]; $latenciesArrayCalculated = array(); // sort the latencies in the array and get min latencies foreach ($latenciesArray as $key => $value) { echo 'sorting latencies for TRid: '.$activeTrId.' Hop: '.$key; //ksort($latenciesArray[$key], SORT_DESC); //rsort($latenciesArray[$key]); sort($latenciesArray[$key]); // just remove all the other latencies, and keep the min latency $latenciesArray[$key]=$latenciesArray[$key][0]; $minL=$latenciesArray[$key]; if($minOriginLatency>$minL && $key>1){ $minOriginLatency=$minL; } }*/ /* loop again and re-asign the min possible latency based on min value in subsequent hops As it works on current Traceroute detail page */ /* foreach ($latenciesArray as $key => $value) { //echo '<br/>... Checking hop '.$key; $minLofAllNext = Traceroute::checkMinLatency($key, $latenciesArray); $latenciesArrayCalculated[$key]=$minLofAllNext; } */ // log: comparison between actual min and calculated latencies /*echo '<textarea>$minOriginLatency: '. $minOriginLatency.''; print_r($latenciesArray); echo '</textarea>';*/ /* echo '<textarea>--- Calculated Latencies for each hop trId: ['.$activeTrId.']:'; print_r($latenciesArrayCalculated); echo '</textarea>'; */ ////////////////////////// // start loop over tr data array, where $i is an index of joined traceroute and tr_item tables for ($i = 0; $i < count($trArr); $i++) { //echo '****************************'.$trArr[$i]['hostname']; // key data for google display $trId = $trArr[$i]['id']; $hop = $trArr[$i]['hop']; $ip = $trArr[$i]['ip_addr']; /* $lat = $trArr[$i]['mm_lat']; $long = $trArr[$i]['mm_long']; */ $lat = $trArr[$i]['lat']; $long = $trArr[$i]['long']; $num = $trArr[$i]['num']; $nameLen = strlen($trArr[$i]['name']); $pattern1 = '/ - /'; preg_match_all($pattern1, $trArr[$i]['name'], $matches, PREG_SET_ORDER); if ($nameLen < 23) { $name = $trArr[$i]['name'] . ''; } else { if (count($matches) == 1) { $nameArr = explode(' - ', $trArr[$i]['name']); $nameLen1 = strlen($nameArr[1]); if ($nameLen1 > 23) { $name = substr($nameArr[1], 0, 22) . '...'; } else { $name = $nameArr[1] . ''; } unset($nameArr); } else { //$nameArr = explode(' ', $trArr[$i]['name']); //$name = $nameArr[0].' : 3'; $name = substr($trArr[$i]['name'], 0, 22) . '...'; } } unset($matches); // data needed for impossible distance calculation $dist_from_origin = 0; $imp_dist = 0; $imp_dist_txt = ''; $time_light_will_do = 0; // old approach: use only the first attempt data $rtt_ms = $trArr[$i]['rtt_ms']; // new approach: use min latency out of the 4 attemps and correct it relative to the min latency of subsequent hops. This seems to be working quite well ;) There seems to be // Stil under development, causing a too much processing for Anto standards ;) //$rtt_ms = $latenciesArrayCalculated[$hop]; // calclulate origin assuming it does have a hop number = 1; Note this is not 100% acurate as there might be traceroutes that have missed it and start on a number > 1 //if($i==0){ if ($hop == 1) { $latOrigin = $lat; $longOrigin = $long; $originAsn = $num; } else { // calculate distance from origin $dist_from_origin = Traceroute::distance($latOrigin, $longOrigin, $lat, $long, false); $time_light_will_do = $dist_from_origin / $SL; $time_light_will_do *= 2; // is it an imposible time? distance? //if($rtt_ms<$time_light_will_do){ // use $minOriginLatency instead if ($rtt_ms < $time_light_will_do) { $imp_dist = 1; //$imp_dist_txt = '<b>YES!</b>'; } } $lastHopIp = 0; $trData[$trId][] = array($ip, $hop, $lat, $long, $trId, $num, $name, $trArr[$i]['dest'], $trArr[$i]['dest_ip'], $trArr[$i]['submitter'], $trArr[$i]['mm_city'], $trArr[$i]['mm_country'], $trArr[$i]['sub_time'], $trArr[$i]['rtt_ms'], $trArr[$i]['gl_override'], $dist_from_origin, $imp_dist, $time_light_will_do, $latOrigin, $longOrigin, $lastHopIp, $trArr[$i]['flagged'], $trArr[$i]['hostname'], $trArr[$i]['country_code']); // write impossible distances to a CSV file: this method seems to be more secure and faster than doing in jQuery: NOTE: this is only for development version. It seems an overhead for production if ($imp_dist == 1) { $impDistanceLog = '' . $trId . ';' . $hop . ';"' . $trArr[$i]['mm_country'] . '";"' . $trArr[$i]['mm_city'] . '";' . $num . ';"' . $ip . '";' . $trArr[$i]['rtt_ms'] . ';' . $time_light_will_do . ';"' . $dist_from_origin . '";' . $trArr[$i]['gl_override'] . ';"' . $latOrigin . '";"' . $longOrigin . '";"' . $originAsn . '" '; //echo '<br/>'.$imp_dist_txt.$impDistanceLog; //fwrite($fhLog, $impDistanceLog); } } // end for //fclose($fhLog); //echo '<br/>Impossible Distances log saved at <a href="'.$myLogFileWeb.'">_log_'.$date.'.csv</a>'; unset($trArr); return $trData; //unset($trData); /* echo '<hr/><textarea>'; print_r($trData); echo '</textarea>'; */ }