java使用oshi获取硬件信息,包括cpu温度

java使用oshi获取硬件信息,包括cpu温度

 SystemInfo si = new SystemInfo();
            HardwareAbstractionLayer hal = si.getHardware();
            CentralProcessor processor = hal.getProcessor();  //获取cpu信息
            long[] prevTicks = processor.getSystemCpuLoadTicks();
            Util.sleep(1000);
            long[] ticks = processor.getSystemCpuLoadTicks();
            long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
            long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
            long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
            long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
            long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
            long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
            long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
            long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
            long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;

            map.put("cpuName", processor.getName());
            map.put("block",processor.getLogicalProcessorCount());//块数
            map.put("cpuUsedLV",new DecimalFormat("#.##").format(1.0-(idle * 1.0 / totalCpu)));//cpu使用率


            Properties props = System.getProperties();
            map.put("vendorName",props.getProperty("os.name"));//系统名称

            //硬盘的信息
            Map drive =  new HashMap();
            ArrayList<Map> arrayList = new ArrayList<>();

            oshi.software.os.OperatingSystem op = si.getOperatingSystem();
            FileSystem fileSystem = op.getFileSystem();
            OSFileStore[] fsArray = fileSystem.getFileStores();
            for (OSFileStore fs : fsArray) {

                drive = new HashMap();

                long free = fs.getUsableSpace();
                long total = fs.getTotalSpace();
                long used = total - free;

                drive.put("name",fs.getName());//盘符名称

                drive.put("driveUses",((int)(((total*1.0)/1024/1024/1024))));//盘符总大小
                drive.put("driveUsed",((int)(((used*1.0)/1024/1024/1024))));//盘使用量
                drive.put("driveUsedLv",(int)((used*1.0 / total*1.0)*100) );//盘使用率
                arrayList.add(drive);
            }
            map.put("driver",arrayList);


            SystemInfo systemInfo = new SystemInfo();
            GlobalMemory memory = systemInfo.getHardware().getMemory();
            long totalByte = memory.getTotal();
            long acaliableByte = memory.getAvailable();

            map.put("memoryAllSize",((int)((memory.getTotal()*1.0)/1024/1024/1024))+"G");//内存总量
            map.put("memoryUsed", ((int)((memory.getTotal() - memory.getAvailable()*1.0)/1024/1024/1024))+"G");//内存使用量
            map.put("memoryUsedLv", new DecimalFormat("#.##").format((totalByte-acaliableByte)*1.0/totalByte));//内存使用率


            InetAddress address = InetAddress.getLocalHost();
            map.put("ip",address.getHostAddress());

            //cpu温度
            Sensors sensors = hal.getSensors();
            double cpuTemperature = sensors.getCpuTemperature();
            map.put("cpuTemperature",cpuTemperature);
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值