Process.pm

filter: Statementstmt 36.2% 33 / 91 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 3 totaltotal 28.1% 43 / 153 CC 22 SCAR 54.8
CC
22
Cov
26%
ct2ft
420.0
conntrack_init
6.0
ptee
6.0
CRAP 241.0
↑ Summary Utils.pm »
1 package Net::Netfilter::NetFlow::Process;
2 {
3 2 $Net::Netfilter::NetFlow::Process::VERSION = '1.113260';
4 }
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our @EXPORT = qw(
11 conntrack_init
12 ct2ft
13 ptee
14 );
15  
16 2 ▶use POSIX; # core
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Time::HiRes 'gettimeofday'; # core
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use IPC::Run 'run';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Log::Dispatch::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Log::Dispatch::Configurator::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Net::Netfilter::NetFlow::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Net::Netfilter::NetFlow::ConntrackFormat;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # poke conntrack kernel hooks into waking up (bug?)
25 0 ▶sub conntrack_init {
Subroutine conntrack_init: not called CC=2 SCAR=17.9
Pod undocumented
26 0 my $config = shift;
27 my $conntrack = can_run($config->{conntrack}->{progname})
28 0 ▶ or die "Failed to find a local copy of conntrack in the path\n";
Branch: 1 branch
BranchTrueFalseTotal
my $conntrack = can_run($config->{"conntrack"}{"progname"}) or die "Failed to find a local copy of conntrack in the path\n" 0 0 0
29  
30 0 run [$conntrack, format_args($config->{conntrack}, 'init_')],
31 '>', '/dev/null', '2>&1';
32 }
33  
34 # convert the conntrack output to flow-tools CSV input format
35 0 ▶sub ct2ft {
Subroutine ct2ft: not called CC=20 SCAR=60.4
Pod undocumented
36 0 my $config = shift;
37 0 my $got_alrm = 0;
38 0 my $tracker = {};
39  
40 # respond to SIGALRM (thanks go to perlipc man page)
41 0 ▶ my $alrm_handler = sub { ++$got_alrm };
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 # POSIX unmasks the sigprocmask properly
43 0 my $action = POSIX::SigAction->new($alrm_handler);
44 0 POSIX::sigaction(&POSIX::SIGALRM, $action);
45  
46 0 ▶ my $ttl = $config->{ct2ft}->{ttl} || 60 * 60 * 24 * 7; # seven days;
Condition0%0/2
$config->{'ct2ft'}{'ttl'} || 604800
l!l
00
MC/DC0%0/1
$config->{'ct2ft'}{'ttl'} || 604800
$config->{'ct2ft'}{'ttl'}
Truth table0%0/2
A
A= $config->{'ct2ft'}{'ttl'}
Aresult
11
00
47 0 alarm $ttl;
48  
49 # XXX alarm will not fire until we have input to process
50 0 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 0 0 0
51  
52 # pruge tracked connections older than TTL seconds
53 0 ▶ if ($got_alrm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_alrm) 0 0 0
54 0 alarm 0;
55 0 foreach my $p (keys %{$tracker}) {
56 0 foreach my $k (keys %{$tracker->{$p}}) {
57 delete $tracker->{$p}->{$k}
58 0 ▶ if $tracker->{$p}->{$k} < ($^T - $ttl);
Branch: 1 branch
BranchTrueFalseTotal
if $tracker->{$p}{$k} < $^T - $ttl 0 0 0
59 }
60 }
61 0 $got_alrm = 0;
62 0 alarm $ttl;
63 }
64  
65 0 chomp;
66 0 s/[^\s\d.A-Z]//g;
67 0 ▶ next if m/^\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s+$/ 0 0 0
68 0 my $line = $_;
69 0 my @fields = split /\s+/, $line;
70 0 ▶ next unless scalar @fields > 12;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @fields > 12 0 0 0
71  
72 0 ▶ next unless $fields[1] =~ m/^(NEW|DESTROY)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields[1] =~ /^(NEW|DESTROY)$/ 0 0 0
73 0 my $mode = $1;
74 0 ▶ next unless $fields[2] =~ m/^(1|6|17)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields[2] =~ /^(1|6|17)$/ 0 0 0
75 0 my $proto = $1;
76  
77 0 ▶ next if $proto == 1 and
Branch: 1 branch
BranchTrueFalseTotal
if $proto == 1 and ($fields[5] ne 8 and $fields[6] ne 8) 0 0 0
Condition0%0/6
$proto == 1 and ($fields[5] ne 8 and $fields[6] ne 8)
!ll&&!rl&&r
000
$fields[5] ne 8 and $fields[6] ne 8
!ll&&!rl&&r
000
MC/DC0%0/3
$proto == 1 and ($fields[5] ne 8 and $fields[6] ne 8)
$proto == 1 $fields[5] ne 8 $fields[6] ne 8
Truth table0%0/4
A and B and C
A= $proto == 1B= $fields[5] ne 8C= $fields[6] ne 8
ABCresult
0XX0
10X0
1100
1111
78 (($fields[5] ne '8') and ($fields[6] ne '8')); # only interested in ECHO
79  
80 0 ▶ if ($mode eq 'NEW') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "NEW") 0 0 0
81 0 my $key = join ',', @fields[ @{$ct_new_key{$proto}} ];
82 0 $tracker->{$proto}->{$key} = $fields[0];
83 0 next;
84 }
85  
86 0 my $key = join ',', @fields[ @{$ct_destroy_key{$proto}} ];
87 0 ▶ next unless exists $tracker->{$proto}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tracker->{$proto}{$key} 0 0 0
88  
89 0 my ($start_secs, $start_micsecs) = split /\./, $tracker->{$proto}->{$key};
90 0 my ($end_secs, $end_micsecs) = split /\./, $fields[0];
91  
92 # secs and nanosecs (^9) since 1970
93 0 my ($unix_secs, $micsecs) = gettimeofday;
94 0 my $unix_nsecs = $micsecs * 1_000;
95  
96 # millisecs (^3) since "boot"
97 0 my $sysuptime = (($unix_secs - $^T) * 1_000) + int ($micsecs / 1_000);
98  
99 # flow start/end in millisecs since "boot"
100 0 my $first = (($start_secs - $^T) * 1_000) + int ($start_micsecs / 1_000);
101 0 my $last = (($end_secs - $^T) * 1_000) + int ($end_micsecs / 1_000);
102  
103 0 for my $dir (qw( private_src public_src dst )) {
104 my ($dpkts, $doctets, $srcaddr, $dstaddr, $srcport, $dstport)
105 0 = @fields[ @{$ct_mask_fields{$proto}{$dir}} ];
106  
107 print join ',',
108 $unix_secs,
109 $unix_nsecs,
110 $sysuptime,
111 0 ▶ $config->{flow_send}->{args}->[0] || '127.0.0.1',
Condition0%0/6
$config->{'flow_send'}{'args'}[0] || '127.0.0.1'
l!l
00
$srcport || 0
l!l
00
$dstport || 0
l!l
00
MC/DC0%0/1
$config->{'flow_send'}{'args'}[0] || '127.0.0.1'
$config->{'flow_send'}{'args'}[0]
MC/DC0%0/1
$srcport || 0
$srcport
MC/DC0%0/1
$dstport || 0
$dstport
Truth table0%0/2
A
A= $config->{'flow_send'}{'args'}[0]
Aresult
11
00
Truth table0%0/2
A
A= $srcport
Aresult
11
00
Truth table0%0/2
A
A= $dstport
Aresult
11
00
112 $dpkts,
113 $doctets,
114 $first,
115 $last,
116 $srcaddr,
117 $dstaddr,
118 '0.0.0.0', # NEXTHOP
119 0, # INPUT (SNMP idx)
120 0, # OUTPUT (SNMP idx)
121 $srcport || 0, # might be ICMP
122 $dstport || 0, # might be ICMP
123 $proto,
124 0, # TOS
125 0; # TCP_FLAGS
126 0 print "\n";
127 }
128 } # while (<>)
129 }
130  
131 # set up output tee to local syslog, and next process in pipe
132 0 ▶sub ptee {
Subroutine ptee: not called CC=2 SCAR=17.9
Pod undocumented
133 0 my $config = shift;
134 Log::Dispatch::Config->configure_and_watch(
135 0 Log::Dispatch::Configurator::Any->new($config->{ptee}->{conf}) );
136 0 my $dispatcher = Log::Dispatch::Config->instance;
137  
138 0 ▶ while (<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 0 0 0
139 0 $dispatcher->notice($_);
140 }
141 }
142  
143 __END__
↑ Summary Utils.pm »