liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.jeeplus.modules.monitor.utils;
 
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
 
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.Swap;
 
public class SystemInfo {
    public static Map SystemProperty() {
        Map<String, Comparable> monitorMap = new HashMap();
        Runtime r = Runtime.getRuntime();
        Properties props = System.getProperties();
        InetAddress addr = null;
        String ip = "";
        String hostName = "";
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            ip = "无法获取主机IP";
            hostName = "无法获取主机名";
        }
        if (null != addr) {
            try {
                ip = addr.getHostAddress();
            } catch (Exception e) {
                ip = "无法获取主机IP";
            }
            try {
                hostName = addr.getHostName();
            } catch (Exception e) {
                hostName = "无法获取主机名";
            }
        }
        monitorMap.put("hostIp", ip);// 本地ip地址
        monitorMap.put("hostName", hostName);// 本地主机名
        monitorMap.put("osName", props.getProperty("os.name"));// 操作系统的名称
        monitorMap.put("arch", props.getProperty("os.arch"));// 操作系统的构架
        monitorMap.put("osVersion", props.getProperty("os.version"));// 操作系统的版本
        monitorMap.put("processors", r.availableProcessors());// JVM可以使用的处理器个数
        monitorMap.put("javaVersion", props.getProperty("java.version"));// Java的运行环境版本
        monitorMap.put("vendor", props.getProperty("java.vendor"));// Java的运行环境供应商
        monitorMap.put("javaUrl", props.getProperty("java.vendor.url"));// Java供应商的URL
        monitorMap.put("javaHome", props.getProperty("java.home"));// Java的安装路径
        monitorMap.put("tmpdir", props.getProperty("java.io.tmpdir"));// 默认的临时文件路径
        return monitorMap;
    }
 
    public static Map memory(Sigar sigar) {
        Map<String, Object> monitorMap = new HashMap();
        try {
            Runtime r = Runtime.getRuntime();
            monitorMap.put("jvmTotal", Common.div(r.totalMemory(), (1024 * 1024), 2) + "M");// java总内存
            monitorMap.put("jvmUse", Common.div(r.totalMemory() - r.freeMemory(), (1024 * 1024), 2) + "M");// JVM使用内存
            monitorMap.put("jvmFree", Common.div(r.freeMemory(), (1024 * 1024), 2) + "M");// JVM剩余内存
            monitorMap.put("jvmUsage", Common.div(r.totalMemory() - r.freeMemory(), r.totalMemory(), 2));// JVM使用率
 
            Mem mem = sigar.getMem();
            // 内存总量
            monitorMap.put("ramTotal", Common.div(mem.getTotal(), (1024 * 1024 * 1024), 2) + "G");// 内存总量
            monitorMap.put("ramUse", Common.div(mem.getUsed(), (1024 * 1024 * 1024), 2) + "G");// 当前内存使用量
            monitorMap.put("ramFree", Common.div(mem.getFree(), (1024 * 1024 * 1024), 2) + "G");// 当前内存剩余量
            monitorMap.put("ramUsage", Common.div(mem.getUsed(), mem.getTotal(), 2));// 内存使用率
 
            Swap swap = sigar.getSwap();
            // 交换区总量
            monitorMap.put("swapTotal", Common.div(swap.getTotal(), (1024 * 1024 * 1024), 2) + "G");
            // 当前交换区使用量
            monitorMap.put("swapUse", Common.div(swap.getUsed(), (1024 * 1024 * 1024), 2) + "G");
            // 当前交换区剩余量
            monitorMap.put("swapFree", Common.div(swap.getFree(), (1024 * 1024 * 1024), 2) + "G");
            monitorMap.put("swapUsage", Common.div(swap.getUsed(), swap.getTotal(), 2));//
 
        } catch (Exception e) {
        }
        return monitorMap;
    }
    public static Map usage(Sigar sigar) {
        Map<String, Long> monitorMap = new HashMap();
        try {
            Runtime r = Runtime.getRuntime();
            monitorMap.put("jvmUsage", Math.round(Common.div(r.totalMemory()-r.freeMemory(), r.totalMemory(), 2)*100));// JVM使用率
 
            Mem mem = sigar.getMem();
            // 内存总量
            monitorMap.put("ramUsage", Math.round(Common.div(mem.getUsed(), mem.getTotal(), 2)*100));// 内存使用率
 
             List<Map> cpu = cpuInfos(sigar);
            double b = 0.0;
            for (Map m : cpu) {
                b += Double.valueOf(m.get("cpuTotal")+"");
            }
            monitorMap.put("cpuUsage", Math.round(b/cpu.size()));// cpu使用率
        } catch (Exception e) {
        }
        return monitorMap;
    }
 
    public static List<Map> cpuInfos(Sigar sigar) {
        List<Map> monitorMaps = new ArrayList<Map>();
        try {
            CpuPerc cpuList[] = sigar.getCpuPercList();
            for (CpuPerc cpuPerc : cpuList) {
                Map<String, Comparable> monitorMap = new HashMap();
                monitorMap.put("cpuUserUse", Math.round(cpuPerc.getUser()*100));// 用户使用率
                monitorMap.put("cpuSysUse", Math.round(cpuPerc.getSys()*100));// 系统使用率
                monitorMap.put("cpuWait", Math.round(cpuPerc.getWait()*100));// 当前等待率
                monitorMap.put("cpuFree", Math.round(cpuPerc.getIdle()*100));// 当前空闲率
                monitorMap.put("cpuTotal",Math.round(cpuPerc.getCombined()*100));// 总的使用率
                monitorMaps.add(monitorMap);
            }
        } catch (Exception e) {
        }
        return monitorMaps;
    }
 
    public List<Map> diskInfos(Sigar sigar) throws Exception {
        List<Map> monitorMaps = new ArrayList<Map>();
        FileSystem fslist[] = sigar.getFileSystemList();
        for (int i = 0; i < fslist.length; i++) {
            Map<Object, Object> monitorMap = new HashMap();
            FileSystem fs = fslist[i];
            // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等
            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 : 本地硬盘
 
                monitorMap.put("diskName", fs.getDevName());// 系统盘名称
                monitorMap.put("diskType", fs.getSysTypeName());// 盘类型
                // 文件系统总大小
                monitorMap.put("diskTotal", fs.getSysTypeName());
                // 文件系统剩余大小
                monitorMap.put("diskFree", usage.getFree());
                // 文件系统已经使用量
                monitorMap.put("diskUse", usage.getUsed());
                double usePercent = usage.getUsePercent() * 100D;
                // 文件系统资源的利用率
                monitorMap.put("diskUsage", usePercent);// 内存使用率
                monitorMaps.add(monitorMap);
                break;
            case 3:// TYPE_NETWORK :网络
                break;
            case 4:// TYPE_RAM_DISK :闪存
                break;
            case 5:// TYPE_CDROM :光驱
                break;
            case 6:// TYPE_SWAP :页面交换
                break;
            }
        }
        return monitorMaps;
    }
 
}