Search in sources :

Example 1 with FullReadingBean

use of com.axway.ats.common.performance.monitor.beans.FullReadingBean in project ats-framework by Axway.

the class MatchedProcess method updateProcessesMatchingMap.

private static List<ReadingInstance> updateProcessesMatchingMap(Sigar sigar, List<FullReadingBean> initialProcessReadings, List<ReadingInstance> currentReadingInstances) throws SigarException {
    // 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 (FullReadingBean 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 : sigar.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(sigar, 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 = sigar.getProcCredName(pid).getUser();
                            } catch (SigarException e) {
                                // 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) FullReadingBean(com.axway.ats.common.performance.monitor.beans.FullReadingBean) SigarException(org.hyperic.sigar.SigarException) HashSet(java.util.HashSet)

Example 2 with FullReadingBean

use of com.axway.ats.common.performance.monitor.beans.FullReadingBean in project ats-framework by Axway.

the class AtsJvmMonitor method createReadingInstances.

private void createReadingInstances(FullReadingBean[] readings) throws UnsupportedReadingException {
    readingInstances = new ArrayList<JvmReadingInstance>();
    for (FullReadingBean reading : readings) {
        String readingName = reading.getName();
        JvmReadingInstance readingInstance = null;
        if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__CPU_USAGE)) {
            readingInstance = getCpuUsage(connection, reading);
        } else if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__MEMORY_HEAP)) {
            readingInstance = getHeap(connection, reading);
        } else if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__MEMORY_HEAP_YOUNG_GENERATION_EDEN)) {
            readingInstance = getHeapEden(connection, reading);
        } else if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__MEMORY_HEAP_YOUNG_GENERATION_SURVIVOR)) {
            readingInstance = getHeapSurvivor(connection, reading);
        } else if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__MEMORY_HEAP_OLD_GENERATION)) {
            readingInstance = getHeapOldGen(connection, reading);
        } else if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__MEMORY_PERMANENT_GENERATION)) {
            readingInstance = getHeapPermGen(connection, reading);
        } else if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__MEMORY_CODE_CACHE)) {
            readingInstance = getHeapCodeCache(connection, reading);
        } else if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__CLASSES_COUNT)) {
            readingInstance = getClassesCount(connection, reading);
        } else if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__THREADS_COUNT)) {
            readingInstance = getThreadsCount(connection, reading);
        } else if (readingName.equalsIgnoreCase(SystemMonitorDefinitions.READING_JVM__THREADS_DAEMON_COUNT)) {
            readingInstance = getDaemonThreadsCount(connection, reading);
        } else if (!StringUtils.isNullOrEmpty(reading.getParameter("MBEAN_NAME"))) {
            readingInstance = getCustomMBeanProperty(connection, reading);
        } else {
            throw new UnsupportedReadingException(readingName);
        }
        readingInstances.add(readingInstance);
    }
}
Also used : UnsupportedReadingException(com.axway.ats.agent.components.monitoring.model.exceptions.UnsupportedReadingException) FullReadingBean(com.axway.ats.common.performance.monitor.beans.FullReadingBean)

Example 3 with FullReadingBean

use of com.axway.ats.common.performance.monitor.beans.FullReadingBean in project ats-framework by Axway.

the class InternalSystemMonitoringOperations method initializeMonitoring.

@Action(name = "Internal System Monitoring Operations Initialize Monitoring")
public void initializeMonitoring(@Parameter(name = "readings") List<FullReadingBean> readings, @Parameter(name = "startTimestamp") long startTimestamp, @Parameter(name = "pollInterval") int pollInterval) throws Exception {
    monitoringAgent = new AtsSystemMonitoringAgent(startTimestamp, pollInterval);
    Map<String, List<FullReadingBean>> readingsPerMonitor = new HashMap<String, List<FullReadingBean>>();
    // load all the monitors and initialize them
    for (FullReadingBean reading : readings) {
        List<FullReadingBean> readingsForThisMonitor = readingsPerMonitor.get(reading.getMonitorName());
        if (readingsForThisMonitor == null) {
            readingsForThisMonitor = new ArrayList<FullReadingBean>();
            readingsPerMonitor.put(reading.getMonitorName(), readingsForThisMonitor);
        }
        readingsForThisMonitor.add(reading);
    }
    for (String monitorClassName : readingsPerMonitor.keySet()) {
        initializeMonitor(monitorClassName, readingsPerMonitor.get(monitorClassName), pollInterval);
    }
}
Also used : HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) FullReadingBean(com.axway.ats.common.performance.monitor.beans.FullReadingBean) AtsSystemMonitoringAgent(com.axway.ats.agent.components.monitoring.model.agents.AtsSystemMonitoringAgent) Action(com.axway.ats.agent.core.model.Action)

Example 4 with FullReadingBean

use of com.axway.ats.common.performance.monitor.beans.FullReadingBean in project ats-framework by Axway.

the class SystemMonitor method startMonitoringPhysicalHosts.

private List<MonitoringException> startMonitoringPhysicalHosts() {
    List<MonitoringException> errors = new ArrayList<MonitoringException>();
    // initialize the monitoring processes
    Iterator<String> monitoredHostsIterator = monitoredHosts.iterator();
    while (monitoredHostsIterator.hasNext()) {
        String monitoredHost = monitoredHostsIterator.next();
        log.debug("Initializing system monitoring on " + monitoredHost);
        final String ERR_MSG = "Could not initialize monitoring " + monitoredHost + ". ";
        Set<FullReadingBean> readings = requestedReadingTypesPerHosts.get(monitoredHost);
        if (readings == null || readings.size() == 0) {
            errors.add(new MonitoringException(ERR_MSG + " as no monitor types are provided"));
            break;
        }
        try {
            initializeSystemMonitoringProcess(monitoredHost, readings);
        } catch (MonitoringException e) {
            errors.add(e);
            break;
        }
        log.info("Successfully initialized monitoring " + monitoredHost);
    }
    // run the monitoring processes
    monitoredHostsIterator = monitoredHosts.iterator();
    while (monitoredHostsIterator.hasNext()) {
        String monitoredHost = monitoredHostsIterator.next();
        log.debug("Starting system monitoring on " + monitoredHost);
        try {
            startSystemMonitoringProcess(monitoredHost);
        } catch (MonitoringException e) {
            errors.add(e);
            break;
        }
        // start the task which will log into the database at a scheduled interval
        log.debug("Starting the logging task for the system monitoring on " + monitoredHost);
        AbstractLoggerTask loggerTask = new SystemStatsLoggerTask(monitoredHost);
        ScheduledFuture<?> loggerTaskFuture = scheduler.scheduleAtFixedRate(loggerTask, loggingInterval, loggingInterval, TimeUnit.SECONDS);
        //put the task in the map
        loggerTasksPerHost.put(monitoredHost, loggerTaskFuture);
        log.info("Successfully started monitoring " + monitoredHost + ". Monitoring results will be collected on every " + loggingInterval + " seconds");
    }
    return errors;
}
Also used : MonitoringException(com.axway.ats.monitoring.model.exceptions.MonitoringException) ArrayList(java.util.ArrayList) AbstractLoggerTask(com.axway.ats.monitoring.model.AbstractLoggerTask) FullReadingBean(com.axway.ats.common.performance.monitor.beans.FullReadingBean) SystemStatsLoggerTask(com.axway.ats.monitoring.model.SystemStatsLoggerTask)

Example 5 with FullReadingBean

use of com.axway.ats.common.performance.monitor.beans.FullReadingBean in project ats-framework by Axway.

the class SystemMonitor method scheduleCustomJvmMonitoring.

/**
     * Schedule custom monitoring a JVM application
     * <br><b>Note:</b>You can monitor just a single process.
     * You must open the tested JVM application for JMX connections by adding the following
     * runtime properties:
     * "-Dcom.sun.management.jmxremote.port=[PORT NUMBER] -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
     *
     * @param monitoredHost the host where the monitored application is
     * @param jmxPort the JMX port which is used for monitoring the JVM application
     * @param alias the process alias name to be used when displaying the results
     * @param mbeanName the name of the mbean than would be monitored
     * @param unit the metric unit
     * @param mbeanAttributes the MBean attribute to capture values for. 
     * </br><b>Note: </b>This can be an array of nested attributes as sometimes the first MBean attribute is not a simple value but a composite element. Then the following attribute is the actual one to track.
     * </br>For example when monitoring the Heap Used Memory, you have to provide "java.lang:type=Memory" as MBean name, the first level attribute is "HeapMemoryUsage" which is a composite element and its "used" attribute is the one of interest. In such case you need to provide here: "HeapMemoryUsage", "used"
     * </br><b>Note: </b> Order of the mbeanAttributes is important
     */
@PublicAtsApi
public void scheduleCustomJvmMonitoring(@Validate(name = "monitoredHost", type = ValidationType.STRING_SERVER_WITH_PORT) String monitoredHost, @Validate(name = "jmxPort", type = ValidationType.NUMBER_PORT_NUMBER) String jmxPort, @Validate(name = "alias", type = ValidationType.NOT_NULL) String alias, @Validate(name = "mbeanName", type = ValidationType.NOT_NULL) String mbeanName, @Validate(name = "unit", type = ValidationType.NOT_NULL) String unit, @Validate(name = "mbeanAttributes", type = ValidationType.NOT_NULL) String... mbeanAttributes) {
    // validate input parameters
    monitoredHost = HostUtils.getAtsAgentIpAndPort(monitoredHost);
    String jvmMonitor = "com.axway.ats.agent.components.monitoring.model.jvmmonitor.AtsJvmMonitor";
    Set<FullReadingBean> readingTypes = requestedReadingTypesPerHosts.get(monitoredHost);
    if (readingTypes == null) {
        readingTypes = new HashSet<FullReadingBean>();
    }
    Map<String, String> readingParameters = new LinkedHashMap<String, String>();
    readingParameters.put("JMX_PORT", jmxPort);
    readingParameters.put("MBEAN_NAME", mbeanName);
    if (!StringUtils.isNullOrEmpty(alias)) {
        readingParameters.put(SystemMonitorDefinitions.PARAMETER_NAME__PROCESS_ALIAS, alias);
    }
    // we just need a list of values, so we are using only the keys of the already existing map
    if (mbeanAttributes.length > 1) {
        for (String att : mbeanAttributes) {
            readingParameters.put(att, "");
        }
    }
    // the first element in the array is always the mbean name
    FullReadingBean reading = new FullReadingBean(jvmMonitor, mbeanAttributes[0], unit);
    reading.setId(String.valueOf(ReadingsRepository.getInstance().getNewUniqueId()));
    reading.setParameters(readingParameters);
    readingTypes.add(reading);
    requestedReadingTypesPerHosts.put(monitoredHost, readingTypes);
    monitoredHosts.add(monitoredHost);
}
Also used : FullReadingBean(com.axway.ats.common.performance.monitor.beans.FullReadingBean) LinkedHashMap(java.util.LinkedHashMap) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

FullReadingBean (com.axway.ats.common.performance.monitor.beans.FullReadingBean)18 ArrayList (java.util.ArrayList)7 PublicAtsApi (com.axway.ats.common.PublicAtsApi)4 HashMap (java.util.HashMap)4 ParentProcessReadingBean (com.axway.ats.common.performance.monitor.beans.ParentProcessReadingBean)3 Validator (com.axway.ats.core.validation.Validator)3 MonitoringException (com.axway.ats.monitoring.model.exceptions.MonitoringException)3 HashSet (java.util.HashSet)3 UnsupportedReadingException (com.axway.ats.agent.components.monitoring.model.exceptions.UnsupportedReadingException)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 SigarException (org.hyperic.sigar.SigarException)2 AtsSystemMonitoringAgent (com.axway.ats.agent.components.monitoring.model.agents.AtsSystemMonitoringAgent)1 MonitorConfigurationException (com.axway.ats.agent.components.monitoring.model.exceptions.MonitorConfigurationException)1 InternalSystemMonitoringOperations (com.axway.ats.agent.components.monitoring.operations.clients.InternalSystemMonitoringOperations)1 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 Action (com.axway.ats.agent.core.model.Action)1 PerformanceMonitor (com.axway.ats.common.performance.monitor.PerformanceMonitor)1 BasicReadingBean (com.axway.ats.common.performance.monitor.beans.BasicReadingBean)1 DbAccessFactory (com.axway.ats.log.autodb.DbAccessFactory)1