-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_vcsa_mem.pl
executable file
·85 lines (66 loc) · 2.42 KB
/
check_vcsa_mem.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
#!/usr/bin/perl
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
$ENV{PERL_NET_HTTPS_SSL_SOCKET_CLASS} = 'Net::SSL';
# nagios: -epn
# --
# check_vcsa_mem - Check VCSA Memory Usage
# Copyright (C) 2019 Alexander Krogloth, git@krogloth.de
# --
# 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 REST::Client;
use Data::Dumper;
use Getopt::Long;
use JSON;
use HTTP::Request::Common;
use POSIX qw(strftime);
GetOptions(
'hostname=s' => \my $Hostname,
'username=s' => \my $Username,
'password=s' => \my $Password,
'warning=i' => \my $Warning,
'critical=i' => \my $Critical,
'help|?' => sub { exec perldoc => -F => $0 or die "Cannot execute perldoc: $!\n"; },
) or Error("$0: Error in command line arguments\n");
sub Error {
print "$0: " . $_[0] . "\n";
exit 2;
}
Error('Option --hostname needed!') unless $Hostname;
Error('Option --username needed!') unless $Username;
Error('Option --password needed!') unless $Password;
Error('Option --warning needed!') unless $Warning;
Error('Option --critical needed!') unless $Critical;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
$ua->ssl_opts(SSL_verify_mode => 0);
$ua->ssl_opts(verify_hostname => 0);
my $request = POST "https://$Hostname/rest/com/vmware/cis/session";
$request->authorization_basic($Username, $Password);
my $response = $ua->request($request);
my $content = $response->decoded_content;
my $token = decode_json($content)->{value};
my $startTime = strftime "%Y-%m-%dT%H:%M:%S.000Z", gmtime ( time - 1200);
my $endTime = strftime "%Y-%m-%dT%H:%M:%S.000Z", gmtime;
my $url = "https://$Hostname/rest/appliance/monitoring/query?item.interval=MINUTES5&item.function=AVG&item.start_time=$startTime&item.end_time=$endTime&item.names.1=mem.usage";
my $client = REST::Client->new();
$client->addHeader('vmware-api-session-id' => $token);
$client->GET($url);
my $res = decode_json($client->responseContent);
my $value = $res->{'value'};
my $memory_percent = @$value[0]->{'data'}[0];
$memory_percent = sprintf("%.2f", $memory_percent);
if($memory_percent > $Critical){
print "CRITICAL: Memory Usage $memory_percent %\n";
exit 2;
} elsif($memory_percent > $Warning){
print "WARNING: Memory Usage $memory_percent %\n";
exit 1;
} else {
print "OK: Memory Usage $memory_percent %\n";
exit 0;
}