summaryrefslogtreecommitdiff
path: root/scripts/vyatta-wlb-connection.pl
blob: c67e8bf4f1595dbce855acd7fbadb4e916c54f48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/perl
#
# Module: vyatta-show-wlb-connection.pl
# 
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
# 
# Author: Michael Larson
# Date: December 2008
# Description: Script to display wlb connection information
# 
# **** End License ****
#

use lib "/opt/vyatta/share/perl5/";

#examine /var/run/load-balance/wlb.conf for disable-source-nat
if (!open($CONFFILE, "<", "/var/run/load-balance/wlb.conf")) {
    return;
}
$_ = <$CONFFILE>;
if (/disable-source-nat/) {
    if (!open($FILE, "<", "/proc/net/ip_conntrack")) {
	return;
    }
}
else {
    if (!open($FILE, "/usr/sbin/conntrack -L -n|")) {
	return;
    }
}

print "Type\tState\t\tSrc\t\t\tDst\t\t\tPackets\tBytes\n";

@line = <$FILE>;
foreach (@line) {
   $_ =~ s/\[\S+\]\s//;


   my $proto,$tmp,$state,$src,$dst,$sport,$dport,$pkts,$bytes;


   if (/tcp/) {
       ($proto,$tmp,$tmp,$state,$src,$dst,$sport,$dport,$pkts,$bytes) = split(' ',$_);
   }
   elsif (/udp/) {
       $state = "";
       ($proto,$tmp,$tmp,$src,$dst,$sport,$dport,$pkts,$bytes) = split(' ',$_);
   }
   ($tmp,$src) = split('=',$src);
   ($tmp,$dst) = split('=',$dst);
   ($tmp,$dport) = split('=',$dport);
   ($tmp,$sport) = split('=',$sport);
   ($tmp,$pkts) = split('=',$pkts);
   ($tmp,$bytes) = split('=',$bytes);

   my $snet = sprintf("%s:%s%-10s",$src,$sport);
   $snet = substr($snet,0,18);

   my $dnet = sprintf("%s:%s%-10s",$dst,$dport);
   $dnet = substr($dnet,0,18);

   $state = sprintf("%s%-12s",$state);
   $state = substr($state,0,12);

   #mark=[1-9]
   if (/ mark=[1-9]/)  {
       print "$proto\t$state\t$snet\t$dnet\t$pkts\t$bytes\n";
   }
}

#now dump out results