| 1 | package HealthCheck::Diagnostic::DBHCheck; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Check a database handle to make sure you have read/write access | ||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use version; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = 'v1.0.1'; # VERSION | |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use parent 'HealthCheck::Diagnostic'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||
| 15 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=20.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 16 | 7 | my ($class, @params) = @_; | |||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | # Allow either a hashref or even-sized list of params | ||||||||||||||||||||||||||||||||||||||
| 19 | my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | ? %{ $params[0] } : @params; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
ref $params[0] || ''
MC/DC0%0/2
@params == 1 and (ref $params[0] || "") eq "HASH"
@params == 1
ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
| |||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||
| 22 | croak("The 'dbh' parameter should be a coderef!") | ||||||||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | if ($params{dbh} && (ref $params{dbh} ne "CODE")); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$params{"dbh"} and ref $params{"dbh"} ne "CODE"
MC/DC100%2/2
$params{"dbh"} and ref $params{"dbh"} ne "CODE"
$params{"dbh"}
ref $params{"dbh"} ne "CODE"
Truth table100%3/3
A and B
A= $params{"dbh"}B= ref $params{"dbh"} ne "CODE"
| |||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | 6 | return $class->SUPER::new( | |||||||||||||||||||||||||||||||||||||
| 26 | tags => [ 'dbh_check' ], | ||||||||||||||||||||||||||||||||||||||
| 27 | label => 'dbh_check', | ||||||||||||||||||||||||||||||||||||||
| 28 | %params | ||||||||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||
| 32 | 16 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=19 SCAR=29.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 33 | 16 | my ( $self, %params ) = @_; | |||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | # 1st, try to get dbh from provided parameters | ||||||||||||||||||||||||||||||||||||||
| 36 | 16 | my $dbh = $params{dbh}; | |||||||||||||||||||||||||||||||||||||
| 37 | # 2nd, if invoked with an object (not the class), then get dbh from object | ||||||||||||||||||||||||||||||||||||||
| 38 | 16 | ▶ | $dbh ||= $self->{dbh} if ref $self; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$dbh ||= $self->{"dbh"}
MC/DC50%1/2
$dbh ||= $self->{"dbh"}
$dbh
$self->{"dbh"}
Truth table66%2/3
A ||= B
A= $dbhB= $self->{"dbh"}
| |||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | 16 | ▶ | croak "Valid 'dbh' is required" unless $dbh; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | 14 | ▶ | croak "The 'dbh' parameter should be a coderef!" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 43 | unless ref $dbh eq "CODE"; | ||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | my $db_access = $params{db_access} # Provided call to check() | ||||||||||||||||||||||||||||||||||||||
| 46 | // ((ref $self) && $self->{db_access}) # Value from new() | ||||||||||||||||||||||||||||||||||||||
| 47 | 13 | ▶ | || "rw"; # default value | ||||||||||||||||||||||||||||||||||||
|
Condition87%7/8
($params{'db_access'} // ref $self && $self->{'db_access'}) || 'rw'
$params{"db_access"} // ref $self && $self->{"db_access"}
ref $self and $self->{"db_access"}
MC/DC66%2/3
($params{'db_access'} // ref $self && $self->{'db_access'}) || 'rw'
$params{"db_access"}
ref $self
$self->{"db_access"}
Truth table75%3/4
A // B and C
A= $params{"db_access"}B= ref $selfC= $self->{"db_access"}
| |||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||
| 49 | 13 | ▶ | croak "The value '$db_access' is not valid for the 'db_access' parameter" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 50 | unless $db_access =~ /^r[ow]$/; | ||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | my $timeout = | ||||||||||||||||||||||||||||||||||||||
| 53 | defined $params{timeout} ? $params{timeout} : | ||||||||||||||||||||||||||||||||||||||
| 54 | (ref $self) && (defined $self->{timeout}) ? $self->{timeout} : | ||||||||||||||||||||||||||||||||||||||
| 55 | 12 | ▶ | 10; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $self and defined $self->{"timeout"}
MC/DC100%2/2
ref $self and defined $self->{"timeout"}
ref $self
defined $self->{"timeout"}
Truth table100%3/3
A and B
A= ref $selfB= defined $self->{"timeout"}
| |||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||
| 57 | 12 | local $@; | |||||||||||||||||||||||||||||||||||||
| 58 | 12 | eval { | |||||||||||||||||||||||||||||||||||||
| 59 | 12 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||||||||||||
| 60 | 12 | ▶ | local $SIG{ALRM} = sub { die "timeout after $timeout seconds.\n" }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 61 | 12 | alarm $timeout; | |||||||||||||||||||||||||||||||||||||
| 62 | 12 | $dbh = $dbh->(%params); | |||||||||||||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||||||||||||
| 64 | 12 | alarm 0; | |||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | 12 | ▶ | if ( $@ =~ /^timeout/ ) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 67 | 2 | chomp $@; | |||||||||||||||||||||||||||||||||||||
| 68 | return { | ||||||||||||||||||||||||||||||||||||||
| 69 | 2 | status => 'CRITICAL', | |||||||||||||||||||||||||||||||||||||
| 70 | info => "Database connection $@", | ||||||||||||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | # re-throw any other exceptions | ||||||||||||||||||||||||||||||||||||||
| 75 | 10 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | croak "The 'dbh' coderef should return an object!" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 78 | unless blessed $dbh; | ||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||
| 80 | my $db_class = $params{db_class} # Provided in call to check() | ||||||||||||||||||||||||||||||||||||||
| 81 | // ((ref $self) && $self->{db_class}) # Value from new | ||||||||||||||||||||||||||||||||||||||
| 82 | 6 | ▶ | || "DBI::db"; # default value | ||||||||||||||||||||||||||||||||||||
|
Condition62%5/8
($params{'db_class'} // ref $self && $self->{'db_class'}) || 'DBI::db'
$params{"db_class"} // ref $self && $self->{"db_class"}
ref $self and $self->{"db_class"}
MC/DC0%0/3
($params{'db_class'} // ref $self && $self->{'db_class'}) || 'DBI::db'
$params{"db_class"}
ref $self
$self->{"db_class"}
Truth table50%2/4
A // B and C
A= $params{"db_class"}B= ref $selfC= $self->{"db_class"}
| |||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | 6 | my $isa = ref $dbh; | |||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||
| 86 | 6 | ▶ | croak "The 'dbh' coderef should return a '$db_class', not a '$isa'" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 87 | unless $dbh->isa($db_class); | ||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||
| 89 | 5 | my $res = $self->SUPER::check( | |||||||||||||||||||||||||||||||||||||
| 90 | %params, | ||||||||||||||||||||||||||||||||||||||
| 91 | dbh => $dbh, | ||||||||||||||||||||||||||||||||||||||
| 92 | db_access => $db_access, | ||||||||||||||||||||||||||||||||||||||
| 93 | db_class => $db_class | ||||||||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||||||||
| 95 | 5 | delete $res->{dbh}; # don't include the object in the result | |||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | 5 | return $res; | |||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | 2 | ▶ | sub _read_write_temp_table { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_write_temp_table: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||
| 102 | 2 | my (%params) = @_; | |||||||||||||||||||||||||||||||||||||
| 103 | 2 | my $dbh = $params{dbh}; | |||||||||||||||||||||||||||||||||||||
| 104 | 2 | ▶ | my $table = $params{table_name} // "__DBH_CHECK__"; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$params{"table_name"} // "__DBH_CHECK__"
MC/DC0%0/1
$params{"table_name"} // "__DBH_CHECK__"
$params{"table_name"}
Truth table50%1/2
A
A= $params{"table_name"}
| |||||||||||||||||||||||||||||||||||||||
| 105 | 2 | my $status = "CRITICAL"; | |||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | 2 | my $qtable = $dbh->quote_identifier($table); | |||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | # Drop it like it's hot | ||||||||||||||||||||||||||||||||||||||
| 110 | 2 | $dbh->do("DROP TEMPORARY TABLE IF EXISTS $qtable"); | |||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||
| 112 | 2 | $dbh->do( | |||||||||||||||||||||||||||||||||||||
| 113 | join( | ||||||||||||||||||||||||||||||||||||||
| 114 | "", | ||||||||||||||||||||||||||||||||||||||
| 115 | "CREATE TEMPORARY TABLE IF NOT EXISTS $qtable (", | ||||||||||||||||||||||||||||||||||||||
| 116 | "check_id INTEGER PRIMARY KEY,", | ||||||||||||||||||||||||||||||||||||||
| 117 | "check_string VARCHAR(64) NOT NULL", | ||||||||||||||||||||||||||||||||||||||
| 118 | ")" | ||||||||||||||||||||||||||||||||||||||
| 119 | ) | ||||||||||||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||
| 122 | 2 | $dbh->do( | |||||||||||||||||||||||||||||||||||||
| 123 | join( | ||||||||||||||||||||||||||||||||||||||
| 124 | "", | ||||||||||||||||||||||||||||||||||||||
| 125 | "INSERT INTO $qtable ", | ||||||||||||||||||||||||||||||||||||||
| 126 | " (check_id, check_string) ", | ||||||||||||||||||||||||||||||||||||||
| 127 | "VALUES (1, 'Hello world')", | ||||||||||||||||||||||||||||||||||||||
| 128 | ) | ||||||||||||||||||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||||||||||||||||||
| 130 | 2 | my @row = $dbh->selectrow_array( | |||||||||||||||||||||||||||||||||||||
| 131 | "SELECT check_string FROM $qtable WHERE check_id = 1" | ||||||||||||||||||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | $status = "OK" if ($row[0] && ($row[0] eq "Hello world")); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$row[0] and $row[0] eq "Hello world"
MC/DC0%0/2
$row[0] and $row[0] eq "Hello world"
$row[0]
$row[0] eq "Hello world"
Truth table33%1/3
A and B
A= $row[0]B= $row[0] eq "Hello world"
| |||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | 2 | $dbh->do("DROP TEMPORARY TABLE $qtable"); | |||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | 2 | return $status; | |||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||
| 141 | 5 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=9 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 142 | 5 | my ( $self, %params ) = @_; | |||||||||||||||||||||||||||||||||||||
| 143 | 5 | my $dbh = $params{dbh}; | |||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | # Get db_access from parameters | ||||||||||||||||||||||||||||||||||||||
| 146 | 5 | my $read_write = ($params{db_access} =~ /^rw$/i); | |||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | 5 | my $status = "OK"; | |||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||
| 150 | RUN_TESTS: { | ||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | # See if we can ping the DB connection | ||||||||||||||||||||||||||||||||||||||
| 153 | 5 | ▶ | if ($dbh->can("ping") && !$dbh->ping) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$dbh->can("ping") and not $dbh->ping
MC/DC50%1/2
$dbh->can("ping") and not $dbh->ping
$dbh->can("ping")
not $dbh->ping
Truth table66%2/3
A and not(B)
A= $dbh->can("ping")B= not $dbh->ping
| |||||||||||||||||||||||||||||||||||||||
| 154 | 2 | $status = "CRITICAL"; | |||||||||||||||||||||||||||||||||||||
| 155 | 2 | last RUN_TESTS; | |||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||
| 158 | # See if a simple SELECT works | ||||||||||||||||||||||||||||||||||||||
| 159 | 3 | my $value = eval { $dbh->selectrow_array("SELECT 1"); }; | |||||||||||||||||||||||||||||||||||||
| 160 | 3 | ▶ | unless (defined $value && $value == 1) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $value and $value == 1
MC/DC0%0/2
defined $value and $value == 1
defined $value
$value == 1
Truth table33%1/3
A and B
A= defined $valueB= $value == 1
| |||||||||||||||||||||||||||||||||||||||
| 161 | 0 | $status = "CRITICAL"; | |||||||||||||||||||||||||||||||||||||
| 162 | 0 | last RUN_TESTS; | |||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||
| 165 | 3 | ▶ | $status = _read_write_temp_table(%params) if $read_write; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||
| 168 | # Generate the human readable info string | ||||||||||||||||||||||||||||||||||||||
| 169 | my $info = sprintf( | ||||||||||||||||||||||||||||||||||||||
| 170 | "%s %s %s check of %s%s", | ||||||||||||||||||||||||||||||||||||||
| 171 | $status eq "OK" ? "Successful" : "Unsuccessful", | ||||||||||||||||||||||||||||||||||||||
| 172 | $dbh->{Driver}->{Name}, | ||||||||||||||||||||||||||||||||||||||
| 173 | $read_write ? "read write" : "read only", | ||||||||||||||||||||||||||||||||||||||
| 174 | $dbh->{Name}, | ||||||||||||||||||||||||||||||||||||||
| 175 | 5 | ▶ | $dbh->{Username} ? " as $dbh->{Username}" : "", | ||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||
| 178 | 5 | return { status => $status, info => $info }; | |||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||
| 181 | 2 | 1; | |||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||||||||||||