blib/lib/Monitoring/Sneck/Checks/http_json.pm

filter: Statementstmt 100% 66 / 66 Branchbran 94.1% 32 / 34 Conditioncond 86.2% 25 / 29 MC/DCmcdc 73.3% 11 / 15 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 93.7% 149 / 159 CC 31 SCAR 21.4
CC
31
Cov
95%
content_problems
14.0
json_path
8.0
_text
6.1
CRAP 8.5
« blib/lib/Monitoring/Sneck/Checks/rbl.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/sshd_config.pm »
1 package Monitoring::Sneck::Checks::http_json;
2  
3 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Monitoring::Sneck::Checks::http';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Monitoring::Sneck::Checks::http_json - checks values in a JSON response
11  
12 =head1 VERSION
13  
14 Version 0.0.1
15  
16 =cut
17  
18 3 our $VERSION = '0.0.1';
19  
20 =head1 SYNOPSIS
21  
22 api_health|perl:http_json -u https://api.example.org/health -J status=ok -J checks.db.status=ok
23 queue|perl:http_json -H 192.0.2.30 -p 8080 -u /stats -V queue.depth -W 100 -C 500
24 first_node|perl:http_json -u https://cluster.example.org/nodes -J nodes.0.healthy=true
25  
26 =head1 DESCRIPTION
27  
28 Fetches a URL the same as L<Monitoring::Sneck::Checks::http>, taking
29 all of its options, then checks values in the JSON it gets back.
30 Content that is not JSON, or a value that is not there or not what it
31 should be, is CRITICAL.
32  
33 Paths are keys split by dots, with numbers indexing arrays, so
34 nodes.0.name is the name of the first node. -1 is the last. Keys with
35 dots in them can not be reached.
36  
37 true, false, and null are compared as those words.
38  
39 =head1 OPTIONS
40  
41 These are on top of the options of L<Monitoring::Sneck::Checks::http>.
42  
43 - -J, --json :: "path=value", a value that must be there, or "path",
44 something that must be there. May be given more than once.
45  
46 - -V, --value :: The path to a number to check with --value-warning and
47 --value-critical, and add to the perfdata.
48  
49 - -W, --value-warning :: Range for --value.
50  
51 - -C, --value-critical :: Range for --value.
52  
53 One of --json or --value must be given. --value-warning and
54 --value-critical need --value.
55  
56 =cut
57  
58 10 ▶sub option_spec {
Subroutine option_spec: called CC=1 SCAR=0.0
Pod documented
59 10 my ($self) = @_;
60  
61 10 return ( $self->SUPER::option_spec, 'json|J=s@', 'value|V=s', 'value-warning|W=s', 'value-critical|C=s' );
62 }
63  
64 10 ▶sub ranges {
Subroutine ranges: called CC=1 SCAR=0.0
Pod documented
65 10 my ($self) = @_;
66  
67 10 return ( $self->SUPER::ranges, 'value-warning', 'value-critical' );
68 }
69  
70 10 ▶sub validate {
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
71 10 my ( $self, $options ) = @_;
72  
73 10 my @problems = $self->SUPER::validate($options);
74 10 foreach my $name ( 'value-warning', 'value-critical' ) {
75 push( @problems, '--' . $name . ' needs --value' )
76 20 ▶ if defined( $options->{$name} ) && !defined( $options->{value} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{$name} and not defined $options->{"value"} 1 19 20
Condition100%3/3
defined $options->{$name} and not defined $options->{"value"}
!ll&&!rl&&r
1631
MC/DC100%2/2
defined $options->{$name} and not defined $options->{"value"}
defined $options->{$name} not defined $options->{"value"}
Truth table100%3/3
A and not(B)
A= defined $options->{$name}B= not defined $options->{"value"}
ABresult
0X0
100
111
77 }
78 push( @problems, 'nothing to check, give --json or --value' )
79 10 ▶ if !defined( $options->{json} ) && !defined( $options->{value} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"json"} or defined $options->{"value"} 1 9 10
Condition100%3/3
defined $options->{"json"} or defined $options->{"value"}
l!l&&r!l&&!r
541
MC/DC100%2/2
defined $options->{"json"} or defined $options->{"value"}
defined $options->{"json"} defined $options->{"value"}
Truth table100%3/3
A or B
A= defined $options->{"json"}B= defined $options->{"value"}
ABresult
1X1
011
000
80  
81 10 return @problems;
82 } ## end sub validate
83  
84 =head1 METHODS
85  
86 =head2 content_problems
87  
88 Checks the content as http does, then as --json and --value say.
89  
90 =cut
91  
92 8 ▶sub content_problems {
Subroutine content_problems: called CC=14 SCAR=26.4
Pod documented
93 8 my ( $self, $response, $content ) = @_;
94  
95 8 require JSON;
96 8 my @found = $self->SUPER::content_problems( $response, $content );
97 8 my $data = eval { JSON->new->allow_nonref->decode($content) };
98 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 7 8
99 1 push( @found, [ $self->CRITICAL, 'content is not JSON' ] );
100 1 return @found;
101 }
102  
103 7 ▶ foreach my $json ( @{ $self->opt('json') // [] } ) {
Condition100%2/2
$self->opt("json") // []
l!l
34
MC/DC100%1/1
$self->opt("json") // []
$self->opt("json")
Truth table100%2/2
A
A= $self->opt("json")
Aresult
11
00
104 11 my ( $path, $expected ) = split( /=/, $json, 2 );
105 11 my ( $there, $value ) = $self->json_path( $data, $path );
106 11 ▶ if ( !$there ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($there) 2 9 11
107 2 push( @found, [ $self->CRITICAL, $path . ' is not there' ] );
108 2 next;
109 }
110 9 ▶ next if !defined($expected);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expected 1 8 9
111 8 my $text = _text($value);
112 8 ▶ push( @found, [ $self->CRITICAL, $path . ' is ' . $text . ', not ' . $expected ] ) if $text ne $expected;
Branch: 1 branch
BranchTrueFalseTotal
if $text ne $expected 2 6 8
113 } ## end foreach my $json ( @{ $self->opt('json') // [] ...})
114  
115 7 ▶ if ( defined( $self->opt('value') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->opt("value")) 4 3 7
116 4 my $path = $self->opt('value');
117 4 my ( $there, $value ) = $self->json_path( $data, $path );
118 4 ▶ if ( !$there || ref($value) || !defined($value) || $value !~ /\A-?[0-9]+(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $there or ref $value or not defined $value or not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\z/u) { } 2 2 4
Condition77%7/9
not $there or ref $value or not defined $value or not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\z/u
l!l&&r!l&&!r
112
not $there or ref $value or not defined $value
l!l&&r!l&&!r
103
not $there or ref $value
l!l&&r!l&&!r
103
MC/DC50%2/4
not $there or ref $value or not defined $value or not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\z/u
not $there ref $value not defined $value not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\z/u
Truth table60%3/5
not(A) or B or not(C) or not(D)
A= not $thereB= ref $valueC= not defined $valueD= not $value =~ /\A-?[0-9]+(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\z/u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
119 2 ▶ push( @found,
Branch: 1 branch
BranchTrueFalseTotal
$there ? : 1 1 2
120 [ $self->CRITICAL, $path . ' is ' . ( $there ? _text($value) : 'not there' ) . ', not a number' ] );
121 } else {
122 2 $self->perfdata(
123 label => $path,
124 value => $value,
125 warning => $self->opt('value-warning'),
126 critical => $self->opt('value-critical')
127 );
128 2 my $code = $self->threshold_status( $value, $self->opt('value-warning'), $self->opt('value-critical') );
129 2 ▶ push( @found, [ $code, $path . ' is ' . $value ] ) if $code != $self->OK;
Branch: 1 branch
BranchTrueFalseTotal
if $code != $self->OK 1 1 2
130 } ## end else [ if ( !$there || ref($value) || !defined($value...))]
131 } ## end if ( defined( $self->opt('value') ) )
132  
133 7 return @found;
134 } ## end sub content_problems
135  
136 =head2 json_path
137  
138 Finds a path in decoded JSON, as said under L</DESCRIPTION>.
139  
140 - data :: The decoded JSON.
141  
142 - path :: The path, such as nodes.0.name.
143  
144 Returns 1 and the value if it is there, else 0.
145  
146 my ( $there, $value ) = $check->json_path( { nodes => [ { name => 'a' } ] }, 'nodes.0.name' );
147 # ( 1, 'a' )
148  
149 =cut
150  
151 15 ▶sub json_path {
Subroutine json_path: called CC=8 SCAR=20.8
Pod documented
152 15 my ( $self, $data, $path ) = @_;
153  
154 15 my $at = $data;
155 15 foreach my $key ( split( /\./, $path ) ) {
156 22 ▶ if ( ref($at) eq 'HASH' && exists( $at->{$key} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $at eq 'HASH' and exists $at->{$key}) { } 17 5 22
elsif (ref $at eq 'ARRAY' and $key =~ /\A-?[0-9]+\z/u and $key < scalar @{$at;} and -$key <= scalar @{$at;}) { } 2 3 5
Condition100%3/3
ref $at eq "HASH" and exists $at->{$key}
!ll&&!rl&&r
3217
MC/DC100%2/2
ref $at eq "HASH" and exists $at->{$key}
ref $at eq "HASH" exists $at->{$key}
Truth table100%3/3
A and B
A= ref $at eq "HASH"B= exists $at->{$key}
ABresult
0X0
100
111
157 17 $at = $at->{$key};
158 ▶ } elsif ( ref($at) eq 'ARRAY'
Condition77%7/9
ref $at eq "ARRAY" and $key =~ /\A-?[0-9]+\z/u and $key < scalar @{$at;} and -$key <= scalar @{$at;}
!ll&&!rl&&r
302
ref $at eq "ARRAY" and $key =~ /\A-?[0-9]+\z/u and $key < scalar @{$at;}
!ll&&!rl&&r
212
ref $at eq "ARRAY" and $key =~ /\A-?[0-9]+\z/u
!ll&&!rl&&r
203
MC/DC50%2/4
ref $at eq "ARRAY" and $key =~ /\A-?[0-9]+\z/u and $key < scalar @{$at;} and -$key <= scalar @{$at;}
ref $at eq "ARRAY" $key =~ /\A-?[0-9]+\z/u $key < scalar @{$at;} -$key <= scalar @{$at;}
Truth table60%3/5
A and B and C and D
A= ref $at eq "ARRAY"B= $key =~ /\A-?[0-9]+\z/uC= $key < scalar @{$at;}D= -$key <= scalar @{$at;}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
159 && $key =~ /\A-?[0-9]+\z/
160 3 && $key < scalar( @{$at} )
161 2 && -$key <= scalar( @{$at} ) )
162 {
163 2 $at = $at->[$key];
164 } else {
165 3 return (0);
166 }
167 } ## end foreach my $key ( split( /\./, $path ) )
168  
169 12 return ( 1, $at );
170 } ## end sub json_path
171  
172 # Turns a decoded JSON value into text for comparing and messages.
173 #
174 # Args...
175 #
176 # - value :: The value.
177 #
178 # Returns true, false, or null for those, "a object" or "a array" for
179 # those, else the value.
180 #
181 # Example...
182 #
183 # _text( JSON::true ); # 'true'
184 # _text(undef); # 'null'
185 9 ▶sub _text {
Subroutine _text: called CC=6 SCAR=18.0
186 9 my ($value) = @_;
187  
188 9 ▶ return 'null' if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 8 9
189 8 ▶ return $value ? 'true' : 'false' if JSON::is_bool($value);
Branch: 2 branches
BranchTrueFalseTotal
if JSON::is_bool($value) 1 7 8
$value ? : 1 0 1
190 7 ▶ return 'a object' if ref($value) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 1 6 7
191 6 ▶ return 'a array' if ref($value) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 0 6 6
192  
193 6 return $value;
194 } ## end sub _text
195  
196 3 1;
« blib/lib/Monitoring/Sneck/Checks/rbl.pm ↑ Summary blib/lib/Monitoring/Sneck/Checks/sshd_config.pm »