-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_imc.pl
276 lines (224 loc) · 7.66 KB
/
check_imc.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
#!/usr/bin/perl
# nagios: -epn
# --
# check_hp_imc - Check HPE iMC status
# Copyright (C) 2017 Giorgio Maggiolo, http://www.maggiolo.net/
# --
# 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 common::sense;
use LWP::UserAgent;
use Data::Dumper;
use HTTP::Request;
use XML::LibXML;
use Getopt::Long;
GetOptions (
"server=s" => \my $server,
"port=i" => \my $port,
"username=s" => \my $username,
"password=s" => \my $password,
"operation=s" => \my $operation,
"realm=s" => \my $realm,
"h|help" => sub { exec perldoc => -F => $0 or die "Cannot execute perldoc: $!\n"; },
"warning=i" => \my $warning,
"critical=i" => \my $critical,
"performance" => \my $performance,
) or Error("$0: Error in command line arguments\n");
sub Error {
print "$0: " . $_[0] . "\n";
exit 2;
}
sub get_xml_text {
my $rest_call_local = $_[0];
my $browser = LWP::UserAgent->new();
my $req = HTTP::Request->new();
$browser->credentials("$server:$port",$realm,$username,$password);
$req->uri("http://$server:$port/imcrs/$rest_call_local");
$req->method('GET');
$req->header('Content_Type' => 'application/xml');
my $response = $browser->request($req);
die "Error: ", $response->status_line, "\n", Dumper($response->headers), "\n\n\n", "Is the REALM correct: ",$response->header("WWW-Authenticate"), "== ", $realm unless $response->is_success;
return $response;
}
sub license_check {
Error('Option --warning required') unless $warning;
Error('Option --critical required') unless $critical;
my $function = "plat/licenseInfo/allLicenseMsg";
my $xml_response = get_xml_text($function);
my $dom = XML::LibXML->load_xml(string => $xml_response->content);
my $license_info_element = $dom->getElementsByTagName("list")->get_node(1)->getElementsByTagName("licenseInfo")->get_node(1);
my $max_count = $license_info_element->getElementsByTagName("maxCount")->string_value();
my $lic_used_count = $license_info_element->getElementsByTagName("licUsedCount")->string_value();
my $free_licences = $max_count - $lic_used_count;
if ($free_licences < $critical){
print "CRITICAL: there are $free_licences free licences";
if ($performance){
print " | 'free_licences'=$free_licences;$warning;$critical\n";
} else {
print "\n";
}
exit(2);
} elsif ($free_licences < $warning) {
print "WARNING: there are $free_licences free licences";
if ($performance){
print " | 'free_licences'=$free_licences;$warning;$critical\n";
} else {
print "\n";
}
exit(1);
} else {
print "OK: there are $free_licences free licences";
if ($performance){
print " | 'free_licences'=$free_licences;$warning;$critical\n";
} else {
print "\n";
}
exit(0);
}
}
sub get_current_alarm {
my $function = "fault/alarm?operatorName=$username&alarmLevel=$_[0]&recStatus=0&ackStatus=0&size=200&desc=false";
my $xml_response = get_xml_text($function);
my $dom = XML::LibXML->load_xml(string => $xml_response->content);
my $list_element = $dom->getElementsByTagName("list")->get_node(1);
return $list_element->getElementsByTagName("alarm");
}
sub get_down_devices {
my $error_level="1";
my @error_devices = get_current_alarm($error_level);
if (@error_devices){
my $return_string;
foreach my $node (@error_devices){
if ($node->getElementsByTagName("alarmDesc")->string_value() =~ /Device "(.*)" does not respond/){
my $device_name = $node->getElementsByTagName("deviceName")->string_value();
$device_name =~ /([^\d]{4,}[\d]{3})(\s)?(-)?/;
$device_name = $1;
if ($return_string eq ""){
$return_string = $device_name;
} else {
$return_string .= ", $device_name";
}
}
}
if ($return_string){
print "CRITICAL: there are offline devices: $return_string\n";
exit(2);
} else {
print "OK - All devices are fine\n";
exit(0);
}
} else{
print "OK - All devices are fine\n";
exit(0);
}
}
sub get_backup_error {
my $error_level="4";
my @error_devices = get_current_alarm($error_level);
my %return_device_hash;
if (@error_devices){
my $return_string;
foreach my $node (@error_devices){
if ($node->getElementsByTagName("alarmDesc")->string_value() =~ /During backup, found that (\w*) configuration file "(.*) baseline/){
my $what_file = $1;
my $device_name = $node->getElementsByTagName("deviceName")->string_value();
$device_name =~ /([^\d]{4,}[\d]{3})(\s)?(-)?/;
$device_name = $1;
if ($return_device_hash{$device_name} eq ""){
$return_device_hash{$device_name} = $what_file;
} else {
$return_device_hash{$device_name} .= ", $what_file";
}
}
}
for my $key (keys %return_device_hash){
if ($return_device_hash{$key} =~ /(.*), (.*)/){
if ($return_string eq ""){
$return_string = "$key (RUN & START)";
} else {
$return_string .= ", $key (RUN & START)";
}
} elsif ($return_device_hash{$key} =~ /running/){
if ($return_string eq ""){
$return_string = "$key (RUN)";
} else {
$return_string .= ", $key (RUN)";
}
} elsif ($return_device_hash{$key} =~ /startup/){
if ($return_string eq ""){
$return_string = "$key (START)";
} else {
$return_string .= ", $key (START)";
}
}
}
if ($return_string){
print "WARNING: there are backups that diverge from the baseline: $return_string\n";
exit(1);
} else {
print "OK - All devices backups are fine\n";
exit(0);
}
} else {
print "OK - All devices backups are fine\n";
exit(0);
}
}
Error('Option --server required') unless $server;
Error('Option --username required') unless $username;
Error('Option --password required') unless $password;
$realm = "iMC RESTful Web Services" unless $realm;
$port = "8080" unless $port;
if($operation eq "license_check"){
license_check();
} elsif ($operation eq "get_down_devices"){
get_down_devices();
} elsif ($operation eq "get_backup_error"){
get_backup_error();
} else {
print "UNKNOWN: Operation parameter not recognized\n";
exit(3);
}
__END__
=head1 NAME
check_hp_imc - Check HPE iMC environment
=head1 SYNOPSIS
check_hp_imc.pl --server SERVER_IP [--port PORT] --username USERNAME --password PASSWORD --operation OPERATION [-h|--help]
=head1 DESCRIPTION
Connects to a HPE iMC and performe some checks on it.
=head1 OPTIONS
=over 4
=item --server SERVER
FQDN or IP Address of the HPE iMC
=item --port PORT
Optional: Port used to connect to the HPE iMC eAPIs
=item -u | --username USERNAME
The Username used to connect to the HPE iMC eAPIs
=item -p | --password PASSWORD
The Password used to connect to the HPE iMC eAPIs
=item --warning WARNING
The Warning threshold for tests that expect a threshold
=item --critical CRITICAL
The Critical threshold for tests that expect a threshold
=item --operation OPERATION
List of the possible operations that the script checks against the SERVER iMC. Where written "W/C" means that WARNING and CRITICAL values are needed.
Possible values:
- license_check (W/C): verify how many free licenses are left and raise a WARNING/CRITICAL alarm if the thresholds are reached
- get_down_devices: verify that there are no down devices and, in case, raise a CRITICAL alarm and write down the list of the devices
- get_backup_error: verify whether any device has a backup that is different from the baseline and raise a WARNING alarm in case
=item --performance
Flag for performance data output
=item -h | --help
Tqo see this Documentation
=back
=head1 EXIT CODE
3 on Unknown Errors
2 if Critical Threshold has been reached
1 if Warning Threshold has been reached
0 if everything is ok
=head1 AUTHORS
Giorgio Maggiolo <giorgio at maggiolo dot net>