com.alibaba.metrics.os.windows.NetTrafficGaugeSet Maven / Gradle / Ivy
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));
}
}
}
}