package org.apereo.cas.monitor.config;

import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.configuration.model.core.monitor.MonitorProperties;
import org.apereo.cas.monitor.HealthCheckMonitor;
import org.apereo.cas.monitor.MemoryMonitor;
import org.apereo.cas.monitor.Monitor;
import org.apereo.cas.monitor.SessionMonitor;
import org.apereo.cas.ticket.registry.TicketRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * This is {@link CasCoreMonitorConfiguration}.
 *
 * @author Misagh Moayyed
 * @since 5.0.0
 */
@Configuration("casCoreMonitorConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class CasCoreMonitorConfiguration {

    @Autowired
    @Qualifier("ticketRegistry")
    private TicketRegistry ticketRegistry;

    @Autowired
    private CasConfigurationProperties casProperties;

    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @ConditionalOnMissingBean(name = "healthCheckMonitor")
    @Bean
    public Monitor healthCheckMonitor() {
        final Map<String, Monitor> beans = applicationContext.getBeansOfType(Monitor.class, false, true);
        final Set<Monitor> monitors = beans.entrySet().stream()
                .map(Map.Entry::getValue)
                .collect(Collectors.toSet());

        final int freeMemThreshold = casProperties.getMonitor().getFreeMemThreshold();
        if (freeMemThreshold > 0) {
            monitors.add(new MemoryMonitor(freeMemThreshold));
        }

        final MonitorProperties.Warn warn = casProperties.getMonitor().getSt().getWarn();
        if (warn.getThreshold() > 0) {
            final SessionMonitor bean = new SessionMonitor(ticketRegistry, warn.getThreshold(), warn.getThreshold());
            monitors.add(bean);
        }

        return new HealthCheckMonitor(monitors);
    }
}

@@ -405,16 +405,73 @@
 gistry;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -1268,16 +1268,117 @@
 ration %7B
+%0A    private static final Logger LOGGER = LoggerFactory.getLogger(CasCoreMonitorConfiguration.class);
 %0A%0A    @A
@@ -2121,56 +2121,257 @@
     
-monitors.add(new MemoryMonitor(freeMemThreshold)
+LOGGER.debug(%22Configured memory monitor with free-memory threshold %5B%7B%7D%5D%22, freeMemThreshold);%0A            monitors.add(new MemoryMonitor(freeMemThreshold));%0A        %7D else %7B%0A            LOGGER.debug(%22Memory monitor is disabled from the configuration%22
 );%0A 
@@ -2421,16 +2421,18 @@
 arn warn
+St
  = casPr
@@ -2484,38 +2484,349 @@
     
-if (warn.getThreshold() %3E 0) %7B
+final MonitorProperties.Warn warnTgt = casProperties.getMonitor().getTgt().getWarn();%0A        if (warnSt.getThreshold() %3E 0 && warnTgt.getThreshold() %3E 0) %7B%0A            LOGGER.debug(%22Configured session monitor with service ticket threshold %5B%7B%7D%5D and session threshold %5B%7B%7D%5D%22,%0A                    warnSt.getThreshold(), warnTgt.getThreshold());
 %0A   
@@ -2897,24 +2897,26 @@
 gistry, warn
+St
 .getThreshol
@@ -2924,16 +2924,19 @@
 (), warn
+Tgt
 .getThre
@@ -2978,32 +2978,129 @@
 bean);%0A        %7D
+ else %7B%0A            LOGGER.debug(%22Session monitor is disabled from the configuration%22);%0A        %7D
 %0A%0A        return

fix bug in session monitor using the wrong property