JMXMonitor.java
1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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) {
try {
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) {
e.printStackTrace();
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) {
e.printStackTrace();
System.exit(1);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}