001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.runtime.plugins.admin.jvmstatus.monitoring.sample;
017
018import java.io.IOException;
019import java.lang.management.ManagementFactory;
020import java.lang.management.ThreadMXBean;
021import java.util.Collections;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.rrd4j.DsType;
026import org.rrd4j.core.RrdDef;
027import org.rrd4j.core.Sample;
028
029import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.SampleManager;
030import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.alerts.AbstractAlertSampleManager;
031import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.alerts.AlertSampleManager.Threshold.Operator;
032
033/**
034 * {@link SampleManager} for collecting the number of live threads.
035 */
036public class ThreadSampleManager extends AbstractAlertSampleManager
037{
038    @Override
039    protected void _configureDatasources(RrdDef rrdDef)
040    {
041        _registerDatasources(rrdDef, "daemon", DsType.GAUGE, 0, Double.NaN);
042        _registerDatasources(rrdDef, "total", DsType.GAUGE, 0, Double.NaN);
043    }
044
045    @Override
046    protected Map<String, Object> _internalCollect(Sample sample) throws IOException
047    {
048        Map<String, Object> result = new HashMap<>();
049        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
050
051        int daemon = threadMXBean.getDaemonThreadCount();
052        sample.setValue("daemon", daemon);
053        result.put("daemon", daemon);
054        
055        int total = threadMXBean.getThreadCount();
056        sample.setValue("total", total);
057        result.put("total", total);
058        
059        return result;
060    }
061
062    @Override
063    protected String _getGraphTitle()
064    {
065        return "Live thread";
066    }
067
068    @Override
069    protected Map<String, String> getThresholdConfigNames()
070    {
071        return Collections.singletonMap("total", "runtime.system.alerts.threads.threshold");
072    }
073    
074    @Override
075    protected Map<String, Operator> getOperators()
076    {
077        return Collections.singletonMap("total", Operator.GEQ);
078    }
079}