/**
 * Copyright (C) 2013~2019 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.commonjava.indy.metrics.system;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * A set of gauges for system gauges, including stats on cpu, process, physical mem, swap mem.
 */
public class SystemGaugesSet
        implements MetricSet
{
    private final Logger logger = LoggerFactory.getLogger( this.getClass() );

    private final OperatingSystemMXBean operatingSystemMXBean;

    public SystemGaugesSet()
    {
        this( ManagementFactory.getOperatingSystemMXBean() );
    }

    public SystemGaugesSet( OperatingSystemMXBean mxBean )
    {
        this.operatingSystemMXBean = mxBean;
    }

    public Map<String, Metric> getMetrics()
    {
        if ( !( operatingSystemMXBean instanceof com.sun.management.OperatingSystemMXBean ) )
        {
            return Collections.emptyMap();
        }

        final com.sun.management.OperatingSystemMXBean osMxBean =
                (com.sun.management.OperatingSystemMXBean) operatingSystemMXBean;

        final Map<String, Metric> gauges = new HashMap<>();

        try
        {
            gauges.put( "process.cpu.load", (Gauge<Double>) osMxBean::getProcessCpuLoad );
            gauges.put( "system.cpu.load", (Gauge<Double>) osMxBean::getSystemCpuLoad );
            gauges.put( "system.load.avg", (Gauge<Double>) osMxBean::getSystemLoadAverage );
            gauges.put( "process.cpu.time.ms", (Gauge<Long>) () -> osMxBean.getProcessCpuTime() * 1000 );

            gauges.put( "mem.total.swap", (Gauge<Long>) osMxBean::getTotalSwapSpaceSize );
            gauges.put( "mem.total.physical", (Gauge<Long>) osMxBean::getTotalPhysicalMemorySize );
            gauges.put( "mem.free.physical", (Gauge<Long>) osMxBean::getFreePhysicalMemorySize );
            gauges.put( "mem.free.swap", (Gauge<Long>) osMxBean::getFreeSwapSpaceSize );
        }
        catch ( Throwable e )
        {
            logger.warn( "Can not get system level metrics. Reason: {}", e.getMessage() );
        }

        return Collections.unmodifiableMap( gauges );

    }

}

@@ -774,54 +774,181 @@
 org.
-slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
+commonjava.indy.conf.IndyConfiguration;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport javax.inject.Inject;%0Aimport javax.inject.Named;%0Aimport java.io.File;
 %0Aimp
@@ -1224,16 +1224,23 @@
 em.%0A */%0A
+@Named%0A
 public c
@@ -1366,24 +1366,75 @@
 Class() );%0A%0A
+    @Inject%0A    private IndyConfiguration config;%0A%0A
     private 
@@ -2515,17 +2515,17 @@
 uTime() 
-*
+/
  1000 );
@@ -2983,17 +2983,16 @@
 rn( %22Can
- 
 not get 
@@ -3067,54 +3067,1248 @@
     
-return Collections.unmodifiableMap( gauges );%0A
+try%0A        %7B%0A            final File storePath = getIndyStorageDir();%0A            if ( storePath.exists() && storePath.isDirectory() )%0A            %7B%0A                gauges.put( %22store.indy.total%22, (Gauge%3CLong%3E) storePath::getTotalSpace );%0A                gauges.put( %22store.indy.usable%22, (Gauge%3CLong%3E) storePath::getUsableSpace );%0A            %7D%0A            else%0A            %7B%0A                logger.warn( %22Cannot trace indy storage usage because storage path %7B%7D not defined.%22,%0A                             storePath.getCanonicalPath() );%0A            %7D%0A        %7D%0A        catch ( Throwable e )%0A        %7B%0A            logger.warn( %22Cannot trace indy storage usage. Reason: %7B%7D%22, e.getMessage() );%0A        %7D%0A%0A        return Collections.unmodifiableMap( gauges );%0A%0A    %7D%0A%0A    private File getIndyStorageDir()%0A    %7B%0A        // We can use indy home directly as it also contains indy storage%0A        final File indyHome = config.getIndyHomeDir();%0A        if ( indyHome != null && indyHome.exists() && indyHome.isDirectory() )%0A        %7B%0A            return indyHome;%0A        %7D%0A%0A        // Or if indy home not defined, we use docker defined one.%0A        final String DEFAULT_STORAGE_DIR = %22/var/lib/indy%22;%0A        return new File( DEFAULT_STORAGE_DIR );
 %0A   

Add metrics to monitor store disk usage