JAvaサーバデバイス情報の取得(モニタサーバ)

12961 ワード

package com.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 SystemInfo {
	// 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("
~~~~~~~~~" + 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("
~~~~~~~~~~" + 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("
name = " + 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(); } } public static void main(String[] args) throws SigarException{ SystemInfo s = new SystemInfo(); // s.getCpuTotal(); // s.getEthernetInfo(); // s.getDefaultIpAddress(); s.testGetOSInfo(); // s.getPhysicalMemory(); System.out.println(getCpuCount()); } }