3. 创建MonitorClient.java, 代码如下所示
public class MonitorClient {
// 表名为jvmMonitor
public
final static String TABLE_NAME = "jvmMonitor";
//列族名 "target", "memory",
"os"
public
final static String[] COLUM_FAMILIES = new String[]{"target",
"memory", "os"};
private MBeanServerConnection
connect;
private
HTable htable;
private
String serverIP; //Web服务器IP
private
int serverPort; //Web服务器端口
private
String userName; //Web服务器用户名
private
String password; //Web服务器密码
private
HBaseAdmin admin;
private
String partRowKey; //行健的前半部分
public
static void main(String[] args) throws Exception
{
//
为了方便测试,将所监控Web服务器的IP,端口,用户名,密码写死在主函数中。
args = new
String[4];
args[0] =
"192.168.1.107";
args[1] =
"8686";
args[2] =
"admin";
args[3] =
"adminadmin";
MonitorClient monitorClient =
new MonitorClient();
monitorClient.setServerIP(args[0]);
monitorClient.setServerPort(Integer.valueOf(args[1]));
monitorClient.setUserName(args[2]);
monitorClient.setPassword(args[3]);
monitorClient.doMonitor();
}
public
void doMonitor() {
try {
//
创建表
this.createTable();
while (true) {
//建立到web服务器的JMX协议连接
if
(this.connect == null) {
this.connect =
getJMXConnector(serverIP, serverPort, userName,
password);
}
//插入数据
this.insertData();
//暂停10秒后继续监控
TimeUnit.SECONDS.sleep(10);
}
} catch (Exception ex)
{
Logger.getLogger(MonitorClient.class.getName()).log(Level.SEVERE,
null, ex);
} finally
{
//若发生异常关闭到HBase连接
if (this.htable != null) {
try
{
this.htable.close();
} catch
(Exception ex) {
}
}
if (this.admin != null) {
try
{
this.admin.close();
} catch
(Exception ex) {
}
}
}
}
//
方法 getJMXConnector 用于建立到Web
服务器的连接
private
MBeanServerConnection getJMXConnector(String serverIP, int
serverPort, String userName, String password) throws Exception
{
String jmxURL =
"service:jmx:rmi:///jndi/rmi://" + serverIP + ":" + serverPort +
"/jmxrmi";
final JMXServiceURL target =
new JMXServiceURL(jmxURL);
Map env = new
HashMap();
env.put(JMXConnector.CREDENTIALS, new String[]{userName,
password});
JMXConnector jmxConnector =
JMXConnectorFactory.connect(target, env);
System.out.println("
---successful to connect to " + this.serverIP + ":" +
this.serverPort + " ---");
return
jmxConnector.getMBeanServerConnection();
}
//
在HBase中建立表,如果已存在则不再创建
private
void createTable() throws Exception {
boolean isExist =
false;
Configuration conf =
HBaseConfiguration.create();
admin = new
HBaseAdmin(conf);
HTableDescriptor[] tables =
admin.listTables();
for (HTableDescriptor eachTtd
: tables) {
if
(TABLE_NAME.equals(eachTtd.getNameAsString()))
{
isExist =
true;
System.out.println(" --- table " + TABLE_NAME + "
exist---");
break;
}
}
if (!isExist)
{
HTableDescriptor htd = new
HTableDescriptor(TABLE_NAME);
HColumnDescriptor hcd0 = new
HColumnDescriptor(COLUM_FAMILIES[0]);
HColumnDescriptor hcd1 = new
HColumnDescriptor(COLUM_FAMILIES[1]);
HColumnDescriptor hcd2 = new
HColumnDescriptor(COLUM_FAMILIES[2]);
htd.addFamily(hcd0);
htd.addFamily(hcd1);
htd.addFamily(hcd2);
admin.createTable(htd);
System.out.println(" ---successful to create
table " + TABLE_NAME + " ---");
}
this.htable = new
HTable(conf, TABLE_NAME);
}
//
从Web服务器从获取信息,并插入表中
private
void insertData() throws Exception {
final MemoryMXBean mxBean =
ManagementFactory.newPlatformMXBeanProxy(
connect,
ManagementFactory.MEMORY_MXBEAN_NAME,
MemoryMXBean.class);
// JVM
内存信息
MemoryUsage muh =
mxBean.getHeapMemoryUsage();
long heapUsedKB =
muh.getUsed() / 1024;
long heapMaxKB = muh.getMax()
/ 1024;
long heapCommitKB =
muh.getCommitted() / 1024;
long heapUtil = (heapUsedKB *
100L) / heapCommitKB;
MemoryUsage munh =
mxBean.getNonHeapMemoryUsage();
long nonHeapUsedKB =
munh.getUsed() / 1024;
long nonHeapMaxKB =
munh.getMax() / 1024;
long nonHeapCommitKB =
munh.getCommitted() / 1024;
long nonHeapUtil =
(nonHeapUsedKB * 100L) / nonHeapCommitKB;
//服务器的操作系统信息
ObjectName objOperatingSystem
= new
ObjectName("java.lang:type=OperatingSystem");
long virtMemCommitKB = (Long)
connect.getAttribute(objOperatingSystem,
"CommittedVirtualMemorySize") / 1024;
long freePhyMemKB = (Long)
connect.getAttribute(objOperatingSystem, "FreePhysicalMemorySize")
/ 1024;
long freeSwapKB = (Long)
connect.getAttribute(objOperatingSystem, "FreeSwapSpaceSize") /
1024;
long totalPhyMemKB = (Long)
connect.getAttribute(objOperatingSystem, "TotalPhysicalMemorySize")
/ 1024;
long totalSwapKB = (Long)
connect.getAttribute(objOperatingSystem, "TotalSwapSpaceSize") /
1024;
long phyMemUtil = 100 - 100 *
freePhyMemKB / totalPhyMemKB;
long swapMemUtil = 100 - 100
* freeSwapKB / totalSwapKB;
long nanoBefore =
System.nanoTime();
long cpuBefore = (Long)
connect.getAttribute(objOperatingSystem,
"ProcessCpuTime");
TimeUnit.SECONDS.sleep(3);
long cpuAfter = (Long)
connect.getAttribute(objOperatingSystem,
"ProcessCpuTime");
long nanoAfter =
System.nanoTime();
long
cpuUtil;
if (nanoAfter
> nanoBefore) {
cpuUtil = ((cpuAfter - cpuBefore) * 100L) /
(nanoAfter - nanoBefore);
} else {
cpuUtil = 0;
}
String rowKey =
this.makeRowKey();
byte[] row =
Bytes.toBytes(rowKey);
byte[] bytesTarget =
Bytes.toBytes(COLUM_FAMILIES[0]);
byte[] bytesMemory =
Bytes.toBytes(COLUM_FAMILIES[1]);
byte[] bytesOS =
Bytes.toBytes(COLUM_FAMILIES[2]);
Put put = new
Put(row);
//插入Web服务器IP、端口信息
put.add(bytesTarget,
Bytes.toBytes("serverIP"),
Bytes.toBytes(String.valueOf(this.serverIP)));
put.add(bytesTarget,
Bytes.toBytes("serverPort"),
Bytes.toBytes(String.valueOf(this.serverPort)));
//插入Web服务器JVM内存信息
put.add(bytesMemory,
Bytes.toBytes("heapUsedKB"),
Bytes.toBytes(String.valueOf(heapUsedKB)));
put.add(bytesMemory,
Bytes.toBytes("heapMaxKB"),
Bytes.toBytes(String.valueOf(heapMaxKB)));
put.add(bytesMemory,
Bytes.toBytes("heapCommitKB"),
Bytes.toBytes(String.valueOf(heapCommitKB)));
put.add(bytesMemory,
Bytes.toBytes("heapUtil"),
Bytes.toBytes(String.valueOf(heapUtil)));
put.add(bytesMemory,
Bytes.toBytes("nonHeapUsedKB"),
Bytes.toBytes(String.valueOf(nonHeapUsedKB)));
put.add(bytesMemory,
Bytes.toBytes("nonHeapMaxKB"),
Bytes.toBytes(String.valueOf(nonHeapMaxKB)));
put.add(bytesMemory,
Bytes.toBytes("nonHeapCommitKB"),
Bytes.toBytes(String.valueOf(nonHeapCommitKB)));
put.add(bytesMemory,
Bytes.toBytes("nonHeapUtil"),
Bytes.toBytes(String.valueOf(nonHeapUtil)));
//插入Web服务器操作系统信息
put.add(bytesOS,
Bytes.toBytes("virtMemCommitKB"),
Bytes.toBytes(String.valueOf(virtMemCommitKB)));
put.add(bytesOS,
Bytes.toBytes("freePhyMemKB"),
Bytes.toBytes(String.valueOf(freePhyMemKB)));
put.add(bytesOS,
Bytes.toBytes("freeSwapKB"),
Bytes.toBytes(String.valueOf(freeSwapKB)));
put.add(bytesOS,
Bytes.toBytes("totalSwapKB"),
Bytes.toBytes(String.valueOf(totalSwapKB)));
put.add(bytesOS,
Bytes.toBytes("phyMemUtil"),
Bytes.toBytes(String.valueOf(phyMemUtil)));
put.add(bytesOS,
Bytes.toBytes("swapMemUtil"),
Bytes.toBytes(String.valueOf(swapMemUtil)));
put.add(bytesOS,
Bytes.toBytes("cpuUtil"),
Bytes.toBytes(String.valueOf(cpuUtil)));
htable.put(put);
htable.flushCommits();
System.out.println(" ---
insert one row : " + rowKey + "
---");
}
//
生成行健,行健用Web服务器IP和数据采集的时间来表示,共24位,前12位是IP,后12位是时间,不够的在前面用0填充
// 例如:IP为
10.1.2.122,时间为2012-11-25
23:01:08,则行健为01000100212220121125230108
private
void makePartRowKey() {
String[] splits =
this.serverIP.split("[.]");
for (int i
=0;i
String s = splits[i];
int len = s.length();
while (len < 3)
{
s="0"+s;
len
++;
}
splits[i] = s;
}
this.partRowKey = splits[0]+
splits[1] + splits[2] + splits[3] ;
}
//
根据Web服务器IP生成行健的前半部分
private
String makeRowKey() {
if (this.partRowKey == null)
{
this.makePartRowKey();
}
SimpleDateFormat sdf = new
SimpleDateFormat("yyyyMMddHHmmss");
Date date = new
Date();
String formatData =
sdf.format(date);
return this.partRowKey +
formatData;
}
// 以下为Getter
Setter
public
String getServerIP() {
return
serverIP;
}
public
void setServerIP(String serverIP) {
this.serverIP =
serverIP;
}
public int
getServerPort() {
return
serverPort;
}
public
void setServerPort(int serverPort) {
this.serverPort =
serverPort;
}
public
String getUserName() {
return
userName;
}
public
void setUserName(String userName) {
this.userName =
userName;
}
public
String getPassword() {
return
password;
}
public
void setPassword(String password) {
this.password =
password;
}
}
程序运行的控制台打印结果:
..............................
12/11/25 22:58:15 INFO
zookeeper.ZooKeeper: Client
environment:java.compiler=<NA>
12/11/25 22:58:15 INFO
zookeeper.ZooKeeper: Client environment:os.name=Windows
7
12/11/25 22:58:15 INFO
zookeeper.ZooKeeper: Client environment:os.arch=x86
12/11/25 22:58:15 INFO
zookeeper.ZooKeeper: Client environment:os.version=6.1
12/11/25 22:58:15 INFO
zookeeper.ZooKeeper: Client
environment:user.name=hui.li
12/11/25 22:58:15 INFO
zookeeper.ZooKeeper: Client
environment:user.home=C:Usershui.li
12/11/25 22:58:15 INFO
zookeeper.ZooKeeper: Client
environment:user.dir=D:CentOS64ShareExample
12/11/25 22:58:15 INFO
zookeeper.ZooKeeper: Initiating client connection,
connectString=192.168.1.201:2181 sessionTimeout=180000
watcher=hconnection
12/11/25 22:58:15 INFO
zookeeper.ClientCnxn: Opening socket connection to server
/192.168.1.201:2181
12/11/25
22:58:15 INFO zookeeper.ClientCnxn: Socket connection established
to server1/192.168.1.201:2181, initiating session
12/11/25 22:58:15 INFO
zookeeper.ClientCnxn: Session establishment complete on server
server1/192.168.1.201:2181, sessionid = 0x3b35ddd5d4001d,
negotiated timeout = 180000
---successful to create table
jvmMonitor ---
12/11/25 22:58:16 INFO
zookeeper.ZooKeeper: Initiating client connection,
connectString=192.168.1.201:2181 sessionTimeout=180000
watcher=hconnection
12/11/25 22:58:16 INFO
zookeeper.ClientCnxn: Opening socket connection to server
/192.168.1.201:2181
12/11/25 22:58:16 INFO
zookeeper.ClientCnxn: Socket connection established to
server1/192.168.1.201:2181, initiating session
12/11/25 22:58:16 INFO
zookeeper.ClientCnxn: Session establishment complete on server
server1/192.168.1.201:2181, sessionid = 0x3b35ddd5d4001e,
negotiated timeout = 180000
---successful to connect to
192.168.1.107:8686 ---
--- insert one row :
19216800110720121125225820 ---
--- insert one row :
19216800110720121125225833 ---
--- insert one row :
19216800110720121125225846 ---
--- insert one row :
19216800110720121125225859 ---
--- insert one row :
19216800110720121125225912 ---
........................
在Hbase shell 下面查看表 jvmMonitor:
hbase(main):041:0> scan
'jvmMonitor'
........................................
19216800110720121126004237
column=memory:heapMaxKB,
timestamp=1353861709757, value=506816
19216800110720121126004237
column=memory:heapUsedKB, timestamp=1353861709757, value=76504
19216800110720121126004237
column=memory:heapUtil, timestamp=1353861709757, value=46
19216800110720121126004237
column=memory:nonHeapCommitKB, timestamp=1353861709757, value=84096
19216800110720121126004237
column=memory:nonHeapMaxKB, timestamp=1353861709757, value=229376
19216800110720121126004237
column=memory:nonHeapUsedKB, timestamp=1353861709757, value=83955
19216800110720121126004237
column=memory:nonHeapUtil, timestamp=1353861709757, value=99
19216800110720121126004237
column=os:cpuUtil, timestamp=1353861709757, value=0
19216800110720121126004237
column=os:freePhyMemKB, timestamp=1353861709757, value=960380
19216800110720121126004237
column=os:freeSwapKB, timestamp=1353861709757, value=4194303
19216800110720121126004237
column=os:phyMemUtil, timestamp=1353861709757, value=78
19216800110720121126004237
column=os:swapMemUtil, timestamp=1353861709757, value=0
19216800110720121126004237
column=os:totalSwapKB, timestamp=1353861709757, value=4194303
19216800110720121126004237
column=os:virtMemCommitKB, timestamp=1353861709757, value=293684
19216800110720121126004237
column=target:serverIP, timestamp=1353861709757,
value=192.168.1.107
19216800110720121126004237
column=target:serverPort, timestamp=1353861709757, value=8686
19216800110720121126004250
column=memory:heapCommitKB, timestamp=1353861722751, value=164192
19216800110720121126004250
column=memory:heapMaxKB, timestamp=1353861722751, value=506816
19216800110720121126004250
column=memory:heapUsedKB, timestamp=1353861722751, value=77188
19216800110720121126004250
column=memory:heapUtil, timestamp=1353861722751, value=47
19216800110720121126004250
column=memory:nonHeapCommitKB, timestamp=1353861722751, value=84096
19216800110720121126004250
column=memory:nonHeapMaxKB, timestamp=1353861722751, value=229376
19216800110720121126004250
column=memory:nonHeapUsedKB, timestamp=1353861722751, value=83955
19216800110720121126004250
column=memory:nonHeapUtil, timestamp=1353861722751, value=99
19216800110720121126004250
column=os:cpuUtil, timestamp=1353861722751, value=0
19216800110720121126004250
column=os:freePhyMemKB, timestamp=1353861722751, value=953164
19216800110720121126004250
column=os:freeSwapKB, timestamp=1353861722751, value=4194303
19216800110720121126004250
column=os:phyMemUtil, timestamp=1353861722751, value=78
19216800110720121126004250
column=os:swapMemUtil, timestamp=1353861722751, value=0
19216800110720121126004250
column=os:totalSwapKB, timestamp=1353861722751, value=4194303
19216800110720121126004250
column=os:virtMemCommitKB, timestamp=1353861722751, value=293684
19216800110720121126004250
column=target:serverIP, timestamp=1353861722751,
value=192.168.1.107
19216800110720121126004250
column=target:serverPort, timestamp=1353861722751, value=8686
483 row(s) in 7.7500
seconds