import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.alibaba.druid.support.http.stat.WebAppStatManager;
public class SystemMonitorHandler {
@XxlJob(value = "systemIndexMonitor")
public ReturnT<String> systemIndexMonitor(String params) throws IOException {
//获取服务指标
//获取系统服务器内存使用率
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long totalPhysicalMemorySize = osmxb.getTotalPhysicalMemorySize();
long usedMemory = osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize();
String memoryUsedRate = new DecimalFormat("#.##").format((double) usedMemory / (double) totalPhysicalMemorySize);
//cpu使用率
String osJson = JSON.toJSONString(osmxb);
JSONObject jsonObject = JSON.parseObject(osJson);
String systemCpuLoad = new DecimalFormat("#.##").format(jsonObject.getDouble("systemCpuLoad"));
//线程活跃数
ThreadMXBean thread = ManagementFactory.getThreadMXBean();
int threadLiveSize = thread.getThreadCount();
//请求错误数
int requestErrorCount = 0;
//平均响应数
String requestAverageTime = "0.00";
//请求总数
int requestCount = 0;
//jdbc活跃连接数
int activeCount = 0;
//Session数
int httpSessionCount = 0;
//被使用的jdbc数
int physicalConnectCount = 0;
try {
List<Map<String, Object>> webAppStatData = WebAppStatManager.getInstance().getWebAppStatData();
List<Map<String, Object>> uriStatData = WebAppStatManager.getInstance().getURIStatData();
List<Map<String, Object>> dataSourceStatData = DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
if (!webAppStatData.isEmpty()) {
requestCount = (int) webAppStatData.get(0).get("RequestCount");
httpSessionCount= (int) webAppStatData.get(0).get("SessionCount");
}
if (!uriStatData.isEmpty()) {
int requestTime = 0;
for (Map<String, Object> uriStatDatum : uriStatData) {
requestErrorCount += (Integer) uriStatDatum.get("ErrorCount");
requestTime += (Integer) uriStatDatum.get("RequestTimeMillis");
}
requestAverageTime = new DecimalFormat("#.##").format(requestTime / requestCount);
}
if (!dataSourceStatData.isEmpty()) {
activeCount = dataSourceStatData.get(0).get("ActiveCount") == null ? 0 : (int) dataSourceStatData.get(0).get("ActiveCount");
physicalConnectCount = dataSourceStatData.get(0).get("PhysicalConnectCount") == null ? 0 : (int) dataSourceStatData.get(0).get("PhysicalConnectCount");
}
} catch (Exception e) {
e.printStackTrace();
}
//磁盘信息
//获取系统环境
String osName = System.getProperty("os.name");
if (osName.startsWith("Windows")) {
osName = "windows";
//设置保留位数
DecimalFormat df=new DecimalFormat("0.00");
//获取电脑所有磁盘信息
File[] roots = File.listRoots();
for (File file : roots) {
long totalSpace = file.getTotalSpace();
long freeSpace = file.getFreeSpace();
long usedSpace = totalSpace - freeSpace;
//转化占用磁盘大小
float totalSpaceG = Float.parseFloat(df.format((float) totalSpace / 1024 / 1024 / 1024));
float freeSpaceG = Float.parseFloat(df.format((float) freeSpace / 1024 / 1024 / 1024));
float usedSpaceG = Float.parseFloat(df.format((float) usedSpace / 1024 / 1024 / 1024));
log.info(file + " 容量 : " + totalSpaceG + "G, 可用 : " + freeSpaceG + "G, 已用 : " + usedSpaceG + "G");
}
} else {
osName = "linux";
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");
try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String str;
int line = 0;
while ((str = in.readLine()) != null) {
line++;
if (line == 1) {
continue;
}
str = str.replaceAll("\\s+", " ");
String[] strArray = str.split(" ");
log.info("文件系统 " + strArray[0] + ", 容量 " + strArray[1] + ", 已用 " + strArray[2] + ", 可用 " + strArray[3] + ", 占比 " + strArray[4] + ", 挂载目录 " + strArray[5]);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
log.info("当前服务运行系统为=> " + osName);
//3.将数据信息封装到SysMonitor对象并插入数据库
log.info("memoryUsedRate:" + memoryUsedRate);
log.info("requestCount:" + requestCount);
log.info("activeCount:" + activeCount);
log.info("physicalConnectCount:" + physicalConnectCount);
log.info("cpuUsageSize:" + systemCpuLoad);
log.info("threadLiveSize:" + threadLiveSize);
log.info("requestErrorCount:" + requestErrorCount);
log.info("httpSessionCount:" + httpSessionCount);
log.info("requestAverageTime:" + requestAverageTime);
return ReturnT.SUCCESS;
}
}
原文地址:http://www.cnblogs.com/qwer78/p/16829112.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性