码迷,mamicode.com
首页 > 其他好文 > 详细

JMX实现远程服务器Tomcat系统监控之三

时间:2014-12-05 12:47:09      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:jmx服务监控   tomcat监控   jmx   系统监控   

前面两篇JMX远程监控Tomcat服务器是没配置密码的,下面介绍在Tomcat监控时配置用户密码。

具体Tomcat地址:http://tomcat.apache.org/tomcat-6.0-doc/monitoring.html#Enabling_JMX_Remote

Note: This syntax is for Microsoft Windows. The command has to be on the same line. It is wrapped to be more readable. If Tomcat is running as a Windows service, use its configuration dialog to set java options for the service. For un*xes remove "set " from beginning of the line.

bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣
bubuko.com,布布扣
set CATALINA_OPTS=-Dcom.sun.management.jmxremote
  -Dcom.sun.management.jmxremote.port=%my.jmx.port%
  -Dcom.sun.management.jmxremote.ssl=false
  -Dcom.sun.management.jmxremote.authenticate=false
bubuko.com,布布扣
bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣
  1. If you require authorization, add and change this :
    bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣
    bubuko.com,布布扣
      -Dcom.sun.management.jmxremote.authenticate=true
      -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password
      -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access
    
    bubuko.com,布布扣
    bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣
  2. edit the access authorization file $CATALINA_BASE/conf/jmxremote.access :
    bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣
    bubuko.com,布布扣
    monitorRole readonly
    controlRole readwrite
    
    bubuko.com,布布扣
    bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣
  3. edit the password file $CATALINA_BASE/conf/jmxremote.password :
    bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣
    bubuko.com,布布扣
    monitorRole tomcat
    controlRole tomcat
    
    bubuko.com,布布扣
    bubuko.com,布布扣 bubuko.com,布布扣 bubuko.com,布布扣
    Tip: The password file should be read-only and only accessible by the operating system user Tomcat is running as.
其内容大概如下:

   1、编辑Tomcat/bin中的catalina.bat。如果配置权限,需要将autenticate设置为true,将1中的下面两行代码添加到最上面一行,注意:这些所有的命令必须在一行!。

   2、在Tomcat/conf目录下新建两个文件:jmxremote.accessjmxremote.password

          第一个文件存的是角色信息,第二个存放的是密码信息(可修改)。


配置完上面信息后:我们启动tomcat时,Tomcat出现一闪而过,上面提示的大概意思是权限问题。

     解决的办法是:选中Tomcat文件夹,点右键 -->“属性”-->“安全”--> 点“高级”--> 点“更改权限”--> 去掉“从父项继承....”--> 弹出窗口中选“删除”,这样就删除了所有访问权限。再选“添加”--> “高级”--> “立即查找”,选中你的用户(或用户组,如果选用户不行那就选用户组),例administrator(当前电脑的权限),点“确定",“确定"。来到权限项目窗口,勾选“完全控制”,点“确定”,重启就可以了。



实现:

  没有密码时,我们采用JMXConnectorFactory工厂类的 connect(JMXServiceURL serviceURL) 创建到位于给定地址的连接器服务器的连接。 设置密码后需connect(JMXServiceURL serviceURL, Map<String,?> environment) ,

  设置map存放environment.put("jmx.remote.credentials", credentials);配置信息。

      String[] credentials = new String[] { "monitorRole", "tomcat" };


其具体实现如下:


package pyc.jvm.monitor;

import java.io.IOException;
import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.ThreadMXBean;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

/**
 * 创建时间:2014-12-5 上午11:06:34
 * 
 * @author zhangtianyou
 * @version 2.2
 */

public class JMXTest3 {

	private final static String SERVICE_1 = "service:jmx:rmi:///jndi/rmi://192.168.85.54:8787/jmxrmi";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// 10秒调用一次
		Timer timer = new Timer();
		timer.schedule(new JMXTest3().new MonitorTask(SERVICE_1), 0, 10000);

	}

	private class MonitorTask extends TimerTask {

		private String service;

		public MonitorTask(String service) {
			this.service = service;
		}

		@Override
		public void run() {

			JMXmonitor(service);
		}

	}

	private static void JMXmonitor(String service) {
		JMXConnector jmxConnector = null;

		try {
			JMXServiceURL ServiceURL = new JMXServiceURL(service);
			Map<String, String[]> environment = new HashMap<String, String[]>();
			// 用户名密码,在jmxremote.password文件中的密码
			String[] credentials = new String[] { "monitorRole", "tomcat" };
			environment.put("jmx.remote.credentials", credentials);
			jmxConnector = JMXConnectorFactory.connect(ServiceURL, environment);

			MBeanServerConnection mBeanServerConnection = jmxConnector
					.getMBeanServerConnection();

			// 获取MemoryMXBean
			System.out.println("\nMemory");
			MemoryMXBean memoryMXBean = ManagementFactory
					.newPlatformMXBeanProxy(mBeanServerConnection,
							ManagementFactory.MEMORY_MXBEAN_NAME,
							MemoryMXBean.class);

			MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
			System.out.println("heapMemoryUsage :");
			System.out.println("committed = "
					+ convertKB(heapMemoryUsage.getCommitted()));
			System.out
					.println("init = " + convertKB(heapMemoryUsage.getInit()));
			System.out.println("max = " + convertKB(heapMemoryUsage.getMax()));
			System.out
					.println("used = " + convertKB(heapMemoryUsage.getUsed()));

			MemoryUsage nonHeapMemoryUsage = memoryMXBean
					.getNonHeapMemoryUsage();
			System.out.println("\nnonHeapMemoryUsage :");
			System.out.println("committed = "
					+ convertKB(nonHeapMemoryUsage.getCommitted()));
			System.out.println("init = "
					+ convertKB(nonHeapMemoryUsage.getInit()));
			System.out.println("max = "
					+ convertKB(nonHeapMemoryUsage.getMax()));
			System.out.println("used = "
					+ convertKB(nonHeapMemoryUsage.getUsed()));

			// 获取 ThreadMXBean
			System.out.println("\nThread");
			ThreadMXBean threadMXBean = ManagementFactory
					.newPlatformMXBeanProxy(mBeanServerConnection,
							ManagementFactory.THREAD_MXBEAN_NAME,
							ThreadMXBean.class);
			System.out
					.println("ThreadCount = " + threadMXBean.getThreadCount());
			System.out.println("DaemonThreadCount = "
					+ threadMXBean.getDaemonThreadCount());
			System.out.println("PeakThreadCount = "
					+ threadMXBean.getPeakThreadCount());
			System.out.println("CurrentThreadCpuTime = "
					+ threadMXBean.getCurrentThreadCpuTime());
			System.out.println("CurrentThreadUserTime = "
					+ threadMXBean.getCurrentThreadUserTime());

			System.out.println("\nClassLoading");
			ClassLoadingMXBean classLoadingMXBean = ManagementFactory
					.newPlatformMXBeanProxy(mBeanServerConnection,
							ManagementFactory.CLASS_LOADING_MXBEAN_NAME,
							ClassLoadingMXBean.class);
			// 当前加载到Java虚拟机中类的数量
			System.out.println("LoadedClassCount = "
					+ classLoadingMXBean.getLoadedClassCount());
			// Java 虚拟机开始执行到目前已经加载的类的总数。
			System.out.println("TotalLoadedClassCount = "
					+ classLoadingMXBean.getTotalLoadedClassCount());
			// Java 虚拟机开始执行到目前已经卸载的类的总数。
			System.out.println("UnloadedClassCount = "
					+ classLoadingMXBean.getUnloadedClassCount());

			System.out.println("\nCpu");
			OperatingSystemMXBean operatingSystemMXBean = ManagementFactory
					.newPlatformMXBeanProxy(mBeanServerConnection,
							ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME,
							OperatingSystemMXBean.class);
			System.out.println("AvailableProcessors = "
					+ operatingSystemMXBean.getAvailableProcessors());
			double ratio = 0.0;
			long start = System.currentTimeMillis();
			long startC;
			try {
				startC = (long) mBeanServerConnection
						.getAttribute(operatingSystemMXBean.getObjectName(),
								"ProcessCpuTime");
				try {
					TimeUnit.SECONDS.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				long end = System.currentTimeMillis();
				long endC = (long) mBeanServerConnection
						.getAttribute(operatingSystemMXBean.getObjectName(),
								"ProcessCpuTime");

				int availableProcessors = operatingSystemMXBean
						.getAvailableProcessors();
				ratio = (endC - startC) / 1000000.0 / (end - start)
						/ availableProcessors;

			} catch (AttributeNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InstanceNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (MBeanException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (ReflectionException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			System.out.println("CUP使用率" + round(ratio * 100) + "%");

		} catch (MalformedURLException e) {
			e.printStackTrace();
			System.out.println("非法的ServiceURL");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (jmxConnector != null) {
					jmxConnector.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	private static String convertKB(long src) {

		if (src <= 0L) {
			return "0KB";
		}
		double conversrc = src / 1024 / 1024;

		return round(conversrc) + "MB";
	}

	private static float round(double src) {
		return (float) (Math.round(src * 100)) / 100;
	}
}

嵌套在JavaEE中的效果如下:

bubuko.com,布布扣

 


JMX实现远程服务器Tomcat系统监控之三

标签:jmx服务监控   tomcat监控   jmx   系统监控   

原文地址:http://blog.csdn.net/fengshizty/article/details/41745291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!