eg/lokiSender

filter: Statementstmt 60.9% 103 / 169 Branchbran 33.3% 32 / 96 Conditioncond 38.4% 15 / 39 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 57.1% 8 / 14 Podpod n/a totaltotal 46.7% 160 / 342 CC 44 SCAR 38.5
CC
44
Cov
49%
daemonize
132.0
sendLogs
40.6
_log
20.0
CRAP 46.8
« blib/lib/Lemonldap/NG/Common/Conf/Serializer.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Logger/Sentry.pm »
1 #!/usr/bin/perl
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = "2.22.0";
9  
10 # Options
11 1 my $opts = {};
12 1 my $help;
13 1 my $opt_user = '__APACHEUSER__';
14 1 my $opt_group = '__APACHEGROUP__';
15  
16 1 my @CMDLINE = @ARGV;
17  
18 GetOptions(
19 'daemon' => \$opts->{daemon},
20 'defer-dir=s' => \$opts->{deferDir},
21 'debug' => \$opts->{debug},
22 'delay=s' => \$opts->{delay},
23 'group|g=s' => \$opts->{group},
24 'help|h' => \$help,
25 'ini-file=s' => \$opts->{iniFile},
26 'syslog-facility' => \$opts->{syslogFacility},
27 'user|u=s' => \$opts->{user},
28 1 ▶) or pod2usage( -exitcode => 1, -verbose => 0 );
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("daemon", \$opts->{"daemon"}, "defer-dir=s", \$opts->{"deferDir"}, "debug", \$opts->{"debug"}, "delay=s", \$opts->{"delay"}, "group|g=s", \$opts->{"group"}, "help|h", \$help, "ini-file=s", \$opts->{"iniFile"}, "syslog-facility", \$opts->{"syslogFacility"}, "user|u=s", \$opts->{"user"}) or pod2usage("-exitcode", 1, "-verbose", 0) 1 0 1
29  
30 1 foreach my $opt ( keys %$opts ) {
31 8 ▶ $opts->{$opt} //= $ENV{ 'LLNG_LOKI_' . uc($opt) };
Condition66%2/3
$opts->{$opt} //= $ENV{'LLNG_LOKI_' . uc($opt)}
l!l&&r!l&&!r
107
MC/DC50%1/2
$opts->{$opt} //= $ENV{'LLNG_LOKI_' . uc($opt)}
$opts->{$opt} $ENV{'LLNG_LOKI_' . uc($opt)}
Truth table66%2/3
A //= B
A= $opts->{$opt}B= $ENV{'LLNG_LOKI_' . uc($opt)}
ABresult
1X1
011
000
32 }
33  
34 1 ▶pod2usage( -exitcode => 0, -verbose => 2 ) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 1 1
35  
36 1 ▶$opts->{delay} ||= 60 if $opts->{daemon};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"daemon"} 0 1 1
Condition0%0/2
$opts->{"delay"} ||= 60
l!l
00
MC/DC0%0/1
$opts->{"delay"} ||= 60
$opts->{"delay"}
Truth table0%0/2
A
A= $opts->{"delay"}
Aresult
11
00
37 1 ▶$opts->{deferDir} ||= &_getDeferDir;
Condition33%1/3
$opts->{"deferDir"} ||= &_getDeferDir
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts->{"deferDir"} ||= &_getDeferDir
$opts->{"deferDir"} &_getDeferDir
Truth table33%1/3
A ||= B
A= $opts->{"deferDir"}B= &_getDeferDir
ABresult
1X1
011
000
38 1 ▶mkdir $opts->{deferDir} unless -e $opts->{deferDir};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $opts->{"deferDir"} 0 1 1
39 1 ▶die "Missing directory $opts->{deferDir}" unless -d $opts->{deferDir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $opts->{"deferDir"} 0 1 1
40 1 ▶die "Cannot write into $opts->{deferDir}" unless -w $opts->{deferDir};
Branch: 1 branch
BranchTrueFalseTotal
unless -w $opts->{"deferDir"} 0 1 1
41  
42 1 ▶die 'Missing --defer-dir' unless $opts->{deferDir};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"deferDir"} 0 1 1
43  
44 # Change owner if asked
45 1 POSIX::nice(10);
46 1 ▶if ( $opts->{group} || $opts->{user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"group"} or $opts->{"user"}) 0 1 1
Condition33%1/3
$opts->{"group"} or $opts->{"user"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$opts->{"group"} or $opts->{"user"}
$opts->{"group"} $opts->{"user"}
Truth table33%1/3
A or B
A= $opts->{"group"}B= $opts->{"user"}
ABresult
1X1
011
000
47 0 eval {
48 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 0 my ( $gid, $uid );
50 0 ▶ if ( $opts->{group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"group"}) 0 0 0
51 0 $gid = getgrnam( $opts->{group} );
52 0 POSIX::setgid($gid);
53 }
54 0 ▶ if ( $opts->{user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"user"}) 0 0 0
55 0 $uid = getpwnam( $opts->{user} );
56 chown( $uid, ( $gid // [ stat( $opts->{deferDir} ) ]->[5] ),
57 $opts->{deferDir} )
58 0 ▶ and chmod( 0700, $opts->{deferDir} );
Branch: 1 branch
BranchTrueFalseTotal
chown $uid, $gid // [stat $opts->{"deferDir"}]->[5], $opts->{"deferDir"} and chmod 0700, $opts->{"deferDir"} 0 0 0
Condition0%0/3
$gid // [stat $opts->{"deferDir"}]->[5]
l!l&&r!l&&!r
000
MC/DC0%0/2
$gid // [stat $opts->{"deferDir"}]->[5]
$gid [stat $opts->{"deferDir"}]->[5]
Truth table0%0/3
A // B
A= $gidB= [stat $opts->{"deferDir"}]->[5]
ABresult
1X1
011
000
59 POSIX::setuid( scalar( getpwnam( $opts->{user} ) ) )
60 0 ▶ if $opts->{user};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"user"} 0 0 0
61 my (
62 undef, undef, undef, undef, undef,
63 undef, undef, $homedir, undef
64 0 ) = getpwnam( $opts->{user} );
65 0 ▶ $ENV{HOME} = $homedir if $homedir;
Branch: 1 branch
BranchTrueFalseTotal
if $homedir 0 0 0
66 }
67 };
68 }
69  
70 1 ▶&daemonize if $opts->{daemon};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"daemon"} 0 1 1
71  
72 1 require HTTP::Request;
73 1 require JSON;
74 1 require LWP::UserAgent;
75 1 require Sys::Syslog;
76  
77 1 my $j = JSON->new->canonical;
78  
79 1 my $syslogOpened;
80  
81 0 ▶sub _log {
Subroutine _log: not called CC=4 SCAR=30.0
82 0 my ( $level, $msg ) = @_;
83 0 ▶ if ( $opts->{daemon} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'daemon'}) { } 0 0 0
84 0 ▶ unless ($syslogOpened) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($syslogOpened) 0 0 0
85 Sys::Syslog::openlog( 'LLNG-Loki', 'cons,pid,ndelay',
86 0 ▶ $opts->{syslogFacility} || 'daemon' );
Condition0%0/2
$opts->{'syslogFacility'} || 'daemon'
l!l
00
MC/DC0%0/1
$opts->{'syslogFacility'} || 'daemon'
$opts->{'syslogFacility'}
Truth table0%0/2
A
A= $opts->{'syslogFacility'}
Aresult
11
00
87 0 $syslogOpened++;
88 }
89 0 Sys::Syslog::syslog( $level, $msg );
90 }
91 else {
92 0 print STDERR "[$level] $msg\n";
93 }
94 }
95  
96 4 ▶sub debug {
Subroutine debug: called CC=2 SCAR=9.2
97 0 ▶ map { _log( 'debug', $_ ) } @_ if $opts->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"debug"} 0 4 4
98 }
99  
100 0 ▶sub warning {
Subroutine warning: not called CC=1 SCAR=6.9
101 0 map { _log( 'warning', $_ ) } @_;
102 }
103  
104 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
105 0 map { _log( 'err', $_ ) } @_;
106 }
107  
108 0 ▶sub _getDeferDir {
Subroutine _getDeferDir: not called CC=4 SCAR=26.1
109  
110 0 require Lemonldap::NG::Common::Conf;
111 my $ca = Lemonldap::NG::Common::Conf->new( { (
112 $opts->{iniFile}
113 ? ( confFile => $opts->{iniFile} )
114 0 ▶ : ()
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'iniFile'} ? : 0 0 0
115 )
116 }
117 );
118  
119 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
120 0 ▶ die $Lemonldap::NG::Common::Conf::msg unless ($ca);
Branch: 1 branch
BranchTrueFalseTotal
unless $ca 0 0 0
121 0 my $conf = $ca->getConf('all');
122 0 my $localConf = $ca->getLocalConf( 'all', $_[0]->{iniFile} );
123 0 $conf = { %$conf, %$localConf };
124  
125 0 ▶ unless ( $conf->{lokiDeferDir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->{"lokiDeferDir"}) 0 0 0
126 0 die 'Missing lokiDeferDir';
127 }
128 0 debug "Conf downloaded from Lemonldap::NG::Common::Conf: $conf->{cfgNum}";
129 0 return $conf->{lokiDeferDir};
130 }
131  
132 2 ▶sub sendLogs {
Subroutine sendLogs: called CC=26 SCAR=37.0
133 2 my $i;
134 2 my $ret = 1;
135 2 ▶ if ( opendir my $dh, $opts->{deferDir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (opendir my $dh, $opts->{'deferDir'}) { } 2 0 2
136 2 debug "Checking for logs into $opts->{deferDir}";
137 2 my $i = 0;
138 2 my ( %logs, %filesToDel, @headers, $hdr_done, %streams, %urls );
139 2 ▶ while ( my $name = readdir($dh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = readdir $dh)) 7 2 9
140 7 $i++;
141 7 my $fn = "$opts->{deferDir}/$name";
142 7 ▶ next if $name =~ m#^\.# or !-f $fn;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\./ or not -f $fn 4 3 7
Condition66%2/3
$name =~ /^\./ or not -f $fn
l!l&&r!l&&!r
403
MC/DC50%1/2
$name =~ /^\./ or not -f $fn
$name =~ /^\./ not -f $fn
Truth table66%2/3
A or not(B)
A= $name =~ /^\./B= not -f $fn
ABresult
1X1
011
000
143 3 my $fh;
144 3 ▶ unless ( open $fh, '<', $fn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $fn) 0 3 3
145 0 error "Unable to open $fn: $!";
146 0 $ret = 0;
147 0 goto END;
148 }
149 3 my $tmp = <$fh>;
150 3 ▶ unless ($tmp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp) 0 3 3
151 0 debug "Empty file $fn";
152 0 goto END;
153 }
154 3 chomp $tmp;
155 3 my ( $meth, $url ) = split( /\s+/, $tmp, 2 );
156 3 ▶ unless ( $meth and $url and $meth eq 'POST' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meth and $url and $meth eq "POST") 0 3 3
Condition33%2/6
$meth and $url and $meth eq "POST"
!ll&&!rl&&r
003
$meth and $url
!ll&&!rl&&r
003
MC/DC0%0/3
$meth and $url and $meth eq "POST"
$meth $url $meth eq "POST"
Truth table25%1/4
A and B and C
A= $methB= $urlC= $meth eq "POST"
ABCresult
0XX0
10X0
1100
1111
157 0 warning "Not a valid file $fn";
158 0 goto END;
159 }
160  
161 # Get headers;
162 3 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 6 0 6
163 6 chomp $line;
164 6 $line =~ s/\r//g;
165 6 ▶ unless ($line) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line) 3 3 6
166 3 $hdr_done = 1;
167 3 last;
168 }
169 3 ▶ next if $hdr_done;
Branch: 1 branch
BranchTrueFalseTotal
if $hdr_done 2 1 3
170 1 my ( $k, $v ) = split( /:\s+/, $line );
171 1 ▶ push @headers, $k, $v if $v and lc($k) ne 'content-length';
Branch: 1 branch
BranchTrueFalseTotal
if $v and lc $k ne "content-length" 1 0 1
Condition33%1/3
$v and lc $k ne "content-length"
!ll&&!rl&&r
001
MC/DC0%0/2
$v and lc $k ne "content-length"
$v lc $k ne "content-length"
Truth table33%1/3
A and B
A= $vB= lc $k ne "content-length"
ABresult
0X0
100
111
172 }
173  
174 # Get content
175 3 my $content;
176 {
177 3 local $/ = undef;
178 3 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 3 3 6
179 3 chomp $line;
180 3 $line =~ s/[\r\n]//g;
181 3 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 3 3
182 3 $content = eval { $j->decode($line)->{streams} };
183 3 ▶ if ( $@ or not $content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $content) 0 3 3
Condition33%1/3
$@ or not $content
l!l&&r!l&&!r
003
MC/DC0%0/2
$@ or not $content
$@ not $content
Truth table33%1/3
A or not(B)
A= $@B= not $content
ABresult
1X1
011
000
184 0 warning "Malformed line ($fn): $line";
185 0 last;
186 }
187 }
188 }
189 3 ▶ next unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 3 3
190 3 foreach my $stream (@$content) {
191 3 my $k = $j->encode( $stream->{stream} );
192 3 push @{ $logs{$k} }, @{ $stream->{values} };
193 3 push @{ $filesToDel{$k} }, $fn;
194 3 ▶ $streams{$k} ||= $stream->{stream};
Condition66%2/3
$streams{$k} ||= $stream->{"stream"}
l!l&&r!l&&!r
120
MC/DC0%0/2
$streams{$k} ||= $stream->{"stream"}
$streams{$k} $stream->{"stream"}
Truth table66%2/3
A ||= B
A= $streams{$k}B= $stream->{"stream"}
ABresult
1X1
011
000
195 3 ▶ $urls{$k} ||= $url;
Condition66%2/3
$urls{$k} ||= $url
l!l&&r!l&&!r
120
MC/DC0%0/2
$urls{$k} ||= $url
$urls{$k} $url
Truth table66%2/3
A ||= B
A= $urls{$k}B= $url
ABresult
1X1
011
000
196 }
197 3 END: close $fh;
198 }
199 2 my $ua = LWP::UserAgent->new;
200 2 $ua->env_proxy;
201 2 foreach my $key ( keys %logs ) {
202 my $req = HTTP::Request->new(
203 POST => $urls{$key},
204 \@headers,
205  
206 #HTTP::Headers->new(@{$headers{$key}}),
207 $j->encode( {
208 streams => [ {
209 stream => $streams{$key},
210 2 values => $logs{$key},
211 },
212 ]
213 }
214 )
215 );
216 2 my $resp = $ua->request($req);
217 2 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 2 0 2
218 2 debug "Logs $key sent";
219 2 ▶ unlink( @{ $filesToDel{$key} } ) or warning $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink @{$filesToDel{$key};} or warning($!) 2 0 2
220 }
221 else {
222 0 $ret = 0;
223 0 error "Fail to send logs: " . $resp->status_line;
224 }
225 }
226 }
227 else {
228 0 error "Unable to open $opts->{deferDir}: $!";
229 }
230 2 return $ret;
231 }
232  
233 0 ▶sub daemonize {
Subroutine daemonize: not called CC=11 SCAR=48.8
234 0 ▶ open STDIN, '<', '/dev/null' or die "Can't read /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", "/dev/null" or die "Can't read /dev/null: $!" 0 0 0
235 0 ▶ open STDOUT, '>', '/dev/null' or die "Can't write to /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", "/dev/null" or die "Can't write to /dev/null: $!" 0 0 0
236 0 ▶ open STDERR, '>', '/dev/null' or die "Can't write to /dev/null: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">", "/dev/null" or die "Can't write to /dev/null: $!" 0 0 0
237  
238 0 ▶ defined( my $pid = fork ) or die "Can't fork: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $pid = fork) or die "Can't fork: $!" 0 0 0
239 0 ▶ exit if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 0 0 0
240  
241 0 ▶ setsid() or die "Can't start a new session: $!";
Branch: 1 branch
BranchTrueFalseTotal
setsid() or die "Can't start a new session: $!" 0 0 0
242  
243 0 ▶ defined( $pid = fork ) or die "Can't fork again: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined($pid = fork) or die "Can't fork again: $!" 0 0 0
244 0 ▶ exit if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 0 0 0
245  
246 0 umask 0;
247  
248 0 ▶ if ( $opts->{pidFile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"pidFile"}) 0 0 0
249 open my $fh, '>', $opts->{pidFile}
250 0 ▶ or die "Can't write $opts->{pidFile} $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $opts->{"pidFile"} or die qq[Can't write $opts->{"pidFile"} $!] 0 0 0
251 0 print $fh "$$\n";
252 0 close $fh;
253 }
254 0 $0 = join ' ', $0, @CMDLINE;
255 }
256  
257 ## MAIN
258  
259 1 ▶if ( $opts->{delay} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'delay'}) { } 0 1 1
260 0 $SIG{$_} = \&sendLogs foreach (qw(ALRM USR1 USR2));
261 0 ▶ $SIG{$_} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
262 0 debug "End asked";
263 0 ▶ exit( &sendLogs ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
&sendLogs ? : 0 0 0
264 }
265 0 foreach (qw(INT TERM QUIT KILL ABRT HUP));
266 0 while (1) {
267 0 &sendLogs;
268 0 sleep $opts->{delay};
269 }
270 }
271 else {
272 1 ▶ &sendLogs || exit 1;
Condition50%1/2
&sendLogs || exit 1
l!l
10
MC/DC0%0/1
&sendLogs || exit 1
&sendLogs
Truth table50%1/2
A
A= &sendLogs
Aresult
11
00
273 }
274  
275 __END__
« blib/lib/Lemonldap/NG/Common/Conf/Serializer.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Logger/Sentry.pm »