Search in sources :

Example 1 with NetInterfaceStat

use of org.hyperic.sigar.NetInterfaceStat in project ats-framework by Axway.

the class MatchedProcess method getNetworkTraffic.

private static List<ReadingInstance> getNetworkTraffic(SigarWrapper sigarWrapper, FullReadingBean reading) throws SigarException {
    // this reading can actually be expanded in more than one reading
    int nextUniqueReadingId = 1;
    List<ReadingInstance> readingInstancesList = new ArrayList<ReadingInstance>();
    String[] ifNames = sigarWrapper.getSigarInstance().getNetInterfaceList();
    // if there are more than one IP addresses on a single interface, Sigar will show these interface names so many times
    Set<String> uniqueIfNames = new HashSet<String>(Arrays.asList(ifNames));
    for (final String ifName : uniqueIfNames) {
        if (ifName.indexOf(':') > -1) {
            // that's why we will skip it, also Sigar throws an exception with message: No such device or address
            continue;
        }
        final long txBytes = sigarWrapper.getSigarInstance().getNetInterfaceStat(ifName).getTxBytes();
        readingInstancesList.add(new ReadingInstance(sigarWrapper, reading.getId() + ", " + (nextUniqueReadingId++), reading.getMonitorName(), reading.getName() + " " + ifName + " TX data", reading.getUnit(), 0) {

            private static final long serialVersionUID = 1L;

            @Override
            public void init() throws SigarException {
                applyMemoryNormalizationFactor();
                this.lastLongValue = fixLongValue(fixOverflow(ifName, txBytes));
            }

            @Override
            public float poll() throws SigarException {
                NetInterfaceStat ifstat = this.sigarWrapper.getSigarInstance().getNetInterfaceStat(ifName);
                long txBytes = fixLongValue(fixOverflow(getName(), ifstat.getTxBytes()));
                double result;
                if (txBytes >= 0) {
                    result = (txBytes - this.lastLongValue) * normalizationFactor;
                    this.lastLongValue = txBytes;
                } else {
                    return -1.0F;
                }
                // calculate TX bytes per second
                result = result / ((double) getElapsedTime() / 1000);
                return new BigDecimal(result).setScale(2, BigDecimal.ROUND_DOWN).floatValue();
            }
        });
        final long rxBytes = sigarWrapper.getSigarInstance().getNetInterfaceStat(ifName).getRxBytes();
        readingInstancesList.add(new ReadingInstance(sigarWrapper, reading.getId() + ", " + (nextUniqueReadingId++), reading.getMonitorName(), reading.getName() + " " + ifName + " RX data", reading.getUnit(), 0) {

            private static final long serialVersionUID = 1L;

            @Override
            public void init() throws SigarException {
                applyMemoryNormalizationFactor();
                this.lastLongValue = fixLongValue(fixOverflow(ifName, rxBytes));
            }

            @Override
            public float poll() throws SigarException {
                NetInterfaceStat ifstat = this.sigarWrapper.getSigarInstance().getNetInterfaceStat(ifName);
                long rxBytes = fixLongValue(fixOverflow(getName(), ifstat.getRxBytes()));
                double result;
                if (rxBytes >= 0) {
                    result = (rxBytes - this.lastLongValue) * normalizationFactor;
                    this.lastLongValue = rxBytes;
                } else {
                    return -1.0F;
                }
                // calculate RX bytes per second
                result = result / ((double) getElapsedTime() / 1000);
                return new BigDecimal(result).setScale(2, BigDecimal.ROUND_DOWN).floatValue();
            }
        });
    }
    return readingInstancesList;
}
Also used : ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) SigarException(org.hyperic.sigar.SigarException) NetInterfaceStat(org.hyperic.sigar.NetInterfaceStat) HashSet(java.util.HashSet)

Example 2 with NetInterfaceStat

use of org.hyperic.sigar.NetInterfaceStat in project graylog2-server by Graylog2.

the class SigarNetworkProbe method networkStats.

@Override
public synchronized NetworkStats networkStats() {
    final Sigar sigar = sigarService.sigar();
    String primaryInterface;
    try {
        final NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(null);
        primaryInterface = netInterfaceConfig.getName();
    } catch (SigarException e) {
        primaryInterface = null;
    }
    final Map<String, NetworkStats.Interface> interfaces = new HashMap<>();
    try {
        final String[] netInterfaceList = firstNonNull(sigar.getNetInterfaceList(), new String[0]);
        for (String interfaceName : netInterfaceList) {
            final NetInterfaceStat netInterfaceStat = sigar.getNetInterfaceStat(interfaceName);
            final NetworkStats.InterfaceStats interfaceStats = NetworkStats.InterfaceStats.create(netInterfaceStat.getRxPackets(), netInterfaceStat.getRxErrors(), netInterfaceStat.getRxDropped(), netInterfaceStat.getRxOverruns(), netInterfaceStat.getRxFrame(), netInterfaceStat.getTxPackets(), netInterfaceStat.getTxErrors(), netInterfaceStat.getTxDropped(), netInterfaceStat.getTxOverruns(), netInterfaceStat.getTxCarrier(), netInterfaceStat.getTxCollisions(), netInterfaceStat.getRxBytes(), netInterfaceStat.getTxBytes());
            final NetInterfaceConfig netInterfaceConfig = sigar.getNetInterfaceConfig(interfaceName);
            final NetworkStats.Interface networkInterface = NetworkStats.Interface.create(netInterfaceConfig.getName(), Collections.singleton(netInterfaceConfig.getAddress()), netInterfaceConfig.getHwaddr(), netInterfaceConfig.getMtu(), interfaceStats);
            interfaces.put(interfaceName, networkInterface);
        }
    } catch (SigarException e) {
    // ignore
    }
    NetworkStats.TcpStats tcpStats;
    try {
        final Tcp tcp = sigar.getTcp();
        tcpStats = NetworkStats.TcpStats.create(tcp.getActiveOpens(), tcp.getPassiveOpens(), tcp.getAttemptFails(), tcp.getEstabResets(), tcp.getCurrEstab(), tcp.getInSegs(), tcp.getOutSegs(), tcp.getRetransSegs(), tcp.getInErrs(), tcp.getOutRsts());
    } catch (SigarException e) {
        tcpStats = null;
    }
    return NetworkStats.create(primaryInterface, interfaces, tcpStats);
}
Also used : Sigar(org.hyperic.sigar.Sigar) Tcp(org.hyperic.sigar.Tcp) NetInterfaceConfig(org.hyperic.sigar.NetInterfaceConfig) HashMap(java.util.HashMap) SigarException(org.hyperic.sigar.SigarException) NetInterfaceStat(org.hyperic.sigar.NetInterfaceStat)

Aggregations

NetInterfaceStat (org.hyperic.sigar.NetInterfaceStat)2 SigarException (org.hyperic.sigar.SigarException)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 NetInterfaceConfig (org.hyperic.sigar.NetInterfaceConfig)1 Sigar (org.hyperic.sigar.Sigar)1 Tcp (org.hyperic.sigar.Tcp)1