public void suspendPoweredOnGuestVMs(ManagedObjectReference attachHostMoref) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { Map<ManagedObjectReference, Map<String, Object>> vms = getMOREFs.inContainerByType( attachHostMoref, "VirtualMachine", new String[] {"name", "runtime.powerState"}, new RetrieveOptions()); suspendedVMList = new ArrayList<ManagedObjectReference>(); for (ManagedObjectReference vm : vms.keySet()) { Map<String, Object> vmProp = vms.get(vm); VirtualMachinePowerState vmPowerState = (VirtualMachinePowerState) vmProp.get("runtime.powerState"); String vmName = (String) vmProp.get("name"); if ((vmPowerState.equals(VirtualMachinePowerState.POWERED_ON))) { try { ManagedObjectReference taskmor = vimPort.suspendVMTask(vm); if (getTaskResultAfterDone(taskmor)) { System.out.println(vmName + "[" + vm.getValue() + "] suspended successfully"); suspendedVMList.add(vm); } } catch (Exception e) { System.out.println(vmName + "Unable to suspend vm : " + vm + "[" + vm.getValue() + "]"); System.err.println("Reason :" + e.getLocalizedMessage()); } } } }
/** * This method queries the Vmware vCenter server for sensor data. * * @param svc the monitored service * @param parameters the parameter map * @return the poll status for this system */ @Override public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { OnmsNode onmsNode = m_nodeDao.get(svc.getNodeId()); // retrieve the assets and String vmwareManagementServer = onmsNode.getAssetRecord().getVmwareManagementServer(); String vmwareManagedEntityType = onmsNode.getAssetRecord().getVmwareManagedEntityType(); String vmwareManagedObjectId = onmsNode.getForeignId(); TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); PollStatus serviceStatus = PollStatus.unknown(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { VmwareViJavaAccess vmwareViJavaAccess = null; try { vmwareViJavaAccess = new VmwareViJavaAccess(vmwareManagementServer); } catch (MarshalException e) { logger.warn( "Error initialising VMware connection to '{}': '{}'", vmwareManagementServer, e.getMessage()); return PollStatus.unavailable( "Error initialising VMware connection to '" + vmwareManagementServer + "'"); } catch (ValidationException e) { logger.warn( "Error initialising VMware connection to '{}': '{}'", vmwareManagementServer, e.getMessage()); return PollStatus.unavailable( "Error initialising VMware connection to '" + vmwareManagementServer + "'"); } catch (IOException e) { logger.warn( "Error initialising VMware connection to '{}': '{}'", vmwareManagementServer, e.getMessage()); return PollStatus.unavailable( "Error initialising VMware connection to '" + vmwareManagementServer + "'"); } try { vmwareViJavaAccess.connect(); } catch (MalformedURLException e) { logger.warn( "Error connecting VMware management server '{}': '{}'", vmwareManagementServer, e.getMessage()); return PollStatus.unavailable( "Error connecting VMware management server '" + vmwareManagementServer + "'"); } catch (RemoteException e) { logger.warn( "Error connecting VMware management server '{}': '{}'", vmwareManagementServer, e.getMessage()); return PollStatus.unavailable( "Error connecting VMware management server '" + vmwareManagementServer + "'"); } if (!vmwareViJavaAccess.setTimeout(tracker.getConnectionTimeout())) { logger.warn( "Error setting connection timeout for VMware management server '{}'", vmwareManagementServer); } String powerState = "unknown"; if ("HostSystem".equals(vmwareManagedEntityType)) { HostSystem hostSystem = vmwareViJavaAccess.getHostSystemByManagedObjectId(vmwareManagedObjectId); if (hostSystem == null) { return PollStatus.unknown("hostSystem=null"); } else { HostRuntimeInfo hostRuntimeInfo = hostSystem.getRuntime(); if (hostRuntimeInfo == null) { return PollStatus.unknown("hostRuntimeInfo=null"); } else { HostSystemPowerState hostSystemPowerState = hostRuntimeInfo.getPowerState(); if (hostSystemPowerState == null) { return PollStatus.unknown("hostSystemPowerState=null"); } else { powerState = hostSystemPowerState.toString(); } } } } else { if ("VirtualMachine".equals(vmwareManagedEntityType)) { VirtualMachine virtualMachine = vmwareViJavaAccess.getVirtualMachineByManagedObjectId(vmwareManagedObjectId); if (virtualMachine == null) { return PollStatus.unknown("virtualMachine=null"); } else { VirtualMachineRuntimeInfo virtualMachineRuntimeInfo = virtualMachine.getRuntime(); if (virtualMachineRuntimeInfo == null) { return PollStatus.unknown("virtualMachineRuntimeInfo=null"); } else { VirtualMachinePowerState virtualMachinePowerState = virtualMachineRuntimeInfo.getPowerState(); if (virtualMachinePowerState == null) { return PollStatus.unknown("virtualMachinePowerState=null"); } else { powerState = virtualMachinePowerState.toString(); } } } } else { logger.warn( "Error getting '{}' for '{}'", vmwareManagedEntityType, vmwareManagedObjectId); vmwareViJavaAccess.disconnect(); return serviceStatus; } } if ("poweredOn".equals(powerState)) { serviceStatus = PollStatus.available(); } else { serviceStatus = PollStatus.unavailable("The system's state is '" + powerState + "'"); } vmwareViJavaAccess.disconnect(); } return serviceStatus; }