Commit 6ee38964 authored by Quxl's avatar Quxl

增加系统,JVM,内存,磁盘,网络监控组件

parent b5c01c14
...@@ -101,6 +101,11 @@ ...@@ -101,6 +101,11 @@
<groupId>org.apache.solr</groupId> <groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId> <artifactId>solr-solrj</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
package com.egolm.shop.util;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;
public class Usage {
private final Log logger = LogFactory.getLog(Usage.class);
public Map<String, ?> get() {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("System", getJvmInfo());
map.put("CPU", getCpuInfo());
map.put("Memory", getMemoryInfo());
map.put("FileSystem", getFileSystemInfo());
map.put("Network", getNetworkInfo());
return map;
}
private Map<String, Object> getJvmInfo() {
Map<String, Object> map = new LinkedHashMap<String, Object>();
try {
Runtime r = Runtime.getRuntime();
Properties props = System.getProperties();
InetAddress addr = InetAddress.getLocalHost();
Map<String, String> envMap = System.getenv();
map.put("用户名", envMap.get("USERNAME"));
map.put("计算机名", envMap.get("COMPUTERNAME"));
map.put("计算机域名", envMap.get("USERDOMAIN"));
map.put("本地IP地址", addr.getHostAddress());
map.put("本地主机名", addr.getHostName());
map.put("JVM可以使用的总内存", r.totalMemory());
map.put("JVM可以使用的剩余内存", r.freeMemory());
map.put("JVM可以使用的处理器个数", r.availableProcessors());
map.put("Java的运行环境版本", props.getProperty("java.version"));
map.put("Java的运行环境供应商", props.getProperty("java.vendor"));
map.put("Java供应商的URL", props.getProperty("java.vendor.url"));
map.put("Java的安装路径", props.getProperty("java.home"));
map.put("Java的虚拟机规范版本", props.getProperty("java.vm.specification.version"));
map.put("Java的虚拟机规范供应商", props.getProperty("java.vm.specification.vendor"));
map.put("Java的虚拟机规范名称", props.getProperty("java.vm.specification.name"));
map.put("Java的虚拟机实现版本", props.getProperty("java.vm.version"));
map.put("Java的虚拟机实现供应商", props.getProperty("java.vm.vendor"));
map.put("Java的虚拟机实现名称", props.getProperty("java.vm.name"));
map.put("Java运行时环境规范版本", props.getProperty("java.specification.version"));
map.put("Java运行时环境规范供应商", props.getProperty("java.specification.vender"));
map.put("Java运行时环境规范名称", props.getProperty("java.specification.name"));
map.put("Java的类格式版本号", props.getProperty("java.class.version"));
map.put("Java的类路径", props.getProperty("java.class.path"));
map.put("加载库时搜索的路径列表", props.getProperty("java.library.path"));
map.put("默认的临时文件路径", props.getProperty("java.io.tmpdir"));
map.put("一个或多个扩展目录的路径", props.getProperty("java.ext.dirs"));
map.put("操作系统的名称", props.getProperty("os.name"));
map.put("操作系统的构架", props.getProperty("os.arch"));
map.put("操作系统的版本", props.getProperty("os.version"));
map.put("文件分隔符", props.getProperty("file.separator"));
map.put("路径分隔符", props.getProperty("path.separator"));
map.put("行分隔符", props.getProperty("line.separator"));
map.put("用户的账户名称", props.getProperty("user.name"));
map.put("用户的主目录", props.getProperty("user.home"));
map.put("用户的当前工作目录", props.getProperty("user.dir"));
} catch (Exception e) {
logger.error("获取JVM信息失败", e);
}
return map;
}
private Map<String, Object> getMemoryInfo() {
Map<String, Object> map = new LinkedHashMap<String, Object>();
try {
Sigar sigar = new Sigar();
Mem mem = sigar.getMem();
Swap swap = sigar.getSwap();
map.put("内存总量", mem.getTotal() / 1024L + "K av");
map.put("当前内存使用量", mem.getUsed() / 1024L + "K used");
map.put("当前内存剩余量", mem.getFree() / 1024L + "K free");
map.put("交换区总量", swap.getTotal() / 1024L + "K av");
map.put("当前交换区使用量", swap.getUsed() / 1024L + "K used");
map.put("当前交换区剩余量", swap.getFree() / 1024L + "K free");
} catch (SigarException e) {
logger.error("获取内存信息失败", e);
}
return map;
}
private List<Map<String, Object>> getCpuInfo() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
Sigar sigar = new Sigar();
CpuInfo infos[] = sigar.getCpuInfoList();
CpuPerc cpuList[] = null;
cpuList = sigar.getCpuPercList();
for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
Map<String, Object> map = new HashMap<String, Object>();
CpuInfo info = infos[i];
CpuPerc cpu = cpuList[i];
map.put("CUP编号", i + 1);
map.put("CPU的总量MHz", info.getMhz());
map.put("CPU生产商", info.getVendor());
map.put("CPU类别", info.getModel());
map.put("CPU缓存数量", info.getCacheSize());
map.put("CPU用户使用率", CpuPerc.format(cpu.getUser()));
map.put("CPU系统使用率", CpuPerc.format(cpu.getSys()));
map.put("CPU当前等待率", CpuPerc.format(cpu.getWait()));
map.put("CPU当前错误率", CpuPerc.format(cpu.getNice()));
map.put("CPU当前空闲率", CpuPerc.format(cpu.getIdle()));
map.put("CPU总的使用率", CpuPerc.format(cpu.getCombined()));
map.put("CPU缓存数量", info.getCacheSize());
list.add(map);
}
} catch (Exception e) {
logger.error("获取CPU信息失败", e);
}
return list;
}
private List<Map<String, Object>> getFileSystemInfo() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
Sigar sigar = new Sigar();
FileSystem fslist[] = sigar.getFileSystemList();
for (int i = 0; i < fslist.length; i++) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("编号", i);
FileSystem fs = fslist[i];
map.put("盘符名称", fs.getDevName());
map.put("盘符路径", fs.getDirName());
map.put("盘符标志", fs.getFlags());//
map.put("文件系统", fs.getSysTypeName());
map.put("磁盘类型", fs.getTypeName());
FileSystemUsage usage = null;
usage = sigar.getFileSystemUsage(fs.getDirName());
switch (fs.getType()) {
case 0: // TYPE_UNKNOWN :未知
break;
case 1: // TYPE_NONE
break;
case 2: // TYPE_LOCAL_DISK : 本地硬盘
// 文件系统总大小
map.put(fs.getDevName() + "总大小", usage.getTotal() + "KB");
// 文件系统剩余大小
map.put(fs.getDevName() + "剩余大小", usage.getFree() + "KB");
// 文件系统可用大小
map.put(fs.getDevName() + "可用大小", usage.getAvail() + "KB");
// 文件系统已经使用量
map.put(fs.getDevName() + "已经使用量", usage.getUsed() + "KB");
double usePercent = usage.getUsePercent() * 100D;
// 文件系统资源的利用率
map.put(fs.getDevName() + "资源的利用率", usePercent + "%");
break;
case 3:// TYPE_NETWORK :网络
break;
case 4:// TYPE_RAM_DISK :闪存
break;
case 5:// TYPE_CDROM :光驱
break;
case 6:// TYPE_SWAP :页面交换
break;
}
map.put(fs.getDevName() + "读出", usage.getDiskReads());
map.put(fs.getDevName() + "写入", usage.getDiskWrites());
list.add(map);
}
} catch (Exception e) {
logger.error("获取磁盘信息失败", e);
}
return list;
}
private List<Map<String, Object>> getNetworkInfo() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
Sigar sigar = new Sigar();
String ifNames[] = sigar.getNetInterfaceList();
for (int i = 0; i < ifNames.length; i++) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
String name = ifNames[i];
NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
map.put("网络设备名", name);// 网络设备名
map.put("IP地址", ifconfig.getAddress());// IP地址
map.put("子网掩码", ifconfig.getNetmask());// 子网掩码
map.put("接收的总包裹数", ifstat.getRxPackets());// 接收的总包裹数
map.put("发送的总包裹数", ifstat.getTxPackets());// 发送的总包裹数
map.put("接收到的总字节数", ifstat.getRxBytes());// 接收到的总字节数
map.put("发送的总字节数", ifstat.getTxBytes());// 发送的总字节数
map.put("接收到的错误包数", ifstat.getRxErrors());// 接收到的错误包数
map.put("发送数据包时的错误数", ifstat.getTxErrors());// 发送数据包时的错误数
map.put("接收时丢弃的包数", ifstat.getRxDropped());// 接收时丢弃的包数
map.put("发送时丢弃的包数", ifstat.getTxDropped());// 发送时丢弃的包数
map.put("网关广播地址", ifconfig.getBroadcast());// 网关广播地址
map.put("网卡MAC地址", ifconfig.getHwaddr());// 网卡MAC地址
map.put("网卡描述信息", ifconfig.getDescription());// 网卡描述信息
map.put("网卡类型", ifconfig.getType());//
list.add(map);
}
} catch (Exception e) {
logger.error("获取网络设备信息失败", e);
}
return list;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment