Exemplo n.º 1
0
Arquivo: ifstat.go Projeto: donh/agent
func CoreNetMetrics(ifacePrefix []string) []*model.MetricValue {

	netIfs, err := nux.NetIfs(ifacePrefix)
	if err != nil {
		log.Println(err)
		return []*model.MetricValue{}
	}

	cnt := len(netIfs)
	ret := make([]*model.MetricValue, cnt*20+1+1)

	for idx, netIf := range netIfs {
		iface := "iface=" + netIf.Iface
		ret[idx*20+0] = CounterValue("net.if.in.bits", netIf.InBytes*8, iface)
		ret[idx*20+1] = CounterValue("net.if.in.packets", netIf.InPackages, iface)
		ret[idx*20+2] = CounterValue("net.if.in.errors", netIf.InErrors, iface)
		ret[idx*20+3] = CounterValue("net.if.in.dropped", netIf.InDropped, iface)
		ret[idx*20+4] = CounterValue("net.if.in.fifo.errs", netIf.InFifoErrs, iface)
		ret[idx*20+5] = CounterValue("net.if.in.frame.errs", netIf.InFrameErrs, iface)
		ret[idx*20+6] = CounterValue("net.if.in.compressed", netIf.InCompressed, iface)
		ret[idx*20+7] = CounterValue("net.if.in.multicast", netIf.InMulticast, iface)
		ret[idx*20+8] = CounterValue("net.if.out.bits", netIf.OutBytes*8, iface)
		ret[idx*20+9] = CounterValue("net.if.out.packets", netIf.OutPackages, iface)
		ret[idx*20+10] = CounterValue("net.if.out.errors", netIf.OutErrors, iface)
		ret[idx*20+11] = CounterValue("net.if.out.dropped", netIf.OutDropped, iface)
		ret[idx*20+12] = CounterValue("net.if.out.fifo.errs", netIf.OutFifoErrs, iface)
		ret[idx*20+13] = CounterValue("net.if.out.collisions", netIf.OutCollisions, iface)
		ret[idx*20+14] = CounterValue("net.if.out.carrier.errs", netIf.OutCarrierErrs, iface)
		ret[idx*20+15] = CounterValue("net.if.out.compressed", netIf.OutCompressed, iface)
		ret[idx*20+16] = CounterValue("net.if.total.bits", netIf.TotalBytes*8, iface)
		ret[idx*20+17] = CounterValue("net.if.total.packets", netIf.TotalPackages, iface)
		ret[idx*20+18] = CounterValue("net.if.total.errors", netIf.TotalErrors, iface)
		ret[idx*20+19] = CounterValue("net.if.total.dropped", netIf.TotalDropped, iface)
	}

	inTotalBits := int64(0)
	outTotalBits := int64(0)
	for _, netIf := range netIfs {
		if strings.Contains(netIf.Iface, "eth") || strings.Contains(netIf.Iface, "em") || strings.Contains(netIf.Iface, "enp") {
			inTotalBits += netIf.InBytes * 8
			outTotalBits += netIf.OutBytes * 8
		}
	}

	if cnt != 0 {
		ret[cnt*20+0] = CounterValue("net.if.in.bits", inTotalBits, "iface=eth_all")
		ret[cnt*20+1] = CounterValue("net.if.out.bits", outTotalBits, "iface=eth_all")
	}

	return ret
}
Exemplo n.º 2
0
func CoreTrafficMetrics() (L []*model.MetricValue) {
	tifs := getfaces()
	if tifs == nil || len(tifs) == 0 {
		log.Println("get faces error")
		return
	}
	ifacePrefix := make([]string, 0, 0)
	for _, v := range tifs {
		isexist := false
		for _, ifa := range ifacePrefix {
			if ifa == v {
				isexist = true
				break
			}
		}
		if isexist == false {
			ifacePrefix = append(ifacePrefix, v)
		}
	}
	netIfs, err := nux.NetIfs(ifacePrefix)
	if err != nil {
		log.Println(err)
		return []*model.MetricValue{}
	}

	for _, netIf := range netIfs {
		ifacetag := "iface=" + netIf.Iface
		L = append(L, CounterValue("netcard.if.in.bytes", netIf.InBytes, ifacetag))
		L = append(L, CounterValue("netcard.if.out.bytes", netIf.OutBytes, ifacetag))
		L = append(L, CounterValue("netcard.if.total.bytes", netIf.TotalBytes, ifacetag))
	}

	for ip, iface := range tifs {
		wi, li, wo, lo, err := gettraffic(iface)
		iptag := "ip=" + ip
		ifacetag := "iface=" + iface
		if err != nil {
			log.Println("get taffic falure. ", err)
			L = append(L, GaugeValue("traffic.collect.status", 1, ifacetag, iptag))
			continue
		}
		L = append(L, GaugeValue("traffic.collect.status", 0, ifacetag, iptag))
		L = append(L, CounterValue("traffic.wan.in", wi, ifacetag, iptag))
		L = append(L, CounterValue("traffic.lan.in", li, ifacetag, iptag))
		L = append(L, CounterValue("traffic.wan.out", wo, ifacetag, iptag))
		L = append(L, CounterValue("traffic.lan.out", lo, ifacetag, iptag))
	}
	return L
}
Exemplo n.º 3
0
func CoreNetMetrics(ifacePrefix []string) []*model.MetricValue {

	netIfs, err := nux.NetIfs(ifacePrefix)
	if err != nil {
		log.Println(err)
		return []*model.MetricValue{}
	}

	cnt := len(netIfs)
	ret := make([]*model.MetricValue, cnt*23)

	for idx, netIf := range netIfs {
		iface := "iface=" + netIf.Iface
		ret[idx*23+0] = CounterValue("net.if.in.bytes", netIf.InBytes, iface)
		ret[idx*23+1] = CounterValue("net.if.in.packets", netIf.InPackages, iface)
		ret[idx*23+2] = CounterValue("net.if.in.errors", netIf.InErrors, iface)
		ret[idx*23+3] = CounterValue("net.if.in.dropped", netIf.InDropped, iface)
		ret[idx*23+4] = CounterValue("net.if.in.fifo.errs", netIf.InFifoErrs, iface)
		ret[idx*23+5] = CounterValue("net.if.in.frame.errs", netIf.InFrameErrs, iface)
		ret[idx*23+6] = CounterValue("net.if.in.compressed", netIf.InCompressed, iface)
		ret[idx*23+7] = CounterValue("net.if.in.multicast", netIf.InMulticast, iface)
		ret[idx*23+8] = CounterValue("net.if.out.bytes", netIf.OutBytes, iface)
		ret[idx*23+9] = CounterValue("net.if.out.packets", netIf.OutPackages, iface)
		ret[idx*23+10] = CounterValue("net.if.out.errors", netIf.OutErrors, iface)
		ret[idx*23+11] = CounterValue("net.if.out.dropped", netIf.OutDropped, iface)
		ret[idx*23+12] = CounterValue("net.if.out.fifo.errs", netIf.OutFifoErrs, iface)
		ret[idx*23+13] = CounterValue("net.if.out.collisions", netIf.OutCollisions, iface)
		ret[idx*23+14] = CounterValue("net.if.out.carrier.errs", netIf.OutCarrierErrs, iface)
		ret[idx*23+15] = CounterValue("net.if.out.compressed", netIf.OutCompressed, iface)
		ret[idx*23+16] = CounterValue("net.if.total.bytes", netIf.TotalBytes, iface)
		ret[idx*23+17] = CounterValue("net.if.total.packets", netIf.TotalPackages, iface)
		ret[idx*23+18] = CounterValue("net.if.total.errors", netIf.TotalErrors, iface)
		ret[idx*23+19] = CounterValue("net.if.total.dropped", netIf.TotalDropped, iface)
		ret[idx*23+20] = GaugeValue("net.if.speed.bits", netIf.SpeedBits, iface)
		ret[idx*23+21] = CounterValue("net.if.in.percent", netIf.InPercent, iface)
		ret[idx*23+22] = CounterValue("net.if.out.percent", netIf.OutPercent, iface)
	}
	return ret
}