背景
虚拟机数量太多,如果全部使用人工巡检工作量太大,zabbix安全性太差,帮需要一款系统信息收集工具完成系统信息数据采集,以便于减轻运维人员工作量。
PS: 网上抓取工具太多,但是安全性更差,其实工作量不大,建议有条件的自行开发。
选型:
oshi-core
以下为示例代码
PS: 在引入包时注意使用maven,否则需要引入全,不然会报错,
<dependency>
<groupId>com.github.dblock</groupId>
<artifactId>oshi-core</artifactId>
<version>LATEST</version>
</dependency>
以下为经编译或独立运行程序段
package test;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import org.springframework.test.context.ContextConfiguration;
import oshi.SystemInfo;
import oshi.hardware.Baseboard;
import oshi.hardware.CentralProcessor;
import oshi.hardware.ComputerSystem;
import oshi.hardware.Firmware;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HWPartition;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.NetworkIF;
import oshi.hardware.PowerSource;
import oshi.hardware.UsbDevice;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import oshi.software.os.OperatingSystemVersion;
import oshi.util.Util;
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class CpuTest{
private static final int OSHI_WAIT_SECOND = 1000;
private static SystemInfo systemInfo = new SystemInfo();
private static HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
private static OperatingSystem osSystem = systemInfo.getOperatingSystem();
public static double convByteToG(long bytes){
double d = 1024*1024*1024*1.0;
return bytes * 1.0 / d;
}
public static void getHardwareInfo()
{
// SystemInfo systemInfo = new SystemInfo();
// HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
// OperatingSystem osSystem = systemInfo.getOperatingSystem();
//cpu信息
CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();
long[] prevTicks = centralProcessor.getSystemCpuLoadTicks();
Util.sleep(OSHI_WAIT_SECOND);
long[] ticks = centralProcessor.getSystemCpuLoadTicks();
long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralP