|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package com.cloud.ha; |
| 20 | + |
| 21 | +import com.cloud.agent.AgentManager; |
| 22 | +import com.cloud.agent.api.Answer; |
| 23 | +import com.cloud.agent.api.CheckOnHostCommand; |
| 24 | +import com.cloud.host.Host; |
| 25 | +import com.cloud.host.HostVO; |
| 26 | +import com.cloud.host.Status; |
| 27 | +import com.cloud.host.dao.HostDao; |
| 28 | +import com.cloud.hypervisor.Hypervisor; |
| 29 | +import com.cloud.resource.ResourceManager; |
| 30 | +import com.cloud.utils.component.AdapterBase; |
| 31 | +import org.apache.log4j.Logger; |
| 32 | + |
| 33 | +import javax.ejb.Local; |
| 34 | +import javax.inject.Inject; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +@Local(value=Investigator.class) |
| 38 | +public class HypervInvestigator extends AdapterBase implements Investigator { |
| 39 | + private final static Logger s_logger = Logger.getLogger(HypervInvestigator.class); |
| 40 | + @Inject HostDao _hostDao; |
| 41 | + @Inject AgentManager _agentMgr; |
| 42 | + @Inject ResourceManager _resourceMgr; |
| 43 | + |
| 44 | + @Override |
| 45 | + public Boolean isVmAlive(com.cloud.vm.VirtualMachine vm, Host host) { |
| 46 | + Status status = isAgentAlive(host); |
| 47 | + if (status == null) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + return status == Status.Up ? true : null; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Status isAgentAlive(Host agent) { |
| 55 | + if (agent.getHypervisorType() != Hypervisor.HypervisorType.Hyperv) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + CheckOnHostCommand cmd = new CheckOnHostCommand(agent); |
| 59 | + List<HostVO> neighbors = _resourceMgr.listHostsInClusterByStatus(agent.getClusterId(), Status.Up); |
| 60 | + for (HostVO neighbor : neighbors) { |
| 61 | + if (neighbor.getId() == agent.getId() || neighbor.getHypervisorType() != Hypervisor.HypervisorType.Hyperv) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + Answer answer = _agentMgr.easySend(neighbor.getId(), cmd); |
| 67 | + if (answer != null) { |
| 68 | + return answer.getResult() ? Status.Down : Status.Up; |
| 69 | + } |
| 70 | + } catch (Exception e) { |
| 71 | + s_logger.debug("Failed to send command to host: " + neighbor.getId()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return null; |
| 76 | + } |
| 77 | +} |
0 commit comments