二次开发 - 修改网卡流量单位

优质
小牛编辑
127浏览
2023-12-01

修改网卡流量单位

目前是以 Bytes 为单位,如果要改为 Bits 的话(如:Mbps),可以修改 agent 代码。

采集原始网卡流量数据的代码 github.com/toolkits/nux/ifstat.go,如下:

  1. func NetIfs(onlyPrefix []string) ([]*NetIf, error) {
  2. ...
  3. {
  4. ...
  5. netIf.InBytes, _ = strconv.ParseInt(fields[0], 10, 64)
  6. netIf.InPackages, _ = strconv.ParseInt(fields[1], 10, 64)
  7. netIf.InErrors, _ = strconv.ParseInt(fields[2], 10, 64)
  8. netIf.InDropped, _ = strconv.ParseInt(fields[3], 10, 64)
  9. netIf.InFifoErrs, _ = strconv.ParseInt(fields[4], 10, 64)
  10. netIf.InFrameErrs, _ = strconv.ParseInt(fields[5], 10, 64)
  11. netIf.InCompressed, _ = strconv.ParseInt(fields[6], 10, 64)
  12. netIf.InMulticast, _ = strconv.ParseInt(fields[7], 10, 64)
  13. netIf.OutBytes, _ = strconv.ParseInt(fields[8], 10, 64)
  14. netIf.OutPackages, _ = strconv.ParseInt(fields[9], 10, 64)
  15. netIf.OutErrors, _ = strconv.ParseInt(fields[10], 10, 64)
  16. netIf.OutDropped, _ = strconv.ParseInt(fields[11], 10, 64)
  17. netIf.OutFifoErrs, _ = strconv.ParseInt(fields[12], 10, 64)
  18. netIf.OutCollisions, _ = strconv.ParseInt(fields[13], 10, 64)
  19. netIf.OutCarrierErrs, _ = strconv.ParseInt(fields[14], 10, 64)
  20. netIf.OutCompressed, _ = strconv.ParseInt(fields[15], 10, 64)
  21. netIf.TotalBytes = netIf.InBytes + netIf.OutBytes
  22. netIf.TotalPackages = netIf.InPackages + netIf.OutPackages
  23. netIf.TotalErrors = netIf.InErrors + netIf.OutErrors
  24. netIf.TotalDropped = netIf.InDropped + netIf.OutDropped
  25. ret = append(ret, &netIf)
  26. }
  27. return ret, nil
  28. }

agent 相对应的代码 github.com/open-falcon/agent/funcs/ifstats.go,如下:

  1. func CoreNetMetrics(ifacePrefix []string) []*model.MetricValue {
  2. netIfs, err := nux.NetIfs(ifacePrefix)
  3. if err != nil {
  4. log.Println(err)
  5. return []*model.MetricValue{}
  6. }
  7. cnt := len(netIfs)
  8. ret := make([]*model.MetricValue, cnt*20)
  9. for idx, netIf := range netIfs {
  10. iface := "iface=" + netIf.Iface
  11. ret[idx*20+0] = CounterValue("net.if.in.bytes", netIf.InBytes, iface)
  12. ret[idx*20+1] = CounterValue("net.if.in.packets", netIf.InPackages, iface)
  13. ret[idx*20+2] = CounterValue("net.if.in.errors", netIf.InErrors, iface)
  14. ret[idx*20+3] = CounterValue("net.if.in.dropped", netIf.InDropped, iface)
  15. ret[idx*20+4] = CounterValue("net.if.in.fifo.errs", netIf.InFifoErrs, iface)
  16. ret[idx*20+5] = CounterValue("net.if.in.frame.errs", netIf.InFrameErrs, iface)
  17. ret[idx*20+6] = CounterValue("net.if.in.compressed", netIf.InCompressed, iface)
  18. ret[idx*20+7] = CounterValue("net.if.in.multicast", netIf.InMulticast, iface)
  19. ret[idx*20+8] = CounterValue("net.if.out.bytes", netIf.OutBytes, iface)
  20. ret[idx*20+9] = CounterValue("net.if.out.packets", netIf.OutPackages, iface)
  21. ret[idx*20+10] = CounterValue("net.if.out.errors", netIf.OutErrors, iface)
  22. ret[idx*20+11] = CounterValue("net.if.out.dropped", netIf.OutDropped, iface)
  23. ret[idx*20+12] = CounterValue("net.if.out.fifo.errs", netIf.OutFifoErrs, iface)
  24. ret[idx*20+13] = CounterValue("net.if.out.collisions", netIf.OutCollisions, iface)
  25. ret[idx*20+14] = CounterValue("net.if.out.carrier.errs", netIf.OutCarrierErrs, iface)
  26. ret[idx*20+15] = CounterValue("net.if.out.compressed", netIf.OutCompressed, iface)
  27. ret[idx*20+16] = CounterValue("net.if.total.bytes", netIf.TotalBytes, iface)
  28. ret[idx*20+17] = CounterValue("net.if.total.packets", netIf.TotalPackages, iface)
  29. ret[idx*20+18] = CounterValue("net.if.total.errors", netIf.TotalErrors, iface)
  30. ret[idx*20+19] = CounterValue("net.if.total.dropped", netIf.TotalDropped, iface)
  31. }
  32. return ret
  33. }

舉例來說,我們可以直接在 agent 要上報數據的時候,將 Bit 轉換為 Byte,同時修改 Counter 的名稱,如下:

  1. ret[idx*20+0] = CounterValue("net.if.in.bits", netIf.InBytes*8, iface)