JavaNetty heartbeat monitor

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;
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.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.Swap;
import org.hyperic.sigar.Who;

public class TestSigar {
    public TestSigar() {
    }

    public static void main(String[] args) {
        try {
            property();
            System.out.println("----------------------------------");
            cpu();
            System.out.println("----------------------------------");
            memory();
            System.out.println("----------------------------------");
            os();
            System.out.println("----------------------------------");
            who();
            System.out.println("----------------------------------");
            file();
            System.out.println("----------------------------------");
            net();
            System.out.println("----------------------------------");
            ethernet();
            System.out.println("----------------------------------");
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    private static void property() throws UnknownHostException {
        Runtime r = Runtime.getRuntime();
        Properties props = System.getProperties();
        InetAddress addr = InetAddress.getLocalHost();
        String ip = addr.getHostAddress();
        Map<String, String> map = System.getenv();
        String userName = (String)map.get("USERNAME");
        ComputerName = String (String) as map.get ( "COMPUTERNAME"); 
        String UserDomain = (String) as map.get ( "USERDOMAIN"); 
        System.out.println ( "User Name:" + the userName); 
        System.out.println ( "computer name:" + computerName); 
        System.out.println ( "computer domain name:" + UserDomain); 
        System.out.println ( "local ip address:" + ip); 
        System.out.println ( "localhost name: "+ addr.getHostName ()); 
        System.out.println (" JVM total memory can be used: "+ r.totalMemory ()); 
        System.out.println (" JVM remaining memory may be used: "+ r.freeMemory ()); 
        System.out.println ( "JVM number of processors can be used:" + r.availableProcessors ()); 
        System.out.println ( "The Java Runtime Environment version: "+ props.getProperty (" java.version "));
        System.out.println ( "Java runtime environment supplier:" + props.getProperty ( "java.vendor"));
        System.out.println ( "Java virtual machine implementation vendor:" + props.getProperty ( "java.vm.vendor" ));
        System.out.println ( "Java vendor's the URL of:" + props.getProperty ( "java.vendor.url")); 
        System.out.println ( "Java installation path:" + props.getProperty ( "java. Home ")); 
        System.out.println (" the Java virtual machine specification version: "+ props.getProperty (" java.vm.specification.version ")); 
        System.out.println (" the Java virtual machine specification supply business: "+ props.getProperty (" java.vm.specification.vendor ")); 
        System.out.println (" the Java virtual machine specification name: "+ props.getProperty (" java.vm.specification.name " ) ); 
        System.out.println ( "the Java virtual machine implementation version:" + props.getProperty ( "java.vm.version")); 
        System.out.println ( "the Java virtual machine implementation name:" + props. getProperty ( "java.vm.name"));
        System.out.println ( "Java Runtime Environment specification version:" + props.getProperty ( "java.specification.version" ));
        System.out.println ( "Java Runtime Environment specification vendor:" + props.getProperty ( "java.specification.vender")); 
        System.out.println ( "Java Runtime Environment specification name:" + props.getProperty ( "java.specification.name")); 
        System.out.println ( "the Java class format version number:" + props.getProperty ( "java.class.version")); 
        System.out.println ( "of the Java classpath: "+ props.getProperty (" java.class.path ")); 
        System.out.println (" list of paths to search when loading libraries: "+ props.getProperty (" java.library.path " )); 
        System.out.println ( "default temporary file path:" + props.getProperty ( "java.io.tmpdir")); 
        System.out.println ( "one or more paths extended directory:" + props.getProperty ( "java.ext.dirs" ));
        System.out.println ( "operating system name:" + props.getProperty ( "os. name ")); 
        System.out.println ( "Operating System Architecture:" + props.getProperty ( "os.arch"));
        System.out.println ( "operating system version:" + props.getProperty ( "os.version")); 
        System.out.println ( "file separator:" + props.getProperty ( "file.separator" )) ; 
        System.out.println ( "path separators:" + props.getProperty ( "path.separator")); 
        System.out.println ( "row delimiter:" + props.getProperty ( "line.separator" )) ; 
        System.out.println ( "user's account name:" + props.getProperty ( "user.name")); 
        "user's home directory:" System.out.println (+ props.getProperty ( " user.home" )); 
        System.out.println ( "user's current working directory:" + props.getProperty ( "user.dir")); 
    } 

    private static void memory() throws SigarException {
        Sigar Sigar SIGAR new new = (); 
        Mem = SIGAR MEM.GetMem (); 
        System.out.println ( "Total Memory:" + mem.getTotal () / 1024L + "K av"); 
        System.out.println ( "current memory usage:" + mem.getUsed () / 1024L + "K Used"); 
        System.out.println ( "remaining amount of current memory:" + mem.getFree () / 1024L + "K Free"); 
        swap = sigar.getSwap the swap (); 
        System.out.println ( "total exchange area:" + swap.getTotal () / 1024L + "K av") ; 
        System.out.println ( "The amount of current switching area:" + swap.getUsed () / 1024L + "K used"); 
        System.out.println ( "current exchange zone residual amount:" + swap.getFree () / 1024L + "K Free"); 
    } 

    Private static void CPU () throws SigarException { 
        Sigar Sigar SIGAR new new = (); 
        cpuinfo [] = sigar.getCpuInfoList the infos ();
        CpuPerc [] cpuList = null;
        System.out.println ( "cpu total parameters are:" + sigar.getCpu ()); getCpu ()); 
        System.out.println ( "total percentage of cpu case:" + sigar.getCpuPerc ());
        cpuList sigar.getCpuPercList = (); 

        for (int I = 0; I <infos.length; I ++) { 
            cpuinfo info the infos = [I]; 
            System.out.println ( "first" + (i + 1) + " CPU information block"); 
            System.out.println ( "CPU total amount MHz:" + info.getMhz ()) ; 
            System.out.println ( "the CPU manufacturers:" + info.getVendor ()); 
            System.out.println ( "the CPU category:" + info.getModel ()); 
            System.out.println ( "number of the CPU cache : "+ info.getCacheSize ()); 
            printCpuPerc (cpuList [I]); 
        } 

    } 

    Private static void printCpuPerc (CpuPerc CPU) {
        System.out.println ( "CPU usage User:" + CpuPerc.format (cpu.getUser ())); 
        System.out.println ( "CPU System Usage:" + CpuPerc.format (cpu.getSys () ) ); 
        System.out.println ( "waiting for the CPU current rate:" + CpuPerc.format (cpu.getWait ())); 
        System.out.println ( "The current error rate of the CPU:" + CpuPerc.format (cpu.getNice ( ))); 
        System.out.println ( "the CPU idle current rate:" + CpuPerc.format (cpu.getIdle ())); 
        "total usage CPU:" System.out.println (+ CpuPerc.format ( cpu .getCombined ())); 
    } 

    Private static void OS () { 
        of the OperatingSystem OperatingSystem.getInstance the OS = (); 
        System.out.println ( "OS:" + OS.getArch ()); 
        System.out.println ( " operating system CpuEndian ():"+ OS.getCpuEndian ());
        System.out.println ( "operating system the DataModel ():" + OS.getDataModel ()); 
        System.out.println ( "Operating System Description:" + OS.getDescription ()); 
        System.out.println ( " operating system vendors: "+ OS.getVendor ());  
        System.out.println ( "OS vendor name:" + OS.getVendorCodeName ()); 
        system .out.println ( "operating system name:"+ OS.getVendorName ());
        System.out.println (" OS vendor type: "+ OS.getVendorVersion ()); 
        System.out.println (" OS version: "+ OS.getVersion ()); 
    } 

    Private static void WHO () throws SigarException { 
        Sigar Sigar SIGAR new new = (); 
        the Who [] = sigar.getWhoList WHO (); 
        ! IF (WHO = null && who.length> 0) { 
            for (int I = 0; I <who.length; I ++) { 
                the Who _who WHO = [I]; 
                System.out.println ( "console user:"+ _who.getDevice()); 
                System.out.println ( "user Host:" + _who.getHost ()); 
                System.out.println ( "current system processes the user name of the table:" + _who .getUser ()); 
            } 
        } 

    } 

    Private File static void () throws Exception {
        = New new SIGAR SIGAR SIGAR (); 
        the FileSystem [] = fslist sigar.getFileSystemList (); 
        int I = 0; 

        the while (I <fslist.length) { 
            System.out.println ( "letter name of the partition" + i); 
            FS = fslist the FileSystem [I]; 
            System.out.println ( "letter name:" + fs.getDevName ()); 
            System.out.println ( "letter path:" + fs.getDirName ()); 
            the System. out.println ( "letter sign:" + fs.getFlags ()); 
            System.out.println ( "type the letter:" + fs.getSysTypeName ());
            System.out.println("盘符类型名:    " + fs.getTypeName()); 
            System.out.println (" letter file system types: "+ fs.getType ());
            FileSystemUsage usage = null;
            usage = sigar.getFileSystemUsage(fs.getDirName());
            switch(fs.getType()) {
            case 2:
                System.out.println(fs.getDevName() + "总大小:    " + usage.getTotal() + "KB");
                System.out.println(fs.getDevName() + "剩余大小:    " + usage.getFree() + "KB");
                System.out.println(fs.getDevName() + "可用大小:    " + usage.getAvail() + "KB");
                System.out.println(fs.getDevName() + "已经使用量:    " + usage.getUsed() + "KB");
                double usePercent = usage.getUsePercent() * 100.0D;
                System.out.println(fs.getDevName () + "resource utilization:" + usePercent + "%");
            case 0:
            case 1:
            case 3:
            case 4:
            case 5:
            case 6:
            default:
                System.out.println(fs.getDevName() + "读出:    " + usage.getDiskReads());
                System.out.println(fs.getDevName() + "写入:    " + usage.getDiskWrites());
                ++i;
            }
        }

    }

    private static void net() 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);
            System.out.println ( "network device name:" + name); 
            System.out.println ( "the IP Address:" + ifconfig.getAddress ()); 
                System.out.println (name + "the total number of bytes received:" + ifstat.getRxBytes ());
            System.out.println ( "Subnet Mask:" + ifconfig.getNetmask ()); 
            IF ((ifconfig.getFlags () & 1L) <= 0L) { 
                System.out.println ( "IFF_UP skipping ...! getNetInterfaceStat "); 
            } the else { 
                NetInterfaceStat ifstat = sigar.getNetInterfaceStat (name); 
                System.out.println (name +" the total number of parcels received: "+ ifstat.getRxPackets ()); 
                System.out.println (name +" the total number of parcels sent: "+ ifstat.getTxPackets ()); 
                System.out.println (name +" total number of bytes transmitted: "+ ifstat.getTxBytes ()); 
                System.out.println (name +" reception the number of packets to the error: "+ ifstat.getRxErrors ()); 
                System.out.println (name +" number of errors when transmitting data packets: "+ ifstat.getTxErrors()); 
                System.out.println (name + "the number of discarded packets received:" + ifstat.getRxDropped ()); 
                System.out.println (name + "the number of discarded packet is transmitted:" + ifstat.getTxDropped ()); 
            } 
        } 

    } 

    Private static void Ethernet () throws SigarException { 
        Sigar SIGAR = null; 
        SIGAR Sigar new new = (); 
        String [] = ifaces sigar.getNetInterfaceList (); 

        for (int I = 0; I <ifaces.length; I ++) { 
            NetInterfaceConfig sigar.getNetInterfaceConfig CFG = (ifaces [I]);
            ! if ( "127.0.0.1" .equals ( cfg.getAddress ()) && (cfg.getFlags () & 8L) == 0L &&! "00: 00: 00: 00: 00: 00" .equals (cfg. getHwaddr ())) { 
                System.out.println (cfg.getName () + "the IP address:" + cfg.getAddress ()); 
                System.out.println (cfg.getName () + "gateway addresses:" + cfg.getBroadcast ());
                System.out.println (cfg.getName () + "MAC address:" + cfg.getHwaddr ()); 
                System.out.println (cfg.getName () + "Subnet Mask:" + cfg.getNetmask ( )); 
                System.out.println (cfg.getName () + "NIC description:" + cfg.getDescription ()); 
                System.out.println (cfg.getName () + "NIC type" + cfg.getType ( )); 
            } 
        } 

    } 
}

  

Guess you like

Origin www.cnblogs.com/sunliyuan/p/10969409.html