001/*
002 *  Copyright 2013 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.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.activity.Initializable;
023import org.rrd4j.DsType;
024import org.rrd4j.core.RrdDef;
025import org.rrd4j.core.Sample;
026
027/**
028 * Sample manager collecting disk I/O operations.
029 */
030public class DiskIOSampleManager extends AbstractSampleManager implements Initializable
031{
032    
033    private DiskIOMonitor _ioMonitor;
034    private Map<String, Double> _previousMetrics;
035    
036    @Override
037    public void initialize() throws Exception
038    {
039        _logger.debug("Initializing DiskIOSampleManager");
040        
041        try
042        {
043            _ioMonitor = new SigarDiskIOMonitor();
044            _logger.info("Loaded Sigar native library");
045        }
046        catch (Exception e)
047        {
048            _logger.warn("Cannot load Sigar native library");
049        }
050    }
051    
052    @Override
053    protected String _getGraphTitle()
054    {
055        return "Filesystem disk I/O";
056    }
057
058    @Override
059    protected void _configureDatasources(RrdDef rrdDef)
060    {
061        _registerDatasources(rrdDef, "reads", DsType.GAUGE, 0, Double.NaN);
062        _registerDatasources(rrdDef, "writes", DsType.GAUGE, 0, Double.NaN);
063    }
064
065    @Override
066    protected  Map<String, Object> _internalCollect(Sample sample) throws IOException
067    {
068        Map<String, Object> result = new HashMap<>();
069        double reads = Double.NaN;
070        double writes = Double.NaN;
071
072        if (_ioMonitor != null)
073        {
074            _ioMonitor.refresh();
075            Map<String, Double> metrics = _ioMonitor.toMap();
076
077            if (_previousMetrics != null)
078            {
079                reads = Math.max(0, metrics.get(DiskIOMonitor.READS) - _previousMetrics.get(DiskIOMonitor.READS));
080                writes = Math.max(0, metrics.get(DiskIOMonitor.WRITES) - _previousMetrics.get(DiskIOMonitor.WRITES));
081                
082                sample.setValue("reads", reads);
083                result.put("reads", reads);
084                sample.setValue("writes", writes);
085                result.put("writes", writes);
086            }
087            
088            _previousMetrics = metrics;
089        }
090        
091        if (_logger.isDebugEnabled())
092        {
093            _logger.debug("reads=" + reads + ", writes=" + writes);
094        }
095        
096        return result;
097    }
098}