001/*
002 *  Copyright 2016 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.util.Collections;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.rrd4j.DsType;
027import org.rrd4j.core.RrdDef;
028import org.rrd4j.core.Sample;
029
030import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.DiskSpaceHelper;
031import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.SampleManager;
032import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.alerts.AbstractAlertSampleManager;
033import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.alerts.AlertSampleManager.Threshold.Operator;
034
035/**
036 * {@link SampleManager} for collecting the free disk space (in MB) on the disk where Ametys Home is.
037 */
038public class DiskSpaceSampleManager extends AbstractAlertSampleManager implements Serviceable
039{
040    private DiskSpaceHelper _diskSpaceHelper;
041
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _diskSpaceHelper = (DiskSpaceHelper) manager.lookup(DiskSpaceHelper.ROLE);
046    }
047    
048    @Override
049    protected void _configureDatasources(RrdDef rrdDef)
050    {
051        _registerDatasources(rrdDef, "free", DsType.GAUGE, 0, Double.NaN);
052    }
053
054    @Override
055    protected Map<String, Object> _internalCollect(Sample sample) throws IOException
056    {
057        Map<String, Object> result = new HashMap<>();
058        long free = _diskSpaceHelper.getAvailableSpace() / 1024 / 1024; //B to MB
059        
060        sample.setValue("free", free);
061        result.put("free", free);
062        
063        return result;
064    }
065
066    @Override
067    protected String _getGraphTitle()
068    {
069        return "Free space on disk";
070    }
071
072    @Override
073    protected Map<String, String> getThresholdConfigNames()
074    {
075        return Collections.singletonMap("free", "runtime.system.alerts.diskspace.threshold");
076    }
077    
078    @Override
079    protected Map<String, Operator> getOperators()
080    {
081        return Collections.singletonMap("free", Operator.LEQ);
082    }
083}