Search in sources :

Example 1 with INetworkInterfaceStat

use of com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.INetworkInterfaceStat in project ats-framework by Axway.

the class MatchedProcess method getNetworkTraffic.

private static List<ReadingInstance> getNetworkTraffic(ISystemInformation systemInfo, ReadingBean reading) throws SystemInformationException {
    List<ReadingInstance> readingInstancesList = new ArrayList<ReadingInstance>();
    String[] ifNames = systemInfo.listNetworkInterface();
    // 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 = systemInfo.getNetworkInterfaceStat(ifName).getTxBytes();
        readingInstancesList.add(new ReadingInstance(systemInfo, String.valueOf(reading.getDbId()), reading.getMonitorName(), reading.getName() + " " + ifName + " TX data", reading.getUnit(), 0) {

            private static final long serialVersionUID = 1L;

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

            @Override
            public float poll() throws SystemInformationException {
                INetworkInterfaceStat ifstat = this.systemInfo.getNetworkInterfaceStat(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 = systemInfo.getNetworkInterfaceStat(ifName).getRxBytes();
        readingInstancesList.add(new ReadingInstance(systemInfo, String.valueOf(reading.getDbId()), reading.getMonitorName(), reading.getName() + " " + ifName + " RX data", reading.getUnit(), 0) {

            private static final long serialVersionUID = 1L;

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

            @Override
            public float poll() throws SystemInformationException {
                INetworkInterfaceStat ifstat = this.systemInfo.getNetworkInterfaceStat(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 : INetworkInterfaceStat(com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.INetworkInterfaceStat) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) HashSet(java.util.HashSet) SystemInformationException(com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException)

Aggregations

INetworkInterfaceStat (com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.INetworkInterfaceStat)1 SystemInformationException (com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1