/[cricket]/parse_iptraf.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /parse_iptraf.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations)
Fri Jul 26 10:10:02 2002 UTC (21 years, 8 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +3 -0 lines
File MIME type: text/plain
better instruction on top of file

1 dpavlin 1.1 #!/usr/bin/perl
2    
3     # based on work of Eric Wong <eric@taedium.net>
4     # available at http://www.taedium.net/rrd-iptraf/ [iptraf.txt]
5 dpavlin 1.5 #
6     # Dobrica Pavlinusic <dpavlin@rot13.org>
7 dpavlin 1.6 # http://www.rot13.org/~dpavlin/sysadm.html
8     #
9     # Usage: parse_iptraf.pl [name of iptraf logfile]
10 dpavlin 1.1
11     use strict;
12     use warnings;
13    
14     my @PORTS = qw (
15     TCP/22 TCP/25 TCP/20 TCP/21 UDP/53
16     TCP/80 TCP/110 TCP/113 TCP/119 UDP/137 UDP/138
17     TCP/210 TCP/443 UDP/514 );
18    
19 dpavlin 1.4 my $log = '/var/log/iptraf/tcp_udp_services-eth1.log';
20     #my $log = '/home/dpavlin/iptraf/tcp_udp_services-eth1.log';
21    
22     $log = $ARGV[0] if ($ARGV[0] && -r $ARGV[0]);
23 dpavlin 1.1
24     open(LOG,$log) || die "Can't open logfile '$log': $!";
25    
26 dpavlin 1.4 my $delta="/var/tmp/";
27     my $tmp_log=$log;
28     $tmp_log=~s/\W/_/g;
29     $delta.=$tmp_log.".offset";
30    
31     if (-e $delta) {
32     open(D,$delta) || die "can't open delta file '$delta' for log '$log': $!";
33     my $offset=<D>;
34     chomp $offset;
35     close(D);
36     my $log_size = -s $log;
37     if ($offset <= $log_size) {
38     seek(LOG,$offset,0);
39     }
40     }
41    
42 dpavlin 1.1 my %hash;
43    
44     while (<LOG>) {
45     chomp;
46     last if ( m/^Running/ );
47     next if ( m/^\s*$/ );
48    
49     ## read data for tcp packets
50     if (my ($port) = $_ =~ m/^(\w+\/\d+)/) {
51     my ($packs, $bytes, $pack_in, $byte_in, $pack_out, $byte_out) =
52     $_ =~ m/\s(\d+)\s/g;
53    
54     $hash{$port} = [$byte_in, $byte_out];
55     }
56     }
57 dpavlin 1.4
58     open(D,"> $delta") || die "can't open delta file '$delta' for log '$log': $!";
59     print D tell(LOG);
60     close(D);
61 dpavlin 1.1
62     close(LOG);
63    
64     foreach my $port (@PORTS) {
65 dpavlin 1.5 print @{$hash{$port}}[0] || "U","\n",@{$hash{$port}}[1] || "U","\n";
66 dpavlin 1.3 delete $hash{$port};
67 dpavlin 1.1 }
68 dpavlin 1.3
69     my ($sum_in,$sum_out) = (0,0);
70    
71     foreach my $port (keys %hash) {
72     $sum_in+=@{$hash{$port}}[0];
73     $sum_out+=@{$hash{$port}}[1];
74     }
75 dpavlin 1.5
76     $sum_in = "U" if ($sum_in == 0);
77     $sum_out = "U" if ($sum_out == 0);
78 dpavlin 1.3 print "$sum_in\n$sum_out\n";

  ViewVC Help
Powered by ViewVC 1.1.26