The example of this article tells the way Java obtains basic information of the server. Share it for everyone for your reference. The specifics are as follows:
Use a third-party jar package: (Hyperic-HQ official website: http://www.hyperic.com) to achieve server-state data obtaining through Sigar.jar of hyperic-HQ products. Sigar.jar package is to call the operating system API through local methods to obtain system -related data. Sigar.jar under Windows operating system relies on Sigar-AMD64-WINNT.DLL or Sigar-X86-Winnt.dll, while linux operating system relies on libsigar-amd64-Linux.so or libsigar-x86-linux.so
Import java.net.inetaddress; Import Java.net.unknownHostexception; Import org.Hyperic.sigar.cpuinfo; Import org.hyper.cpuperc; Import g.Hyperic.sigar.filesystem; Import org.Hyperic.sigar.filesystemusage; Import org.Hyperic.sigar.Mem; Import org.Hyperic.sigar.netflags; Import Org.Hyperic.sigar.netInterfaceConfig; Import CESTAT; Import org.Hyperic.sigar.operativeSystem; Import org.hyperic. SIGAR.SIGAR; Import org.Hyperic.sigarexception; Import org.Hyperic.sigarnotimeMenTException; Import ic class sysinfo {// 1.cpu resource information // a) CPU quantity (unit :) Public static int Getcpucount () Throws Sigarexception {sigar size = new sign (); Try {return size.getcpuinfolist (). LENGTH;} Finally {sigar. close ();}} // b) The total amount of CPU (Unit: Hz) The relevant information of and CPUs Public Void Getcputotal () {Sigar Sigar = new sign (); fos. length; i ++) {// Whether it is a single CPU or a multi -CPU, it is applicable to cpuinfo info = Infos [i]; System.out.println ("mHz ="+Info.getMHz ()); // The total amount of cpu mHz System.out.println ("Vendor =" + Info.getvendor ()); // Obtain the seller of the CPU, such as: Intel System.out.println ("Model =" + Info.getmodel ()); // The category, such as: Celeron system.out.println ("cache size =" + info.getCacheSize ()); // The quantity of buffer deposits}} Catch (Sigarexception E) {e.printstacktrace ();} // C) CPU's user usage, system use surplus, total remaining amount, total usage occupation, etc. (unit: 100%) Public void testcpuperc () {size size = new sign (); The situation of a CPU CPUPERC CPU; TRY {CPU = Sigar.Getcpuperc (); Printcpuperc (CPU);} Catch (Sigarexception E) {e.printstacktrace (); PU Suitable for cpuperc cpulist [] = null; try {cpulist = signCPuperClist ();} 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 ())); // The user use rate system m.out. Println ("SYS:" + CPUPERC.FORMAT (cpu.getsys ())); // System.out.println ("WAIT:" + CPUPERC.FORMAT (cpu.getwait ())); // Waiting rate system.out.println ("nice:" + cpuperc.Format (cpu.getnice ())); // System.out.println ("IDLE:" + CPUPERC.FORMAT (cpu.getidle ())); // The current free rate system.out.println ("Total:" + CPUPERC.FORMAT (cpu.getCombined ())); // Total usage} // 2. Memory resources information public void getphysicalMemory () {/// a) Physical memory information sigar size = new sign (); mem mem; try {memory = size.getMem (); // The total amount of memory system.out.println ("total =" + mem.gettotal () / 1024L + + "K av"); // The current memory usage system.out.println ("used =" + mem.getused () / 1024L + "k userd"); // The current memory remaining System.out.println ("" Free = " + MEM.Getfree () / 1024L +" K Free "); // b) System page File Switch Information Swap Swap = Sigar.getswap (); // The total amount of System.out.println (" Total = " + swap.gettotal () /) / 1024L +" K AV "); // The current exchange area uses systerm.out.println (" used = " + swap.getUset () / 1024L +" k use "); // The remaining amount of the current exchange area system.out.println ("free =" + swap.getFree () / 1024L + "K free"); The operating system information // a) Get the name of the current operating system: Public String Getplatformname () {String hostname = "" "" "; try {hostname = indadres.getLocalHost (). (Exception EXC) {Sigar sigar = new size (); try {hostname = size.getNetinfo (). Gethostname ();} Catch (sigarexception E) {hostname = "localhost.unknown";} Finally { sigar.close ();}} Return hostname; } // b) Take the information of the current operating system. Then, then Getarch () = " + OS.Getarch ()); System.out.println (" OS.Getcpuendian () = " + OS.Getcpuendian ()); // System.out.println (" OS.GetDataModel () = " + OS.GetDataModel ()); // // The system describes system.out.println (" Os.GetDescription () = " + OS.GetDescripting ()); = " + OS.Getmachine ()); // // The operating system type system.out.println (" os.getName () = " + OS.GetName ()); = " + OS.getPatchLevel ()); // // The seller System.out.println (" Os.getVendor () = " + OS.GetVendor ()); // "Os.getvendorCodename () =" + OS.GetVendorCodename ()); // The operating system name system.out.println ("os.getvendorname () =" + OS.GetVendorname ()); // EM .out.println ("Os.getvendorVersion () =" + Os.getvendorVersion ()); // The version number system.out.println ("os.getVersion () =" + OS.GetVersion ()); } // c) Take the user information in the current system process table. ! = NULL && WHO.LENGTH> 0) {for (int i = 0; i <ho.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 ()); // The user in the current system process table Name System.out.println ("Getuser () =" + _Who.getUser ());}}}} Catch (Sigarexception E) {e.printstacktrace ();} // 4. Resource information (mainly hard disk) / / A) Take the existing partition and details of the hard disk (through Sigar.getFilesystemList () to obtain the FileSystem list object, and then compile it): Public Void TestfilesystemsystemFo () Throws Exception {Sigar Sigar = New Sigar ( ); Filesystem fslist [] = size.getFilesystemList (); // string dir = system.getproperty ("user.home"); // The current user file path for (int i = 0; i <fslist.Length; I ++) {{ System.out.println ("/n ~~~~~~~~~~~~~~" + I + "~~~~~~~~~~~"); The name of the character system.out.println ("fs.getDevname () =" + fs.getDevname ()); // The name of the partition name system.out.println ("fs.getdirname () =" + fs.GetDiRName () ); System.out.println ("fs.getflags () =" + fs.getflags ()); // // File system type, such as fat32, ntfs system.out.println ("fs.getSYSTYPENAME ()) = " + fs.getSYSTYPENAME ()); // File system type names, such as local hard disk, optical drive, network file system and other system.out.println (" fs.gettypename () = " + fs.gettypename ()); // File system type System.out.println ("fs.gettype () =" + fs.gettype ()); iRName ());} Catch (Sigarexception E ) {if (fs.gettype () == 2) Throw e; Continue;} switch (fs.gettype ()) {case 0: // type_unknown: unknown break; case 1: // type_none break; case 2: / / / / / Type_local_disk: Local hard disk // File system Total size System.out.println ("Total =" + USAGE.Gettotal () + "Kb"); // The remaining size of the file system System.out.println ("free =" + " + USAGE.GetFree () + "Kb"); // The file system can use systerm.out.println ("avail =" + usage.getavail () + "kb"); // The file system has already used System.out. Println ("userd =" + usage.getused () + "kb"); Double usercent = usage.getUsepercent () * 100d; // The utilization rate of file system resources system.outln ("usage =" + usepercent + "%"); Break; Case 3: // Type_network: Network Break; Case 4: // Type_ram_disk: Case 5: // Type_cdrom: Case 6: // Type_swap: } Systemm .out.println ("diskReads =" + USAGE.Getdiskreams ()); System.out.println ("DiskWrites =" + usage.getdiskWrites ()); Information // A) Current The formal domain name of the machine Public String Getfqdn () {Sigar Sigar = NULL; Try {Return Inetaddress.getLocalHost (). GetcanonicalHostname ();} Catch (UNKNOWNHOTEXC event e) {try {size = new size (); return size.getfqdn () ;} Catch (Sigarexception EX) {Return Null;} Finally {sign.close ();} // b) Get the IP address of the current machine. s = null; TRY {address = indadress . AttlocalHost (). Gethostaddress (); // When the IP that is not abnormal and normal, if not the network card is not returned, it will return // then use the method in the SIGAR tool package to obtain if ( ! Netflags.Loopback_Address.equals (address) {Return Address;} Catch (unknownhostexception E) {// hostname not in dns or /etc /hosts} Sigar igar = new size (); try {address = size.getNetInterfaceConfig ( ) .getaddress ();} Catch (Sigarexception E) {address = netflags.loopback_address;} finally {sigar.close ();} Return address;} // Mac address public string getmac () {{) Sigar size = null; try {size = new size (); string [] ifaces = size.getNetNetInterfacelist (); string hwaddr = null; for (int i = 0; i <T 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;} / * * If there are multiple network cards, including network cards including virtual machines, only the MAC address of the first network card is taken by default. If you want to return all network cards (including physical and virtual) The return type of the method is array or collection *, by multiple MAC addresses obtained in the for loop. */ hwaddr = cfg.gethwadddr (); Break;} Return Hwaddr! = Null Hwaddr: Null;} Catch (Exception E) {RETURN NULL;} Finally {if (size! = null) sigar.close ();}} // d) Get information such as network traffic and other information. <iFNames.Length; I ++ ) {String name = iFNames [i]; NetinterfaceConfig iFconfig = Sigar.GetNetInterface (name); "/nname =" + name); // network device name ("Address = " + ifconfig.getaddress ()); // IP address print ("netmask =" + ifconfig.getnetmask ()); // The subnet mask if (iFconfig.getflags () & 1L) <= 0L) {Print ("! IFF_UP ... Skipping AT "); Continue;} Try {Netinterfacestat Ifstat = Sigar.getNetNetinterfacestat (name); Print (" rxpackets = " + iFSTATRXPACKETS ()); // xpackets = " + ifstat.gettxpackets ( ); // The total number of wraps sent ("rxbytes =" + ifstat.getrxbytes ()); // The total number of bytes received ("txbytes =" + ifstat.gettxbytes ()); // Send The total number of bytes of the byte ("rxerrs =" + ifStat.getrxerrs ()); // The number of error packs received ("txerror =" + ifstat.gettxerrs ()); // Print ("rxdropped =" + ifstat.getrxdropped ()); // The number of packets discarded when receiving ("txdropped =" + ifstat.gettxdropped ()); // MentedException E) {} Catch (Sigarexception E) {Print (e.getMessage ());}}} Void Print (String msg) {System.out.println (msg);} // E) etinfe () {Sigar size = null; try {size = new size (); string [] iFaces = Sigar.getNetNetInterfacelist (); for (int i = 0; i <i <i ++) {NetInterface Config CFG = Sigar.getNetinterfaceConfig (iFaces [i]); if (netflags.loopback_address.equals (cfg.getaddress ()) || (cfg.getflags () & netflags.iff_loopback)! .quals (cfg.gethwaddr ()))))))) {Continue;} System.out.println ("cfg.getaddress () =" + cfg.getaddress ()); // ip address system.outln ("cfg.getBroadcast () =" + cfg.getbroa dcast () ); // The gateway broadcast address system.out.println ("cfg.Gethwaddr () =" + cfg.Gethwaddr ()); // The network card mac address system.out.println ("cfg.GetNetMask () =" + cfg .getNetMask ()); // The subnet mask system.out.println ("cfg.getdescript () =" + cfg.getDescripting ()); e ( ) = " + 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.getName () =" + cfg.getName ()); System.out.println ();}} {System.out.println ("Error While Creating Guid " + E);} Finally {if (size! = Null) size.close ();}}}It is hoped that this article is helpful to everyone's Java program design.