JMXMonitor.java 1.46 KB
package com.yoho.ops;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.ops.bean.Connection;
import com.yoho.ops.bean.JvmData;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import javax.management.remote.JMXServiceURL;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.text.MessageFormat;
import java.util.*;

/**
 * Hello world!
 */
public class JMXMonitor {
    public static final String RAW_URL = "service:jmx:rmi:///jndi/rmi://{0}:{1}/jmxrmi";

    /**
     * jmxhost jmxport user passwd service
     *
     * @param args
     */
    public static void main(String[] args) {
        if (null == args || 5 != args.length)
            System.exit(1);
        String jmxHost = args[0];
        String jmxPort = args[1];
        String user = args[2];
        String passwd = args[3];
        String service = args[4];

        String rawUrl = MessageFormat.format(RAW_URL, jmxHost, jmxPort);
        JMXServiceURL serviceURL = null;
        try {
            serviceURL = new JMXServiceURL(rawUrl);
        } catch (MalformedURLException e) {
            System.exit(1);
        }
        Connection con = new Connection(rawUrl, jmxHost, Integer.valueOf(jmxPort), serviceURL, user, passwd, service);

        try {
            System.out.println(con.readJvmData());
        } catch (Exception e) {
            System.exit(1);
        }
    }

}