Java Get server basic information method _java

Source: Internet
Author: User
Tags memory usage

This article illustrates how Java obtains server basic information. Share to everyone for your reference. Specifically as follows:

Use of Third-party jar Packs: (HYPERIC-HQ official website: http://www.hyperic.com) To achieve server state data acquisition through HYPERIC-HQ product Base package Sigar.jar. The Sigar.jar package is a local method that invokes the operating system API to obtain system-related data. Sigar.jar relies on Sigar-amd64-winnt.dll or sigar-x86-winnt.dll,linux under Windows operating systems Depending on the libsigar-amd64-linux.so or libsigar-x86-linux.so under the operating system

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 resource information//a) Number of CPUs (units:) public static int Getcpucount () throws Sigarexception {SIGAR Siga 
 R = new SIGAR (); 
 try {return sigar.getcpuinfolist (). length; 
 finally {sigar.close (); 
 }//b Total CPU (unit: HZ) and CPU related information public void Getcputotal () {SIGAR SIGAR = new SIGAR (); 
 Cpuinfo[] Infos; 
  try {infos = Sigar.getcpuinfolist (); for (int i = 0; i < infos.length; i++) {///whether a single CPU or multiple CPUs are applicable to CpuInfo info = infos[i]; System.out.println ("mhz=" + Info.getmhz ());//CPU total MHz SYSTEM.OUT.PRINTLN ("vendor=" + Info.getvendor ());//Get the vendor of the CPU, such as: Intel System.out.println ("model=" + Info.getmodel ());//Get CPU category, such as: Celeron System.out.println ("cache size=" + info 
 . Getcachesize ());//buffer memory number} catch (Sigarexception e) {e.printstacktrace (); 
 }//C CPU User usage, System usage remaining, total remaining, total usage, etc. (unit: 100%) public void Testcpuperc () {SIGAR SIGAR = new SIGAR (); 
 Mode one, mainly for a piece of CPU situation CPUPERC CPU; 
  try {CPU = SIGAR.GETCPUPERC (); 
 PRINTCPUPERC (CPU); 
 catch (Sigarexception e) {e.printstacktrace (); 
 }//Mode two, whether it is a single CPU or multiple CPUs are applicable 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 ())//User usage System.out.println ("Sys:" + Cpuperc.format (Cpu.getsys ()));//System Usage System.out.println ("Wait:" + Cpuperc.format (Cpu.getwait ()))//Current wait rate System.out.println ("nice:" + Cpuperc.format (Cpu.getnice ());//SYSTEM.OUT.P Rintln ("Idle:" + Cpuperc.format (Cpu.getidle ()));//Current Idle rate System.out.println ("total:" + cpuperc.format ( 
 Cpu.getcombined ())///Total usage}//2. Memory resource Information public void Getphysicalmemory () {//a) Physical memory information SIGAR SIGAR = new SIGAR (); 
 MEM Mem; 
  try {mem = Sigar.getmem (); 
  Total Memory System.out.println ("sum =" + mem.gettotal ()/1024L + "K av"); 
  Current Memory usage System.out.println ("Used =" + mem.getused ()/1024L + "K Used");  Current Memory surplus System.out.println ("free =" + mem.getfree ()/1024L + "K free"); 
  b) System paging File Exchange area Information swap = Sigar.getswap (); 
  Total System.out.println of the swap area ("sum =" + swap.gettotal ()/1024L + "K av"); 
  Current swap area usage System.out.println ("Used =" + swap.getused ()/1024L + "K Used"); Current swap area Remaining Amount SystEm.out.println ("free =" + swap.getfree ()/1024L + "K-free"); 
 catch (Sigarexception e) {e.printstacktrace (); 
 }//3. Operating System Information//a) take to the name of the current operating system: 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) Take the information of the current operating system public void Testgetosinfo () {OperatingSystem OS = operatingsystem.getinstance (); 
 Operating system kernel types such as: 386, 486, 586, etc. x86 System.out.println ("os.getarch () =" + Os.getarch ()); System.out.println ("Os.getcpuendian () =" + Os.getcpuendian ());//System.out.println ("os.getdatamodel () =" + Os.getdat 
 Amodel ());////System Description System.out.println ("os.getdescription () =" + os.getdescription ()); System.out.println ("os.getmachine () =" + Os.getmachine ());//operationSystem type SYSTEM.OUT.PRINTLN ("os.getname () =" + Os.getname ()); System.out.println ("os.getpatchlevel () =" + Os.getpatchlevel ());////the seller of the operating system System.out.println ("os.getvendor () =" 
 + Os.getvendor ()); 
 Seller name System.out. println ("os.getvendorcodename () =" + Os.getvendorcodename ()); 
 Operating system name System.out.println ("os.getvendorname () =" + Os.getvendorname ()); 
 Operating system vendor Type SYSTEM.OUT.PRINTLN ("os.getvendorversion () =" + os.getvendorversion ()); 
The operating system version number System.out.println ("os.getversion () =" + os.getversion ()); 
  //C) Take the user information in the current system process table 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 ()); 
  User name System.out.println in the current system process table ("getuser () =" + _who.getuser ()); 
 A catch (Sigarexception e) {e.printstacktrace ()); }//4. Resource information (primarily hard disk)//a) take the existing partition of the hard disk and its details (by Sigar.getfilesystemlist () to obtain the FileSystem list object and then make a calendar): public void 
 Testfilesysteminfo () throws Exception {SIGAR SIGAR = new SIGAR (); 
 FileSystem fslist[] = Sigar.getfilesystemlist (); String dir = system.getproperty ("User.home");//Current User folder path for (int i = 0; i < fslist.length; i++) {SYSTEM.OUT.P 
  Rintln ("\n~~~~~~~~~~" + i + "~~~~~~~~~~"); 
  FileSystem fs = Fslist[i]; 
  The partition's letter name System.out.println ("fs.getdevname () =" + Fs.getdevname ()); 
  The partition's letter 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.getsystypen 
  Ame () = "+ Fs.getsystypename ()); File system type name, such as local hard drive, optical drive, network file system, etc. System.out.println ("Fs.gettypename () =" + Fs.gettypename ()); 
  File system type 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: Unknown break; 
  Case 1://Type_none break; 
  Case 2://Type_local_disk: The total size of the local hard disk//File system System.out.println ("sum =" + usage.gettotal () + "KB"); 
  File system remaining size System.out.println ("free =" + usage.getfree () + "KB"); 
  File system available Size System.out.println ("avail =" + usage.getavail () + "KB"); 
  The file system has been used System.out.println ("Used =" + usage.getused () + "KB"); 
  Double usepercent = usage.getusepercent () * 100D; 
  Utilization of File system resources System.out.println ("Usage =" + usepercent + "%"); 
  Break 
  Case 3://type_network: Network break; 
  Case 4://Type_ram_disk: Flash break; 
  Case 5://Type_cdrom: optical drive break; CaSE 6://type_swap: page swap break; 
  } System.out.println ("diskreads =" + usage.getdiskreads ()); 
 System.out.println ("diskwrites =" + usage.getdiskwrites ()); 
} return; 
 //5. Network information//a) The current machine's official domain name 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 (); 
 The IP address of the current machine is public string getdefaultipaddress () {string addresses = null; 
  try {address = Inetaddress.getlocalhost (). gethostaddress (); No exception and normal when the IP is taken, if the fetch is not the network card back to the address of the return//or through the SIGAR Toolkit in the method to get 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 to address; 
 The MAC address public String Getmac () {SIGAR SIGAR = NULL for the current machine) is fetched 
  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;  
   * * * If there are multiple net cards including the network card of the virtual machine, by default, only the MAC address of the first net card is taken, and if all network adapters (both physical and virtual) are returned, the return type of the method can be changed to an array or collection *, through multiple MAC addresses that are fetched in the For loop. 
  * * hwaddr = CFG.GETHWADDR (); 
  Break 
 return hwaddr!= null hwaddr:null; 
 catch (Exception e) {return null; 
 Finally {if (SIGAR!= null) sigar.close (); 
 }//d) obtain information such as network traffic 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);//Network device name print ("address =" + ifconfig.getaddress ());//IP addresses print ("Netmask =" + ifconfig . Getnetmask ());//Subnet mask if ((Ifconfig.getflags () & 1L) <= 0L) {print (!) 
  Iff_up...skipping Getnetinterfacestat "); 
  Continue 
  try {netinterfacestat ifstat = sigar.getnetinterfacestat (name); Print ("rxpackets =" + ifstat.getrxpackets ())//Total number of packages received print ("txpackets =" + ifstat.gettxpackets ());//Total number of packages sent PR int ("rxbytes =" + ifstat.getrxbytes ()), the total number of bytes received print ("txbytes =" + ifstat.gettxbytes ());//Total Bytes sent print ("Rxer Rors = "+ ifstat.getrxerrors ()), number of error packets received print (" txerrors = "+ ifstat.gettxerrors ());//number of errors sent when packet print (" Rxdrop PED = "+ ifstat.getrxdropped ());//number of packets dropped on receive print (" txdropped = "+ ifstat.gettxdropped ());Number of packets dropped} catch (Sigarnotimplementedexception e) {} catch (Sigarexception e) {print (E.getmessage ()); 
} void Print (String msg) {System.out.println (msg); 
 }//E) Some other information 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 address System.out. println ("cfg.getbroadcast () = "+ cfg.getbroadcast ());//Gateway broadcast address SYSTEM.OUT.PRINTLN (" cfg.gethwaddr () = "+ cfg.gethwaddr ());//Nic MAC address System.out. println ("cfg.getnetmask () =" + Cfg.getnetmask ());//Subnet Mask System.out.println ("cfg.getdescription () =" + Cfg.getdesc RiptiOn ())//NIC description information 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 (); 
 The catch (Exception e) {System.out.println ("Error while creating GUID" + E); 
 Finally {if (SIGAR!= null) sigar.close ();

 } 
}
}

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.