#!/usr/bin/php4 -q as an extension for GRH (http://www.sixxs.net/tools/grh/) # # log.txt format: # [: ... ] # # eg: # 2006-01-01 286-b 1273:625 4589:6 5539:13 6175:3 8237:1 8472:7 12731:2 20704:1 21385:1 29259:4 30128:1 # # Well known transits $transits = array( 109, 1273, 1752, 2497 ,2914, 3257, 3356, 3549, 6175, 6830, 6939, 10566, 11537, 12702, 17715, 18084, 20965, 30071 ); $test = 0; global $fhs; $fhs = array(); $top = array(); $dat = array(); $totp = array(); $tots = array(); $f = fopen("log.txt", "r"); if (!$f) { print "FAIL, couldn't open log.txt\n"; return; } while (!feof($f)) { $l = fgets($f); $l = trim($l); if ($l == "" || $l[0] == "#") continue; $items = split(" ", $l); $date = array_shift($items); $peer = array_shift($items); if ($test && $peer != 109) continue; $cnt = array(); $cnt{0} = 0; while (list($i, $asn) = each($items)) { list($asn, $count) = split(":", trim($asn)); if (!in_array($asn, $transits)) $cnt[0] += $count; else $cnt[$asn] = $count; if (!isset($top[$peer][$asn])) $top[$peer][$asn] = $count; else $top[$peer][$asn] += $count; $dat[$peer][$date][$asn] = $count; if (!isset($top[0][$asn])) $top[0][$asn] = $count; else $top[0][$asn] += $count; #echo $peer." ".$asn." ".$date." ".$count."\n"; } $tr = $transits; reset($tr); $tot = 0; $t = ""; while (list($i, $asn) = each($tr)) { if (!isset($cnt[$asn])) $c = 0; else $c = $cnt[$asn]; $t .= "\t".$c; $tot += $c; } $tot += $cnt[0]; $s = ""; $s .= $date; $s .= "\t".$tot; $s .= "\t".$cnt[0]; $s .= $t; $s .= "\n"; writepeer($peer, $s, $transits); if (isset($totp[$peer])) $totp[$peer] += $tot; else $totp[$peer] = $tot; $tots[$date][$peer] = $tot; } $done = array(); /* Write out the totals in one graph */ arsort($totp); while (list($date, $i) = each($tots)) { if (isset($done[$date])) continue; $done[$date] = 1; $t = ""; reset($totp); while (list($peer, $i) = each($totp)) { $t.= "\t".$tots[$date][$peer]; } $s = ""; $s .= $date; $s .= $t; $s .= "\n"; writepeer("total", $s, $totp, false); } /* Write out per-peer statistics */ ksort($top); while (list($peer, $i) = each($top)) { if ($peer == 0) continue; if ($test && $peer != 109) continue; $tr = array(); arsort($i); $c = 0; while (list($asn, $count) = each($i)) { $c++; if ($c > 10) break; $tr[$asn] = $asn; } while (list($date, $zeta) = @each($dat[$peer])) { $tot = 0; $cnt = 0; $t = ""; reset($tr); while (list($j, $asn) = each($tr)) { if (!isset($dat[$peer][$date][$asn])) $c = 0; else $c = $dat[$peer][$date][$asn]; $t .= "\t".$c; $tot += $c; } while (list($asn, $c) = each($dat[$peer][$date])) { if (isset($tr[$asn])) continue; $cnt+=$c; } $s = ""; $s .= $date; $s .= "\t".($tot+$cnt); $s .= "\t".$cnt; $s .= $t; $s .= "\n"; writepeer($peer."-top", $s, $tr); } } exit(0); function writepeer($name, $s, $tr, $tototr = true) { global $fhs; if (!isset($fhs[$name])) { $fhs[$name] = fopen("peers/peer-".$name.".dat", "w"); if (!$fhs[$name]) { print "open(peers/peer-".$name.".dat): failed\n"; exit(0); } fwrite($fhs[$name], "# date\t\t".($tototr ? "total\tothers\t" : "").implode("\t", $tr)."\n"); create_plotfile($name, $tr, true); create_plotfile($name, $tr, false); } fwrite($fhs[$name], $s); } # http://gnuplot.sourceforge.net/demo_4.1/histograms.html function create_plotfile($name, $tr, $filled) { $f = fopen("peers/peer-".$name."-".($filled ? "filled" : "lines").".dem", "w"); if (!$f) { print "Could not open open(peers/peer-".$name.".dem)\n"; exit(0); } fwrite($f, "set terminal png xffffff font \"/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf\" 10 enhanced size 900,580\n"); fwrite($f, "set output \"peer-".$name."-".($filled ? "filled" : "lines").".png\"\n"); fwrite($f, "set key out noreverse box linetype -2 linewidth 1.000 samplen 2 spacing 0.7\n"); fwrite($f, "set key title \"peers of AS".$name."\n"); fwrite($f, "set ylabel \"Prefixes\"\n"); fwrite($f, "set xlabel \"Time\"\n"); fwrite($f, "set ytics 25\n"); fwrite($f, "set grid y\n"); fwrite($f, "set grid x\n"); fwrite($f, "set auto x\n"); fwrite($f, "set format x \"%Y-%b\"\n"); if ($filled) fwrite($f, "set style fill solid\n"); fwrite($f, "set xdata time\n"); fwrite($f, "set timefmt \"%Y-%m-%d\"\n"); fwrite($f, "plot"); $type = $filled ? "boxes" : "linespoint"; $max = count($tr)+4; $col = 2; fwrite($f, " \\\n\t\"peer-".$name.".dat\" using 1:".$col." title \"total\" with linespoint"); $col++; if ($filled) { $w = "(".$col; for ($i=($col+1);$i<$max;$i++) $w .= "+\$".$i; $w.= ")"; } else $w = $col; fwrite($f, ", \\\n\t\"peer-".$name.".dat\" using 1:".$w." title \"other\" with ".$type); $col++; reset($tr); while (list($i, $asn) = each($tr)) { if ($filled) { $w = "(".$col; for ($i=($col+1);$i<$max;$i++) $w .= "+\$".$i; $w.= ")"; } else $w = $col; fwrite($f, ", \\\n\t\"peer-".$name.".dat\" using 1:".$w." title \"AS".$asn."\" with ".$type); $col++; } fwrite($f, "\n"); fclose($f); }