org.hyperic.sigar.NetInterfaceConfig Java Examples

The following examples show how to use org.hyperic.sigar.NetInterfaceConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: sigar.java    From sigar-system_runtime with MIT License 6 votes vote down vote up
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("网络设备名:    " + name);// 网络设备名
        System.out.println("IP地址:    " + ifconfig.getAddress());// IP地址
        System.out.println("子网掩码:    " + ifconfig.getNetmask());// 子网掩码
        if ((ifconfig.getFlags() & 1L) <= 0L) {
            System.out.println("!IFF_UP...skipping getNetInterfaceStat");
            continue;
        }
        NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
        
        System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数
        System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数
        System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数
        System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数
        System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数
        System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数
        System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数
        System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数
    }
}
 
Example #2
Source File: PerformanceService.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
/**
 * getNetSpeed.
 * 
 * @return
 */
public Map<String, Long> getNetSpeed()
        throws UnknownHostException, SigarException, InterruptedException {
    Map<String, Long> map = new HashMap<String, Long>();
    String ip = getIp();

    String[] ifNames = sigar.getNetInterfaceList();
    long rxbps = 0;
    long txbps = 0;
    for (int i = 0; i < ifNames.length; i++) {
        String name = ifNames[i];
        NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
        if (ifconfig.getAddress().equals(ip)) {
            long start = System.currentTimeMillis();
            NetInterfaceStat statStart = sigar.getNetInterfaceStat(name);
            long rxBytesStart = statStart.getRxBytes();
            long txBytesStart = statStart.getTxBytes();
            Thread.sleep(1000);
            long end = System.currentTimeMillis();
            NetInterfaceStat statEnd = sigar.getNetInterfaceStat(name);
            long rxBytesEnd = statEnd.getRxBytes();
            long txBytesEnd = statEnd.getTxBytes();

            rxbps = ((rxBytesEnd - rxBytesStart) * 8 / (end - start) * 1000) / 1024 / 8;
            txbps = ((txBytesEnd - txBytesStart) * 8 / (end - start) * 1000) / 1024 / 8;
            break;
        }
    }
    map.put(RXBPS, rxbps);
    map.put(TXBPS, txbps);
    return map;
}
 
Example #3
Source File: SystemRuntime.java    From sigar-system_runtime with MIT License 5 votes vote down vote up
public NetInterfaceStat netBytes(String ip) throws Exception {
        String ifNames[] = sigar.getNetInterfaceList();
        NetInterfaceStat result = null;
        for (int i = 0; i < ifNames.length; i++) {
            String name = ifNames[i];
            NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
            if(ifconfig.getAddress().equals(ip)){
                result = sigar.getNetInterfaceStat(name);
                break;
            }
//            System.out.println("网络设备名:    " + name);// 网络设备名
//            System.out.println("IP地址:    " + ifconfig.getAddress());// IP地址
//            System.out.println("子网掩码:    " + ifconfig.getNetmask());// 子网掩码
//            if ((ifconfig.getFlags() & 1L) <= 0L) {
//                System.out.println("!IFF_UP...skipping getNetInterfaceStat");
//                continue;
//            }
//            NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
//            
//            System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数
//            System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数
//            System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数
//            System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数
//            System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数
//            System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数
//            System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数
//            System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数
        }
        return result;
    }
 
Example #4
Source File: NetworkIOMetric.java    From perfmon-agent with Apache License 2.0 5 votes vote down vote up
static void logAvailableInterfaces(SigarProxy sigar) {
    log.info("*** Logging available network interfaces ***");

    try {
        String[] list = sigar.getNetInterfaceList();
        for (int n = 0; n < list.length; n++) {
            NetInterfaceConfig ifc = sigar.getNetInterfaceConfig(list[n]);
            log.info("Network interface: iface=" + ifc.getName() + " addr=" + ifc.getAddress() + " type=" + ifc.getType());
        }
    } catch (SigarException e) {
        log.debug("Can't get network info", e);
    }
}
 
Example #5
Source File: MachineDescriptionOperations.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
private StringBuilder getInfoFromSigar() throws Exception {

        StringBuilder sb = new StringBuilder();
        Sigar sigar = new Sigar();

        sb.append("Fully Qualified Domain Name:\n\t" + sigar.getFQDN().toString());
        sb.append("\nSystem uptime:\n\t" + new Double(sigar.getUptime().getUptime()).intValue()
                  + " seconds");
        sb.append("\nDate:\n\t" + new Date());
        sb.append("\nNetwork settings:" + format(sigar.getNetInfo().toString(), 1));

        sb.append("\nCPU info:");
        CpuInfo[] cpuInfoList = sigar.getCpuInfoList();
        for (int i = 0; i < cpuInfoList.length; i++) {
            CpuInfo cpuInfo = cpuInfoList[i];
            sb.append("\n\tCPU " + i + ": ");
            sb.append(format(cpuInfo.toString(), 2));
        }

        double totalMemory = (new Long(sigar.getMem().getTotal()).doubleValue() / 1024 / 1024 / 1024);
        sb.append("\nTotal memory:\n\t"
                  + new BigDecimal(totalMemory).setScale(2, BigDecimal.ROUND_DOWN).floatValue() + " GB");

        String[] nicList = sigar.getNetInterfaceList();
        sb.append("\nNIC info: ");
        for (int i = 0; i < nicList.length; i++) {
            NetInterfaceConfig nic = sigar.getNetInterfaceConfig(nicList[i]);
            sb.append("\n\tNIC " + i + ": ");
            sb.append(format(nic.toString(), 2));
        }

        FileSystem[] fileSystemList = sigar.getFileSystemList();
        sb.append("\nFile system info: ");
        for (int i = 0; i < fileSystemList.length; i++) {
            FileSystem fileSystem = fileSystemList[i];

            sb.append("\n\t" + fileSystem.getDevName() + " is a");
            if (fileSystem.getType() == FileSystem.TYPE_LOCAL_DISK) {
                sb.append(" local");
            } else if (fileSystem.getType() == FileSystem.TYPE_NETWORK) {
                sb.append(" network");
            } else if (fileSystem.getType() == FileSystem.TYPE_RAM_DISK) {
                sb.append(" ram disk");
            } else if (fileSystem.getType() == FileSystem.TYPE_SWAP) {
                sb.append(" swap");
            }

            sb.append(" " + fileSystem.getSysTypeName() + ", dir name '" + fileSystem.getDirName()
                      + "', options '" + fileSystem.getOptions() + "'");
        }

        sb.append("\nResource limits:" + format(sigar.getResourceLimit().toString(), "max", 1));
        return sb;
    }
 
Example #6
Source File: NetInterfaceInfo.java    From maintain with MIT License 4 votes vote down vote up
public NetInterfaceInfo(String name, NetInterfaceConfig netInterfaceConfig, NetInterfaceStat netInterfaceStat) {
	this.name = name;
	this.netInterfaceConfig = netInterfaceConfig;
	this.netInterfaceStat = netInterfaceStat;
}
 
Example #7
Source File: NetInterfaceInfo.java    From maintain with MIT License 4 votes vote down vote up
public NetInterfaceConfig getNetInterfaceConfig() {
	return netInterfaceConfig;
}
 
Example #8
Source File: NetInterfaceInfo.java    From maintain with MIT License 4 votes vote down vote up
public void setNetInterfaceConfig(NetInterfaceConfig netInterfaceConfig) {
	this.netInterfaceConfig = netInterfaceConfig;
}