java代码调出系统信息

代码很简单 不做过多说明

DecimalFormat DECIMALFORMAT = new DecimalFormat("#.##");
//系统监控
    @PostMapping("/systemInformation")
    public Results systemInformation() {
        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        String osJson = JSON.toJSONString(operatingSystemMXBean);
        System.out.println("osJson is " + osJson);
        JSONObject jsonObject = JSON.parseObject(osJson);
        SystemInformation systemInformation = new SystemInformation();
        //操作系统
        systemInformation.setOperatingSystem(jsonObject.getString("name"));
        //系统架构
        systemInformation.setSystemArchitecture(jsonObject.getString("arch"));
        CPU核心数
        systemInformation.setSystemCPU(jsonObject.getString("availableProcessors"));
        //总物理内存
        systemInformation.setTotalPhysicalMemory(DECIMALFORMAT.format((1.0 * jsonObject.getLong("totalPhysicalMemorySize")) / (1024 * 1024 * 1024)));
        //可用物理内存
        systemInformation.setAvailablePhysicalMemory(DECIMALFORMAT.format(1.0 * jsonObject.getLong("freePhysicalMemorySize") / (1024 * 1024 * 1024)));
        //总内存
        Long totalPhysicalMemorySize = jsonObject.getLong("totalPhysicalMemorySize");
        //剩余内存
        Long freePhysicalMemorySize = jsonObject.getLong("freePhysicalMemorySize");
        //已使用用物理内存
        double totalPhysicalMemorySized = Double.valueOf(DECIMALFORMAT.format(1.0 * totalPhysicalMemorySize / (1024 * 1024 * 1024)));
        double freePhysicalMemorySized = Double.valueOf(DECIMALFORMAT.format(1.0 * freePhysicalMemorySize / (1024 * 1024 * 1024)));
        double c = Double.valueOf(DECIMALFORMAT.format(totalPhysicalMemorySized - freePhysicalMemorySized));
        systemInformation.setAvailablePhysicalMemoryUsed(DECIMALFORMAT.format(totalPhysicalMemorySized - freePhysicalMemorySized));
        //内存使用率
        double v = Double.valueOf(DECIMALFORMAT.format((totalPhysicalMemorySized - freePhysicalMemorySized) / totalPhysicalMemorySized * 100));
        systemInformation.setMemoryUsage(DECIMALFORMAT.format((totalPhysicalMemorySized - freePhysicalMemorySized) / totalPhysicalMemorySized * 100));
        //系统CPU占用率
        systemInformation.setSystemCpuLoad(DECIMALFORMAT.format(jsonObject.getDouble("systemCpuLoad") * 100));
        //进程占用CPU
        systemInformation.setProcessProportion(DECIMALFORMAT.format(jsonObject.getDouble("processCpuLoad") * 100));
        log.info(String.valueOf(systemInformation));
        return Results.success(systemInformation);
    }
DecimalFormat DECIMALFORMAT = new DecimalFormat("#.##");
//磁盘监控
    @PostMapping("/diskUsage")
    public Results diskUsage() {
        List<DiskUsages> diskUsagesList = new ArrayList<>();
        // 获取磁盘分区列表
        File[] roots = File.listRoots();
        for (File file : roots) {
            DiskUsages diskUsages = new DiskUsages();
            //可用空间
            Long freeSpace = file.getFreeSpace();
            //总空间
            Long totalSpace = file.getTotalSpace();
            //已使用空间
            Long usableSpace = totalSpace - freeSpace;
            //盘符
            diskUsages.setPath(file.getPath());
            //空闲空间
            diskUsages.setFreeSpace(file.getFreeSpace() / (1024 * 1024 * 1024) + "GB");
            //总空间
            diskUsages.setTotalSpace(file.getTotalSpace() / (1024 * 1024 * 1024) + "GB");
            //已使用用空间
            diskUsages.setUsableSpace(usableSpace / (1024 * 1024 * 1024) + "GB");
            //使用百分比
            diskUsages.setPercentage(DECIMALFORMAT.format(((double) usableSpace / (double) totalSpace) * 100) + "%");
            diskUsagesList.add(diskUsages);
        }
        log.info(String.valueOf(diskUsagesList));
        return Results.success(diskUsagesList);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值