add the Monitor about linux os

This commit is contained in:
coderfengyun 2013-07-25 18:19:02 +08:00
parent c7fd4970d1
commit 4851a07144
3 changed files with 259 additions and 89 deletions

View File

@ -37,6 +37,11 @@
<artifactId>jackson-mapper-asl</artifactId> <artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version> <version>1.9.12</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>2.4</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -1,6 +1,7 @@
package org.bench4q.monitor; package org.bench4q.monitor;
import java.io.File; import java.io.File;
import java.util.Properties;
import org.bench4q.monitor.performance.windows.LogicalDiskMonitor; import org.bench4q.monitor.performance.windows.LogicalDiskMonitor;
import org.bench4q.monitor.performance.windows.MemoryMonitor; import org.bench4q.monitor.performance.windows.MemoryMonitor;
@ -58,113 +59,123 @@ public class Main {
} }
public static void main(String[] args) { public static void main(String[] args) {
LogicalDiskMonitor logicalDiskMonitor = new LogicalDiskMonitor(); Properties prop = System.getProperties();
String[] logicalDiskInstances = logicalDiskMonitor.getInstances();
for (String elem : logicalDiskInstances) {
System.out.println(elem);
}
String[] logicalDiskCounter = logicalDiskMonitor.getCounterList();
for (String elem : logicalDiskCounter) {
System.out.println(elem);
}
System.out.println(logicalDiskMonitor.getFreeMegabytes("_Total"));
MemoryMonitor memoryMonitor = new MemoryMonitor(); String osName = prop.getProperty("os.name");
String[] memoryCounter = memoryMonitor.getCounterList(); System.out.println(osName);
for (String elem : memoryCounter) {
System.out.println(elem);
}
System.out.println(memoryMonitor.getAvailableBytes());
System.out.println(memoryMonitor.getAvailableKiloBytes());
System.out.println(memoryMonitor.getAvailableMegaBytes());
NetworkInterfaceMonitor networkInterfaceMonitor = new NetworkInterfaceMonitor(); if (osName.substring(0, 3).equalsIgnoreCase("win")) {
String[] networkInterfaceInstances = networkInterfaceMonitor LogicalDiskMonitor logicalDiskMonitor = new LogicalDiskMonitor();
.getInstances(); String[] logicalDiskInstances = logicalDiskMonitor.getInstances();
for (String elem : networkInterfaceInstances) { for (String elem : logicalDiskInstances) {
System.out.println(elem); System.out.println(elem);
System.out.println("bandwidth:" }
+ networkInterfaceMonitor.getCurrentBandwidth(elem)); String[] logicalDiskCounter = logicalDiskMonitor.getCounterList();
} for (String elem : logicalDiskCounter) {
System.out.println(elem);
}
System.out.println(logicalDiskMonitor.getFreeMegabytes("_Total"));
String[] networkInterfaceCounter = networkInterfaceMonitor MemoryMonitor memoryMonitor = new MemoryMonitor();
.getCounterList(); String[] memoryCounter = memoryMonitor.getCounterList();
for (String elem : networkInterfaceCounter) { for (String elem : memoryCounter) {
System.out.println(elem); System.out.println(elem);
} }
System.out.println(memoryMonitor.getAvailableBytes());
System.out.println(memoryMonitor.getAvailableKiloBytes());
System.out.println(memoryMonitor.getAvailableMegaBytes());
PhysicalDiskMonitor physicalDiskMonitor = new PhysicalDiskMonitor(); NetworkInterfaceMonitor networkInterfaceMonitor = new NetworkInterfaceMonitor();
String[] physicalDiskInstances = physicalDiskMonitor.getInstances(); String[] networkInterfaceInstances = networkInterfaceMonitor
for (String elem : physicalDiskInstances) { .getInstances();
System.out.println(elem); for (String elem : networkInterfaceInstances) {
} System.out.println(elem);
System.out.println("bandwidth:"
+ networkInterfaceMonitor.getCurrentBandwidth(elem));
}
String[] physicalDiskCounter = physicalDiskMonitor.getCounterList(); String[] networkInterfaceCounter = networkInterfaceMonitor
for (String elem : physicalDiskCounter) { .getCounterList();
System.out.println(elem); for (String elem : networkInterfaceCounter) {
} System.out.println(elem);
}
ProcessMonitor processMonitor = new ProcessMonitor(); PhysicalDiskMonitor physicalDiskMonitor = new PhysicalDiskMonitor();
String[] processInstances = processMonitor.getInstances(); String[] physicalDiskInstances = physicalDiskMonitor.getInstances();
for (String elem : processInstances) { for (String elem : physicalDiskInstances) {
System.out.println(elem); System.out.println(elem);
} }
String[] processCounter = processMonitor.getCounterList(); String[] physicalDiskCounter = physicalDiskMonitor.getCounterList();
for (String elem : processCounter) { for (String elem : physicalDiskCounter) {
System.out.println(elem); System.out.println(elem);
} }
ProcessorInformationMonitor processorInformationMonitor = new ProcessorInformationMonitor(); ProcessMonitor processMonitor = new ProcessMonitor();
String[] processorInformationInstances = processorInformationMonitor String[] processInstances = processMonitor.getInstances();
.getInstances(); for (String elem : processInstances) {
for (String elem : processorInformationInstances) { System.out.println(elem);
System.out.println(elem); }
}
String[] processorInformationCounter = processorInformationMonitor String[] processCounter = processMonitor.getCounterList();
.getCounterList(); for (String elem : processCounter) {
for (String elem : processorInformationCounter) { System.out.println(elem);
System.out.println(elem); }
}
ProcessorMonitor processorMonitor = new ProcessorMonitor(); ProcessorInformationMonitor processorInformationMonitor = new ProcessorInformationMonitor();
String[] processorInstances = processorMonitor.getInstances(); String[] processorInformationInstances = processorInformationMonitor
for (String elem : processorInstances) { .getInstances();
System.out.println(elem); for (String elem : processorInformationInstances) {
} System.out.println(elem);
}
String[] processorCounter = processorMonitor.getCounterList(); String[] processorInformationCounter = processorInformationMonitor
for (String elem : processorCounter) { .getCounterList();
System.out.println(elem); for (String elem : processorInformationCounter) {
} System.out.println(elem);
}
TCPv4Monitor tcpv4Monitor = new TCPv4Monitor(); ProcessorMonitor processorMonitor = new ProcessorMonitor();
String[] tcpv4Counter = tcpv4Monitor.getCounterList(); String[] processorInstances = processorMonitor.getInstances();
for (String elem : tcpv4Counter) { for (String elem : processorInstances) {
System.out.println(elem); System.out.println(elem);
} }
TCPv6Monitor tcpv6Monitor = new TCPv6Monitor(); String[] processorCounter = processorMonitor.getCounterList();
String[] tcpv6Counter = tcpv6Monitor.getCounterList(); for (String elem : processorCounter) {
for (String elem : tcpv6Counter) { System.out.println(elem);
System.out.println(elem); }
}
UDPv4Monitor udpv4Monitor = new UDPv4Monitor(); TCPv4Monitor tcpv4Monitor = new TCPv4Monitor();
String[] udpv4Counter = udpv4Monitor.getCounterList(); String[] tcpv4Counter = tcpv4Monitor.getCounterList();
for (String elem : udpv4Counter) { for (String elem : tcpv4Counter) {
System.out.println(elem); System.out.println(elem);
} }
UDPv6Monitor udpv6Monitor = new UDPv6Monitor(); TCPv6Monitor tcpv6Monitor = new TCPv6Monitor();
String[] udpv6Counter = udpv6Monitor.getCounterList(); String[] tcpv6Counter = tcpv6Monitor.getCounterList();
for (String elem : udpv6Counter) { for (String elem : tcpv6Counter) {
System.out.println(elem); System.out.println(elem);
}
UDPv4Monitor udpv4Monitor = new UDPv4Monitor();
String[] udpv4Counter = udpv4Monitor.getCounterList();
for (String elem : udpv4Counter) {
System.out.println(elem);
}
UDPv6Monitor udpv6Monitor = new UDPv6Monitor();
String[] udpv6Counter = udpv6Monitor.getCounterList();
for (String elem : udpv6Counter) {
System.out.println(elem);
}
} else {
OSUtils.main(args);
} }
MonitorServer monitorServer = new MonitorServer(5555); MonitorServer monitorServer = new MonitorServer(5555);
monitorServer.start(); monitorServer.start();
return;
} }
} }

View File

@ -0,0 +1,154 @@
package org.bench4q.monitor;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.io.FileSystemUtils;
public class OSUtils {
public static int disk() {
try {
long total = FileSystemUtils.freeSpaceKb("/home");
double disk = (double) total / 1024 / 1024;
return (int) disk;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
public static int cpuUsage() {
try {
Map<?, ?> map1 = OSUtils.cpuinfo();
Thread.sleep(5 * 1000);
Map<?, ?> map2 = OSUtils.cpuinfo();
long user1 = Long.parseLong(map1.get("user").toString());
long nice1 = Long.parseLong(map1.get("nice").toString());
long system1 = Long.parseLong(map1.get("system").toString());
long idle1 = Long.parseLong(map1.get("idle").toString());
long user2 = Long.parseLong(map2.get("user").toString());
long nice2 = Long.parseLong(map2.get("nice").toString());
long system2 = Long.parseLong(map2.get("system").toString());
long idle2 = Long.parseLong(map2.get("idle").toString());
long total1 = user1 + system1 + nice1;
long total2 = user2 + system2 + nice2;
float total = total2 - total1;
long totalIdle1 = user1 + nice1 + system1 + idle1;
long totalIdle2 = user2 + nice2 + system2 + idle2;
float totalidle = totalIdle2 - totalIdle1;
float cpusage = (total / totalidle) * 100;
return (int) cpusage;
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
}
public static Map<?, ?> cpuinfo() {
InputStreamReader inputs = null;
BufferedReader buffer = null;
Map<String, Object> map = new HashMap<String, Object>();
try {
inputs = new InputStreamReader(new FileInputStream("/proc/stat"));
buffer = new BufferedReader(inputs);
String line = "";
while (true) {
line = buffer.readLine();
if (line == null) {
break;
}
if (line.startsWith("cpu")) {
StringTokenizer tokenizer = new StringTokenizer(line);
List<String> temp = new ArrayList<String>();
while (tokenizer.hasMoreElements()) {
String value = tokenizer.nextToken();
temp.add(value);
}
map.put("user", temp.get(1));
map.put("nice", temp.get(2));
map.put("system", temp.get(3));
map.put("idle", temp.get(4));
map.put("iowait", temp.get(5));
map.put("irq", temp.get(6));
map.put("softirq", temp.get(7));
map.put("stealstolen", temp.get(8));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
inputs.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return map;
}
public static int memoryUsage() {
Map<String, Object> map = new HashMap<String, Object>();
InputStreamReader inputs = null;
BufferedReader buffer = null;
try {
inputs = new InputStreamReader(new FileInputStream("/proc/meminfo"));
buffer = new BufferedReader(inputs);
String line = "";
while (true) {
line = buffer.readLine();
if (line == null)
break;
int beginIndex = 0;
int endIndex = line.indexOf(":");
if (endIndex != -1) {
String key = line.substring(beginIndex, endIndex);
beginIndex = endIndex + 1;
endIndex = line.length();
String memory = line.substring(beginIndex, endIndex);
String value = memory.replace("kB", "").trim();
map.put(key, value);
}
}
long memTotal = Long.parseLong(map.get("MemTotal").toString());
long memFree = Long.parseLong(map.get("MemFree").toString());
long memused = memTotal - memFree;
long buffers = Long.parseLong(map.get("Buffers").toString());
long cached = Long.parseLong(map.get("Cached").toString());
double usage = (double) (memused - buffers - cached) / memTotal
* 100;
return (int) usage;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
inputs.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return 0;
}
public static void main(String[] args) {
System.out.println(OSUtils.disk());
System.out.println(OSUtils.cpuUsage());
System.out.println(OSUtils.memoryUsage());
System.out.println(OSUtils.cpuinfo());
}
}