-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_soap
executable file
·202 lines (174 loc) · 6.38 KB
/
check_soap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/perl
#
# check_soap a nagios check plugin for webservices
#
# Copyright (C) 2010 Jesse Morgan
#
# check_soap
# This file is part of morgnagplug
#
# check_soap is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# check_soap is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use Data::Dumper;
use Nagios::Plugin;
use Nagios::Plugin::Threshold;
use LWP::UserAgent;
use HTTP::Request::Common;
use Time::HiRes;
my $np = Nagios::Plugin->new(
usage => "Usage: %s -u <url> [-m <striddng>] [-n <string>] [-p <filename>] [-w <float>] [-c <float>] [--ignore-faults] [-s <soapaction>]",
version => '1.0',
blurb => 'a check for soap services',
url => 'https://morgnagplug.svn.sourceforge.net/',
license => 'GPL 2',
timeout => 15, # default value explictly defined
);
################
# define flags #
################
$np->add_arg(
'spec' => "url|u=s",
'help' => "URL of SOAP service",
'required' => 1,
);
$np->add_arg(
'spec' => "match|m=s@",
'help' => "Succeeds if string is matched via regex",
);
$np->add_arg(
'spec' => "nomatch|n=s@",
'help' => "Fails if string is matched via regex",
);
$np->add_arg(
'spec' => "post|p=s",
'help' => "File containing post data",
);
$np->add_arg(
'spec' => "warning|w=s",
'help' => "Minimum response time to WARN in seconds (default: 8)",
'default' =>8,
);
$np->add_arg(
'spec' => "critical|c=s",
'help' => "Minimum response time to CRIT in seconds (default: 10)",
'default' =>10,
);
$np->add_arg(
'spec' => "soapaction|s=s",
'help' => "Soap action (default is blank)",
'default' =>'',
);
$np->add_arg(
'spec' => "ignorefaults",
'help' => "Do not fail on Fault",
'default' =>0,
);
$np->getopts;
alarm $np->opts->timeout;
###########################
# parameter sanity checks #
###########################
if ($np->opts->url !~ /https?:\/\/.*/ ){
$np->nagios_exit(UNKNOWN, "Bad URL: ".$np->opts->url." didn't meet a simple regex rule");
}elsif (defined $np->opts->post and ( ! -e $np->opts->post or ! -r $np->opts->post) ){
$np->nagios_exit(UNKNOWN, "Bad POST file: ".$np->opts->post." doesn't exist or is unreadable");
}elsif (defined $np->opts->critical and $np->opts->critical !~ /^[0-9]+(.[0-9]+)?$/ ){
$np->nagios_exit(UNKNOWN, "Bad Critical value: ".$np->opts->critical." should be a numeric value!");
}elsif (defined $np->opts->warning and $np->opts->warning !~ /^[0-9]+(.[0-9]+)?$/ ){
$np->nagios_exit(UNKNOWN, "Bad Warning value: ".$np->opts->warning." should be a numeric value!");
}elsif (defined $np->opts->warning and defined $np->opts->critical and $np->opts->warning >=$np->opts->critical ){
$np->nagios_exit(UNKNOWN, "Bad Warning value: ".$np->opts->warning." should be less than crit (".$np->opts->critical.")");
}
#################################
# Load Data to POST (if needed) #
#################################
my ($httpmethod, $postdata);
if (defined $np->opts->post){
$httpmethod='POST';
open POSTDATA, $np->opts->post or $np->nagios_exit(UNKNOWN, "Bad POST file: ".$np->opts->post." won't open for reading!");
$postdata=join '\n', <POSTDATA>;
close POSTDATA;
}
##############################
# Actual SOAP call goes here #
##############################
my $userAgent = LWP::UserAgent->new(agent => $np->shortname);
$userAgent->timeout($np->opts->critical+1);
my $response;
my $starttime=[ Time::HiRes::gettimeofday( ) ];
if ($httpmethod eq "POST"){
print "Posting..." if ($np->opts->verbose);
$response = $userAgent->post($np->opts->url,
'Content_Type' => 'text/xml;charset=UTF-8',
'Accept-Encoding' => 'gzip,deflate',
'SOAPAction' => $np->opts->soapaction,
'Content' => $postdata
);
}else{
print "Getting..." if ($np->opts->verbose);
$response = $userAgent->get( $np->opts->url,
'Content_Type' => 'text/xml;charset=UTF-8',
'Accept-Encoding' => 'gzip,deflate',
'SOAPAction' => $np->opts->soapaction,
);
}
my $elapsedtime = Time::HiRes::tv_interval( $starttime );
my $threshold = Nagios::Plugin::Threshold->set_thresholds(
warning => $np->opts->warning,
critical => $np->opts->critical,
);
$np->add_perfdata( label => "time", value => $elapsedtime, uom => "s", threshold => $threshold );
if ($np->opts->verbose ==3){
print Dumper $response;
print "$elapsedtime seconds\n";
}
###################
# Process Results #
###################
# Check for Soap Faults
if ($np->opts->ignorefaults == 0 and $response->content =~ /(<[^<]*\:Fault[^>]*>.*<\/[^<]*\:Fault[^>]*>)/s ){
my $error=$1;
$np->nagios_exit(CRITICAL, "Fault: Soap failed with the following: $error");
}
#check for 4xx or 5xx errors
if ( $response->code >=400 ){
$np->nagios_exit(CRITICAL, "code ".$response->code .": ".$response->message);
}
# ensure all requested matches match
if (defined $np->opts->match){
foreach my $match (@{$np->opts->match}){
if ($response->content !~/$match/){
$np->nagios_exit(CRITICAL, "Match not found: '$match' was not matched to content");
}
}
}
# ensure all requested nomatches do not match
if (defined $np->opts->nomatch){
foreach my $nomatch (@{$np->opts->nomatch}){
if ($response->content =~/$nomatch/){
$np->nagios_exit(CRITICAL, "Match found: '$nomatch' should not be matched to content");
}
}
}
# If it took longer than CRIT
if ($elapsedtime >= $np->opts->critical){
$np->nagios_exit(CRITICAL, "Timeout exceeded: it took $elapsedtime second(s); should be under ".$np->opts->critical);
}
# If it took longer than WARN
if ($elapsedtime >= $np->opts->warning){
$np->nagios_exit(WARNING, "Timeout exceeded: it took $elapsedtime second(s); should be under ".$np->opts->warning);
}
#gauntlet complete!
$np->nagios_exit(OK, "Success");
#The End