Debian Codegraph @

lib/plugins/stonith/external/vcenter

raw ◀ ▶
1 Debian patch touch this file
•1 #!/usr/bin/perl
2 #
3 # External STONITH module for VMWare vCenter/ESX
4 #
5 # Author: Nhan Ngo Dinh
6 # License: GNU General Public License (GPL)
7 #
8
9 require 5.010;
10
11 use strict;
12 use warnings;
13
14 sub dielog {
15 my $msg = "[";
16 $msg .= "$ARGV[0]" if defined($ARGV[0]);
17 $msg .= " $ARGV[1]" if defined($ARGV[1]);
18 $msg .= "]";
19 ( $_ ) = @_;
20 $msg .= " $_";
21 system("ha_log.sh", "err", "$msg");
22 die();
23 }
24
25 # Define command groups
26 my @configCommands = qw{getconfignames getinfo-devid getinfo-devname getinfo-devdescr getinfo-devurl getinfo-xml};
27 my @actionCommands = qw{reset on off};
28 my @netCommands = (@actionCommands, qw{status gethosts listvms});
29
30 # Process command line arguments
31 my $command = $ARGV[0] || dielog("No command specified\n");
32
33 # Command belongs to the group of commands that do not require any connection to VMware vCenter
34 if ($command ~~ @configCommands) {
35 if ($command eq "getconfignames") {
36 print "VI_SERVER\nVI_PORTNUMBER\nVI_PROTOCOL\nVI_SERVICEPATH\nVI_CREDSTORE\nHOSTLIST\nRESETPOWERON\n";
37 }
38 elsif ($command eq "getinfo-devid") {
39 print "VMware vCenter STONITH device\n";
40 }
41 elsif ($command eq "getinfo-devname") {
42 print "VMware vCenter STONITH device\n";
43 }
44 elsif ($command eq "getinfo-devdescr") {
45 print "VMWare vCenter STONITH device\n";
46 }
47 elsif ($command eq "getinfo-devurl") {
48 print "http://www.vmware.com/\n";
49 }
50 elsif ($command eq "getinfo-xml") {
51 print q{<parameters>
52 <parameter name="HOSTLIST" required="1">
53 <content type="string"/>
54 <shortdesc lang="en">List of hosts and virtual machines (required)</shortdesc>
55 <longdesc lang="en">
56 The list of hosts that the VMware vCenter STONITH device controls.
57 Syntax is:
58 hostname1[=VirtualMachineName1];hostname2[=VirtualMachineName2]
59
60 NOTE: omit =VirtualMachineName if hostname and virtual machine names are identical
61
62 Example:
63 cluster1=VMCL1;cluster2=VMCL2
64 </longdesc>
65 </parameter>
66 <parameter name="VI_SERVER">
67 <content type="string" default="localhost"/>
68 <shortdesc lang="en">VMware vCenter address</shortdesc>
69 <longdesc lang="en">
70 The VMware vCenter address
71 </longdesc>
72 </parameter>
73 <parameter name="VI_PROTOCOL">
74 <content type="string" default="https"/>
75 <shortdesc lang="en">VMware vCenter protocol</shortdesc>
76 <longdesc lang="en">
77 The VMware vCenter protocol
78 </longdesc>
79 </parameter>
80 <parameter name="VI_PORTNUMBER">
81 <content type="string" default="443"/>
82 <shortdesc lang="en">VMware vCenter port number</shortdesc>
83 <longdesc lang="en">
84 The VMware vCenter port number
85 </longdesc>
86 </parameter>
87 <parameter name="VI_SERVICEPATH">
88 <content type="string" default="/sdk"/>
89 <shortdesc lang="en">VMware vCenter service path</shortdesc>
90 <longdesc lang="en">
91 The VMware vCenter services path
92 </longdesc>
93 </parameter>
94 <parameter name="VI_CREDSTORE" required="1">
95 <content type="string"/>
96 <shortdesc lang="en">VMware vCenter credentials store file</shortdesc>
97 <longdesc lang="en">
98 VMware vCenter credentials store file
99 </longdesc>
100 </parameter>
101 <parameter name="RESETPOWERON">
102 <content type="string" default="1"/>
103 <shortdesc lang="en">PowerOnVM on reset</shortdesc>
104 <longdesc lang="en">
105 Enable/disable a PowerOnVM on reset when the target virtual machine is off
106 Allowed values: 0, 1
107 </longdesc>
108 </parameter>
109 <parameter name="PERL_LWP_SSL_VERIFY_HOSTNAME">
110 <content type="string"/>
111 <shortdesc lang="en">Enable or disable SSL hostname verification</shortdesc>
112 <longdesc lang="en">
113 To disable SSL hostname verification set this option to 0.
114 To enable hostname verification, set this option to 1.
115 This option is actually part of the LWP Perl library.
116 See LWP(3pm) for more information.
117 </longdesc>
118 </parameter>
119 </parameters>} . "\n";
120 }
121 else { dielog("Invalid command specified: $command\n"); }
122 }
123
124 # Command belongs to the group of commands that require connecting to VMware vCenter
125 elsif ($command ~~ @netCommands) {
126
127 eval { require VMware::VIRuntime; }
128 or dielog("Missing perl module VMware::VIRuntime. Download and install 'VMware Infrastructure (VI) Perl Toolkit', available at http://www.vmware.com/support/developer/viperltoolkit/ \n");
129
130 # A valid VI_CREDSTORE is required to avoid interactive prompt
131 ( exists $ENV{'VI_CREDSTORE'} ) || dielog("VI_CREDSTORE not specified\n");
132
133 # HOSTLIST is mandatory
134 exists $ENV{'HOSTLIST'} || dielog("HOSTLIST not specified\n");
135
136 # Parse HOSTLIST to %host_to_vm and %vm_to_host
137 my @hostlist = split(';', $ENV{'HOSTLIST'});
138 my %host_to_vm = ();
139 my %vm_to_host = ();
140 foreach my $host (@hostlist) {
141 my @config = split(/=/, $host);
142 my $key = $config[0]; my $value = $config[1];
143 if (!defined($value)) { $value = $config[0]; }
144 $host_to_vm{$key} = $value;
145 $vm_to_host{(lc $value)} = $key;
146 }
147
148 eval {
149 # VI API: reads options from the environment variables into appropriate data structures for validation.
150 Opts::parse();
151 # VI API: ensures that input values from environment variable are complete, consistent and valid.
152 Opts::validate();
153 # VI API: establishes a session with the VirtualCenter Management Server or ESX Server Web service
154 Util::connect();
155 };
156 if ($@) {
157 # This is just a placeholder for any error handling procedure
158 dielog($@);
159 }
160
161 # Command belongs to the group of commands that performs actions on Virtual Machines
162 if ($command ~~ @actionCommands) {
163
164 my $targetHost = $ARGV[1] || dielog("No target specified\n");
165
166 # Require that specified target host exists in the specified HOSTLIST
167 if (exists $host_to_vm{$targetHost}) {
168
169 my $vm;
170 my $esx;
171 eval {
172 # VI API: searches the inventory tree for a VirtualMachine managed entity whose name matches
173 # the name of the virtual machine assigned to the target host in HOSTLIST
174 $vm = Vim::find_entity_view(view_type => "VirtualMachine", filter => { name => qr/^\Q$host_to_vm{$targetHost}\E/i });
175 if (!defined $vm) {
176 dielog("Machine $targetHost was not found");
177 }
178
179 # VI API: retrieves the properties of the managed object reference runtime.host of the VirtualMachine
180 # managed entity obtained by the previous command
181 # NOTE: This is essentially a workaround to vSphere Perl SDK
182 # to allow pointing to the right HostSystem. This is probably
183 # done by changing the current HostSystem in the Web Service
184 # session context. WARNING: Do not use the same session for any
185 # other concurrent operation.
186 $esx = Vim::get_view(mo_ref => $vm->{"runtime"}{"host"})->name;
187 };
188 if ($@) {
189 if (ref($@) eq "SoapFault") { dielog("$@->detail\n"); }
190 dielog($@);
191 }
192
193 my $powerState = $vm->get_property('runtime.powerState')->val;
194 if ($powerState eq "suspended") {
195 # This implementation assumes that suspending a cluster node can cause
196 # severe failures on shared resources, thus any failover operation should
197 # be blocked.
198 dielog("Machine $esx:$vm->{'name'} is in a suspended state\n");
199 }
200
201 eval {
202 if ($command eq "reset") {
203 if ($powerState eq "poweredOn") {
204 $vm->ResetVM();
205 system("ha_log.sh", "info", "Machine $esx:$vm->{'name'} has been reset");
206 } else {
207 system("ha_log.sh", "warn", "Tried to ResetVM $esx:$vm->{'name'} that was $powerState");
208 # Start a virtual machine on reset only if explicitly allowed by RESETPOWERON
209 if ($powerState eq "poweredOff" && (! exists $ENV{'RESETPOWERON'} || $ENV{'RESETPOWERON'} ne 0)) {
210 $vm->PowerOnVM();
211 system("ha_log.sh", "info", "Machine $esx:$vm->{'name'} has been powered on");
212 } else {
213 dielog("Could not complete $esx:$vm->{'name'} power cycle");
214 }
215 }
216 }
217 elsif ($command eq "off") {
218 if ($powerState eq "poweredOn") {
219 $vm->PowerOffVM();
220 system("ha_log.sh", "info", "Machine $esx:$vm->{'name'} has been powered off");
221 } else {
222 system("ha_log.sh", "warn", "Tried to PowerOffVM $esx:$vm->{'name'} that was $powerState");
223
224 }
225 }
226 elsif ($command eq "on") {
227 if ($powerState eq "poweredOff") {
228 $vm->PowerOnVM();
229 system("ha_log.sh", "info", "Machine $esx:$vm->{'name'} has been powered on");
230 } else {
231 system("ha_log.sh", "warn", "Tried to PowerOnVM $esx:$vm->{'name'} that was $powerState");
232 }
233 }
234 else { dielog("Invalid command specified: $command\n"); }
235 };
236 if ($@) {
237 if (ref($@) eq "SoapFault") { dielog("$@->detail\n"); }
238 dielog($@);
239 }
240
241 } else { dielog("Invalid target specified\n"); }
242 } else {
243 # Command belongs to the group of commands that lookup the status of VMware vCenter and/or virtual machines
244 if ($command eq "status") {
245 # we already connect to the vcenter, no need to do
246 # anything else in status
247 ;
248 }
249 elsif ($command eq "gethosts") {
250 foreach my $key (keys(%host_to_vm)) {
251 print "$key \n";
252 }
253 }
254 elsif ($command eq "listvms") {
255 eval {
256 # VI API: Searches the inventory tree for all VirtualMachine managed objects
257 my $vms = Vim::find_entity_views(view_type => "VirtualMachine");
258 if (defined $vms) {
259 printf(STDERR "%-50s %-20s\n", "VM Name", "Power state");
260 print STDERR "-" x 70 . "\n";
261 foreach my $vm (@$vms) {
262 my $powerState = $vm->get_property('runtime.powerState')->val;
263 printf("%-50s %-20s\n", $vm->{name}, $powerState);
264 }
265 }
266 };
267 }
268 else { dielog("Invalid command specified: $command\n"); }
269 }
270 eval {
271 Util::disconnect();
272 };
273 if ($@) {
274 # This is just a placeholder for any error handling procedure
275 dielog($@);
276 }
277 }
278 else { dielog("Invalid command specified: $command\n"); }
279
280 exit(0);