forked from aleex42/netapp-cdot-nagios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_cdot_quota.pl
executable file
·277 lines (222 loc) · 7.37 KB
/
check_cdot_quota.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
#!/usr/bin/perl
# nagios: -epn
# --
# check_cdot_quota - Check quota usage
# Copyright (C) 2016 Joshua Malone ([email protected])
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --
use strict;
use warnings;
use lib "/usr/lib/netapp-manageability-sdk/lib/perl/NetApp";
use NaServer;
use NaElement;
use Getopt::Long qw(:config no_ignore_case);
GetOptions(
'H|hostname=s' => \my $Hostname,
'u|username=s' => \my $Username,
'p|password=s' => \my $Password,
'w|warning=i' => \my $SizeWarning,
'c|critical=i' => \my $SizeCritical,
'P|perf' => \my $perf,
"V|volume=s" => \my $Volume,
't|target=s' => \my $Quota,
'vserver=s' => \my $Vserver,
'v|verbose' => \my $verbose,
'h|help' => sub { exec perldoc => -F => $0 or die "Cannot execute perldoc: $!\n"; },
) or Error("$0: Error in command line arguments\n");
sub Error {
print "UNKNOWN: $0: " . $_[0] . "\n";
exit 3;
}
Error('Option --hostname needed!') unless $Hostname;
Error('Option --username needed!') unless $Username;
Error('Option --password needed!') unless $Password;
Error('Option -w needed!') unless $SizeWarning;
Error('Option -c needed!') unless $SizeCritical;
Error('Critical space threshold must be greater than Warning!') if ($SizeWarning > $SizeCritical);
if ($Quota && !$Volume) {
Error('Option -t requires a Volume name (-V)!');
}
# Set some conservative default thresholds for the more
# esoteric metrics
my ($crit_msg, $warn_msg, $ok_msg);
# Store all perf data points for output at end
my %perfdata=();
my $s = NaServer->new( $Hostname, 1, 3 );
$s->set_transport_type("HTTPS");
$s->set_style("LOGIN");
$s->set_admin_user( $Username, $Password );
my $iterator = NaElement->new("quota-report-iter");
my $tag_elem = NaElement->new("tag");
$iterator->child_add($tag_elem);
my $quota_query = NaElement->new("query");
my $quota_info = NaElement->new("quota");
if ($Volume) {
print("Querying only volume $Volume\n") if ($verbose);
$iterator->child_add($quota_query);
$quota_query->child_add($quota_info);
$quota_info->child_add_string('volume', $Volume);
}
if ($Quota) {
$quota_info->child_add_string('quota-target', $Quota);
}
if ($Vserver) {
$quota_info->child_add_string('vserver', $Vserver);
}
my $next = "";
my (@crit_msg, @warn_msg, @ok_msg);
while(defined($next)){
unless($next eq ""){
$tag_elem->set_content($next);
}
$iterator->child_add_string("max-records", 100);
my $output = $s->invoke_elem($iterator);
last if ($output->child_get_string("num-records") == 0 );
if ($output->results_errno != 0) {
my $r = $output->results_reason();
print "UNKNOWN: $r\n";
exit 3;
}
foreach my $getQuota ( $output->child_get("attributes-list")->children_get() ) {
# Disk limit is in KB
my $diskLimit = $getQuota->child_get_string('disk-limit');
next if ($diskLimit eq "-" or $diskLimit == 0 );
# Also in KB
$diskLimit *= 1024;
my $diskUsed = $getQuota->child_get_string('disk-used') * 1024;
my $fileLimit = $getQuota->child_get_string('file-limit');
my $filesUsed = $getQuota->child_get_string('files-used');
my $volume = $getQuota->child_get_string('volume');
my $type = $getQuota->child_get_string('quota-type');
my $target;
if ($type eq "user") {
my $qUsers = $getQuota->child_get('quota-users');
next unless ($qUsers);
my $qUser= $qUsers->child_get('quota-user');
next unless ($qUser);
my $quotaUser = $qUser->child_get_string('quota-user-name');
printf("Found quota for %s on %s\n", $quotaUser, $volume) if ($verbose);
$target = sprintf("%s/%s", $volume, $quotaUser);
} else {
$target = $getQuota->child_get_string('quota-target');
}
printf ("Quota %s: %s %s %s %s\n", $target, $diskLimit, $diskUsed, $fileLimit, $filesUsed) if ($verbose);
my $diskPercent=($diskUsed/$diskLimit*100);
# Generate pretty-printed scaled numbers
my $msg = sprintf ("Quota %s is %d%% full (used %s of %s)",
$target, $diskPercent, humanScale($diskUsed), humanScale($diskLimit) );
if ($diskPercent >= $SizeCritical) {
push (@crit_msg, $msg);
} elsif ($diskPercent >= $SizeWarning) {
push (@warn_msg, $msg);
} else {
push (@ok_msg, $msg);
}
if ($fileLimit ne "-" ) {
# Check files limit as well as space
}
$perfdata{$target}{'byte_used'}=$diskUsed;
$perfdata{$target}{'byte_total'}=$diskLimit;
$perfdata{$target}{'files_used'}=$filesUsed;
$perfdata{$target}{'file_limit'}=$fileLimit;
}
$next = $output->child_get_string("next-tag");
}
# Build perf data string for output
my $perfdatastr="";
foreach my $vol ( keys(%perfdata) ) {
# DS[1] - Data space used
$perfdatastr.=sprintf(" %s_space_used=%dB;%d;%d;%d;%d", $vol, $perfdata{$vol}{'byte_used'},
$SizeWarning*$perfdata{$vol}{'byte_total'}/100, $SizeCritical*$perfdata{$vol}{'byte_total'}/100,
0, $perfdata{$vol}{'byte_total'} );
}
if(scalar(@crit_msg) ){
print "CRITICAL: ";
print join (". ", @crit_msg);
if(scalar(@warn_msg)){
print "\nWARNING: ";
print join (", ", @warn_msg);
}
if(scalar(@ok_msg)){
print "\nOK: ";
print join (", ", @ok_msg);
}
print "|$perfdatastr\n";
exit 2;
} elsif(scalar(@warn_msg) ){
print "WARNING: ";
print join ("; ", @warn_msg);
if(scalar(@ok_msg)){
print "\nOK: ";
print join (", ", @ok_msg);
}
print "|$perfdatastr\n";
exit 1;
} elsif(scalar(@ok_msg) ){
print "OK: ";
print join (", ", @ok_msg);
print "|$perfdatastr\n";
exit 0;
} else {
print "WARNING: no online volume found\n";
exit 1;
}
sub humanScale {
my ($metric) = @_;
my $unit='B';
my @units = qw( KB MB GB TB PB EB );
while ($metric > 1100) {
if (scalar(@units)<1) {
# Hit our max scaling factor - bail out
last;
}
$unit=shift(@units);
$metric=$metric/1024;
}
return sprintf("%.1f %s", $metric, $unit);
}
__END__
=encoding utf8
=head1 NAME
check_cdot_volume - Check Volume Usage
=head1 SYNOPSIS
check_cdot_quota.pl -H HOSTNAME -u USERNAME -p PASSWORD \
-w PERCENT_WARNING -c PERCENT_CRITICAL \
--files-warning PERCENT_WARNING \
--files-critical PERCENT_CRITICAL [-V VOLUME] [-P]
=head1 DESCRIPTION
Checks the space and files usage of a quota / qtree and alerts
if warning or critical thresholds are reached
=head1 OPTIONS
=over 4
=item -H | --hostname FQDN
The Hostname of the NetApp to monitor (Cluster or Node MGMT)
=item -u | --username USERNAME
The Login Username of the NetApp to monitor
=item -p | --password PASSWORD
The Login Password of the NetApp to monitor
=item -w PERCENT_WARNING
The Warning threshold for data space usage.
=item -c PERCENT_CRITICAL
The Critical threshold for data space usage.
=item -V | --volume VOLUME
Optional: The name of the Volume on which quotas should be checked.
=item -t | --target TARGET
Optional: The target of a specific quota / qtree that should be checked.
To use this option, you **MUST** specify a volume.
=item -help
=item -h
to see this Documentation
=back
=head1 EXIT CODE
3 on Unknown Error
2 if Critical Threshold has been reached
1 if Warning Threshold has been reached or any problem occured
0 if everything is ok
=head1 AUTHORS
Joshua Malone <jmalone at nrao.edu>
Alexander Krogloth <git at krogloth.de>
Stefan Grosser <sgr at firstframe.net>