blib/lib/Mojolicious/Plugin/ServerStatus.pm

filter: Statementstmt 13.3% 27 / 203 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 43.7% 7 / 16 Podpod 25.0% 1 / 4 totaltotal 9.4% 35 / 371 CC 67 SCAR 55.4
CC
67
Cov
8%
_handle_server_status
552.0
register
342.0
counter
132.0
CRAP 254.9
↑ Summary
1 package Mojolicious::Plugin::ServerStatus;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Net::CIDR::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Parallel::Scoreboard;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.04';
11  
12 1 my $JSON = JSON->new->utf8(0);
13  
14 0 ▶has conf => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
15 1 has skip_ps_command => 0;
16  
17 0 ▶sub register {
Subroutine register: not called CC=18 SCAR=58.3
Pod documented
18 0 my ($plugin, $app, $args) = @_;
19  
20 0 $plugin->{uptime} = time;
21 0 ▶ $args->{allow} ||= [ '127.0.0.1', '192.168.0.0/16' ];
Condition0%0/2
$args->{"allow"} ||= ["127.0.0.1", "192.168.0.0/16"]
l!l
00
MC/DC0%0/1
$args->{"allow"} ||= ["127.0.0.1", "192.168.0.0/16"]
$args->{"allow"}
Truth table0%0/2
A
A= $args->{"allow"}
Aresult
11
00
22 0 ▶ $args->{path} ||= '/server-status';
Condition0%0/2
$args->{"path"} ||= "/server-status"
l!l
00
MC/DC0%0/1
$args->{"path"} ||= "/server-status"
$args->{"path"}
Truth table0%0/2
A
A= $args->{"path"}
Aresult
11
00
23 0 ▶ $args->{counter_file} ||= '/tmp/counter_file';
Condition0%0/2
$args->{"counter_file"} ||= "/tmp/counter_file"
l!l
00
MC/DC0%0/1
$args->{"counter_file"} ||= "/tmp/counter_file"
$args->{"counter_file"}
Truth table0%0/2
A
A= $args->{"counter_file"}
Aresult
11
00
24 0 ▶ $args->{scoreboard} ||= '/var/run/server';
Condition0%0/2
$args->{"scoreboard"} ||= "/var/run/server"
l!l
00
MC/DC0%0/1
$args->{"scoreboard"} ||= "/var/run/server"
$args->{"scoreboard"}
Truth table0%0/2
A
A= $args->{"scoreboard"}
Aresult
11
00
25 0 ▶ $plugin->conf( $args ) if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
26  
27 0 ▶ if ( $args->{allow} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'allow'}) { } 0 0 0
28 0 ▶ my @ip = ref $args->{allow} ? @{ $args->{allow} } : ($args->{allow});
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{'allow'} ? : 0 0 0
29 0 my @ipv4;
30 my @ipv6;
31 0 for (@ip) {
32 # hacky check, but actual checks are done in Net::CIDR::Lite.
33 0 ▶ if (/:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/:/u) { } 0 0 0
34 0 push @ipv6, $_;
35 } else {
36 0 push @ipv4, $_;
37 }
38 }
39 0 ▶ if ( @ipv4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ipv4) 0 0 0
40 0 my $cidr4 = Net::CIDR::Lite->new();
41 0 $cidr4->add_any($_) for @ipv4;
42 0 $plugin->{__cidr4} = $cidr4;
43 }
44 0 ▶ if ( @ipv6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ipv6) 0 0 0
45 0 my $cidr6 = Net::CIDR::Lite->new();
46 0 $cidr6->add_any($_) for @ipv6;
47 0 $plugin->{__cidr6} = $cidr6;
48 }
49 }
50 else {
51 0 warn "[Mojolicious::Plugin::ServerStatus] 'allow' is not provided. Any host will not be able to access server-status page.\n";
52 }
53
54 0 ▶ if ( $args->{scoreboard} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"scoreboard"}) 0 0 0
55 my $scoreboard = Parallel::Scoreboard->new(
56 base_dir => $args->{scoreboard}
57 0 );
58 0 $plugin->{__scoreboard} = $scoreboard;
59 }
60  
61 0 ▶ if ( $args->{counter_file} && ! -f $args->{counter_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"counter_file"} and not -f $args->{"counter_file"}) 0 0 0
Condition0%0/3
$args->{"counter_file"} and not -f $args->{"counter_file"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"counter_file"} and not -f $args->{"counter_file"}
$args->{"counter_file"} not -f $args->{"counter_file"}
Truth table0%0/3
A and not(B)
A= $args->{"counter_file"}B= not -f $args->{"counter_file"}
ABresult
0X0
100
111
62 open( my $fh, '>>:unix', $args->{counter_file} )
63 0 ▶ or die "could not open counter_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>:unix", $args->{"counter_file"} or die "could not open counter_file: $!" 0 0 0
64 }
65  
66 0 my $r = $app->routes;
67 $r->route($args->{path})->to(
68 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
69 0 my $self = shift;
70 0 my $req = $self->req;
71 0 my $env = $req->env;
72 0 my $tx = $self->tx;
73  
74 0 ▶ if ( ! $plugin->allowed($tx->remote_address) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plugin->allowed($tx->remote_address)) 0 0 0
75 0 return $self->render(text => 'Forbidden', status => 403);
76 }
77  
78 0 my ($body, $status) = $plugin->_handle_server_status;
79  
80 0 ▶ if ( ($env->{QUERY_STRING} || $req->url->query->to_string ||'') =~ m!\bjson\b!i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($env->{"QUERY_STRING"} || $req->url->query->to_string || "") =~ /\bjson\b/iu) 0 0 0
Condition0%0/5
$env->{'QUERY_STRING'} || $req->url->query->to_string || ''
l!l
00
$env->{"QUERY_STRING"} or $req->url->query->to_string
l!l&&r!l&&!r
000
MC/DC0%0/2
$env->{'QUERY_STRING'} || $req->url->query->to_string || ''
$env->{"QUERY_STRING"} $req->url->query->to_string
Truth table0%0/3
A or B
A= $env->{"QUERY_STRING"}B= $req->url->query->to_string
ABresult
1X1
011
000
81 0 return $self->render(json => $status)
82 }
83 0 return $self->render(text => $body, format => 'txt');
84 }
85 0 );
86  
87 0 ▶ $app->hook(before_dispatch => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
88 0 my $self = shift;
89 0 my $tx = $self->tx;
90 0 my $req = $self->req;
91 0 my $headers = $req->headers;
92 0 ▶ my $env = %{ $req->env } ? $req->env
Branch: 2 branches
BranchTrueFalseTotal
%{$req->env;} ? : 0 0 0
$req->is_secure ? : 0 0 0
Condition0%0/4
$headers->host || ''
l!l
00
$req->url->path->to_string || ''
l!l
00
MC/DC0%0/1
$headers->host || ''
$headers->host
MC/DC0%0/1
$req->url->path->to_string || ''
$req->url->path->to_string
Truth table0%0/2
A
A= $headers->host
Aresult
11
00
Truth table0%0/2
A
A= $req->url->path->to_string
Aresult
11
00
93 : {
94 REMOTE_ADDR => $tx->remote_address,
95 HTTP_HOST => $headers->host || '',
96 REQUEST_METHOD => $req->method,
97 REQUEST_URI => $req->url->path->to_string || '',
98 SERVER_PROTOCOL => $req->is_secure ? 'HTTPS' : 'HTTP',
99 };
100 0 ▶ ($env->{USER}) = defined $self->req->url->to_abs->userinfo ? (split /:/smx,$self->req->url->to_abs->userinfo,2) : '-';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->req->url->to_abs->userinfo ? : 0 0 0
101 0 $plugin->set_state("A", $env);
102 0 });
103  
104 0 ▶ $app->hook(after_render => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
105 0 my ($c, $output, $format) = @_;
106 0 ▶ if ( $plugin->conf->{counter_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plugin->conf->{"counter_file"}) 0 0 0
107 0 $plugin->counter(1, length($output) );
108 }
109 0 $plugin->set_state('.');
110 0 });
111 }
112  
113 1 my $prev={};
114 0 ▶sub set_state {
Subroutine set_state: not called CC=5 SCAR=29.9
Pod undocumented
115 0 my $self = shift;
116 0 ▶ return if !$self->{__scoreboard};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__scoreboard"} 0 0 0
117  
118 0 ▶ my $status = shift || '_';
Condition0%0/2
shift() || '_'
l!l
00
MC/DC0%0/1
shift() || '_'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
119 0 my $env = shift;
120 0 ▶ if ( $env ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env) 0 0 0
121 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
122 $prev = {
123 remote_addr => $env->{REMOTE_ADDR},
124 host => defined $env->{HTTP_HOST} ? $env->{HTTP_HOST} : '-',
125 method => $env->{REQUEST_METHOD},
126 uri => $env->{REQUEST_URI},
127 protocol => $env->{SERVER_PROTOCOL},
128 user => $env->{USER},
129 0 ▶ time => time(),
Branch: 1 branch
BranchTrueFalseTotal
defined $env->{'HTTP_HOST'} ? : 0 0 0
130 };
131 }
132 $self->{__scoreboard}->update($JSON->encode({
133 0 %{$prev},
134 pid => $$,
135 ppid => getppid(),
136 uptime => $self->{uptime},
137 0 status => $status,
138 }));
139 }
140  
141 0 ▶sub _handle_server_status {
Subroutine _handle_server_status: not called CC=23 SCAR=63.1
142 0 my ($self) = @_;
143  
144  
145 0 my $upsince = time - $self->{uptime};
146 0 my $duration = "";
147 0 my @spans = (86400 => 'days', 3600 => 'hours', 60 => 'minutes');
148 0 ▶ while (@spans) {
Branch: 1 branch
BranchTrueFalseTotal
if (@spans) 0 0 0
149 0 my ($seconds,$unit) = (shift @spans, shift @spans);
150 0 ▶ if ($upsince > $seconds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upsince > $seconds) 0 0 0
151 0 $duration .= int($upsince/$seconds) . " $unit, ";
152 0 $upsince = $upsince % $seconds;
153 }
154 }
155 0 $duration .= "$upsince seconds";
156  
157 0 my $body="Uptime: $self->{uptime} ($duration)\n";
158 0 my %status = ( 'Uptime' => $self->{uptime} );
159  
160 0 ▶ if ( $self->conf->{counter_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"counter_file"}) 0 0 0
161 0 my ($counter,$bytes) = $self->counter;
162 0 my $kbytes = int($bytes / 1_000);
163 0 $body .= sprintf "Total Accesses: %s\n", $counter;
164 0 $body .= sprintf "Total Kbytes: %s\n", $kbytes;
165 0 $status{TotalAccesses} = $counter;
166 0 $status{TotalKbytes} = $kbytes;
167 }
168  
169 0 ▶ if ( my $scoreboard = $self->{__scoreboard} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $scoreboard = $self->{'__scoreboard'}) { } 0 0 0
170 0 my $stats = $scoreboard->read_all();
171 0 my $idle = 0;
172 0 my $busy = 0;
173  
174 0 my @all_workers = ();
175 0 my $parent_pid = getppid;
176
177 0 ▶ if ( $self->skip_ps_command ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->skip_ps_command) { } 0 0 0
elsif ($^O eq 'cygwin') { } 0 0 0
elsif (not $^O =~ /mswin32/iu) { } 0 0 0
178 # none
179 0 @all_workers = keys %$stats;
180 }
181 elsif ( $^O eq 'cygwin' ) {
182 0 my $ps = `ps -ef`;
183 0 $ps =~ s/^\s+//mg;
184 0 for my $line ( split /\n/, $ps ) {
185 0 ▶ next if $line =~ m/^\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\D/u 0 0 0
186 0 my @proc = split /\s+/, $line;
187 0 ▶ push @all_workers, $proc[1] if $proc[2] == $parent_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $proc[2] == $parent_pid 0 0 0
188 }
189 }
190 elsif ( $^O !~ m!mswin32!i ) {
191 0 ▶ my $psopt = $^O =~ m/bsd$/ ? '-ax' : '-e';
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /bsd$/u ? : 0 0 0
192 0 my $ps = `LC_ALL=C command ps $psopt -o ppid,pid`;
193 0 $ps =~ s/^\s+//mg;
194 0 for my $line ( split /\n/, $ps ) {
195 0 ▶ next if $line =~ m/^\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\D/u 0 0 0
196 0 my ($ppid, $pid) = split /\s+/, $line, 2;
197 0 ▶ push @all_workers, $pid if $ppid == $parent_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $ppid == $parent_pid 0 0 0
198 }
199 }
200 else {
201 # todo windows?
202 0 @all_workers = keys %$stats;
203 }
204  
205 0 my $process_status = '';
206 0 my @process_status;
207 0 for my $pid ( @all_workers ) {
208 0 my $json = $stats->{$pid};
209 0 my $pstatus = eval {
210 0 ▶ $JSON->decode($json || '{}');
Condition0%0/2
$json || '{}'
l!l
00
MC/DC0%0/1
$json || '{}'
$json
Truth table0%0/2
A
A= $json
Aresult
11
00
211 };
212 0 ▶ $pstatus ||= {};
Condition0%0/2
$pstatus ||= {}
l!l
00
MC/DC0%0/1
$pstatus ||= {}
$pstatus
Truth table0%0/2
A
A= $pstatus
Aresult
11
00
213 0 ▶ if ( $pstatus->{status} && $pstatus->{status} eq 'A' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pstatus->{'status'} and $pstatus->{'status'} eq 'A') { } 0 0 0
Condition0%0/3
$pstatus->{"status"} and $pstatus->{"status"} eq "A"
!ll&&!rl&&r
000
MC/DC0%0/2
$pstatus->{"status"} and $pstatus->{"status"} eq "A"
$pstatus->{"status"} $pstatus->{"status"} eq "A"
Truth table0%0/3
A and B
A= $pstatus->{"status"}B= $pstatus->{"status"} eq "A"
ABresult
0X0
100
111
214 0 $busy++;
215 }
216 else {
217 0 $idle++;
218 }
219  
220 0 ▶ if ( defined $pstatus->{time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pstatus->{"time"}) 0 0 0
221 0 $pstatus->{ss} = time - $pstatus->{time};
222 }
223 0 ▶ $pstatus->{pid} ||= $pid;
Condition0%0/3
$pstatus->{"pid"} ||= $pid
l!l&&r!l&&!r
000
MC/DC0%0/2
$pstatus->{"pid"} ||= $pid
$pstatus->{"pid"} $pid
Truth table0%0/3
A ||= B
A= $pstatus->{"pid"}B= $pid
ABresult
1X1
011
000
224 0 delete $pstatus->{time};
225 0 delete $pstatus->{ppid};
226 0 delete $pstatus->{uptime};
227 $process_status .= sprintf "%s\n",
228 0 ▶ join(" ", map { defined $pstatus->{$_} ? $pstatus->{$_} : '' } qw/pid status remote_addr host user method uri protocol ss/);
Branch: 1 branch
BranchTrueFalseTotal
defined $pstatus->{$_} ? : 0 0 0
229 0 push @process_status, $pstatus;
230 }
231 0 $body .= <<EOF;
232 BusyWorkers: $busy
233 IdleWorkers: $idle
234 --
235 pid status remote_addr host user method uri protocol ss
236 $process_status
237 EOF
238 0 chomp $body;
239 0 $status{BusyWorkers} = $busy;
240 0 $status{IdleWorkers} = $idle;
241 0 $status{stats} = \@process_status;
242 }
243 else {
244 0 $body .= "WARN: Scoreboard has been disabled\n";
245 0 $status{WARN} = 'Scoreboard has been disabled';
246 }
247 0 return ($body, \%status);
248  
249 }
250  
251 0 ▶sub allowed {
Subroutine allowed: not called CC=4 SCAR=30.0
Pod undocumented
252 0 my ( $self , $address ) = @_;
253 0 ▶ if ( $address =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($address =~ /:/u) 0 0 0
254 0 ▶ return unless $self->{__cidr6};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__cidr6"} 0 0 0
255 0 return $self->{__cidr6}->find( $address );
256 }
257 0 ▶ return unless $self->{__cidr4};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__cidr4"} 0 0 0
258 0 return $self->{__cidr4}->find( $address );
259 }
260  
261 0 ▶sub counter {
Subroutine counter: not called CC=11 SCAR=48.8
Pod undocumented
262 0 my $self = shift;
263 0 my $parent_pid = getppid;
264 0 ▶ if ( ! $self->{__counter} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"__counter"}) 0 0 0
265 0 ▶ open( my $fh, '+<:unix', $self->conf->{counter_file} ) or die "cannot open counter_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<:unix", $self->conf->{"counter_file"} or die "cannot open counter_file: $!" 0 0 0
266 0 $self->{__counter} = $fh;
267 0 flock $fh, LOCK_EX;
268 0 my $len = sysread $fh, my $buf, 10;
269 0 ▶ if ( !$len || $buf != $parent_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $len or $buf != $parent_pid) 0 0 0
Condition0%0/3
not $len or $buf != $parent_pid
l!l&&r!l&&!r
000
MC/DC0%0/2
not $len or $buf != $parent_pid
not $len $buf != $parent_pid
Truth table0%0/3
not(A) or B
A= not $lenB= $buf != $parent_pid
ABresult
1X1
011
000
270 0 seek $fh, 0, 0;
271 0 syswrite $fh, sprintf("%-10d%-20d%-20d", $parent_pid, 0, 0);
272 }
273 0 flock $fh, LOCK_UN;
274 }
275 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
276 0 my ($count, $bytes) = @_;
277 0 ▶ $count ||= 1;
Condition0%0/2
$count ||= 1
l!l
00
MC/DC0%0/1
$count ||= 1
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
278 0 ▶ $bytes ||= 0;
Condition0%0/2
$bytes ||= 0
l!l
00
MC/DC0%0/1
$bytes ||= 0
$bytes
Truth table0%0/2
A
A= $bytes
Aresult
11
00
279 0 my $fh = $self->{__counter};
280 0 flock $fh, LOCK_EX;
281 0 seek $fh, 10, 0;
282 0 sysread $fh, my $buf, 40;
283 0 my $counter = substr($buf, 0, 20);
284 0 my $total_bytes = substr($buf, 20, 20);
285 0 ▶ $counter ||= 0;
Condition0%0/2
$counter ||= 0
l!l
00
MC/DC0%0/1
$counter ||= 0
$counter
Truth table0%0/2
A
A= $counter
Aresult
11
00
286 0 ▶ $total_bytes ||= 0;
Condition0%0/2
$total_bytes ||= 0
l!l
00
MC/DC0%0/1
$total_bytes ||= 0
$total_bytes
Truth table0%0/2
A
A= $total_bytes
Aresult
11
00
287 0 $counter += $count;
288 0 ▶ if ($total_bytes + $bytes > 2**53){ # see docs
Branch: 1 branch
BranchTrueFalseTotal
if ($total_bytes + $bytes > 9007199254740992.0) { } 0 0 0
289 0 $total_bytes = 0;
290 } else {
291 0 $total_bytes += $bytes;
292 }
293 0 seek $fh, 0, 0;
294 0 syswrite $fh, sprintf("%-10d%-20d%-20d", $parent_pid, $counter, $total_bytes);
295 0 flock $fh, LOCK_UN;
296 0 return $counter;
297 }
298 else {
299 0 my $fh = $self->{__counter};
300 0 flock $fh, LOCK_EX;
301 0 seek $fh, 10, 0;
302 0 sysread $fh, my $counter, 20;
303 0 sysread $fh, my $total_bytes, 20;
304 0 flock $fh, LOCK_UN;
305 0 return $counter + 0, $total_bytes + 0;
306 }
307 }
308  
309 1 1;
310 __END__
↑ Summary