-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_vmware_snapshots.pl
executable file
·339 lines (307 loc) · 10.4 KB
/
check_vmware_snapshots.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
331
332
333
334
335
336
337
338
339
#!/usr/bin/perl
BEGIN {
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
eval {
# required for new IO::Socket::SSL versions
require IO::Socket::SSL;
IO::Socket::SSL->import();
IO::Socket::SSL::set_ctx_defaults( SSL_verify_mode => 0 );
};
};
# check_vmware_snapshots.pl
# Extra packages required (URL given for vMA suitable RPMs)
# * Date::Parse from http://vault.centos.org/5.2/extras/i386/RPMS/
# 2012 Simon Meggle, <[email protected]>
# this program 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.
#
# this program 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, write To the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# VERSION 0.18
#
# Version history:
# 2019/12/11 - v0.18
# fixes "Server version unavailable" Error (Thanks to PetrMa)
# 2018/12/17 - v0.17
# now uses Monitoring::Plugin instead of Nagios::Plugin(not in cpan anymore)
# 2015/05/29 - v0.16
# added properties to API query; improves execution speed. (Thanks to Natxo Asenjo)
# 2013/12/24 - v0.15
# added parameter "match_snapshot_names" to also black/whiteliste snapshot names
# 2013/12/12 - v0.14
# fixed bug when there are no snapshots at all (thanks to Andreas Daubner, Christian Joy, James)
# 2013/04/26 - v0.13
# added parameter 'separator' to specify another msg separator than comma. (Thanks to Olaf Assmus).
# Examples:
# --separator '<br>' : Nagios sees the whole output as a single line. All snapshots/VMs
# are displayed as well in service overview as in service details.
# Each message gets its own line.
# --separator '\n' : Nagios sees n lines. Only the first line is displayed in the service overview.
# All lines are displayed in the service details.
# Each message gets its own line.
# First Output line is now a summation of the badcount.
# 2013/04/18 - v0.12 added black/whitelist function to exclude/filter VMs (thanks to Andi Seemueller)
# 2012/10/29 - v0.11 initial commit
#
# command 'check_vmware_snapshots'
#define command{
# command_name check_vmware_snapshots
# command_line $USER1$/check_vmware_snapshots.pl --server $HOSTADDRESS$ --username $ARG1$ --password $ARG2$ --mode $ARG3$ --critical $ARG4$ --warning $ARG5$ $ARG6$
#}
#
# service
# service 'Snapshot Age'
#define service{
# service_description Snapshot Age
# check_command check_vmware_snapshots!$USER4$!$USER5$!age!7!30
# ...
# }
#
# service 'Snapshot Count'
#define service{
# service_description Snapshot Count
# check_command check_vmware_snapshots!$USER4$!$USER5$!count!1!2
# ...
# }
#
## service 'Snapshot Count for all DWH VMs
#define service{
# service_description Snapshot Count for all DWH VMs
# check_command check_vmware_snapshots!$USER4$!$USER5$!count!1!2!--whitelist 'emDWH.*'
# ...
# }
#
## service 'Snapshot count with Snapshot blacklist'
#define service{
# service_description Snapshot Count without Dev Snapshots
# check_command check_vmware_snapshots!$USER4$!$USER5$!count!1!2!--blacklist 'snapshot_dev_.*' --match_snapshot_names=1
# ...
# }
# Example Output 1:
# CRITICAL - Snapshot "Before update" (VM: 'vmHDX03-1') is 18.2 days old
# Snapshot "20120914_rc2" (VM: 'win2k8r2') is 32.9 days old
#
use strict;
use warnings;
use VMware::VIRuntime;
use Date::Parse;
use Monitoring::Plugin;
my %STATES = (
0 => "ok",
1 => "warning",
2 => "critical",
3 => "unknown",
);
{
no warnings 'redefine';
*Monitoring::Plugin::Functions::get_shortname = sub {
return undef;
};
}
my $perfdata_label;
my $perfdata_uom;
my $ok_msg;
my $nok_msg;
my $np = Monitoring::Plugin->new(
shortname => "",
usage => "",
);
my %opts = (
mode => {
type => "=s",
variable => "mode",
help => "count (per VM) | age (per snapshot)",
required => 1,
},
warning => {
type => "=i",
variable => "warning",
help => "days after a snapshot is alarmed as warning.",
required => 1,
},
critical => {
type => "=i",
variable => "critical",
help => "days after a snapshot is alarmed as critical.",
required => 1,
},
blacklist => {
type => "=s",
variable => "blacklist",
help => "regex blacklist",
required => 0,
},
whitelist => {
type => "=s",
variable => "whitelist",
help => "regex whitelist",
required => 0,
},
separator => {
type => "=s",
variable => "separator",
help => "field separator for VMs/snapshots (default: ', '). ",
required => 0,
default => ", "
},
match_snapshot_names => {
type => ":i",
help => "If set, match also names of snapshots in black/whitelist",
required => 0,
default => 0,
},
);
my $badcount = 0;
my $worststate = 0;
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
Util::connect();
my $warn = Opts::get_option('warning');
my $crit = Opts::get_option('critical');
my $blacklist = Opts::get_option('blacklist');
my $whitelist = Opts::get_option('whitelist');
my $separator = Opts::get_option('separator');
my $match_snapshot_names = Opts::get_option('match_snapshot_names');
$np->set_thresholds(
warning => $warn,
critical => $crit,
);
my $mode = Opts::get_option('mode');
my $sc = Vim::get_service_content();
my $vms = Vim::find_entity_views(
view_type => 'VirtualMachine',
filter => {},
properties => [ 'name', 'snapshot' ],
);
if ( uc($mode) eq "AGE" ) {
$perfdata_label = "outdated_snapshots";
$perfdata_uom = "snapshots";
$ok_msg = "No outdated VM snapshots found.";
$nok_msg = "outdated VM snapshots found!";
} elsif ( uc($mode) eq "COUNT" ) {
$perfdata_label = "snapshot_count";
$perfdata_uom = "snapshots";
$ok_msg = "All VMs have the allowed number of snapshots.";
$nok_msg = "VMs with too much snapshots!";
}
foreach my $vm_view ( @{$vms} ) {
#my $vm_name = $vm_view->{summary}->{config}->{name};
my $vm_name = $vm_view->{name};
my $vm_snapinfo = $vm_view->{snapshot};
next unless defined $vm_snapinfo;
next if (isblacklisted(\$blacklist,$vm_name ));
next if (isnotwhitelisted(\$whitelist,$vm_name));
if ( uc($mode) eq "AGE" ) {
check_snapshot_age( $vm_name, $vm_snapinfo->{rootSnapshotList} );
}
elsif ( uc($mode) eq "COUNT" ) {
my %vm_snapshot_count;
check_snapshot_count( $vm_name, $vm_snapinfo->{rootSnapshotList},
\%vm_snapshot_count );
my $status = $np->check_threshold( $vm_snapshot_count{$vm_name} );
if ($status) {
$np->add_message(
$status,
sprintf(
"VM \"%s\" has %d snapshots",
$vm_name, $vm_snapshot_count{$vm_name}
)
);
$badcount++;
$worststate = ($status > $worststate ? $status : $worststate);
}
}
# elsif ( uc($mode) eq "SIZE" ) {
# $perfdata_label = "snapshot size";
# $perfdata_uom = "MB";
# $ok_msg = "All snapshots are within allowed size bounds.";
#
# }
else {
$np->nagios_die("Unknown Mode.");
}
}
$np->add_perfdata(
label => $perfdata_label,
value => $badcount,
uom => $perfdata_uom,
threshold => $np->threshold(),
);
{
Util::disconnect();
}
if ($worststate) {
unshift( @{$np->{messages}->{ $STATES{$worststate} } }, $badcount . " " . $nok_msg);
$np->nagios_exit(
$np->check_messages(
join => $separator,
join_all => $separator,
)
);
}
else {
$np->nagios_exit( 0, $ok_msg );
}
sub check_snapshot_age {
my $vm_name = shift;
my $vm_snaplist = shift;
foreach my $vm_snap ( @{$vm_snaplist} ) {
if ( $vm_snap->{childSnapshotList} ) {
check_snapshot_age( $vm_name, $vm_snap->{childSnapshotList} );
}
next if (isblacklisted(\$blacklist,$vm_snap->{name}) and $match_snapshot_names );
next if (isnotwhitelisted(\$whitelist,$vm_snap->{name}) and $match_snapshot_names );
my $epoch_snap = str2time( $vm_snap->{createTime} );
my $days_snap = sprintf("%0.1f", ( time() - $epoch_snap ) / 86400 );
my $status = $np->check_threshold($days_snap);
if ($status) {
$np->add_message(
$status,
sprintf(
"Snapshot \"%s\" (VM: '%s') is %d days old",
$vm_snap->{name}, $vm_name, $days_snap
)
);
$badcount++;
$worststate = ($status > $worststate ? $status : $worststate);
}
}
}
sub check_snapshot_count {
my $vm_name = shift;
my $vm_snaplist = shift;
my $vm_snapcount = shift;
foreach my $vm_snap ( @{$vm_snaplist} ) {
if ( $vm_snap->{childSnapshotList} ) {
check_snapshot_count( $vm_name, $vm_snap->{childSnapshotList},
$vm_snapcount );
}
next if (isblacklisted(\$blacklist,$vm_snap->{name}) and $match_snapshot_names );
next if (isnotwhitelisted(\$whitelist,$vm_snap->{name}) and $match_snapshot_names );
$vm_snapcount->{$vm_name}++;
}
}
sub isblacklisted {
my ($blacklist_ref,@candidates) = @_;
return 0 if (!defined $$blacklist_ref);
my $ret;
$ret = grep (/$$blacklist_ref/, @candidates);
return $ret;
}
sub isnotwhitelisted {
my ($whitelist_ref,@candidates) = @_;
return 0 if (!defined $$whitelist_ref);
my $ret;
$ret = ! grep (/$$whitelist_ref/, @candidates);
return $ret;
}