| 1 | package Clustericious::Command::status; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 1 | ▶ | use Clustericious::Log; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 1 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 1 | ▶ | use Clustericious::App; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 1 | ▶ | use Clustericious::Config; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 1 | ▶ | use File::Basename qw( dirname ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 1 | ▶ | use base 'Clustericious::Command'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | # ABSTRACT: Clustericious command to report status of Clustericious application | ||||||||||||||||||||||
| 14 | 1 | our $VERSION = '1.29'; # VERSION | |||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 1 | __PACKAGE__->attr(description => <<''); | |||||||||||||||||||||
| 18 | Report the status of a daemon. | ||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | 1 | __PACKAGE__->attr(usage => <<""); | |||||||||||||||||||||
| 21 | usage: $0 status | ||||||||||||||||||||||
| 22 | Report the status of a clustericious daemon. | ||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | sub _check_pidfile | ||||||||||||||||||||||
| 25 | 0 | ▶ | { | ||||||||||||||||||||
|
Subroutine
_check_pidfile: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||
| 26 | 0 | my($filename) = @_; | |||||||||||||||||||||
| 27 | 0 | ▶ | return ( state => 'error', message => 'missing pid filename' ) unless $filename; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 28 | 0 | ▶ | return ( state => 'down', message => 'no pid file' ) unless -e $filename; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 29 | 0 | my $pid = Mojo::Asset::File->new(path => $filename)->slurp; | |||||||||||||||||||||
| 30 | 0 | ▶ | return ( state => 'down', messasge => 'no pid in file' ) unless $pid; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 31 | 0 | ▶ | return ( state => 'ok' ) if kill 0, $pid; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 32 | 0 | return ( state => 'down', message => "Pid $pid in file is not running." ); | |||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | sub _check_database | ||||||||||||||||||||||
| 36 | 0 | ▶ | { | ||||||||||||||||||||
|
Subroutine
_check_database: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 37 | 0 | my($db_class) = @_; | |||||||||||||||||||||
| 38 | 0 | my $db = $db_class->new_or_cached; | |||||||||||||||||||||
| 39 | 0 | my ( $domain, $type ) = ( $db->default_domain, $db->default_type ); | |||||||||||||||||||||
| 40 | 0 | my $dbh = $db->dbh; | |||||||||||||||||||||
| 41 | 0 | my ( $state, $message ); | |||||||||||||||||||||
| 42 | 0 | ▶ | if ($dbh) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||
| 44 | 0 | $state = 'ok'; | |||||||||||||||||||||
| 45 | 0 | ▶ | ($message) = join ':', grep {defined && length} $dbh->selectrow_array('select current_database(), inet_server_addr(), inet_server_port()'); | ||||||||||||||||||||
|
Condition0%0/3
defined $_ && length $_
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||
| 47 | else | ||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||
| 49 | 0 | $state = 'down'; | |||||||||||||||||||||
| 50 | 0 | $message = $db_class->name; | |||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||
| 52 | return { | ||||||||||||||||||||||
| 53 | 0 | name => "database", | |||||||||||||||||||||
| 54 | state => $state, | ||||||||||||||||||||||
| 55 | message => "$domain:$type $message", | ||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | sub run | ||||||||||||||||||||||
| 60 | 0 | ▶ | { | ||||||||||||||||||||
|
Subroutine
run: not called CC=20 SCAR=60.4
Pod
documented
| |||||||||||||||||||||||
| 61 | 0 | my($self, @args) = @_; | |||||||||||||||||||||
| 62 | 0 | ▶ | exit 2 unless $self->app->sanity_check; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 63 | 0 | my $app = $ENV{MOJO_APP}; | |||||||||||||||||||||
| 64 | 0 | my $conf = $self->app->config; | |||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | 0 | eval "require $app"; | |||||||||||||||||||||
| 67 | 0 | ▶ | die $@ if $@; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | 0 | my @status; # array of { name =>.., state =>.., message =>.. } hashrefs. | |||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | 0 | my $exe = $0; | |||||||||||||||||||||
| 72 | # webserver | ||||||||||||||||||||||
| 73 | 0 | for ($self->app->config->start_mode) | |||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||
| 75 | 0 | ▶ | push @status, { name => $_, | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 76 | ( | ||||||||||||||||||||||
| 77 | # THIS IS A MESS PLEASE CLEAN IT UP | ||||||||||||||||||||||
| 78 | /hypnotoad/ ? _check_pidfile($conf->hypnotoad->pid_file(default => dirname($exe).'/hypnotoad.pid')) | ||||||||||||||||||||||
| 79 | : /apache/ ? _check_pidfile($conf->apache->pid_file) | ||||||||||||||||||||||
| 80 | : /plackup/ ? _check_pidfile($conf->plackup->pidfile) | ||||||||||||||||||||||
| 81 | # NB: see http://redmine.lighttpd.net/issues/2137 | ||||||||||||||||||||||
| 82 | # lighttpd's pid files disappear. Time to switch to nginx? | ||||||||||||||||||||||
| 83 | : /lighttpd/ ? _check_pidfile($conf->lighttpd->pid_file) | ||||||||||||||||||||||
| 84 | : ( state => 'error', message => "Status for start_mode $_ is unimplemented." ))}; | ||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||
| 86 | |||||||||||||||||||||||
| 87 | # Do a HEAD request if the webserver(s) are ok. | ||||||||||||||||||||||
| 88 | 0 | ▶ | if ((grep {$_->{state} eq 'ok'} @status)==@status) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||
| 90 | 0 | my $res = Mojo::UserAgent->new->head($conf->url)->res; | |||||||||||||||||||||
| 91 | 0 | ▶ | printf "%10s : %-10s (%s %s)\n", "url", $conf->url, $res->code || '?', $res->message || ''; | ||||||||||||||||||||
|
Condition0%0/4
$res->code || '?'
$res->message || ''
MC/DC0%0/1
$res->code || '?'
$res->code
MC/DC0%0/1
$res->message || ''
$res->message
Truth table0%0/2
A
A= $res->code
Truth table0%0/2
A
A= $res->message
| |||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | # Database | ||||||||||||||||||||||
| 95 | 0 | ▶ | if ( $INC{'Rose/Planter/DB.pm'} ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||
| 97 | 0 | ▶ | if ( my $db_class = Rose::Planter::DB->registered_by($app) ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||
| 99 | 0 | push @status, _check_database($db_class); | |||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | 0 | my $ok; | |||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | # Send as YAML if requested? | ||||||||||||||||||||||
| 106 | 0 | for (@status) | |||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||
| 108 | 0 | ▶ | $_->{message} &&= "($_->{message})";; | ||||||||||||||||||||
|
Condition0%0/2
$_->{"message"} &&= qq[($_->{"message"})]
MC/DC0%0/1
$_->{"message"} &&= qq[($_->{"message"})]
$_->{"message"}
Truth table0%0/2
A
A= $_->{"message"}
| |||||||||||||||||||||||
| 109 | 0 | ▶ | $_->{message} ||= ""; | ||||||||||||||||||||
|
Condition0%0/2
$_->{"message"} ||= ""
MC/DC0%0/1
$_->{"message"} ||= ""
$_->{"message"}
Truth table0%0/2
A
A= $_->{"message"}
| |||||||||||||||||||||||
| 110 | 0 | printf "%10s : %-10s %s\n", @$_{qw/name state message/}; | |||||||||||||||||||||
| 111 | 0 | ▶ | $ok //= 1 if $_->{state} eq 'ok'; | ||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$ok //= 1
MC/DC0%0/1
$ok //= 1
$ok
Truth table0%0/2
A
A= $ok
| |||||||||||||||||||||||
| 112 | 0 | ▶ | $ok = 0 if $_->{state} ne 'ok'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | 0 | ▶ | exit($ok ? 0 : 2); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | 1 | 1; | |||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | __END__ | ||||||||||||||||||||||