服务监控

服务器监控主要包括以下几个方面‌:
‌CPU使用率‌:监控服务器的CPU使用情况,确保没有过载
‌内存使用‌:监控服务器的内存使用情况,避免内存溢出
‌磁盘使用‌:监控磁盘空间,确保有足够的存储空间
‌网络吞吐量:监控网络单位时间内成功地传送数据的数量

代码实现

获取内存信息

public void getCpuMessage() {
	OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
	double cpuAverageUsedRatio = osBean.getSystemCpuLoad();
	//CPU使用率
	double cpuValue = (cpuAverageUsedRatio * 100);
}

获取CPU核心数

public void getCpuMessage() {
	//获取Runtime实例
	Runtime runtime = Runtime.getRuntime();
	//获取CPU核心数
	int coreCount = runtime.availableProcessors();
}

获取内存相关信息

public void getMemoryMessage() {
	//获取MemoryMXBean实例
	MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
	//获取内存使用情况
	MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage();
	MemoryUsage nonHeapUsage = memoryBean.getNonHeapMemoryUsage();
	//内存总数
	long totalMemory = heapUsage.getMax() + nonHeapUsage.getMax();
	//内存使用数
	long usedMemory = heapUsage.getUsed() + nonHeapUsage.getUsed();
	//内存使用率
	double memoryUsagePercentage = (double) usedMemory / totalMemory * 100;
}

磁盘分区信息

public static List<String> getPartitionInfo() {
	List<String> partitionInfoList = new ArrayList<>();
	File[] roots = File.listRoots();
	for (File root : roots) {
		totalSpace += root.getTotalSpace();
        totalFreeSpace += root.getFreeSpace();
	    partitionInfoList.add("根目录:" + root);
	    partitionInfoList.add("可用空间:" + root.getFreeSpace() + " 字节");
	    partitionInfoList.add("总空间:" + root.getTotalSpace() + " 字节");
	    partitionInfoList.add("可用空间百分比:" + (root.getFreeSpace() / (double) root.getTotalSpace()) * 100 + "%");
	}
	partitionInfoList.add("磁盘总量:" + totalSpace + " 字节");
    partitionInfoList.add("磁盘可用空间总量:" + totalFreeSpace + " 字节");
	return partitionInfoList;
}

获取网络吞吐量

private static void printNetworkThroughput(OperatingSystemMXBean osBean) throws Exception {
    //接收总字节数
    long sumReceiveBytes = 0;
    //发送总字节数
    long sumSendBytes = 0;
    try {
        //记录当前时间
        long startTime = System.currentTimeMillis();
        //使用命令的形式获取流量信息
        Process process = Runtime.getRuntime().exec("cat /proc/net/dev");
        InputStream inputStream;
        BufferedReader bufferedReader;
        
        inputStream = process.getInputStream();
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (line.trim().startsWith("Inter") || line.trim().startsWith("face")) {
                continue;
            }
            //解析输出数据
            String[] parts = line.trim().split("\\s+");
            //接收字节数
            long rxBytesStart = Long.parseLong(parts[1]);
            sumReceiveBytes -= rxBytesStart;
            //发送字节数
            long txBytesStart = Long.parseLong(parts[9]);
            sumSendBytes -= txBytesStart;
        }
        
		//数据采集时间间隔
        Thread.sleep(100);

        //记录当前时间
        long endTime = System.currentTimeMillis();
        process = Runtime.getRuntime().exec("cat /proc/net/dev");
        inputStream = process.getInputStream();
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        while ((line = bufferedReader.readLine()) != null) {
            if (line.trim().startsWith("Inter") || line.trim().startsWith("face")) {
                continue;
            }
            //解析输出数据
            String[] parts = line.trim().split("\\s+");
            //接收字节数
            long rxBytesEnd = Long.parseLong(parts[1]);
            sumReceiveBytes += rxBytesEnd;
            //发送字节数
            long txBytesEnd = Long.parseLong(parts[9]);
            sumSendBytes += txBytesEnd;
        }
        //接收与发送字节数总和
        long sumBytes = sumReceiveBytes + sumSendBytes;
        //花费时间
        long spendTime = endTime - startTime;
        //计算平均bps;吞吐量
        double bps = sumBytes * 8 * 1000 / (double) spendTime;
        //等待命令执行完成
        process.waitFor();
        //关闭流
        bufferedReader.close();
        inputStream.close();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐