本文实例讲述了java获取服务器基本信息的方法。 Compártelo para todos para su referencia. Los detalles son los siguientes:
Use un paquete JAR de terceros: (Sitio web oficial de Hyperic-HQ: http://www.hyperic.com) para lograr datos de estado de servidor que se obtengan a través de SIGAR.JAR de Hyperic-HQ Products. Sigar.jar包是通过本地方法来调用操作系统API来获取系统相关数据。 SIGAR.JAR bajo el sistema operativo de Windows se basa en SIGAR-AMD64-WINNT.DLL o SIGAR-X86-WINNT.DLL, mientras que el sistema operativo Linux se basa en Libsigar-AMD64-Linux.so o Libsigar-X86-Linux.soSo
Importar java.net.inetaddress; Hyperic.sigar.mem; .SigARException; {return size.getCpuInfolist (). nuevo signo (); + Info.getMhz ()); .println ("model =" + info.getModel ()); }} Catch (Sigarexception e) {E.PrintStackTrace ();} // c) Uso del usuario de la CPU, System Use el excedente, cantidad total restante, Ocupación de uso total, etc. (Unidad: 100%) Public Void TestCpuPerC () {Tamaño size = nuevo signo (); = NULL; i]);}} Private Void PrintCpuCpERC (CPUPERC CPU) {System.out.println ("Usuario:" + CPUPERC.FORMAT (CPU.GETUSER ())); "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 ())); size = nuevo signo (); "); // El sistema de uso de memoria actual.out.println (" usado = " + mem.getused () / 1024l +" k userd "); // La memoria actual restante System.out.println (" "Free = " + Mem.getFree () / 1024l +" k free "); // b) Página del sistema Información del archivo del archivo Información Swap = SIGAR.GETSWAP (); // La cantidad total de System.out.println (" Total = " + swap.gettotal () /) / 1024l + "k av"); / La cantidad restante del área de Exchange actual.out.println ("free =" + swap.getfree () / 1024l + "k gratis"); : Public String getplatformName () {String hostname = "" "" "; try {hostName = indadres.getLocalHost (). (Exception exc) {sigar sigar = new size (); try {hostname = size.getnetinfo (). GethostNameName ();} Catch (SIGARException e) {hostname = "localhost.unknown";} Finalmente {sigar.close ();}} return hostName; () = " + OS.Getarch ()); System.out.println (" OS.Getcpuendian () = " + OS.Getcpuendian ()); // System.out.println (" OS.GetDataModel () = " + OS.GetDatAmodel ()); / El tipo de operación tipo System.out.println ("OS.GetName () =" + OS.GetName ()); OS.GetVendor () = " + OS.GetVendor ()); //" OS.GetVendInCodeName () = " + OS.GetVendIrCodeName ()); // El nombre del sistema operativo System.out.println (" OS.GetVendOname ( ) = " + Os.getVendOnname ()); // em .out.println (" OS.getVendorversion () = " + OS.GetVendorversion ()); // El número de versión System.out.println (" OS.GetVersion () = " + OS.GetVersion ());} // c) Tome la información del usuario en la tabla de proceso del sistema actual.! = 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 () ); ));}}}} Catch (Sigarexception e) {E.PrintStackTrace ();} // 4. Información de recursos (principalmente disco duro) / / a) Tome la partición y los detalles existentes del disco duro (a través de SIGAR.GetFilesystemList () para obtener el objeto de lista de sistemas de archivos y luego compilarlo): public Void TestFilesSystemSystemFo () lanza la excepción {SIGAR SIGAR = NEW SIGAR (); ("user.home"); // The current user file path for (int i = 0; i <fslist.Length; I ++) {{ System.out.println ("/n ~~~~~~~ ~~~~~~~~ " + i +" ~~~~~~~~~~~~ "); el nombre del personaje System.out.println (" fs.getDevName () = " + fs.getDevName ( )); GetFlags ()); // Tipo de sistema de archivos, como FAT32, NTFS System.out.println ("FshetSystypename () =" + Fs.getSystypename ()); Disco duro local, unidad óptica, sistema de archivos de red y otro sistema.out.println ("fs.gettypename () =" + fs.gettypename ()); ) = " + fs.gettype ()); irname ());} catch (Sigarexception e) {if (fs.gettype () == 2) throw e; continuar;} switch (fs.gettype ()) {case 0: // type_unknown: Break desconocido; () + "KB"); out.println ("dispone =" + use.getavail () + "kb"); Double UserCent = Usage.getUsePcent () * 100d; : // type_ram_disk: case 5: // type_cdrom: case 6: // type_swap:} systemm .out.println ("diskreads =" + use.getdiskreams ()); .getDiskWrites ()); {try {size = new size (); S = NULL; Paquete de herramienta de herramienta de SIGAR para obtener if ( = size.getNetInterfACeConFig () .getAddress ();} catch (SIGARException e) {dirección = netflags.loopback_address;} finalmente {SIGAR.CLOSE ();} Dirección de retorno;} // dirección MACS public String getMac () {{) Size de SIGAR = NULL; (ifaces [i]); ) {Continuar;} / * * Si hay varias tarjetas de red, incluidas las tarjetas de red, incluidas las máquinas virtuales, solo la dirección MAC de la primera tarjeta de red se toma de forma predeterminada. The return type of the method is array or collection *, by multiple MAC addresses obtained in the for loop. */ hwaddr = cfg.gethwadddr (); // d) Obtener información como el tráfico de red y otra información. ; / / Nombre del dispositivo de red ("Dirección =" + Ifconfig.getAddress ()); 1l) <= 0l) {imprime ("! Iff_up ... omitir en"); = " + ifstat.gettxpackets (); // El número total de envolturas enviadas (" rxbytes = " + ifstat.getrxbytes ()); // El número total de bytes recibidos (" txbytes = " + ifstat.gettxbytes ()) ; / Enviar el número total de bytes del byte ("rxerrs =" + ifstat.getrxerrs ()); "rxdropped =" + ifstat.getrxdropped ()); .getMessage ());}}} Void Print (String Msg) {System.out.println (msg);} // e) etinfe () {SIGAR size = null; ] ifaces = Sigar.getNetNetInterfaceList (); ()) || + cfg.getAddress ()); // direcciones IP System.outln ("cfg.getBroadcast () =" + cfg.getbroa dcast ()); ) = " + cfg.gethwaddr ()); // La dirección de la tarjeta de red MAC System.out.println (" cfg.getnetmask () = " + cfg .getnetmask ()); // El sistema de máscara de subnet.println ("cfg.getDescript () =" + cfg.getDescripting ()); ()); ); ;} Finalmente {if (size! = Null) size.close ();}}}Se espera que este artículo sea útil para el diseño del programa Java de todos.