Search in sources :

Example 1 with SystemInformationException

use of com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException 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)

Example 2 with SystemInformationException

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

the class MatchedProcess method updateProcessesMatchingMap.

private static List<ReadingInstance> updateProcessesMatchingMap(ISystemInformation systemInfo, List<ReadingBean> initialProcessReadings, List<ReadingInstance> currentReadingInstances) throws SystemInformationException {
    // remember to user process regex and alias
    Map<String, Pattern> processPatterns = new HashMap<String, Pattern>();
    Map<String, String> processAliases = new HashMap<String, String>();
    Map<String, String> processUsernames = new HashMap<String, String>();
    for (ReadingBean processReading : initialProcessReadings) {
        String userRegex = processReading.getParameter(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_RECOGNITION_PATTERN);
        Pattern compiledPattern = Pattern.compile(userRegex);
        processPatterns.put(userRegex, compiledPattern);
        String userProcessAlias = processReading.getParameter(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_ALIAS);
        processAliases.put(userRegex, userProcessAlias);
        String processUsername = processReading.getParameter(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_USERNAME);
        processUsernames.put(userRegex, processUsername);
    }
    // at the end only processes that are not alive will be in this list
    Set<Long> finishedProcessesIds = new HashSet<Long>(matchedProcessesIds);
    // iterate all system processes and remember the ones we want to monitor
    for (long pid : systemInfo.getProcList()) {
        // check if we know this process from a previous poll, we do not want to add it again
        if (!matchedProcessesIds.contains(pid)) {
            // we try to match a process by its start command
            String processStartCommand = constructProcessStartCommand(systemInfo, pid);
            if (processStartCommand != null && !processStartCommand.isEmpty()) {
                String processUsername = null;
                // check this process against all patterns
                for (String userRegex : processPatterns.keySet()) {
                    // by default we search processes from all users
                    boolean isExpectedProcessUsername = true;
                    // check if it matters who started this process
                    String requestedProcessUsername = processUsernames.get(userRegex);
                    if (!StringUtils.isNullOrEmpty(requestedProcessUsername)) {
                        if (processUsername == null) {
                            // we still do not know the user of this process
                            try {
                                processUsername = systemInfo.getProcessInformation(pid).getUser();
                            } catch (Exception e) {
                                // SystemInformationException was here before
                                // a specific username is required, but we can not get the info about this process
                                isExpectedProcessUsername = false;
                            }
                        }
                        isExpectedProcessUsername = requestedProcessUsername.equalsIgnoreCase(processUsername);
                    }
                    if (isExpectedProcessUsername) {
                        Pattern processPattern = processPatterns.get(userRegex);
                        if (processPattern.matcher(processStartCommand.trim()).matches()) {
                            Map<Long, MatchedProcess> processesMatchedThisRegex = matchedProcessesMap.get(userRegex);
                            if (processesMatchedThisRegex == null) {
                                processesMatchedThisRegex = new HashMap<Long, MatchedProcess>();
                            }
                            int processIndexForThisPattern = getNextIndexForThisProcessPattern(userRegex);
                            MatchedProcess matchedProcess = new MatchedProcess(pid, userRegex, processAliases.get(userRegex), processIndexForThisPattern, processStartCommand);
                            processesMatchedThisRegex.put(pid, matchedProcess);
                            matchedProcessesMap.put(userRegex, processesMatchedThisRegex);
                            matchedProcessesIds.add(pid);
                            log.info("We will monitor process: " + matchedProcess.toString());
                        }
                    }
                }
            }
        } else {
            // the process is still alive
            finishedProcessesIds.remove(pid);
        }
    }
    // check if some processes have died, we do not want to monitor them anymore
    for (Long finishedProcessId : finishedProcessesIds) {
        // cleanup the maps
        matchedProcessesIds.remove(finishedProcessId);
        for (Map<Long, MatchedProcess> matchedProcessMap : matchedProcessesMap.values()) {
            MatchedProcess removedProcess = matchedProcessMap.remove(finishedProcessId);
            if (removedProcess != null) {
                log.info("This process is no more alive: " + removedProcess.toString());
            }
        }
        Iterator<String> it = processesReadingInstanceIdentifiers.iterator();
        while (it.hasNext()) {
            String processesReadingInstanceIdentifier = it.next();
            if (Long.parseLong(processesReadingInstanceIdentifier.split("->")[1]) == finishedProcessId) {
                it.remove();
            }
        }
    }
    // return the updated list of reading instances we poll
    List<ReadingInstance> newReadingInstances = new ArrayList<ReadingInstance>();
    for (ReadingInstance currentReadingInstance : currentReadingInstances) {
        boolean stillActiveInstance = true;
        for (Long finishedProcessId : finishedProcessesIds) {
            if (finishedProcessId == currentReadingInstance.getPid()) {
                stillActiveInstance = false;
                break;
            }
        }
        if (stillActiveInstance) {
            newReadingInstances.add(currentReadingInstance);
        }
    }
    return newReadingInstances;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SystemInformationException(com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException) UnsupportedReadingException(com.axway.ats.core.monitoring.UnsupportedReadingException) ReadingBean(com.axway.ats.common.performance.monitor.beans.ReadingBean) ParentProcessReadingBean(com.axway.ats.common.performance.monitor.beans.ParentProcessReadingBean) HashSet(java.util.HashSet)

Example 3 with SystemInformationException

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

the class AtsSystemMonitor method init.

@Override
public void init(ReadingBean[] readings) throws Exception {
    log.info("Initializing the ATS System Monitor");
    try {
        this.systemInfo = SystemInformationFactory.get();
    } catch (Exception e) {
        String errorMessage = "Error initializing the provider of system information. System monitoring will not work.";
        log.error(errorMessage, e);
        throw new SystemInformationException(errorMessage, e);
    }
    List<ReadingBean> staticReadings = new ArrayList<ReadingBean>();
    List<ReadingBean> dynamicReadings = new ArrayList<ReadingBean>();
    for (ReadingBean reading : readings) {
        if (!reading.isDynamicReading()) {
            staticReadings.add(reading);
        } else {
            // check if this process has a parent
            String parentProcessName = reading.getParameter(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_PARENT_NAME);
            if (parentProcessName != null) {
                final String parentProcessId = parentProcessName + "-" + reading.getName();
                if (!parentProcessReadingInstances.containsKey(parentProcessId)) {
                    ParentProcessReadingBean prentProcessBean = new ParentProcessReadingBean(reading.getId(), reading.getMonitorName(), parentProcessName, reading.getName(), reading.getUnit());
                    prentProcessBean.setParameters(reading.getParameters());
                    parentProcessReadingInstances.put(parentProcessId, prentProcessBean);
                }
            }
            dynamicReadings.add(reading);
        }
    }
    ReadingInstancesFactory.init(systemInfo, getPollInterval());
    // create the actual static reading instances
    staticReadingInstances = ReadingInstancesFactory.createStaticReadingInstances(systemInfo, staticReadings);
    // remember the initial dynamic readings
    initialDynamicReadings = new ArrayList<ReadingBean>(dynamicReadings);
    // calculations on the first poll
    if (initialDynamicReadings.size() > 0) {
        dynamicReadingInstances = ReadingInstancesFactory.createOrUpdateDynamicReadingInstances(systemInfo, parentProcessReadingInstances, initialDynamicReadings, dynamicReadingInstances);
    }
}
Also used : ReadingBean(com.axway.ats.common.performance.monitor.beans.ReadingBean) ParentProcessReadingBean(com.axway.ats.common.performance.monitor.beans.ParentProcessReadingBean) ParentProcessReadingBean(com.axway.ats.common.performance.monitor.beans.ParentProcessReadingBean) ArrayList(java.util.ArrayList) SystemInformationException(com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException) SystemInformationException(com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException)

Example 4 with SystemInformationException

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

the class SystemInformationFactory method get.

/**
 * Depending on the System property {@link AtsSystemProperties.SYSTEM_INFORMATION_CLASS}, a {@link ISystemInformation} will be returned.</br>
 */
public static synchronized ISystemInformation get() {
    String providerName = AtsSystemProperties.getPropertyAsString(AtsSystemProperties.SYSTEM_MONITORING_PROVIDER, "sigar");
    if (!MONITORING_PROVIDERS_MAP.containsKey(providerName)) {
        throw new UnsupportedOperationException("Provider '" + providerName + "' is not supported as implementation for system information/monitoring provider. Currently available once are: " + Arrays.toString(MONITORING_PROVIDERS_MAP.keySet().toArray(new String[MONITORING_PROVIDERS_MAP.size()])));
    }
    LOG.info("System information/monitoring provider will be: '" + providerName + "'");
    ISystemInformation providerInstance = null;
    try {
        Class<?> clazz = Class.forName(MONITORING_PROVIDERS_MAP.get(providerName));
        providerInstance = (ISystemInformation) clazz.getConstructors()[0].newInstance(new Object[] {});
    } catch (Exception e) {
        throw new SystemInformationException("Error while initializing monitoring provider '" + providerName + "'", e);
    }
    return providerInstance;
}
Also used : ISystemInformation(com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.ISystemInformation) SystemInformationException(com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException) SystemInformationException(com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException)

Example 5 with SystemInformationException

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

the class MatchedProcess method getProcessCpuUsageRunningKernel.

private static List<ReadingInstance> getProcessCpuUsageRunningKernel(ISystemInformation systemInfo, ReadingBean reading, ParentProcessReadingBean parentProcess) throws SystemInformationException {
    String readingProcessPattern = reading.getParameter(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_RECOGNITION_PATTERN);
    // we can match more than 1 process
    List<ReadingInstance> readingInstancesList = new ArrayList<ReadingInstance>();
    // iterate all processes in the map
    Map<Long, MatchedProcess> processesForThisPattern = matchedProcessesMap.get(readingProcessPattern);
    if (processesForThisPattern != null) {
        for (final MatchedProcess processInfo : processesForThisPattern.values()) {
            // check if we are not already measuring this reading for this process
            String processesReadingInstanceIdentifier = reading.getName() + "->" + processInfo.getPid();
            if (!processesReadingInstanceIdentifiers.contains(processesReadingInstanceIdentifier)) {
                processesReadingInstanceIdentifiers.add(processesReadingInstanceIdentifier);
                // create a new reading for this process
                Map<String, String> parameters = constructProcessParametersMap(processInfo, readingProcessPattern, "2");
                readingInstancesList.add(new ReadingInstance(systemInfo, parentProcess, String.valueOf(reading.getDbId()), processInfo.getPid(), reading.getMonitorName(), "[process] " + processInfo.getAlias() + " - CPU usage - Kernel", reading.getUnit(), parameters, 1.0F) {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void init() throws SystemInformationException {
                        this.lastLongValue = fixLongValue(fixOverflow(getName(), systemInfo.getProcessCpuTimeRunningKernel(processInfo.getPid()), ReadingInstance.CPU_PROCESS_OVERFLOW_VALUE));
                    }

                    @Override
                    public float poll() throws SystemInformationException {
                        long kernelTime = fixLongValue(fixOverflow(getName(), systemInfo.getProcessCpuTimeRunningKernel(processInfo.getPid()), ReadingInstance.CPU_PROCESS_OVERFLOW_VALUE));
                        double deltaKernelTime;
                        if (kernelTime > 0) {
                            deltaKernelTime = (kernelTime - this.lastLongValue) / numberOfCPUs;
                            this.lastLongValue = kernelTime;
                        } else {
                            return -1;
                        }
                        float deltaKernelPercentUsage = 0.0F;
                        long elapsedTime = getElapsedTime();
                        // moment of creating the ReadingInstance, it is normal for new processes
                        if (elapsedTime > 900) {
                            // calculate kernel code time for 1ms
                            deltaKernelTime = deltaKernelTime / elapsedTime;
                            // convert to percentage usage
                            deltaKernelPercentUsage = new BigDecimal(deltaKernelTime).setScale(2, BigDecimal.ROUND_DOWN).floatValue() * 100.0F;
                        }
                        addValueToParentProcess(deltaKernelPercentUsage);
                        return deltaKernelPercentUsage;
                    }
                });
            }
        }
    }
    return readingInstancesList;
}
Also used : ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) SystemInformationException(com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException)

Aggregations

SystemInformationException (com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.exceptions.SystemInformationException)8 ArrayList (java.util.ArrayList)6 BigDecimal (java.math.BigDecimal)4 ParentProcessReadingBean (com.axway.ats.common.performance.monitor.beans.ParentProcessReadingBean)2 ReadingBean (com.axway.ats.common.performance.monitor.beans.ReadingBean)2 HashSet (java.util.HashSet)2 IFileSystem (com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.IFileSystem)1 INetworkInterfaceStat (com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.INetworkInterfaceStat)1 ISystemInformation (com.axway.ats.agent.core.monitoring.systemmonitor.systeminformation.ISystemInformation)1 UnsupportedReadingException (com.axway.ats.core.monitoring.UnsupportedReadingException)1 HashMap (java.util.HashMap)1 Pattern (java.util.regex.Pattern)1 FileSystem (oshi.software.os.FileSystem)1 OSFileStore (oshi.software.os.OSFileStore)1