All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.metrics.os.windows.NetTrafficGaugeSet Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.alibaba.metrics.os.windows;

import com.alibaba.metrics.CachedMetricSet;
import com.alibaba.metrics.Clock;
import com.alibaba.metrics.Metric;
import com.alibaba.metrics.MetricName;
import com.alibaba.metrics.PersistentGauge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class NetTrafficGaugeSet extends CachedMetricSet {

    private static final Logger logger = LoggerFactory.getLogger(NetTrafficGaugeSet.class);

    private static final String[] FIELDS = {
            "net.in.bytes", "net.in.packets", "net.in.errs", "net.in.dropped",
            "net.in.fifo.errs", "net.in.frame.errs", "net.in.compressed", "net.in.multicast",
            "net.out.bytes", "net.out.packets", "net.out.errs", "net.out.dropped",
            "net.out.fifo.errs", "net.out.collisions", "net.out.carrier.errs", "net.out.compressed"
    };

    private Map countByFace;
    private Map rateByFace;

    private Map gauges;

    public NetTrafficGaugeSet(long dataTTL, TimeUnit unit) {
        this(dataTTL, unit, Clock.defaultClock());
    }

    public NetTrafficGaugeSet(long dataTTL, TimeUnit unit, Clock clock) {
        super(dataTTL, unit, clock);
        this.gauges = new HashMap();
        countByFace = new HashMap();
        rateByFace = new HashMap();
        getValueInternal();
        populateGauges();
    }

    @Override
    public Map getMetrics() {
        return null;
    }

    @Override
    protected void getValueInternal() {

//        String[] lst = sigar.getNetInterfaceList();
//        NetInterfaceStat netStat;
//
//        if (lst != null && lst.length > 2){
//            netStat = sigar.getNetInterfaceStat(lst[1]);
//        }
//
//        try {
//
//            for (String line: lines) {
//                Matcher netMatcher = NET_PATTERN.matcher(line);
//
//                if (netMatcher.matches()) {
//                    String face = netMatcher.group(1);
//                    if (countByFace.get(face) == null) {
//                        Long[] counts = new Long[FIELDS.length];
//                        Double[] rates = new Double[FIELDS.length];
//                        for (int i=0; i {

        private String face;
        private int index;

        public NetGauge(String face, int index) {
            this.face = face;
            this.index = index;
        }

        @Override
        public Double getValue() {
            try {
                refreshIfNecessary();
                return rateByFace.get(face)[index];
            } catch (Exception e) {
                return 0.0d;
            }
        }
    }

    private void populateGauges() {
        for (Map.Entry entry: countByFace.entrySet()) {
            for (int i = 0; i < entry.getValue().length; i++) {
                gauges.put(MetricName.build(FIELDS[i]).tagged("face", entry.getKey()),
                        new NetGauge(entry.getKey(), i));
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy