Javaサーバホスト情報監視ツール類のコード例


フロントエンドをドッキングした後の効果を図のように示します。
在这里插入图片描述
在这里插入图片描述
1、CPU関連情報エンティティ類

/**
 * CPU    
 * 
 * @author csp
 */
public class Cpu
{
    /**
     *    
     */
    private int cpuNum;

    /**
     * CPU     
     */
    private double total;

    /**
     * CPU     
     */
    private double sys;

    /**
     * CPU     
     */
    private double used;

    /**
     * CPU     
     */
    private double wait;

    /**
     * CPU     
     */
    private double free;

    public int getCpuNum()
    {
        return cpuNum;
    }

    public void setCpuNum(int cpuNum)
    {
        this.cpuNum = cpuNum;
    }

    public double getTotal()
    {
        return Arith.round(Arith.mul(total, 100), 2);
    }

    public void setTotal(double total)
    {
        this.total = total;
    }

    public double getSys()
    {
        return Arith.round(Arith.mul(sys / total, 100), 2);
    }

    public void setSys(double sys)
    {
        this.sys = sys;
    }

    public double getUsed()
    {
        return Arith.round(Arith.mul(used / total, 100), 2);
    }

    public void setUsed(double used)
    {
        this.used = used;
    }

    public double getWait()
    {
        return Arith.round(Arith.mul(wait / total, 100), 2);
    }

    public void setWait(double wait)
    {
        this.wait = wait;
    }

    public double getFree()
    {
        return Arith.round(Arith.mul(free / total, 100), 2);
    }

    public void setFree(double free)
    {
        this.free = free;
    }
}
2、騯嘴匦畔⑹堤謇

/**
 *      
 * 
 * @author csp
 */
public class Mem
{
    /**
     *     
     */
    private double total;

    /**
     *     
     */
    private double used;

    /**
     *     
     */
    private double free;

    public double getTotal()
    {
        return Arith.div(total, (1024 * 1024 * 1024), 2);
    }

    public void setTotal(long total)
    {
        this.total = total;
    }

    public double getUsed()
    {
        return Arith.div(used, (1024 * 1024 * 1024), 2);
    }

    public void setUsed(long used)
    {
        this.used = used;
    }

    public double getFree()
    {
        return Arith.div(free, (1024 * 1024 * 1024), 2);
    }

    public void setFree(long free)
    {
        this.free = free;
    }

    public double getUsage()
    {
        return Arith.mul(Arith.div(used, total, 4), 100);
    }
}
3、JVM関連情報エンティティ類

/**
 * JVM    
 * 
 * @author csp
 */
public class Jvm
{
    /**
     *   JVM       (M)
     */
    private double total;

    /**
     * JVM        (M)
     */
    private double max;

    /**
     * JVM    (M)
     */
    private double free;

    /**
     * JDK  
     */
    private String version;

    /**
     * JDK  
     */
    private String home;

    public double getTotal()
    {
        return Arith.div(total, (1024 * 1024), 2);
    }

    public void setTotal(double total)
    {
        this.total = total;
    }

    public double getMax()
    {
        return Arith.div(max, (1024 * 1024), 2);
    }

    public void setMax(double max)
    {
        this.max = max;
    }

    public double getFree()
    {
        return Arith.div(free, (1024 * 1024), 2);
    }

    public void setFree(double free)
    {
        this.free = free;
    }

    public double getUsed()
    {
        return Arith.div(total - free, (1024 * 1024), 2);
    }

    public double getUsage()
    {
        return Arith.mul(Arith.div(total - free, total, 4), 100);
    }

    /**
     *   JDK  
     */
    public String getName()
    {
        return ManagementFactory.getRuntimeMXBean().getVmName();
    }

    public String getVersion()
    {
        return version;
    }

    public void setVersion(String version)
    {
        this.version = version;
    }

    public String getHome()
    {
        return home;
    }

    public void setHome(String home)
    {
        this.home = home;
    }

    /**
     * JDK    
     */
    public String getStartTime()
    {
        return DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getServerStartDate());
    }

    /**
     * JDK    
     */
    public String getRunTime()
    {
        return DateUtils.getDatePoor(DateUtils.getNowDate(), DateUtils.getServerStartDate());
    }
}
4、ホストシステム関連情報エンティティ類

/**
 *       
 * 
 * @author csp
 */
public class Sys
{
    /**
     *      
     */
    private String computerName;

    /**
     *    Ip
     */
    private String computerIp;

    /**
     *     
     */
    private String userDir;

    /**
     *     
     */
    private String osName;

    /**
     *     
     */
    private String osArch;

    public String getComputerName()
    {
        return computerName;
    }

    public void setComputerName(String computerName)
    {
        this.computerName = computerName;
    }

    public String getComputerIp()
    {
        return computerIp;
    }

    public void setComputerIp(String computerIp)
    {
        this.computerIp = computerIp;
    }

    public String getUserDir()
    {
        return userDir;
    }

    public void setUserDir(String userDir)
    {
        this.userDir = userDir;
    }

    public String getOsName()
    {
        return osName;
    }

    public void setOsName(String osName)
    {
        this.osName = osName;
    }

    public String getOsArch()
    {
        return osArch;
    }

    public void setOsArch(String osArch)
    {
        this.osArch = osArch;
    }
}
5、システムファイルに関する情報エンティティ類

/**
 *         
 * 
 * @author csp
 */
public class SysFile
{
    /**
     *     
     */
    private String dirName;

    /**
     *     
     */
    private String sysTypeName;

    /**
     *     
     */
    private String typeName;

    /**
     *    
     */
    private String total;

    /**
     *     
     */
    private String free;

    /**
     *      
     */
    private String used;

    /**
     *       
     */
    private double usage;

    public String getDirName()
    {
        return dirName;
    }

    public void setDirName(String dirName)
    {
        this.dirName = dirName;
    }

    public String getSysTypeName()
    {
        return sysTypeName;
    }

    public void setSysTypeName(String sysTypeName)
    {
        this.sysTypeName = sysTypeName;
    }

    public String getTypeName()
    {
        return typeName;
    }

    public void setTypeName(String typeName)
    {
        this.typeName = typeName;
    }

    public String getTotal()
    {
        return total;
    }

    public void setTotal(String total)
    {
        this.total = total;
    }

    public String getFree()
    {
        return free;
    }

    public void setFree(String free)
    {
        this.free = free;
    }

    public String getUsed()
    {
        return used;
    }

    public void setUsed(String used)
    {
        this.used = used;
    }

    public double getUsage()
    {
        return usage;
    }

    public void setUsage(double usage)
    {
        this.usage = usage;
    }
}
6、サーバ関連情報エンティティ類

/**
 *        
 * 
 * @author csp
 */
public class Server
{
    private static final int OSHI_WAIT_SECOND = 1000;
    
    /**
     * CPU    
     */
    private Cpu cpu = new Cpu();

    /**
     *      
     */
    private Mem mem = new Mem();

    /**
     * JVM    
     */
    private Jvm jvm = new Jvm();

    /**
     *        
     */
    private Sys sys = new Sys();

    /**
     *       
     */
    private List<SysFile> sysFiles = new LinkedList<SysFile>();

    public Cpu getCpu()
    {
        return cpu;
    }

    public void setCpu(Cpu cpu)
    {
        this.cpu = cpu;
    }

    public Mem getMem()
    {
        return mem;
    }

    public void setMem(Mem mem)
    {
        this.mem = mem;
    }

    public Jvm getJvm()
    {
        return jvm;
    }

    public void setJvm(Jvm jvm)
    {
        this.jvm = jvm;
    }

    public Sys getSys()
    {
        return sys;
    }

    public void setSys(Sys sys)
    {
        this.sys = sys;
    }

    public List<SysFile> getSysFiles()
    {
        return sysFiles;
    }

    public void setSysFiles(List<SysFile> sysFiles)
    {
        this.sysFiles = sysFiles;
    }

    /**
     *            
     * @throws Exception
     */
    public void copyTo() throws Exception
    {
        //       
        SystemInfo si = new SystemInfo();
        //   SystemInfo      
        HardwareAbstractionLayer hal = si.getHardware();
        //     CPU  
        setCpuInfo(hal.getProcessor());
        //         
        setMemInfo(hal.getMemory());

        //        
        setSysInfo();

        //   Java   
        setJvmInfo();

        //       
        setSysFiles(si.getOperatingSystem());
    }

    /**
     *   CPU  
     */
    private void setCpuInfo(CentralProcessor processor)
    {
        // CPU  
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        Util.sleep(OSHI_WAIT_SECOND);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()];
        long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()];
        long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()];
        long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()];
        long cSys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()];
        long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()];
        long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()];
        long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()];
        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
        cpu.setCpuNum(processor.getLogicalProcessorCount());// Cpu   
        cpu.setTotal(totalCpu);// CPU     
        cpu.setSys(cSys);// CPU     
        cpu.setUsed(user);// CPU     
        cpu.setWait(iowait);// CPU     
        cpu.setFree(idle);// CPU     
    }

    /**
     *       
     */
    private void setMemInfo(GlobalMemory memory)
    {
        mem.setTotal(memory.getTotal());//      
        mem.setUsed(memory.getTotal() - memory.getAvailable());//        
        mem.setFree(memory.getAvailable());//       
    }

    /**
     *        
     */
    private void setSysInfo()
    {
        //          
        Properties props = System.getProperties();
        sys.setComputerName(IpUtils.getHostName());//       
        sys.setComputerIp(IpUtils.getHostIp());//     IP
        sys.setOsName(props.getProperty("os.name"));//        Windows 10
        sys.setOsArch(props.getProperty("os.arch"));//          amd64
        sys.setUserDir(props.getProperty("user.dir"));//          F:\git\ruoyi\RuoYi-Vue
    }

    /**
     *   Java   
     */
    private void setJvmInfo() throws UnknownHostException
    {
        Properties props = System.getProperties();
        jvm.setTotal(Runtime.getRuntime().totalMemory());// JVM    625.5M
        jvm.setMax(Runtime.getRuntime().maxMemory());// JVM      347.99M
        jvm.setFree(Runtime.getRuntime().freeMemory());// JVM     277.51M
        jvm.setVersion(props.getProperty("java.version"));// jdk   1.8
        jvm.setHome(props.getProperty("java.home"));// JDK     C:\Program Files\Java\jdk1.8.0_201\jre
    }

    /**
     *       
     */
    private void setSysFiles(OperatingSystem os)
    {
        //        (OS)    FileSystem
        FileSystem fileSystem = os.getFileSystem();
        //    FileSystem         list  
        List<OSFileStore> fsArray = fileSystem.getFileStores();
        for (OSFileStore fs : fsArray)
        {
            long free = fs.getUsableSpace();//       
            long total = fs.getTotalSpace();//      
            long used = total - free;//        
            SysFile sysFile = new SysFile();
            sysFile.setDirName(fs.getMount());//      C:\
            sysFile.setSysTypeName(fs.getType());//      NTFS
            sysFile.setTypeName(fs.getName());//             (C:)
            sysFile.setTotal(convertFileSize(total));//      
            sysFile.setFree(convertFileSize(free));//       
            sysFile.setUsed(convertFileSize(used));//        
            sysFile.setUsage(Arith.mul(Arith.div(used, total, 4), 100));//         
            sysFiles.add(sysFile);
        }
    }

    /**
     *     
     * 
     * @param size     
     * @return     
     */
    public String convertFileSize(long size)
    {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
        if (size >= gb)
        {
            return String.format("%.1f GB", (float) size / gb);
        }
        else if (size >= mb)
        {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        }
        else if (size >= kb)
        {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        }
        else
        {
            return String.format("%d B", size);
        }
    }
}
7、Controllerインターフェースのドッキング先端

/**
 *      
 * 
 * @author csp
 */
@RestController
@RequestMapping("/monitor/server")
public class ServerController
{
    /**
     *          
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('monitor:server:list')")// Spring Security         ,     ,    
    // AjaxResult         ,     ,     
    @GetMapping()
    public AjaxResult getInfo() throws Exception
    {
        //          
        Server server = new Server();
        server.copyTo();

        return AjaxResult.success(server);
    }
}
ここで、Javaサーバーのホストコンピュータ情報監視ツールに関する記事を紹介します。Javaホストコンピュータに関する情報監視の内容は以前の文章を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。