자바 서버 호스트 정보 모니터링 도구 류 의 예제 코드

도 킹 전단 후 효 과 는 그림 과 같 습 니 다:
在这里插入图片描述
在这里插入图片描述
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.창 어 부리 갑 옆 에 있 는 건 9337

/**
 *      
 * 
 * @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.컨트롤 러 인터페이스 도 킹 전단

/**
 *      
 * 
 * @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);
    }
}
자바 서버 호스트 정보 모니터링 도구 류 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 자바 호스트 정보 모니터링 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 지원 바 랍 니 다!

좋은 웹페이지 즐겨찾기