Sigar簡介
Sigar是Hyperic-hq產品的基礎包,是Hyperic HQ主要的數據收集組件。它用來從許多平台收集系統和處理信息。
這些平台包括:Linux, Windows, Solaris, AIX, HP-UX, FreeBSD and Mac OSX。
Sigar有C,C#,Java和Perl API,java版的API為sigar.jar,sigar.jar的底層是用C語言編寫的,它通過本地方法來調用操作系統API來獲取系統相關數據。
(查看源碼,可以發現,各種獲取信息的方法都是native的接口,更多原理看這里:java中native關鍵字的用法 )
Sigar壓縮包下載
Hyperic-hq官方網站:http://www.hyperic.com
Sigar.jar下載地址:http://sigar.hyperic.com
備用下載地址:點擊下載
Sigar為不同平台提供的不同庫文件
這些文件在下載下來的壓縮包里
參考官方主頁上的配置項。
File | Language | Description | Required |
---|---|---|---|
sigar.jar | Java | Java API | Yes (for Java only) |
log4j.jar | Java | Java logging API | No |
libsigar-x86-linux.so | C | Linux AMD/Intel 32-bit | * |
libsigar-amd64-linux.so | C | Linux AMD/Intel 64-bit | * |
libsigar-ppc-linux.so | C | Linux PowerPC 32-bit | * |
libsigar-ppc64-linux.so | C | Linux PowerPC 64-bit | * |
libsigar-ia64-linux.so | C | Linux Itanium 64-bit | * |
libsigar-s390x-linux.so | C | Linux zSeries 64-bit | * |
sigar-x86-winnt.dll | C | Windows AMD/Intel 32-bit | * |
sigar-amd64-winnt.dll | C | Windows AMD/Intel 64-bit | * |
libsigar-ppc-aix-5.so | C | AIX PowerPC 32-bit | * |
libsigar-ppc64-aix-5.so | C | AIX PowerPC 64-bit | * |
libsigar-pa-hpux-11.sl | C | HP-UX PA-RISC 32-bit | * |
libsigar-ia64-hpux-11.sl | C | HP-UX Itanium 64-bt | * |
libsigar-sparc-solaris.so | C | Solaris Sparc 32-bit | * |
libsigar-sparc64-solaris.so | C | Solaris Sparc 64-bit | * |
libsigar-x86-solaris.so | C | Solaris AMD/Intel 32-bit | * |
libsigar-amd64-solaris.so | C | Solaris AMD/Intel 64-bit | * |
libsigar-universal-macosx.dylib | C | Mac OS X PowerPC/Intel 32-bit | * |
libsigar-universal64-macosx.dylib | C | Mac OS X PowerPC/Intel 64-bit | * |
libsigar-x86-freebsd-5.so | C | FreeBSD 5.x AMD/Intel 32-bit | * |
libsigar-x86-freebsd-6.so | C | FreeBSD 6.x AMD/Intel 64-bit | * |
libsigar-amd64-freebsd-6.so | C | FreeBSD 6.x AMD/Intel 64-bit | * |
Sigar API
Sigar API 提供一個方便的接口來收集系統信息,如:
◆系統內存,頁面交換,cpu,平均負載,運行時間,登錄信息
◆每個進程占用的內存,cpu,帳號信息,狀態,參數,環境,打開的文件
◆文件系統探測和度量
◆網絡接口探測,配置信息和度量
◆網絡路由和連接表
寫代碼前的准備
1.按照主頁上的說明解壓包后將相應的文件copy到java路徑。比如windows64位操作系統需要將lib中sigar-amd64-winnt.dll文件拷貝到java SDK目錄的bin內。
2.把上面的sigar.jar添加到項目里。
Sigar Java代碼使用示例
1、獲取CPU信息代碼
(1)代碼解析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
// CPU數量(單位:個)
int
cpuLength = sigar.getCpuInfoList().length;
print(cpuLength);
// CPU的總量(單位:HZ)及CPU的相關信息
CpuInfo infos[] = sigar.getCpuInfoList();
for
(
int
i =
0
; i < infos.length; i++) {
// 不管是單塊CPU還是多CPU都適用
CpuInfo info = infos[i];
print(
"mhz="
+ info.getMhz());
// CPU的總量MHz
print(
"vendor="
+ info.getVendor());
// 獲得CPU的賣主,如:Intel
print(
"model="
+ info.getModel());
// 獲得CPU的類別,如:Celeron
print(
"cache size="
+ info.getCacheSize());
// 緩沖存儲器數量
}
/** CPU的用戶使用量、系統使用剩余量、總的剩余量、總的使用占用量等(單位:100%) **/
// 方式一,主要是針對一塊CPU的情況
CpuPerc cpu;
try
{
cpu = sigar.getCpuPerc();
printCpuPerc(cpu);
}
catch
(SigarException e) {
e.printStackTrace();
}
// 方式二,不管是單塊CPU還是多CPU都適用
CpuPerc cpuList[] =
null
;
try
{
cpuList = sigar.getCpuPercList();
}
catch
(SigarException e) {
e.printStackTrace();
}
for
(
int
i =
0
; i < cpuList.length; i++) {
// printCpuPerc(cpuList[i]);
}
|
(2)靜態工具類合成
/** * 靜態工具類獲取cpu的信息 * @throws SigarException */ private static void cpu() throws SigarException { Sigar sigar = new Sigar(); //CPU的總量(單位:HZ)及CPU的相關信息 CpuInfo infos[] = sigar.getCpuInfoList(); CpuPerc cpuList[] = null; cpuList = sigar.getCpuPercList(); for (int i = 0; i < infos.length; i++) {// 不管是單塊CPU還是多CPU都適用 CpuInfo info = infos[i]; System.out.println("第" + (i + 1) + "塊CPU信息"); System.out.println("CPU的總量MHz: " + info.getMhz());// CPU的總量MHz System.out.println("CPU生產商: " + info.getVendor());// 獲得CPU的賣主,如:Intel System.out.println("CPU類別: " + info.getModel());// 獲得CPU的類別,如:Celeron System.out.println("CPU緩存數量: " + info.getCacheSize());// 緩沖存儲器數量 //當前CPU的用戶使用率、系統使用率、當前等待率、當前空閑率、總的使用率 printCpuPerc(cpuList[i]); } } /** * 靜態工具類:獲取當前CPU的用戶使用率、系統使用率、當前等待率、當前空閑率、總的使用率 * @param cpu:當前CPU */ private static void printCpuPerc(CpuPerc cpu) { System.out.println("CPU用戶使用率: " + CpuPerc.format(cpu.getUser()));// 用戶使用率 System.out.println("CPU系統使用率: " + CpuPerc.format(cpu.getSys()));// 系統使用率 System.out.println("CPU當前等待率: " + CpuPerc.format(cpu.getWait()));// 當前等待率 System.out.println("CPU當前錯誤率: " + CpuPerc.format(cpu.getNice()));//當前錯誤率 System.out.println("CPU當前空閑率: " + CpuPerc.format(cpu.getIdle()));// 當前空閑率 System.out.println("CPU總的使用率: " + CpuPerc.format(cpu.getCombined()));// 總的使用率 }
2、獲取內存信息代碼
(1)代碼解析
// 物理內存信息 Mem mem = sigar.getMem(); // 內存總量 print("Total = " + mem.getTotal() / 1024L / 1024 + "M av"); // 當前內存使用量 print("Used = " + mem.getUsed() / 1024L / 1024 + "M used"); // 當前內存剩余量 print("Free = " + mem.getFree() / 1024L / 1024 + "M free"); // 系統頁面文件交換區信息 Swap swap = sigar.getSwap(); // 交換區總量 print("Total = " + swap.getTotal() / 1024L + "K av"); // 當前交換區使用量 print("Used = " + swap.getUsed() / 1024L + "K used"); // 當前交換區剩余量 print("Free = " + swap.getFree() / 1024L + "K free");
(2)靜態工具類合成
/** * 靜態工具類:獲取內存信息 * @throws SigarException */ private static void memory() throws SigarException { Sigar sigar = new Sigar(); // 物理內存信息 Mem mem = sigar.getMem(); // 內存總量 System.out.println("內存總量: " + mem.getTotal() / 1024L + "K av"); // 當前內存使用量 System.out.println("當前內存使用量: " + mem.getUsed() / 1024L + "K used"); // 當前內存剩余量 System.out.println("當前內存剩余量: " + mem.getFree() / 1024L + "K free"); //系統頁面文件交換區信息 Swap swap = sigar.getSwap(); // 交換區總量 System.out.println("交換區總量: " + swap.getTotal() / 1024L + "K av"); // 當前交換區使用量 System.out.println("當前交換區使用量: " + swap.getUsed() / 1024L + "K used"); // 當前交換區剩余量 System.out.println("當前交換區剩余量: " + swap.getFree() / 1024L + "K free"); }
3、獲取操作系統信息代碼
(1)代碼解析
<span style="white-space: normal; "> </span><span style="white-space: normal; ">// 取到當前操作系統的名稱</span> String hostname = ""; try { hostname = InetAddress.getLocalHost().getHostName(); } catch (Exception exc) { try { hostname = sigar.getNetInfo().getHostName(); } catch (SigarException e) { hostname = "localhost.unknown"; } finally { sigar.close(); } } print(hostname); // 取當前操作系統的信息 OperatingSystem OS = OperatingSystem.getInstance(); // 操作系統內核類型如: 386、486、586等x86 print("OS.getArch() = " + OS.getArch()); print("OS.getCpuEndian() = " + OS.getCpuEndian());// print("OS.getDataModel() = " + OS.getDataModel());// // 系統描述 print("OS.getDescription() = " + OS.getDescription()); print("OS.getMachine() = " + OS.getMachine());// // 操作系統類型 print("OS.getName() = " + OS.getName()); print("OS.getPatchLevel() = " + OS.getPatchLevel());// // 操作系統的賣主 print("OS.getVendor() = " + OS.getVendor()); // 賣主名稱 System.out.println("OS.getVendorCodeName() = " + OS.getVendorCodeName()); // 操作系統名稱 print("OS.getVendorName() = " + OS.getVendorName()); // 操作系統賣主類型 print("OS.getVendorVersion() = " + OS.getVendorVersion()); // 操作系統的版本號 print("OS.getVersion() = " + OS.getVersion()); // 取當前系統進程表中的用戶信息 Who who[] = sigar.getWhoList(); if (who != null && who.length > 0) { for (int i = 0; i < who.length; i++) { print("\n~~~~~~~~~" + String.valueOf(i) + "~~~~~~~~~"); Who _who = who[i]; print("getDevice() = " + _who.getDevice()); print("getHost() = " + _who.getHost()); print("getTime() = " + _who.getTime()); // 當前系統進程表中的用戶名 print("getUser() = " + _who.getUser()); } }
(2)靜態工具類合成
/** * 靜態工具類:獲取操作系統信息代碼 */ private static void os() { // 取當前操作系統的信息 OperatingSystem OS = OperatingSystem.getInstance(); // 操作系統內核類型如: 386、486、586等x86 System.out.println("操作系統: " + OS.getArch()); System.out.println("操作系統CpuEndian(): " + OS.getCpuEndian());// System.out.println("操作系統DataModel(): " + OS.getDataModel());// // 系統描述 System.out.println("操作系統的描述: " + OS.getDescription()); // 操作系統類型 System.out.println("OS.getName(): " + OS.getName()); System.out.println("OS.getPatchLevel(): " + OS.getPatchLevel());// // 操作系統的賣主 System.out.println("操作系統的賣主: " + OS.getVendor()); // 賣主名稱 System.out.println("操作系統的賣主名: " + OS.getVendorCodeName()); // 操作系統名稱 System.out.println("操作系統名稱: " + OS.getVendorName()); // 操作系統賣主類型 System.out.println("操作系統賣主類型: " + OS.getVendorVersion()); // 操作系統的版本號 System.out.println("操作系統的版本號: " + OS.getVersion()); }
4、獲取當前系統進程表中的用戶信息
(1)代碼解析
// 取當前系統進程表中的用戶信息 Who who[] = sigar.getWhoList(); if (who != null && who.length > 0) { for (int i = 0; i < who.length; i++) { print("\n~~~~~~~~~" + String.valueOf(i) + "~~~~~~~~~"); Who _who = who[i]; print("getDevice() = " + _who.getDevice()); print("getHost() = " + _who.getHost()); print("getTime() = " + _who.getTime()); // 當前系統進程表中的用戶名 print("getUser() = " + _who.getUser()); } }
(2)靜態工具類合成
/** * 靜態工具類:取當前系統進程表中的用戶信息 * @throws SigarException */ private static void who() throws SigarException { Sigar sigar = new Sigar(); Who who[] = sigar.getWhoList(); if (who != null && who.length > 0) { for (int i = 0; i < who.length; i++) { System.out.println("當前系統進程表中的用戶名" + String.valueOf(i)); Who _who = who[i]; System.out.println("用戶控制台: " + _who.getDevice()); System.out.println("用戶host: " + _who.getHost()); System.out.println("getTime(): " + _who.getTime()); // 當前系統進程表中的用戶名 System.out.println("當前系統進程表中的用戶名: " + _who.getUser()); } } }
5、獲取磁盤信息代碼
(1)代碼解析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<span style=
"white-space: normal; "
>
// 取硬盤已有的分區及其詳細信息(通過sigar.getFileSystemList()來獲得FileSystem列表對象,然后對其進行編歷</span>
FileSystem fslist[] = sigar.getFileSystemList();
String dir = System.getProperty(
"user.home"
);
// 當前用戶文件夾路徑
print(dir +
" "
+ fslist.length);
for
(
int
i =
0
; i < fslist.length; i++) {
print(
"\n~~~~~~~~~~"
+ i +
"~~~~~~~~~~"
);
FileSystem fs = fslist[i];
// 分區的盤符名稱
print(
"fs.getDevName() = "
+ fs.getDevName());
// 分區的盤符名稱
print(
"fs.getDirName() = "
+ fs.getDirName());
print(
"fs.getFlags() = "
+ fs.getFlags());
//
// 文件系統類型,比如 FAT32、NTFS
print(
"fs.getSysTypeName() = "
+ fs.getSysTypeName());
// 文件系統類型名,比如本地硬盤、光驅、網絡文件系統等
print(
"fs.getTypeName() = "
+ fs.getTypeName());
// 文件系統類型
print(
"fs.getType() = "
+ fs.getType());
FileSystemUsage usage =
null
;
try
{
usage = sigar.getFileSystemUsage(fs.getDirName());
}
catch
(SigarException e) {
if
(fs.getType() ==
2
)
throw
e;
continue
;
}
switch
(fs.getType()) {
case
0
:
// TYPE_UNKNOWN :未知
break
;
case
1
:
// TYPE_NONE
break
;
case
2
:
// TYPE_LOCAL_DISK : 本地硬盤
// 文件系統總大小
print(
" Total = "
+ usage.getTotal() +
"KB"
);
// 文件系統剩余大小
print(
" Free = "
+ usage.getFree() +
"KB"
);
// 文件系統可用大小
print(
" Avail = "
+ usage.getAvail() +
"KB"
);
// 文件系統已經使用量
print(
" Used = "
+ usage.getUsed() +
"KB"
);
double
usePercent = usage.getUsePercent() * 100D;
// 文件系統資源的利用率
print(
" Usage = "
+ usePercent +
"%"
);
break
;
case
3
:
// TYPE_NETWORK :網絡
break
;
case
4
:
// TYPE_RAM_DISK :閃存
break
;
case
5
:
// TYPE_CDROM :光驅
break
;
case
6
:
// TYPE_SWAP :頁面交換
break
;
}
print(
" DiskReads = "
+ usage.getDiskReads());
print(
" DiskWrites = "
+ usage.getDiskWrites());
}
|
(2)靜態工具類合成
/** * 靜態工具類:獲取磁盤信息 * @throws Exception */ private static void file() throws Exception { Sigar sigar = new Sigar(); //通過sigar.getFileSystemList()來獲得FileSystem列表對象,然后對其進行編歷 FileSystem fslist[] = sigar.getFileSystemList(); for (int i = 0; i < fslist.length; i++) { System.out.println("分區的盤符名稱" + i); FileSystem fs = fslist[i]; // 分區的盤符名稱 System.out.println("盤符名稱: " + fs.getDevName()); // 分區的盤符名稱 System.out.println("盤符路徑: " + fs.getDirName()); System.out.println("盤符標志: " + fs.getFlags());// // 文件系統類型,比如 FAT32、NTFS System.out.println("盤符類型: " + fs.getSysTypeName()); // 文件系統類型名,比如本地硬盤、光驅、網絡文件系統等 System.out.println("盤符類型名: " + fs.getTypeName()); // 文件系統類型 System.out.println("盤符文件系統類型: " + fs.getType()); FileSystemUsage usage = null; usage = sigar.getFileSystemUsage(fs.getDirName()); switch (fs.getType()) { case 0: // TYPE_UNKNOWN :未知 break; case 1: // TYPE_NONE break; case 2: // TYPE_LOCAL_DISK : 本地硬盤 // 文件系統總大小 System.out.println(fs.getDevName() + "總大小: " + usage.getTotal() + "KB"); // 文件系統剩余大小 System.out.println(fs.getDevName() + "剩余大小: " + usage.getFree() + "KB"); // 文件系統可用大小 System.out.println(fs.getDevName() + "可用大小: " + usage.getAvail() + "KB"); // 文件系統已經使用量 System.out.println(fs.getDevName() + "已經使用量: " + usage.getUsed() + "KB"); double usePercent = usage.getUsePercent() * 100D; // 文件系統資源的利用率 System.out.println(fs.getDevName() + "資源的利用率: " + usePercent + "%"); break; case 3:// TYPE_NETWORK :網絡 break; case 4:// TYPE_RAM_DISK :閃存 break; case 5:// TYPE_CDROM :光驅 break; case 6:// TYPE_SWAP :頁面交換 break; } System.out.println(fs.getDevName() + "讀出: " + usage.getDiskReads()); System.out.println(fs.getDevName() + "寫入: " + usage.getDiskWrites()); } return; }
6、獲取System信息代碼(從JVM獲取)
(1)靜態工具類合成
/** * 靜態工具類:獲取當前(操作系統)信息,從jvm獲取 * @throws UnknownHostException */ private static void property() throws UnknownHostException { Runtime r = Runtime.getRuntime(); Properties props = System.getProperties(); InetAddress addr; addr = InetAddress.getLocalHost(); String ip = addr.getHostAddress(); Map<String, String> map = System.getenv(); String userName = map.get("USERNAME");// 獲取用戶名 String computerName = map.get("COMPUTERNAME");// 獲取計算機名 String userDomain = map.get("USERDOMAIN");// 獲取計算機域名 System.out.println("用戶名: " + userName); System.out.println("計算機名: " + computerName); System.out.println("計算機域名: " + userDomain); System.out.println("本地ip地址: " + ip); System.out.println("本地主機名: " + addr.getHostName()); System.out.println("JVM可以使用的總內存: " + r.totalMemory()); System.out.println("JVM可以使用的剩余內存: " + r.freeMemory()); System.out.println("JVM可以使用的處理器個數: " + r.availableProcessors()); System.out.println("Java的運行環境版本: " + props.getProperty("java.version")); System.out.println("Java的運行環境供應商: " + props.getProperty("java.vendor")); System.out.println("Java供應商的URL: " + props.getProperty("java.vendor.url")); System.out.println("Java的安裝路徑: " + props.getProperty("java.home")); System.out.println("Java的虛擬機規范版本: " + props.getProperty("java.vm.specification.version")); System.out.println("Java的虛擬機規范供應商: " + props.getProperty("java.vm.specification.vendor")); System.out.println("Java的虛擬機規范名稱: " + props.getProperty("java.vm.specification.name")); System.out.println("Java的虛擬機實現版本: " + props.getProperty("java.vm.version")); System.out.println("Java的虛擬機實現供應商: " + props.getProperty("java.vm.vendor")); System.out.println("Java的虛擬機實現名稱: " + props.getProperty("java.vm.name")); System.out.println("Java運行時環境規范版本: " + props.getProperty("java.specification.version")); System.out.println("Java運行時環境規范供應商: " + props.getProperty("java.specification.vender")); System.out.println("Java運行時環境規范名稱: " + props.getProperty("java.specification.name")); System.out.println("Java的類格式版本號: " + props.getProperty("java.class.version")); System.out.println("Java的類路徑: " + props.getProperty("java.class.path")); System.out.println("加載庫時搜索的路徑列表: " + props.getProperty("java.library.path")); System.out.println("默認的臨時文件路徑: " + props.getProperty("java.io.tmpdir")); System.out.println("一個或多個擴展目錄的路徑: " + props.getProperty("java.ext.dirs")); System.out.println("操作系統的名稱: " + props.getProperty("os.name")); System.out.println("操作系統的構架: " + props.getProperty("os.arch")); System.out.println("操作系統的版本: " + props.getProperty("os.version")); System.out.println("文件分隔符: " + props.getProperty("file.separator")); System.out.println("路徑分隔符: " + props.getProperty("path.separator")); System.out.println("行分隔符: " + props.getProperty("line.separator")); System.out.println("用戶的賬戶名稱: " + props.getProperty("user.name")); System.out.println("用戶的主目錄: " + props.getProperty("user.home")); System.out.println("用戶的當前工作目錄: " + props.getProperty("user.dir")); }
7、獲取網絡流量等信息代碼
/** * 靜態工具類:獲取網絡流量等信息 * @throws Exception */ private static void net() throws Exception { Sigar sigar = new Sigar(); String ifNames[] = sigar.getNetInterfaceList(); for (int i = 0; i < ifNames.length; i++) { String name = ifNames[i]; NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name); System.out.println("網絡設備名: " + name);// 網絡設備名 System.out.println("IP地址: " + ifconfig.getAddress());// IP地址 System.out.println("子網掩碼: " + ifconfig.getNetmask());// 子網掩碼 if ((ifconfig.getFlags() & 1L) <= 0L) { System.out.println("!IFF_UP...skipping getNetInterfaceStat"); continue; } NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name); System.out.println(name + "接收的總包裹數:" + ifstat.getRxPackets());// 接收的總包裹數 System.out.println(name + "發送的總包裹數:" + ifstat.getTxPackets());// 發送的總包裹數 System.out.println(name + "接收到的總字節數:" + ifstat.getRxBytes());// 接收到的總字節數 System.out.println(name + "發送的總字節數:" + ifstat.getTxBytes());// 發送的總字節數 System.out.println(name + "接收到的錯誤包數:" + ifstat.getRxErrors());// 接收到的錯誤包數 System.out.println(name + "發送數據包時的錯誤數:" + ifstat.getTxErrors());// 發送數據包時的錯誤數 System.out.println(name + "接收時丟棄的包數:" + ifstat.getRxDropped());// 接收時丟棄的包數 System.out.println(name + "發送時丟棄的包數:" + ifstat.getTxDropped());// 發送時丟棄的包數 } }
7、獲取以太網信息代碼
/** * 靜態工具類:獲取以太網信息 * @throws SigarException */ private static void ethernet() throws SigarException { Sigar sigar = null; sigar = new Sigar(); String[] ifaces = sigar.getNetInterfaceList(); for (int i = 0; i < ifaces.length; i++) { NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]); if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0 || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) { continue; } System.out.println(cfg.getName() + "IP地址:" + cfg.getAddress());// IP地址 System.out.println(cfg.getName() + "網關廣播地址:" + cfg.getBroadcast());// 網關廣播地址 System.out.println(cfg.getName() + "網卡MAC地址:" + cfg.getHwaddr());// 網卡MAC地址 System.out.println(cfg.getName() + "子網掩碼:" + cfg.getNetmask());// 子網掩碼 System.out.println(cfg.getName() + "網卡描述信息:" + cfg.getDescription());// 網卡描述信息 System.out.println(cfg.getName() + "網卡類型" + cfg.getType());// } }
8、獲取用戶信息代碼
/** * 靜態工具類:取當前系統進程表中的用戶信息 * @throws SigarException */ private static void who() throws SigarException { Sigar sigar = new Sigar(); Who who[] = sigar.getWhoList(); if (who != null && who.length > 0) { for (int i = 0; i < who.length; i++) { System.out.println("當前系統進程表中的用戶名" + String.valueOf(i)); Who _who = who[i]; System.out.println("用戶控制台: " + _who.getDevice()); System.out.println("用戶host: " + _who.getHost()); System.out.println("getTime(): " + _who.getTime()); // 當前系統進程表中的用戶名 System.out.println("當前系統進程表中的用戶名: " + _who.getUser()); } } }