-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_netscaler_gateway.pl
330 lines (279 loc) · 10.3 KB
/
check_netscaler_gateway.pl
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/perl
##############################################################################
# check_netscaler_gateway.pl
# Nagios Plugin for Citrix NetScaler Gateway
# Simon Lauger <[email protected]>
#
# https://github.com/slauger/check_netscaler_gateway
#
# Copyright 2015-2018 Simon Lauger
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
use strict;
use warnings;
use LWP;
use JSON;
use HTTP::Cookies;
use URI::Escape;
use Data::Dumper;
use Monitoring::Plugin;
my $plugin = Monitoring::Plugin->new(
plugin => 'check_netscaler_gateway',
shortname => 'NetScaler Gateway',
version => 'v1.2.0',
url => 'https://github.com/slauger/check_netscaler_gateway',
blurb => 'Nagios Plugin for Citrix NetScaler Gateway Appliance (VPX/MPX/SDX)',
usage => 'Usage: %s -H <hostname> [ -u <username> ] [ -p <password> ] -S <store>
[-w <warning>] [-c <critical>] [ -v|--verbose ] [ -d|--debug ] [ -t <timeout> ]',
license => 'http://www.apache.org/licenses/LICENSE-2.0',
extra => '
This is a Nagios monitoring plugin for the Citrix NetScaler Gateway. The plugin
emulates a full login proccess on a NetScaler Gateway vServer and checks if there
are any available resources.
See https://github.com/slauger/check_netscaler_gateway for details.'
);
my @args = (
{
spec => 'hostname|H=s',
usage => '-H, --hostname=STRING',
desc => 'Hostname of the NetScaler appliance to connect to',
required => 1,
},
{
spec => 'username|u=s',
usage => '-u, --username=STRING',
desc => 'Username to log into box as',
required => 1,
},
{
spec => 'password|p=s',
usage => '-p, --password=STRING',
desc => 'Password for login username',
required => 1,
},
{
spec => 'store|S=s',
usage => '-S, --store=STRING',
desc => 'Name of the Store in Storefront (default: Store)',
default => 'Store',
required => 0,
},
{
spec => 'warning|w=i',
usage => '-w, --warning=INTEGER',
desc => 'Warning threshold for the numbers of found applications',
required => 0,
},
{
spec => 'critical|c=i',
usage => '-c, --critical=INTEGER',
desc => 'Critical threshold for the numbers of found applications',
required => 0,
},
{
spec => 'debug|d!',
usage => '-d, --debug',
desc => 'Debug mode, print out every single HTTP request',
default => 0,
required => 0,
},
);
foreach my $arg (@args) {
add_arg( $plugin, $arg );
}
$plugin->getopts;
netscaler_gateway_client($plugin);
sub add_arg {
my $plugin = shift;
my $arg = shift;
my $spec = $arg->{'spec'};
my $help = $arg->{'usage'};
my $default = $arg->{'default'};
my $required = $arg->{'required'};
if ( defined $arg->{'desc'} ) {
my @desc;
if ( ref( $arg->{'desc'} ) ) {
@desc = @{ $arg->{'desc'} };
}
else {
@desc = ( $arg->{'desc'} );
}
foreach my $d (@desc) {
$help .= "\n $d";
}
}
$plugin->add_arg(
spec => $spec,
help => $help,
default => $default,
required => $required,
);
}
sub netscaler_gateway_client {
my $plugin = shift;
my $lwp = LWP::UserAgent->new( keep_alive => 1, env_proxy => 1 );
$lwp->timeout( $plugin->opts->timeout );
$lwp->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0 );
my $cookie_jar = HTTP::Cookies->new;
$lwp->cookie_jar($cookie_jar);
$lwp->cookie_jar( {} );
if ( $plugin->opts->verbose ) {
$lwp->show_progress(1);
}
my $request;
my $response;
my @resources;
my $baseurl = 'https://' . $plugin->opts->hostname;
my $storeurl = $baseurl . '/Citrix/' . $plugin->opts->store . 'Web';
# Step 1: Login to NetScaler Gateway
$response = $lwp->post(
$baseurl . '/cgi/login',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Referer' => $baseurl . '/vpn/index.html',
Content => [
login => $plugin->opts->username,
passwd => $plugin->opts->password
]
);
if ( $plugin->opts->debug ) {
print Dumper($response);
}
if ( HTTP::Status::is_error( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/cgi/login failed with HTTP ' . $response->code );
}
elsif ( HTTP::Status::is_redirect( $response->code ) ) {
if ( $response->header('Location') ne '/cgi/setclient?wica' ) {
# this may happen if invalid credentials are given or missing required headers
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/cgi/login redirected with HTTP ' . $response->code );
}
}
# Step 2: Request to setclient script
$response = $lwp->post( $baseurl . $response->header('Location'), Content => [ $plugin->opts->username, passwd => $plugin->opts->password ] );
if ( $plugin->opts->debug ) {
print Dumper($response);
}
if ( HTTP::Status::is_error( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/cgi/setclient?wica failed with HTTP ' . $response->code );
}
elsif ( HTTP::Status::is_redirect( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/cgi/setclient?wica redirected with HTTP ' . $response->code );
}
# Step 3: Get CSRF Token & ASP.NET session ID
$response = $lwp->post(
$storeurl . '/Home/Configuration',
'Accept' => 'application/xml, text/xml, */*; q=0.01',
'Content-Length' => 0,
'X-Citrix-IsUsingHTTPS' => 'Yes'
);
if ( $plugin->opts->debug ) {
print Dumper($response);
}
if ( HTTP::Status::is_error( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/Home/Configuration failed with HTTP ' . $response->code );
}
elsif ( HTTP::Status::is_redirect( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/Home/Configuration redirected with HTTP ' . $response->code );
}
# Step 4: Store the CSRF token in a new variable
$cookie_jar->extract_cookies($response);
my $csrf_token = undef;
my $cookies = $cookie_jar->as_string();
if ( $cookies =~ /^Set-Cookie3: CsrfToken=([A-Z0-9]+);/m ) {
$csrf_token = $1;
}
else {
$plugin->nagios_exit( CRITICAL, 'failed to get CsrfToken' );
}
# Step 4b: Get Authentication Methods from Storefront
$response = $lwp->post(
$storeurl . '/Authentication/GetAuthMethods',
'Accept' => 'application/xml, text/xml, */*; q=0.01',
'Content-Length' => 0,
'X-Citrix-IsUsingHTTPS' => 'Yes',
'Csrf-Token' => $csrf_token
);
if ( $plugin->opts->debug ) {
print Dumper($response);
}
if ( HTTP::Status::is_error( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/Authentication/GetAuthMethods failed with HTTP ' . $response->code );
}
elsif ( HTTP::Status::is_redirect( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/Authentication/GetAuthMethods redirected with HTTP ' . $response->code );
}
# Step 5: Login to Storefront
$response = $lwp->post(
$storeurl . '/GatewayAuth/Login',
'Accept' => 'application/xml, text/xml, */*; q=0.01',
'Content-Length' => 0,
'X-Citrix-IsUsingHTTPS' => 'Yes',
'Csrf-Token' => $csrf_token
);
if ( $plugin->opts->debug ) {
print Dumper($response);
}
if ( HTTP::Status::is_error( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/GatewayAuth/Login failed with HTTP ' . $response->code );
}
elsif ( HTTP::Status::is_redirect( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/GatewayAuth/Login redirected with HTTP ' . $response->code );
}
# Step 6: List Resources
$response = $lwp->post(
$storeurl . '/Resources/List',
'Accept' => 'application/json, text/javascript, */*; q=0.01',
'X-Citrix-IsUsingHTTPS' => 'Yes',
'Csrf-Token' => $csrf_token,
Content => ['{ "format": "json", "resourceDetails": "Default" }']
);
if ( $plugin->opts->debug ) {
print Dumper($response);
}
if ( HTTP::Status::is_error( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/Resources/List failed with HTTP ' . $response->code );
}
elsif ( HTTP::Status::is_redirect( $response->code ) ) {
$plugin->nagios_exit( CRITICAL, 'request to ' . $storeurl . '/Resources/List redirected with HTTP ' . $response->code );
}
$response = JSON->new->allow_blessed->convert_blessed->decode( $response->content );
$response = $response->{resources};
foreach my $resource ( @{$response} ) {
$plugin->add_message( OK, $resource->{name} . ';' );
push @resources, ( $resource->{name} );
}
my $resourcecnt = @{resources};
# Step 7: Logout
$response = $lwp->get( $baseurl . '/cgi/logout' );
if ( $plugin->opts->debug ) {
print Dumper($response);
}
if ( HTTP::Status::is_error( $response->code ) ) {
$plugin->add_message( WARNING, 'Logout failed with HTTP ' . $response->code );
}
# Step 8: Threshold check (optional) and exit
if ( $plugin->opts->critical && $resourcecnt < $plugin->opts->critical ) {
$plugin->add_message( CRITICAL, 'Only ' . $resourcecnt . ' applications found, expected at least ' . $plugin->opts->critical );
my ( $code, $message ) = $plugin->check_messages;
$plugin->plugin_exit( $code, $message );
}
elsif ( $plugin->opts->warning && $resourcecnt < $plugin->opts->warning ) {
$plugin->add_message( WARNING, 'Only ' . $resourcecnt . ' applications found, expected at least ' . $plugin->opts->warning );
my ( $code, $message ) = $plugin->check_messages;
$plugin->plugin_exit( WARNING, $message );
}
else {
my ( $code, $message ) = $plugin->check_messages;
$plugin->nagios_exit( $code, $message );
}
}