-
Notifications
You must be signed in to change notification settings - Fork 3
/
hp-msa.pl
executable file
·178 lines (149 loc) · 5.67 KB
/
hp-msa.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
#!/usr/bin/perl
use warnings;
use strict;
use Net::SSL;
use LWP::UserAgent;
use Digest::MD5 qw(md5_hex);
use XML::Simple;
use JSON;
my $zabbixSender="/usr/bin/zabbix_sender";
my $zabbixConfd="/etc/zabbix/zabbix_agentd.conf";
my $sendFile="/var/tmp/zabbixSenderHPP2000";
my $zabbixSendCommand="$zabbixSender -c $zabbixConfd -i ";
my $USERNAME = "manage";
my $PASSWORD = "\!manage";
sub getHPP200Objects {
my $ua = shift;
my $sessionKey = shift;
my $url = shift;
my $objectName = shift;
my $idName = shift;
my $type = shift;
my $zbxArray = shift;
my $req = HTTP::Request->new(GET => $url);
$req->header('sessionKey' => $sessionKey );
$req->header('dataType' => 'api' );
my $res = $ua->request($req);
my $ref = XMLin($res->content, KeyAttr => "oid");
foreach my $oid (values %{$ref->{OBJECT}}) {
if ($oid->{name} eq $objectName) {
my $reference;
foreach my $entry (@{$oid->{PROPERTY}}) {
if ($entry->{name} =~ /^($idName)$/) {
$reference = {'{#HP_P2000_ID}' => $entry->{content}, '{#HP_P2000_TYPE}' => $type};
last;
}
}
push @{$zbxArray}, {%{$reference}};
}
}
}
sub getHPP200Stats {
my $ua = shift;
my $sessionKey = shift;
my $url = shift;
my $objectName = shift;
my $idName = shift;
my $colHash = shift;
my $req = HTTP::Request->new(GET => $url);
$req->header('sessionKey' => $sessionKey );
$req->header('dataType' => 'api' );
my $res = $ua->request($req);
my $ref = XMLin($res->content, KeyAttr => "oid");
foreach my $oid (values %{$ref->{OBJECT}}) {
if ($oid->{name} eq $objectName) {
my $reference;
my $hashKey;
foreach my $entry (@{$oid->{PROPERTY}}) {
if ($entry->{name} =~ /^($idName|bytes-per-second-numeric|iops|number-of-reads|number-of-writes|data-read-numeric|data-written-numeric)$/) {
my $key = $1;
if ($key =~ /durable-id/) {
$hashKey = lc($entry->{content});
} elsif ($key eq $idName) {
$hashKey = $entry->{content};
} else {
$reference->{$key} = $entry->{content};
}
}
}
$colHash->{$hashKey} = {%{$reference}};
}
}
}
sub getZabbixValues {
my $hostname = shift;
my $colHash = shift;
my $type = shift;
my $outputString = "";
foreach my $key (keys %{$colHash}) {
foreach my $itemKey (keys %{$colHash->{$key}}) {
$outputString .= "$hostname hp.p2000.stats[$type,$key,$itemKey] $colHash->{$key}->{$itemKey}\n";
}
}
$outputString;
}
sub logOut {
my $ua = shift;
my $sessionKey = shift;
my $hostname = shift;
my $url = "https://$hostname/api/exit";
my $req = HTTP::Request->new(GET => $url);
$req->header('sessionKey' => $sessionKey );
$req->header('dataType' => 'api' );
$ua->request($req);
}
my $hostname = $ARGV[0] or die("Usage: hp-msa-lld.pl <HOSTNAME> [lld|stats]");
my $function = $ARGV[1] || 'lld';
die("Usage: hp-msa-lld.pl <HOSTNAME> [lld|stats]") unless ($function =~ /^(lld|stats)$/);
my $md5_data = "${USERNAME}_${PASSWORD}";
my $md5_hash = md5_hex( $md5_data );
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
my $url = "https://$hostname/api/login/" . $md5_hash;
my $req = HTTP::Request->new(GET => $url);
my $res = $ua->request($req);
my $ref = XMLin($res->content);
my $sessionKey;
if (exists $ref->{OBJECT}->{PROPERTY}->{"return-code"} && $ref->{OBJECT}->{PROPERTY}->{"return-code"}->{content} == 1) {
$sessionKey = $ref->{OBJECT}->{PROPERTY}->{"response"}->{content};
} else {
die($ref->{OBJECT}->{PROPERTY}->{"response"}->{content});
}
if ($function eq 'lld') {
my $zbxArray = [];
getHPP200Objects ( $ua, $sessionKey, "https://$hostname/api/show/controllers",
"controllers", "durable-id", "Controller", $zbxArray);
getHPP200Objects ( $ua, $sessionKey, "https://$hostname/api/show/vdisks",
"virtual-disk", "name", "Vdisk", $zbxArray);
getHPP200Objects ( $ua, $sessionKey, "https://$hostname/api/show/volumes",
"volume", "volume-name", "Volume", $zbxArray);
print to_json({data => $zbxArray} , { ascii => 1, pretty => 1 }) . "\n";
logOut($ua, $sessionKey, $hostname);
} else {
my $ctrls = {};
my $vdisks = {};
my $volumes = {};
my $outputString = "";
getHPP200Stats ( $ua, $sessionKey, "https://$hostname/api/show/controller-statistics",
"controller-statistics", "durable-id", $ctrls);
getHPP200Stats ( $ua, $sessionKey, "https://$hostname/api/show/vdisk-statistics",
"vdisk-statistics", "name", $vdisks);
getHPP200Stats ( $ua, $sessionKey, "https://$hostname/api/show/volume-statistics",
"volume-statistics", "volume-name", $volumes);
logOut($ua, $sessionKey, $hostname);
$outputString .= getZabbixValues($hostname, $ctrls, "Controller");
$outputString .= getZabbixValues($hostname, $vdisks, "Vdisk");
$outputString .= getZabbixValues($hostname, $volumes, "Volume");
$sendFile .= "_${hostname}_$$";
die "Could not open file $sendFile!" unless (open(FH, ">", $sendFile));
print FH $outputString;
die "Could not close file $sendFile!" unless (close(FH));
$zabbixSendCommand .= $sendFile;
if ( qx($zabbixSendCommand) =~ /Failed 0/ ) {
$res = 1;
} else {
$res = 0;
}
die "Can not remove file $sendFile!" unless(unlink ($sendFile));
print "$res\n";
exit ($res - 1);
}