叙述

谈到监控,最快捷的还是使用Java本身进行对JVM和操作系统的监控。方便快捷又可靠。

解决方案

maven

        <dependency><groupId>com.github.oshi</groupId><artifactId>oshi-core</artifactId><version>3.5.0</version></dependency>

API

对象    方法    描述
SystemInfo    getOperatingSystem()    获取操作系统信息,OperatingSystem
getHardware()    获取硬件信息,HardwareAbstractionLayer
OperatingSystem    getFamily()    获取操作系统名称,例如 Windows
getManufacturer()    获取供货商,例如 Microsoft
getVersion()    获取操作系统版本信息
getFileSystem()    获取系统硬盘信息,FileSystem
getProcesses(int pId, OperatingSystem.ProcessSort var2)    通过 进程id 获取进程信息,并设置排序方式,OSProcess[]
getProcess(int pId)    通过 进程id 获取一个进程信息,OSProcess
getProcesses(Collection<Integer> pId)    获取多个进程信息,List<OSProcess>
getChildProcesses(int var1, int var2, OperatingSystem.ProcessSort var3)     获取子进程,OSProcess[]
getProcessId()    获取进程id
getProcessCount()    获取进程数量
getThreadCount()    获取线程数
getNetworkParams()    获取网络相关参数,NetworkParams
HardwareAbstractionLayer    getComputerSystem()    获取计算机系统信息,ComputerSystem
getProcessor()    获取处理器信息,CentralProcessor
getMemory()    获取内存信息,GlobalMemory
getPowerSources()    获取电源信息,PowerSource
getDiskStores()    获取硬件磁盘信息,HWDiskStore
getNetworkIFs()    获取网络信息,NetworkIF
getDisplays()    获取显示信息,Display
getSensors()    获取传感器信息,Sensors
 getUsbDevices(boolean var1)    获取USB设备信息,UsbDevice
OperatingSystemVersion    getBuildNumber()    获取内部编号
getCodeName()    代码名称
getVersion()    获取版本
FileSystem    getMaxFileDescriptors()    获取最大文件描述符
getOpenFileDescriptors()    获取打开文件描述符
getFileStores()    获取盘符相关信息
OSProcess    getName()    进程程序名称
getPath()    进程程序所在位置
getCommandLine()    获取命令行
getCurrentWorkingDirectory()    获取当前工作目录
 getUser()    获取用户信息
getUserID()    获取用户id
getGroup()    获取组信息
getGroupID()    获取组id
getState()    状态
getProcessID()    获取进程id
getParentProcessID()    获取父进程id
getThreadCount()    获取线程数
getPriority()    优先级
getVirtualSize()    虚拟大小
getResidentSetSize()    实际使用物理内存
 getKernelTime()    内核时间
 getUserTime()    用户时间
getUpTime()    正常运行时间
getStartTime()    开始时间
getBytesRead()    读取字节
getBytesWritten()    写入字节
getOpenFiles()    打开文件数量
NetworkParams    getDnsServers()    获取域名地址
getHostName()    获取主机名
getDomainName()    获取域名
getIpv4DefaultGateway()    获取默认Ipv4
getIpv6DefaultGateway()    获取默认Ipv6
OSFileStore    getName()    磁盘名称
getVolume()    文件集
getLogicalVolume()    合理的文件集
getMount()    盘符
getDescription()    描述
getType()    类型
getUUID()    磁盘UUID
getUsableSpace()    可用空间
getTotalSpace()    总空间
ComputerSystem    getManufacturer()    获取制造商
getModel()    获取型号
getSerialNumber()    获取序列号
getFirmware()    获取固件信息,Firmware
getBaseboard()    获取外壳信息,Baseboard
Firmware    getManufacturer()    获取制造商信息
getName()    获取名称
getDescription()    获取描述信息
getVersion()    获取版本
getReleaseDate()    获取发布时间
Baseboard    getManufacturer()    获取制造商信息
getModel()    获取型号
getVersion()    获取版本信息
getSerialNumber()    获取序列号
CentralProcessor    getVendor()    获取供应商
getName()    获取cpu名称
getVendorFreq()    获取供应商频率
getProcessorID()    获取处理器id
getIdentifier()    获取标识符
isCpu64bit()    判断cpu是否为64位的
getStepping()    获取cpu步进
getModel()    获取型号
getFamily()    获取家族
getSystemCpuLoadBetweenTicks()    获取cpu负载间隔刻度
getSystemCpuLoadTicks()    获取cpu负载刻度
getSystemCpuLoad()    获取cpu负载
getSystemLoadAverage()    获取cpu平均负载
getSystemLoadAverage(int var1)    获取cpu平均负载
getProcessorCpuLoadBetweenTicks()    获取处理器cpu负载间隔刻度
getProcessorCpuLoadTicks()    获取处理器cpu负载刻度
getSystemUptime()    获取正常运行时间
getLogicalProcessorCount()    获取逻辑处理器数量
getPhysicalProcessorCount()    获取物理处理器数量
getPhysicalPackageCount()    获取物理包装数量
getContextSwitches()    获取上下文切换数量
getInterrupts()    获取中断
GlobalMemory    getTotal()    获取总内存
getAvailable()    获取可用系统运行内存
getSwapTotal()    获取可用虚拟总内存
getSwapUsed()    获取已用虚拟总内存
PowerSource    getName()    获取名称
getRemainingCapacity()    获取剩余容量
getTimeRemaining()    获取剩余时间
HWDiskStore    getName()    获取名称
getModel()    获取型号
getSerial()    获取序列号
getSize()    获取大小
getReads()    (读长)是高通量测序中一个反应获得的测序序列
getReadBytes()    读取字节
getWrites()    写长
getWriteBytes()    写入字节
getTransferTime()    获取转移时间
getPartitions()    获取分区,HWPartition
getTimeStamp()    获取时间戳
NetworkIF    getName()    获取名称
getDisplayName()    获取显示名称
getMTU()    获取最大传输单元
getMacaddr()    获取MAC地址
getIPv4addr()    获取IPv4
getIPv6addr()    获取IPv6
getBytesRecv()    获取接收字节数
getBytesSent()    获取发送字节数
getPacketsRecv()    获取接收数据包
getPacketsSent()    获取发送数据包
getInErrors()    是否可达,正常 0
getOutErrors()    响应错误,无错误 0
 getSpeed()    获取速率
getTimeStamp()    获取时间错
Display    getEdid()    中文名称扩展显示器识别数据
Sensors    getCpuTemperature()    获取CPU温度
getFanSpeeds()    获取风扇速度
getCpuVoltage()    获取CPU电压
UsbDevice    getName()    获取名称
getVendor()    获取供应商
getVendorId()    获取供应商id
getProductId()    获取商品id
getSerialNumber()    获取序列号
getConnectedDevices()    获取连接设备

代码

package com.zp.utils;import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import oshi.util.Util;import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.util.Properties;/**
* 系统消息工具类
*
**/
public class SystemInfoUtils {private static final int OSHI_WAIT_SECOND = 1000;private static SystemInfo systemInfo = new SystemInfo();private static HardwareAbstractionLayer hardware = systemInfo.getHardware();private static OperatingSystem operatingSystem = systemInfo.getOperatingSystem();public static JSONObject getCpuInfo() {JSONObject cpuInfo = new JSONObject();CentralProcessor processor = hardware.getProcessor();// CPU信息long[] prevTicks = processor.getSystemCpuLoadTicks();Util.sleep(OSHI_WAIT_SECOND);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;//cpu核数cpuInfo.put("cpuNum", processor.getLogicalProcessorCount());//cpu系统使用率cpuInfo.put("cSys", new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));//cpu用户使用率cpuInfo.put("user", new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));//cpu当前等待率cpuInfo.put("iowait", new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));//cpu当前使用率cpuInfo.put("idle", new DecimalFormat("#.##%").format(1.0 - (idle * 1.0 / totalCpu)));return cpuInfo;}/*** 系统jvm信息*/public static JSONObject getJvmInfo() {JSONObject cpuInfo = new JSONObject();Properties props = System.getProperties();Runtime runtime = Runtime.getRuntime();long jvmTotalMemoryByte = runtime.totalMemory();long freeMemoryByte = runtime.freeMemory();//jvm总内存cpuInfo.put("total", formatByte(runtime.totalMemory()));//空闲空间cpuInfo.put("free", formatByte(runtime.freeMemory()));//jvm最大可申请cpuInfo.put("max", formatByte(runtime.maxMemory()));//vm已使用内存cpuInfo.put("user", formatByte(jvmTotalMemoryByte - freeMemoryByte));//jvm内存使用率cpuInfo.put("usageRate", new DecimalFormat("#.##%").format((jvmTotalMemoryByte - freeMemoryByte) * 1.0 / jvmTotalMemoryByte));//jdk版本cpuInfo.put("jdkVersion", props.getProperty("java.version"));//jdk路径cpuInfo.put("jdkHome", props.getProperty("java.home"));return cpuInfo;}/*** 系统内存信息*/public static JSONObject getMemInfo() {JSONObject cpuInfo = new JSONObject();GlobalMemory memory = systemInfo.getHardware().getMemory();//总内存long totalByte = memory.getTotal();//剩余long acaliableByte = memory.getAvailable();//总内存cpuInfo.put("total", formatByte(totalByte));//使用cpuInfo.put("used", formatByte(totalByte - acaliableByte));//剩余内存cpuInfo.put("free", formatByte(acaliableByte));//使用率cpuInfo.put("usageRate", new DecimalFormat("#.##%").format((totalByte - acaliableByte) * 1.0 / totalByte));return cpuInfo;}/*** 系统盘符信息*/public static JSONArray getSysFileInfo() {JSONObject cpuInfo;JSONArray sysFiles = new JSONArray();FileSystem fileSystem = operatingSystem.getFileSystem();OSFileStore[] fsArray = fileSystem.getFileStores();for (OSFileStore fs : fsArray) {cpuInfo = new JSONObject();//盘符路径cpuInfo.put("dirName", fs.getMount());//盘符类型cpuInfo.put("sysTypeName", fs.getType());//文件类型cpuInfo.put("typeName", fs.getName());//总大小cpuInfo.put("total", formatByte(fs.getTotalSpace()));//剩余大小cpuInfo.put("free", formatByte(fs.getUsableSpace()));//已经使用量cpuInfo.put("used", formatByte(fs.getTotalSpace() - fs.getUsableSpace()));if (fs.getTotalSpace() == 0) {//资源的使用率cpuInfo.put("usage", 0);} else {cpuInfo.put("usage",new DecimalFormat("#.##%").format((fs.getTotalSpace() - fs.getUsableSpace()) * 1.0 / fs.getTotalSpace()));}sysFiles.add(cpuInfo);}return sysFiles;}/*** 系统信息*/public static JSONObject getSysInfo() throws UnknownHostException {JSONObject cpuInfo = new JSONObject();Properties props = System.getProperties();//操作系统名cpuInfo.put("osName", props.getProperty("os.name"));//系统架构cpuInfo.put("osArch", props.getProperty("os.arch"));//服务器名称cpuInfo.put("computerName", InetAddress.getLocalHost().getHostName());//服务器IpcpuInfo.put("computerIp", InetAddress.getLocalHost().getHostAddress());//项目路径cpuInfo.put("userDir", props.getProperty("user.dir"));return cpuInfo;}/*** 所有系统信息*/public static JSONObject getInfo() throws UnknownHostException {JSONObject info = new JSONObject();info.put("cpuInfo", getCpuInfo());info.put("jvmInfo", getJvmInfo());info.put("memInfo", getMemInfo());info.put("sysInfo", getSysInfo());info.put("sysFileInfo", getSysFileInfo());return info;}/*** 单位转换*/private static String formatByte(long byteNumber) {//换算单位double FORMAT = 1024.0;double kbNumber = byteNumber / FORMAT;if (kbNumber < FORMAT) {return new DecimalFormat("#.##KB").format(kbNumber);}double mbNumber = kbNumber / FORMAT;if (mbNumber < FORMAT) {return new DecimalFormat("#.##MB").format(mbNumber);}double gbNumber = mbNumber / FORMAT;if (gbNumber < FORMAT) {return new DecimalFormat("#.##GB").format(gbNumber);}double tbNumber = gbNumber / FORMAT;return new DecimalFormat("#.##TB").format(tbNumber);}
}

springboot 整合 oshi-core 监控 JVM及操作系统相关推荐

  1. springboot开启jms服务监控jvm运行情况

    启动命令 nohup java -jar -Djava.rmi.server.hostname=服务所在ip -Dcom.sun.management.jmxremote -Dcom.sun.mana ...

  2. springboot整合Actuator监控

    springboot整合Actuator监控. 1.简要说明: Actuator提供了对springboot应用程序监视和管理的能力,可以选择通过使用HTTP Endpoint或者使用JMX来管理和监 ...

  3. Redis学习篇3_事务及其监控(锁)、Jedis、SpringBoot整合Redis、RedisTemplate的json序列化、RedisUtil工具类

    目录 事务及其监控(锁) Jedis SpringBoot整合Redis RedisTemplate 默认RedisTemplate来源 关于中文序列化问题 RedisUtil工具类 一.事务及其监控 ...

  4. SpringBoot项目优化和Jvm调优及VisualVM远程连接监控JVM(JMX连接)

    项目调优 作为一名工程师,项目调优这事,是必须得熟练掌握的事情. 在SpringBoot项目中,调优主要通过配置文件和配置JVM的参数的方式进行. 在这边有一篇比较好的文章,推荐给大家! Spring ...

  5. SpringBoot 整合Druid数据源SQL监控不显示问题

    SpringBoot 整合Druid数据源SQL监控不显示问题 项目场景: 问题描述: 解决方案: 项目场景: SpringBoot 整合Druid数据源SQL监控不显示 问题描述: 明明使用Drui ...

  6. SpringBoot整合Druid,开启druid监控平台

    SpringBoot整合Druid 1.导入依赖pom.xml <!-- https://mvnrepository.com/artifact/log4j/log4j --><dep ...

  7. SpringBoot整合第三方技术学习笔记(自用)

    SpringBoot整合第三方技术学习笔记 搬运黑马视频配套笔记 KF-4.数据层解决方案 KF-4-1.SQL 回忆一下之前做SSMP整合的时候数据层解决方案涉及到了哪些技术?MySQL数据库与My ...

  8. SpringBoot整合:Druid、MyBatis、MyBatis-Plus、多数据源、knife4j、日志、Redis,Redis的Java操作工具类、封装发送电子邮件等等

    SpringBoot笔记 一.SpringBoot 介绍 1.1.SpringBoot简介 SpringBoot 是一个快速开发的框架, 封装了Maven常用依赖.能够快速的整合第三方框架:简化XML ...

  9. 跟着狂神学Redis(NoSql+环境配置+五大数据类型+三种特殊类型+Hyperloglog+Bitmap+事务+Jedis+SpringBoot整合+Redis持久化+...)

    跟着狂神学Redis 狂神聊Redis 学习方式:不是为了面试和工作学习!仅仅是为了兴趣!兴趣才是最好的老师! 基本的理论先学习,然后将知识融汇贯通! 狂神的Redis课程安排: nosql 讲解 阿 ...

最新文章

  1. 输入过欠压保护电路原理图
  2. Intent Flag介绍 intent.addFlags()
  3. Python字典(dict )的几种遍历方式
  4. 图像处理之 opencv 学习---opencv 中的常用算法
  5. 视频直播:实时数据可视化分析
  6. SAP UI5库对浏览器类型检测的实现
  7. mysqlfront连接MySQL错误,使用MySQL-Front连接mysql的时候发生的错误解决方法
  8. F5 V9 利用SCCP实现带外管理及重装系统
  9. System学习笔记005---如何查看远程的一台电脑的某个端口有没有打开_centos查看某个端口是否打开
  10. Servlet学习的两个案例之网站访问次数的统计
  11. python可视化入门 plotly
  12. 编译报错:make: *** No rule to make target (例如:starg.h)解决
  13. python -m a.py 和 python a.py区别
  14. 计算二阶矩阵特征值的技巧
  15. 解决Excel表格输入身份证号码显示异常的问题
  16. C++ Reference: Standard C++ Library reference: C Library: cfenv: FE_DOWNWARD
  17. 关于yuv rtp 打包_【讲堂】关于KNX编程基本规律
  18. 视频怎么更换封面图片,视频封面更换的步骤演示
  19. 外边距合并及解决方法
  20. Pandas的iloc、loc、ix的区别

热门文章

  1. 前端实现微信授权登录
  2. js添加多个子节点_JS原生追加子节点
  3. python将bytes转为对象_Python3中bytes类型转换为str类型
  4. 如何在IDEA的包下创建子包
  5. json schema 关键字速查
  6. 江苏自考计算机免考与论文答辩,江苏自考毕业答辩一年有几次?答辩难吗?
  7. 局部变量,全局变量(外部变量),静态局部变量,静态全局变量
  8. git怎样查看分支关系图
  9. 【设计模式第一弹】策略模式
  10. Java Stream Reduce