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;
017
018import java.io.File;
019import java.io.IOException;
020
021import org.apache.commons.io.FileUtils;
022import org.rrd4j.core.RrdDb;
023import org.rrd4j.core.RrdDef;
024
025import org.ametys.runtime.plugin.ExtensionPoint;
026import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
027import org.ametys.runtime.servlet.RuntimeConfig;
028
029/**
030 * {@link ExtensionPoint} for collecting sample of data in order to be
031 * monitored.
032 */
033public class MonitoringExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<SampleManager> implements MonitoringConstants
034{
035    /** Avalon role.*/
036    public static final String ROLE = MonitoringExtensionPoint.class.getName();
037    
038    @Override
039    public void initializeExtensions() throws Exception
040    {
041        super.initializeExtensions();
042        
043        File rrdStorageDir = FileUtils.getFile(RuntimeConfig.getInstance().getAmetysHome(), RRD_STORAGE_DIRECTORY);
044        
045        if (!rrdStorageDir.exists())
046        {
047            if (!rrdStorageDir.mkdirs())
048            {
049                throw new Exception("Unable to create monitoring directory: " + rrdStorageDir);
050            }
051        }
052
053        for (String extensionId : getExtensionsIds())
054        {
055            SampleManager sampleManager = getExtension(extensionId);
056            
057            String sampleName = sampleManager.getId();
058            File rrdFile = new File(rrdStorageDir, sampleName + RRD_EXT);
059            
060            if (getLogger().isInfoEnabled())
061            {
062                getLogger().info("Creating RRD file: " + rrdFile);
063            }
064            
065            if (!rrdFile.exists())
066            {
067                RrdDef rrdDef = new RrdDef(rrdFile.getPath(), FEEDING_PERIOD);
068                
069                sampleManager.configureRRDDef(rrdDef);
070                
071                try (RrdDb rrdDb = RrdDb.of(rrdDef))
072                {
073                    // empty
074                }
075                catch (IOException e)
076                {
077                    throw new Exception("Unable to create RRD file: " + rrdFile);
078                }
079            }
080        }
081    }
082}