From d0892a964dcedca7b827a02cfe273f486769116a Mon Sep 17 00:00:00 2001 From: Kelven Yang Date: Mon, 7 Mar 2011 18:40:12 -0800 Subject: [PATCH] Add JMX monitoring to cluster management --- build/developer.xml | 13 ++++- .../com/cloud/cluster/ClusterManagerImpl.java | 15 +++++- .../src/com/cloud/vm/UserVmManagerImpl.java | 10 ++-- utils/src/com/cloud/utils/mgmt/JmxUtil.java | 53 ++++++++++++++++++- 4 files changed, 83 insertions(+), 8 deletions(-) diff --git a/build/developer.xml b/build/developer.xml index a5417d02a18..89d08d5b7e5 100755 --- a/build/developer.xml +++ b/build/developer.xml @@ -69,7 +69,10 @@ - + + + + @@ -87,6 +90,9 @@ + + + @@ -97,7 +103,10 @@ - + + + + diff --git a/server/src/com/cloud/cluster/ClusterManagerImpl.java b/server/src/com/cloud/cluster/ClusterManagerImpl.java index 6fa65c4913f..42682664ec2 100644 --- a/server/src/com/cloud/cluster/ClusterManagerImpl.java +++ b/server/src/com/cloud/cluster/ClusterManagerImpl.java @@ -47,6 +47,7 @@ import com.cloud.utils.db.DB; import com.cloud.utils.db.Transaction; import com.cloud.utils.events.SubscriptionMgr; import com.cloud.utils.exception.CloudRuntimeException; +import com.cloud.utils.mgmt.JmxUtil; import com.cloud.utils.net.MacAddress; import com.cloud.utils.net.NetUtils; import com.google.gson.Gson; @@ -566,6 +567,12 @@ public class ClusterManagerImpl implements ClusterManager { for(ManagementServerHostVO mshost : removedNodeList) { activePeers.remove(mshost.getId()); + + try { + JmxUtil.unregisterMBean("ClusterManager", "Node " + mshost.getId()); + } catch(Exception e) { + s_logger.warn("Unable to deregiester cluster node from JMX monitoring due to exception " + e.toString()); + } } List newNodeList = new ArrayList(); @@ -577,6 +584,12 @@ public class ClusterManagerImpl implements ClusterManager { s_logger.debug("Detected management node joined, id:" + mshost.getId() + ", nodeIP:" + mshost.getServiceIP()); } newNodeList.add(mshost); + + try { + JmxUtil.registerMBean("ClusterManager", "Node " + mshost.getId(), new ClusterManagerMBeanImpl(mshost)); + } catch(Exception e) { + s_logger.warn("Unable to regiester cluster node into JMX monitoring due to exception " + e.toString()); + } } } @@ -764,7 +777,7 @@ public class ClusterManagerImpl implements ClusterManager { if(_currentServiceAdapter == null) { throw new ConfigurationException("Unable to set current cluster service adapter"); } - + if(s_logger.isInfoEnabled()) { s_logger.info("Cluster manager is configured."); } diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index f406f64a6ae..2517a469aa9 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -106,9 +106,9 @@ import com.cloud.host.dao.HostDao; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.network.IPAddressVO; import com.cloud.network.Network; +import com.cloud.network.Network.GuestIpType; import com.cloud.network.NetworkManager; import com.cloud.network.NetworkVO; -import com.cloud.network.Network.GuestIpType; import com.cloud.network.Networks.TrafficType; import com.cloud.network.dao.FirewallRulesDao; import com.cloud.network.dao.IPAddressDao; @@ -2244,9 +2244,13 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) { UserVmVO vm = profile.getVirtualMachine(); Account owner = _accountDao.findById(vm.getAccountId()); + + if (owner == null) { + throw new PermissionDeniedException("The owner of " + vm + " does not exist: " + vm.getAccountId()); + } - if (owner == null || owner.getState() == Account.State.disabled) { - throw new PermissionDeniedException("The owner of " + vm + " either does not exist or is disabled: " + vm.getAccountId()); + if (owner.getState() == Account.State.disabled) { + throw new PermissionDeniedException("The owner of " + vm + " is disabled: " + vm.getAccountId()); } VirtualMachineTemplate template = profile.getTemplate(); diff --git a/utils/src/com/cloud/utils/mgmt/JmxUtil.java b/utils/src/com/cloud/utils/mgmt/JmxUtil.java index b042119b73a..6e8c38034de 100755 --- a/utils/src/com/cloud/utils/mgmt/JmxUtil.java +++ b/utils/src/com/cloud/utils/mgmt/JmxUtil.java @@ -17,9 +17,11 @@ */ package com.cloud.utils.mgmt; +import java.lang.management.ManagementFactory; import java.util.ArrayList; import javax.management.InstanceAlreadyExistsException; +import javax.management.InstanceNotFoundException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; @@ -28,12 +30,59 @@ import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; public class JmxUtil { - public static void registerMBean(ManagementBean mbean) throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { + public static ObjectName registerMBean(ManagementBean mbean) + throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { + + return registerMBean(mbean.getName(), null, mbean); + } + + public static ObjectName registerMBean(String objTypeName, String objInstanceName, Object mbean) + throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { + + String name = "com.cloud:type=" + objTypeName; + if(objInstanceName != null && !objInstanceName.isEmpty()) + name += ", name=" + objInstanceName; + ObjectName objectName = new ObjectName(name); + ArrayList server = MBeanServerFactory.findMBeanServer(null); if (server.size() > 0) { MBeanServer mBeanServer = server.get(0); - ObjectName objectName = new ObjectName("com.cloud: type=" + mbean.getName()); mBeanServer.registerMBean(mbean, objectName); + return objectName; + } else { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + mBeanServer.registerMBean(mbean, objectName); + return objectName; } } + + public static void unregisterMBean(String objTypeName, String objInstanceName) + throws MalformedObjectNameException, MBeanRegistrationException, InstanceNotFoundException { + + ObjectName name = composeMBeanName(objTypeName, objInstanceName); + unregisterMBean(name); + } + + public static void unregisterMBean(ObjectName name) + throws MalformedObjectNameException, MBeanRegistrationException, InstanceNotFoundException { + + ArrayList server = MBeanServerFactory.findMBeanServer(null); + if (server.size() > 0) { + MBeanServer mBeanServer = server.get(0); + mBeanServer.unregisterMBean(name); + } else { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + mBeanServer.unregisterMBean(name); + } + } + + private static ObjectName composeMBeanName(String objTypeName, String objInstanceName) + throws MalformedObjectNameException { + + String name = "com.cloud:type=" + objTypeName; + if(objInstanceName != null && !objInstanceName.isEmpty()) + name += ", name=" + objInstanceName; + + return new ObjectName(name); + } }