java获取服务器基本信息的方法
程序员文章站
2024-03-05 21:25:37
本文实例讲述了java获取服务器基本信息的方法。分享给大家供大家参考。具体如下:
利用第三方的jar包:(hyperic-hq官方网站:http://www.hyperi...
本文实例讲述了java获取服务器基本信息的方法。分享给大家供大家参考。具体如下:
利用第三方的jar包:(hyperic-hq官方网站:http://www.hyperic.com) 通过hyperic-hq产品的基础包sigar.jar来实现服务器状态数据的获取。sigar.jar包是通过本地方法来调用操作系统api来获取系统相关数据。windows操作系统下sigar.jar依赖sigar-amd64-winnt.dll或sigar-x86-winnt.dll,linux 操作系统下则依赖libsigar-amd64-linux.so或libsigar-x86-linux.so
import java.net.inetaddress; import java.net.unknownhostexception; import org.hyperic.sigar.cpuinfo; import org.hyperic.sigar.cpuperc; import org.hyperic.sigar.filesystem; import org.hyperic.sigar.filesystemusage; import org.hyperic.sigar.mem; import org.hyperic.sigar.netflags; import org.hyperic.sigar.netinterfaceconfig; import org.hyperic.sigar.netinterfacestat; import org.hyperic.sigar.operatingsystem; import org.hyperic.sigar.sigar; import org.hyperic.sigar.sigarexception; import org.hyperic.sigar.sigarnotimplementedexception; import org.hyperic.sigar.swap; public class sysinfo { // 1.cpu资源信息 // a)cpu数量(单位:个) public static int getcpucount() throws sigarexception { sigar sigar = new sigar(); try { return sigar.getcpuinfolist().length; } finally { sigar.close(); } } // b)cpu的总量(单位:hz)及cpu的相关信息 public void getcputotal() { sigar sigar = new sigar(); cpuinfo[] infos; try { infos = sigar.getcpuinfolist(); for (int i = 0; i < infos.length; i++) {// 不管是单块cpu还是多cpu都适用 cpuinfo info = infos[i]; system.out.println("mhz=" + info.getmhz());// cpu的总量mhz system.out.println("vendor=" + info.getvendor());// 获得cpu的卖主,如:intel system.out.println("model=" + info.getmodel());// 获得cpu的类别,如:celeron system.out.println("cache size=" + info.getcachesize());// 缓冲存储器数量 } } catch (sigarexception e) { e.printstacktrace(); } } // c)cpu的用户使用量、系统使用剩余量、总的剩余量、总的使用占用量等(单位:100%) public void testcpuperc() { sigar sigar = new sigar(); // 方式一,主要是针对一块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(); return; } for (int i = 0; i < cpulist.length; i++) { printcpuperc(cpulist[i]); } } private void printcpuperc(cpuperc cpu) { system.out.println("user :" + cpuperc.format(cpu.getuser()));// 用户使用率 system.out.println("sys :" + cpuperc.format(cpu.getsys()));// 系统使用率 system.out.println("wait :" + cpuperc.format(cpu.getwait()));// 当前等待率 system.out.println("nice :" + cpuperc.format(cpu.getnice()));// system.out.println("idle :" + cpuperc.format(cpu.getidle()));// 当前空闲率 system.out.println("total :" + cpuperc.format(cpu.getcombined()));// 总的使用率 } // 2.内存资源信息 public void getphysicalmemory() { // a)物理内存信息 sigar sigar = new sigar(); mem mem; try { mem = sigar.getmem(); // 内存总量 system.out.println("total = " + mem.gettotal() / 1024l + "k av"); // 当前内存使用量 system.out.println("used = " + mem.getused() / 1024l + "k used"); // 当前内存剩余量 system.out.println("free = " + mem.getfree() / 1024l + "k free"); // b)系统页面文件交换区信息 swap swap = sigar.getswap(); // 交换区总量 system.out.println("total = " + swap.gettotal() / 1024l + "k av"); // 当前交换区使用量 system.out.println("used = " + swap.getused() / 1024l + "k used"); // 当前交换区剩余量 system.out.println("free = " + swap.getfree() / 1024l + "k free"); } catch (sigarexception e) { e.printstacktrace(); } } // 3.操作系统信息 // a)取到当前操作系统的名称: public string getplatformname() { string hostname = ""; try { hostname = inetaddress.getlocalhost().gethostname(); } catch (exception exc) { sigar sigar = new sigar(); try { hostname = sigar.getnetinfo().gethostname(); } catch (sigarexception e) { hostname = "localhost.unknown"; } finally { sigar.close(); } } return hostname; } // b)取当前操作系统的信息 public void testgetosinfo() { operatingsystem os = operatingsystem.getinstance(); // 操作系统内核类型如: 386、486、586等x86 system.out.println("os.getarch() = " + os.getarch()); system.out.println("os.getcpuendian() = " + os.getcpuendian());// system.out.println("os.getdatamodel() = " + os.getdatamodel());// // 系统描述 system.out.println("os.getdescription() = " + os.getdescription()); system.out.println("os.getmachine() = " + os.getmachine());// // 操作系统类型 system.out.println("os.getname() = " + os.getname()); system.out.println("os.getpatchlevel() = " + os.getpatchlevel());// // 操作系统的卖主 system.out.println("os.getvendor() = " + os.getvendor()); // 卖主名称 system.out .println("os.getvendorcodename() = " + os.getvendorcodename()); // 操作系统名称 system.out.println("os.getvendorname() = " + os.getvendorname()); // 操作系统卖主类型 system.out.println("os.getvendorversion() = " + os.getvendorversion()); // 操作系统的版本号 system.out.println("os.getversion() = " + os.getversion()); } // c)取当前系统进程表中的用户信息 public void testwho() { try { sigar sigar = new sigar(); org.hyperic.sigar.who[] who = sigar.getwholist(); if (who != null && who.length > 0) { for (int i = 0; i < who.length; i++) { system.out.println("\n~~~~~~~~~" + string.valueof(i)+ "~~~~~~~~~"); org.hyperic.sigar.who _who = who[i]; system.out.println("getdevice() = " + _who.getdevice()); system.out.println("gethost() = " + _who.gethost()); system.out.println("gettime() = " + _who.gettime()); // 当前系统进程表中的用户名 system.out.println("getuser() = " + _who.getuser()); } } } catch (sigarexception e) { e.printstacktrace(); } } // 4.资源信息(主要是硬盘) // a)取硬盘已有的分区及其详细信息(通过sigar.getfilesystemlist()来获得filesystem列表对象,然后对其进行编历): public void testfilesysteminfo() throws exception { sigar sigar = new sigar(); filesystem fslist[] = sigar.getfilesystemlist(); //string dir = system.getproperty("user.home");// 当前用户文件夹路径 for (int i = 0; i < fslist.length; i++) { system.out.println("\n~~~~~~~~~~" + i + "~~~~~~~~~~"); filesystem fs = fslist[i]; // 分区的盘符名称 system.out.println("fs.getdevname() = " + fs.getdevname()); // 分区的盘符名称 system.out.println("fs.getdirname() = " + fs.getdirname()); system.out.println("fs.getflags() = " + fs.getflags());// // 文件系统类型,比如 fat32、ntfs system.out.println("fs.getsystypename() = " + fs.getsystypename()); // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等 system.out.println("fs.gettypename() = " + fs.gettypename()); // 文件系统类型 system.out.println("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 : 本地硬盘 // 文件系统总大小 system.out.println(" total = " + usage.gettotal() + "kb"); // 文件系统剩余大小 system.out.println(" free = " + usage.getfree() + "kb"); // 文件系统可用大小 system.out.println(" avail = " + usage.getavail() + "kb"); // 文件系统已经使用量 system.out.println(" used = " + usage.getused() + "kb"); double usepercent = usage.getusepercent() * 100d; // 文件系统资源的利用率 system.out.println(" usage = " + 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(" diskreads = " + usage.getdiskreads()); system.out.println(" diskwrites = " + usage.getdiskwrites()); } return; } // 5.网络信息 // a)当前机器的正式域名 public string getfqdn() { sigar sigar = null; try { return inetaddress.getlocalhost().getcanonicalhostname(); } catch (unknownhostexception e) { try { sigar = new sigar(); return sigar.getfqdn(); } catch (sigarexception ex) { return null; } finally { sigar.close(); } } } // b)取到当前机器的ip地址 public string getdefaultipaddress() { string address = null; try { address = inetaddress.getlocalhost().gethostaddress(); // 没有出现异常而正常当取到的ip时,如果取到的不是网卡循回地址时就返回 // 否则再通过sigar工具包中的方法来获取 if (!netflags.loopback_address.equals(address)) { return address; } } catch (unknownhostexception e) { // hostname not in dns or /etc/hosts } sigar sigar = new sigar(); try { address = sigar.getnetinterfaceconfig().getaddress(); } catch (sigarexception e) { address = netflags.loopback_address; } finally { sigar.close(); } return address; } // c)取到当前机器的mac地址 public string getmac() { sigar sigar = null; try { sigar = new sigar(); 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; } /* * 如果存在多张网卡包括虚拟机的网卡,默认只取第一张网卡的mac地址,如果要返回所有的网卡(包括物理的和虚拟的)则可以修改方法的返回类型为数组或collection * ,通过在for循环里取到的多个mac地址。 */ hwaddr = cfg.gethwaddr(); break; } return hwaddr != null hwaddr : null; } catch (exception e) { return null; } finally { if (sigar != null) sigar.close(); } } // d)获取网络流量等信息 public void testnetiflist() 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); 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()); } } } void print(string msg) { system.out.println(msg); } // e)一些其他的信息 public void getethernetinfo() { sigar sigar = null; try { 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.getaddress() = " + cfg.getaddress());// ip地址 system.out .println("cfg.getbroadcast() = " + cfg.getbroadcast());// 网关广播地址 system.out.println("cfg.gethwaddr() = " + cfg.gethwaddr());// 网卡mac地址 system.out.println("cfg.getnetmask() = " + cfg.getnetmask());// 子网掩码 system.out.println("cfg.getdescription() = " + cfg.getdescription());// 网卡描述信息 system.out.println("cfg.gettype() = " + cfg.gettype());// system.out.println("cfg.getdestination() = " + cfg.getdestination()); system.out.println("cfg.getflags() = " + cfg.getflags());// system.out.println("cfg.getmetric() = " + cfg.getmetric()); system.out.println("cfg.getmtu() = " + cfg.getmtu()); system.out.println("cfg.getname() = " + cfg.getname()); system.out.println(); } } catch (exception e) { system.out.println("error while creating guid" + e); } finally { if (sigar != null) sigar.close(); } } }
希望本文所述对大家的java程序设计有所帮助。