日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

使用Sigar包获取操作系统信息[通俗易懂]

發布時間:2023/12/19 综合教程 34 生活家
生活随笔 收集整理的這篇文章主要介紹了 使用Sigar包获取操作系统信息[通俗易懂] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

項目中的一個需求是獲取操作系統的相關信息,在網上找了相關的資料,發現了一個好的玩意,就是Sigar,它是通過java api的方式來調用程序,基本上能夠獲取操作系統的全部信息,感覺挺強大的。Sigar(System Information Gatherer And Reporter),是一個開源的工具,提供了跨平臺的系統信息收集的API,核心由C語言實現的,它可以被多種語言調用,包括C/C++、java、Perl、Ruby、PHP等,可以收集的信息包括:

1, CPU信息,包括基本信息(vendor、model、mhz、cacheSize)和統計信息(user、sys、idle、nice、wait)
2, 文件系統信息,包括Filesystem、Size、Used、Avail、Use%、Type
3, 事件信息,類似Service Control Manager
4, 內存信息,物理內存和交換內存的總數、使用數、剩余數;RAM的大小
5, 網絡信息,包括網絡接口信息和網絡路由信息
6, 進程信息,包括每個進程的內存、CPU占用數、狀態、參數、句柄
7, IO信息,包括IO的狀態,讀寫大小等
8, 服務狀態信息,系統日志信息
9, 系統信息,包括操作系統版本,系統資源限制情況,系統運行時間以及負載,JAVA的版本信息等

Sigar現在在github上面是屬于開源軟件,大家可以看它的源代碼:https://github.com/hyperic/sigar

系統中如果要使用Sigar,可以下載它的jar包,下載地址:https://sourceforge.net/projects/sigar/,如果是maven工程的話,可以在pom.xml文件中添加:

                 <dependency>
			<groupId>org.hyperic</groupId>
			<artifactId>sigar</artifactId>
			<version>1.6.5.132-6</version>
		</dependency>

Jetbrains全家桶1年46,售后保障穩定
新建工程,導入sigar的jar包,然后編寫測試代碼獲取windows操作系統CPU信息:

package com.harderxin.test;

import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

import com.harderxin.util.SigarUtil;

public class TestSigar {
	public static void main(String[] args) {
		try {
			Sigar sigar = new Sigar();
			CpuPerc cpu = sigar.getCpuPerc();
			System.out.println(String.valueOf(cpu.getCombined()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

} 

運行上面一段代碼程序,會發現報錯了,錯誤信息如下:

原因是因為操作系統底層采用C語言實現,我們通過Sigar調用操作系統信息,實際上是通過JNI調用C語言相關的api,所以這里面需要用到dll文件,如果我們獲取windows操作系統信息,我們需要設置這幾個文件的環境變量:sigar-amd64-winnt.dll、sigar-x86-winnt.dll、sigar-x86-winnt.lib,如果需要獲取mac系統或者linux系統,那么需要相對應的*-mac.dll,*-linux.dll,*-unix.dll文件,這些文件可以在下載包的hyperic-sigar-1.6.4\sigar-bin\lib目錄中找到,環境 設變量設置方式可以有幾種,我是需要獲取windows系統信息,所以以windows操作系統為例,一種是直接將這三個文件放入jdk的bin目錄,第二種方式是把上面三個文件添加到c:\WINDOWS\system32目錄下,第三種方式就是在程序中通過System.setProperty(“java.library.path”, path)的方式動態設置環境變量,推薦使用第三種方式,如果用第三種方式,那么需要將相關文件放到工程目錄下。

我在工程中將文件放到了工程目錄的conf目錄下,使用了一個工具類來動態加載環境變量,代碼如下:

import java.io.File;

import org.apache.log4j.Logger;
import org.hyperic.sigar.win32.EventLog;
import org.hyperic.sigar.win32.Win32Exception;

/**
 * Support classes using the sigar package
 * 
 * @author phil.wu
 */
public class SigarUtil {

	private static Logger logger = Logger.getLogger(SigarUtil.class.getName());

	/**
	 * set sigar package system variable
	 */
	public static final void setSystemVariable() {
		try {
			String classPath = System.getProperty("user.dir") + File.separator + "conf";
			String path = System.getProperty("java.library.path");
			if (OsCheck.getOperatingSystemType() == OsCheck.OSType.Windows) {
				path += ";" + classPath;
			} else {
				path += ":" + classPath;
			}
			System.setProperty("java.library.path", path);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

加上這個輔助類之后,我們在源程序中添加一行代碼,就可以獲取到系統信息了:

package com.harderxin.test;

import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

import com.harderxin.util.SigarUtil;

public class TestSigar {
	public static void main(String[] args) {
		try {
			//set sigar variable
			SigarUtil.setSystemVariable();
			Sigar sigar = new Sigar();
			CpuPerc cpu = sigar.getCpuPerc();
			System.out.println(String.valueOf(cpu.getCombined()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

} 

1、使用Sigar api獲取操作系統應用日志信息,還可以獲取安全日志信息、系統日志信息等:

import org.hyperic.sigar.win32.EventLog;
import org.hyperic.sigar.win32.EventLogRecord;
import org.hyperic.sigar.win32.Win32Exception;

import com.harderxin.util.SigarUtil;

public class EventLogInfoTest {
	public static void main(String[] args) throws Exception {
		SigarUtil.setSystemVariable();
		EventLogRecord record;
		EventLog log = new EventLog();
		log.open(EventLog.APPLICATION);
		int oldestRecord = log.getOldestRecord();
		System.out.println("oldestRecord : " + oldestRecord);
		int numRecords = log.getNumberOfRecords();
		System.out.println("numRecords : " + numRecords);
		int newsRecords = log.getNewestRecord();
		System.out.println("newsRecords : " + newsRecords);
		record = log.read(newsRecords);
		System.out.println(record.getComputerName() + "--" + record.getEventId() + "--" + record.getEventType()
		+ "--" + record.getMessage());
		
		for (int i = oldestRecord; i < newsRecords; i++) {
			try {
				record = log.read(i);
				System.out.println(record.getComputerName() + "--" + record.getEventId() + "--" + record.getEventType()
						+ "--" + record.getMessage());
			} catch (Win32Exception e) {
				e.printStackTrace();
			}
		}
		log.close();
	}
}

2、使用Sigar api獲取CPU使用率信息

	// 打印cpu的使用率
	private void printCpuPerc(CpuPerc cpuPerc) {
		String cpuPercUser = CpuPerc.format(cpuPerc.getUser());// 用戶使用率
		String cpuPercSys = CpuPerc.format(cpuPerc.getSys());// 用戶使用率
		String cpuPercWait = CpuPerc.format(cpuPerc.getWait());// 用戶使用率
		String cpuPercNice = CpuPerc.format(cpuPerc.getNice());// 用戶使用率
		String cpuPercIdle = CpuPerc.format(cpuPerc.getIdle());// 用戶使用率
		String cpuPercCombined = CpuPerc.format(cpuPerc.getCombined());// 用戶使用率

		System.out.println("用戶使用率:" + cpuPercUser);
		System.out.println("系統使用率:" + cpuPercSys);// 系統使用率
		System.out.println("當前等待率:" + cpuPercWait);// 當前等待率
		System.out.println("Nice :" + cpuPercNice);//
		System.out.println("當前空閑率:" + cpuPercIdle);// 當前空閑率
		System.out.println("總的使用率:" + cpuPercCombined);// 總的使用率
		System.out.println("**************");
	}

3、使用Sigar api獲取內存資源信息:

	// 物理內存信息
	public void getPhyssicalMemory() {
		DecimalFormat df = new DecimalFormat("#0.00");
		Sigar sigar = new Sigar();
		Mem mem;
		try {
			mem = sigar.getMem();
			// 內存總量
			String memTotal = df.format((float) mem.getTotal() / 1024 / 1024 / 1024) + "G";
			// 當前內存使用量
			String memUsed = df.format((float) mem.getUsed() / 1024 / 1024 / 1024) + "G";
			// 當前內存剩余量
			String memFree = df.format((float) mem.getFree() / 1024 / 1024 / 1024) + "G";

			// 系統頁面文件交換區信息
			Swap swap = sigar.getSwap();
			// 交換區總量
			String swapTotal = df.format((float) swap.getTotal() / 1024 / 1024 / 1024) + "G";
			// 當前交換區使用量
			String swapUsed = df.format((float) swap.getUsed() / 1024 / 1024 / 1024) + "G";
			// 當前交換區剩余量
			String swapFree = df.format((float) swap.getFree() / 1024 / 1024 / 1024) + "G";

			// 打印信息
			System.out.println("內存總量:" + memTotal);
			System.out.println("當前內存使用量:" + memUsed);
			System.out.println("當前內存剩余量:" + memFree);
			System.out.println("交換區總量:" + swapTotal);
			System.out.println("當前交換區使用量:" + swapUsed);
			System.out.println("當前交換區剩余量:" + swapFree);

		} catch (SigarException e) {
			e.printStackTrace();
		}
	}

其他Demo案例以及Sigar的jar包和dll依賴文件,請在我的資源庫中進行下載:點擊我進行下載

sigar學習參考博文:http://www.cnblogs.com/mr-totoro/p/4974979.html

總結

以上是生活随笔為你收集整理的使用Sigar包获取操作系统信息[通俗易懂]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。