获取磁盘信息代码
// 取硬盘已有的分区及其详细信息(通过sigar.getFileSystemList()来获得FileSystem列表对象,然后对其进行编历
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());
}
获取网络信息代码
// 当前机器的正式域名
try {
print(InetAddress.getLocalHost().getCanonicalHostName());
} catch (UnknownHostException e) {
try {
print(sigar.getFQDN());
} catch (SigarException ex) {
} finally {
sigar.close();
}
}
// 取到当前机器的IP地址
String address = null;
try {
address = InetAddress.getLocalHost().getHostAddress();
// 没有出现异常而正常当取到的IP时,如果取到的不是网卡循回地址时就返回
// 否则再通过Sigar工具包中的方法来获取
print(address);
if (!NetFlags.LOOPBACK_ADDRESS.equals(address)) {
}
} catch (UnknownHostException e) {
// hostname not in DNS or /etc/hosts
}
try {
address = sigar.getNetInterfaceConfig().getAddress();
} catch (SigarException e) {
address = NetFlags.LOOPBACK_ADDRESS;
} finally {
}
print(address);
// 取到当前机器的MAC地址
String[] ifaces = sigar.getNetInterfaceList();
String hwaddr = null;
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;
}
hwaddr = cfg.getHwaddr();
print(hwaddr);
// break;
}
print(hwaddr != null ? hwaddr : null);
// 获取网络流量等信息
String ifNames[] = sigar.getNetInterfaceList();
for (int i = 0; i < ifNames.length; i++) {
String name = ifNames[i];
NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
print("\nname = " + name);// 网络设备名
print("Address = " + ifconfig.getAddress());// IP地址
print("Netmask = " + ifconfig.getNetmask());// 子网掩码
if ((ifconfig.getFlags() & 1L) <= 0L) {
print("!IFF_UP...skipping getNetInterfaceStat");
continue;
}
try {
NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
print("RxPackets = " + ifstat.getRxPackets());// 接收的总包裹数
print("TxPackets = " + ifstat.getTxPackets());// 发送的总包裹数
print("RxBytes = " + ifstat.getRxBytes());// 接收到的总字节数
print("TxBytes = " + ifstat.getTxBytes());// 发送的总字节数
print("RxErrors = " + ifstat.getRxErrors());// 接收到的错误包数
print("TxErrors = " + ifstat.getTxErrors());// 发送数据包时的错误数
print("RxDropped = " + ifstat.getRxDropped());// 接收时丢弃的包数
print("TxDropped = " + ifstat.getTxDropped());// 发送时丢弃的包数
} catch (SigarNotImplementedException e) {
} catch (SigarException e) {
print(e.getMessage());
}
}
// 一些其他的信息
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;
}
print("cfg.getAddress() = " + cfg.getAddress());// IP地址
print("cfg.getBroadcast() = " + cfg.getBroadcast());// 网关广播地址
print("cfg.getHwaddr() = " + cfg.getHwaddr());// 网卡MAC地址
print("cfg.getNetmask() = " + cfg.getNetmask());// 子网掩码
System.out
.println("cfg.getDescription() = " + cfg.getDescription());// 网卡描述信息
print("cfg.getType() = " + cfg.getType());//
System.out
.println("cfg.getDestination() = " + cfg.getDestination());
print("cfg.getFlags() = " + cfg.getFlags());//
print("cfg.getMetric() = " + cfg.getMetric());
print("cfg.getMtu() = " + cfg.getMtu());
print("cfg.getName() = " + cfg.getName());
}
三、sigar的包及相关文件
该类需要引入jar包,另外为了防止程序运行时的错误,还需要相关文件,进入下载
--------------------------------------分割线 --------------------------------------