home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / nstat / nsum.perl < prev    next >
Encoding:
Text File  |  1993-06-18  |  4.4 KB  |  202 lines

  1. #!/usr/local/bin/perl -s
  2. #
  3. # histogram summary of nstat logs (file or stdin)
  4. #
  5. # nsum [- M | A | D | E | N ] [- w | W ] [ filename ... ]
  6. #        M = morning (8am-11am)
  7. #        A = afternoon (noon - 5pm)
  8. #        D = day  (morning | afternoon)
  9. #        E = evening (6pm - 11pm)
  10. #        N = night (midnignt - 7am)
  11. #
  12. #        w = workday
  13. #        W = weekend
  14. #
  15.  
  16. # tailor this for your site: our fractional T1 has 384 Kbits/sec:
  17. $BANDWIDTH=((384*1024)/8);
  18.  
  19. # N-Night, M-Morning, D-Day, A-Afternoon, E-Evening
  20. for (0..7){$HOURCLASS[$_]="N";}
  21. for (8..11){$HOURCLASS[$_]="MD";}
  22. for (12..17){$HOURCLASS[$_]="AD";}
  23. for (18..23){$HOURCLASS[$_]="E";}
  24.  
  25. $DAYCLASS{"Mon"}="work";
  26. $DAYCLASS{"Tue"}="work";
  27. $DAYCLASS{"Wed"}="work";
  28. $DAYCLASS{"Thu"}="work";
  29. $DAYCLASS{"Fri"}="work";
  30. $DAYCLASS{"Sat"}="wend";
  31. $DAYCLASS{"Sun"}="wend";
  32.  
  33. sub sortsub {
  34.     $AR{$b} <=> $AR{$a};
  35. }
  36.  
  37. $reportcount=0;
  38.     
  39. $record=1;
  40.  
  41. while(<>){
  42.     chop;
  43.     ($proto, $port, $x, $count, $y, $bytes) = split;
  44.     if(/^#Start/){
  45.        ($x, $dow, $mon, $dom, $tod, $year) = split;
  46.        ($hour, $min, $sec) = split(/:/,$tod);
  47.        $record = 1;
  48.        $record = 0 if($W && $DAYCLASS{$dow} ne "wend");
  49.        $record = 0 if($w && $DAYCLASS{$dow} ne "work");
  50.        $record = 0 if($N && $HOURCLASS[$hour] !~ /N/);
  51.        $record = 0 if($M && $HOURCLASS[$hour] !~ /M/);
  52.        $record = 0 if($D && $HOURCLASS[$hour] !~ /D/);
  53.        $record = 0 if($A && $HOURCLASS[$hour] !~ /A/);
  54.        $record = 0 if($E && $HOURCLASS[$hour] !~ /E/);
  55.        $reportcount++ if($record);
  56.     }
  57.     if(/^#Stop/){
  58.        ($x, $dow, $mon, $dom, $tod, $year) = split;
  59.     }
  60.     if($record == 1){
  61.     if($proto eq "t"){
  62.         $TCP{$port} += $bytes;
  63.         $TCPtotal += $bytes;
  64.     }
  65.     elsif($proto eq "u"){
  66.         $UDP{$port} += $bytes;
  67.         $UDPtotal += $bytes;
  68.     }
  69.     elsif($proto eq "i"){
  70.         $IP{$port} += $bytes;
  71.         $IPtotal += $bytes;
  72.     }
  73.     elsif($proto eq "e"){
  74.         if(hex($port) < 1514){
  75.         $ETHER{"0"} += $bytes;
  76.         }
  77.         else {
  78.         $ETHER{hex($port)} += $bytes;
  79.         }
  80.         $ETHERtotal += $bytes;
  81.     }
  82.     }
  83. }
  84.  
  85. if($reportcount){
  86.     printf("Utilization: %5.2f%%\n\n", 
  87.        (($ETHERtotal/36.00)/$BANDWIDTH)/$reportcount);
  88. }
  89. $TCPtotal /= 120.00;
  90. $UDPtotal /= 120.00;
  91. $IPtotal /= 60.00;
  92. $ETHERtotal /= 60.00;
  93.  
  94. for (keys %TCP){
  95.     $TCPpercent{$_} = $TCP{$_}/$TCPtotal if($TCP{$_} >= $TCPtotal);
  96. }
  97.  
  98. for (keys %UDP){
  99.     $UDPpercent{$_} = $UDP{$_}/$UDPtotal if($UDP{$_} >= $UDPtotal);
  100. }
  101.  
  102. $IPlow = $IPtotal/10;
  103.  
  104. for (keys %IP){
  105.     $IPpercent{$_} = $IP{$_}/$IPtotal if($IP{$_} >= $IPlow);
  106. }
  107.  
  108. $ETHERlow = $ETHERtotal/10;
  109.  
  110. for (keys %ETHER){
  111.     $ETHERpercent{$_} = $ETHER{$_}/$ETHERtotal if($ETHER{$_} >= $ETHERlow);
  112. }
  113.  
  114. *AR=ETHERpercent;
  115. $ethtype{"0"}="802.3";
  116. $ethtype{hex("0800")}="IP";
  117. $ethtype{hex("0BAD")}="Banyan";
  118. $ethtype{hex("6002")}="DECRCONS";
  119. $ethtype{hex("6003")}="DECIVDNA";
  120. $ethtype{hex("6004")}="DECLAT";
  121. $ethtype{hex("6007")}="DECLAVC";
  122. $ethtype{hex("8037")}="IPX";
  123. $ethtype{hex("8038")}="DECLBM"; #/* DEC LAN Bridge Mgt */
  124. $ethtype{hex("8137")}="oldIPX";
  125.  
  126. $count=0;
  127. for (sort(sortsub keys %ETHERpercent)){
  128.     $iperc = int($ETHERpercent{$_} + 0.5);
  129.     if(defined($ethtype{$_})){
  130.     $name=$ethtype{$_};
  131.     }
  132.     else {
  133.     $name=sprintf("%04X", $_);
  134.     }
  135.     printf("ETH %-8s  (%2.0f%%/%2.0f%%):%s\n",
  136.        $name,
  137.        $ETHERpercent{$_},
  138.        (($ETHER{$_}/36.00)/$BANDWIDTH)/$reportcount,
  139.        '#' x $iperc);
  140.     last if(++$count >= 10);
  141. }
  142.  
  143. print "\n";
  144.  
  145. *AR=IPpercent;
  146.  
  147. $IPproto{1}="ICMP";
  148. $IPproto{6}="TCP";
  149. $IPproto{9}="IGP";
  150. $IPproto{17}="UDP";
  151.  
  152. $count=0;
  153. for (sort(sortsub keys %IPpercent)){
  154.     $iperc = int($IPpercent{$_} + 0.5);
  155.     $name=$_;
  156.     $name=$IPproto{$_} if(defined($IPproto{$_}));
  157.     printf("IP  %-8s (%2.0f%%/%2.0f%%):%s\n",
  158.        $name,
  159.        $IPpercent{$_},
  160.        $IP{$_}/$ETHERtotal,
  161.        '#' x $iperc);
  162.     last if(++$count >= 10);
  163. }
  164.  
  165. print "\n";
  166.  
  167. *AR=TCPpercent;
  168.  
  169. $count=0;
  170. for (sort(sortsub keys %TCPpercent)){
  171.     $iperc = int($TCPpercent{$_} + 0.5);
  172.     @se = getservbyport($_, "tcp");
  173.     $name = $_;
  174.     $name = $se[0] if($#se != -1);
  175.     printf("TCP %-8s (%2.0f%%/%2.0f%%):%s\n",
  176.        $name,
  177.        $TCPpercent{$_},
  178.        $TCP{$_}/$ETHERtotal,
  179.        '#' x $iperc);
  180.     last if(++$count >= 10);
  181. }
  182.  
  183. print "\n";
  184.  
  185. *AR=UDPpercent;
  186.  
  187. $count=0;
  188. for (sort(sortsub keys %UDPpercent)){
  189.     $iperc = int($UDPpercent{$_} + 0.5);
  190.     @se = getservbyport($_, "udp");
  191.     $name = $_;
  192.     $name = $se[0] if($#se != -1);
  193.     printf("UDP %-8s (%2.0f%%/%2.0f%%):%s\n",
  194.        $name,
  195.        $UDPpercent{$_},
  196.        $UDP{$_}/$ETHERtotal,
  197.        '#' x $iperc);
  198.     last if(++$count >= 10);
  199. }
  200.  
  201. print "\n";
  202.