2021-03-25 14:47:35 +01:00
|
|
|
package collectors
|
|
|
|
|
|
|
|
import (
|
2021-11-25 15:11:39 +01:00
|
|
|
"encoding/json"
|
2021-03-25 14:47:35 +01:00
|
|
|
"io/ioutil"
|
2021-03-25 17:47:08 +01:00
|
|
|
"log"
|
2021-03-25 14:47:35 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-02-04 12:39:59 +01:00
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
2021-03-25 14:47:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const NETSTATFILE = `/proc/net/dev`
|
|
|
|
|
2021-11-25 14:04:03 +01:00
|
|
|
type NetstatCollectorConfig struct {
|
2021-11-26 18:19:45 +01:00
|
|
|
ExcludeDevices []string `json:"exclude_devices"`
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:47:35 +01:00
|
|
|
type NetstatCollector struct {
|
2022-01-25 15:37:43 +01:00
|
|
|
metricCollector
|
2021-11-25 15:11:39 +01:00
|
|
|
config NetstatCollectorConfig
|
2021-10-04 15:23:43 +02:00
|
|
|
matches map[int]string
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *NetstatCollector) Init(config json.RawMessage) error {
|
2021-03-25 17:47:08 +01:00
|
|
|
m.name = "NetstatCollector"
|
2021-03-25 14:47:35 +01:00
|
|
|
m.setup()
|
2022-01-25 15:37:43 +01:00
|
|
|
m.meta = map[string]string{"source": m.name, "group": "Memory"}
|
2021-10-04 15:23:43 +02:00
|
|
|
m.matches = map[int]string{
|
2022-02-04 12:39:59 +01:00
|
|
|
1: "net_bytes_in",
|
|
|
|
9: "net_bytes_out",
|
|
|
|
2: "net_pkts_in",
|
|
|
|
10: "net_pkts_out",
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
2021-11-26 18:19:45 +01:00
|
|
|
if len(config) > 0 {
|
2021-11-29 15:32:58 +01:00
|
|
|
err := json.Unmarshal(config, &m.config)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
2021-11-26 19:02:09 +01:00
|
|
|
_, err := ioutil.ReadFile(string(NETSTATFILE))
|
2021-10-04 15:23:43 +02:00
|
|
|
if err == nil {
|
2021-10-04 15:47:03 +02:00
|
|
|
m.init = true
|
|
|
|
}
|
2021-05-14 19:21:16 +02:00
|
|
|
return nil
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
func (m *NetstatCollector) Read(interval time.Duration, output chan lp.CCMetric) {
|
2021-03-25 14:47:35 +01:00
|
|
|
data, err := ioutil.ReadFile(string(NETSTATFILE))
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lines := strings.Split(string(data), "\n")
|
|
|
|
for _, l := range lines {
|
|
|
|
if !strings.Contains(l, ":") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
f := strings.Fields(l)
|
|
|
|
dev := f[0][0 : len(f[0])-1]
|
2021-11-25 14:04:03 +01:00
|
|
|
cont := false
|
|
|
|
for _, d := range m.config.ExcludeDevices {
|
2021-11-25 15:11:39 +01:00
|
|
|
if d == dev {
|
|
|
|
cont = true
|
|
|
|
}
|
2021-11-25 14:04:03 +01:00
|
|
|
}
|
|
|
|
if cont {
|
2021-03-25 14:47:35 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-11-25 15:11:39 +01:00
|
|
|
tags := map[string]string{"device": dev, "type": "node"}
|
2021-10-04 15:23:43 +02:00
|
|
|
for i, name := range m.matches {
|
2021-03-25 14:47:35 +01:00
|
|
|
v, err := strconv.ParseInt(f[i], 10, 0)
|
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
y, err := lp.New(name, tags, m.meta, map[string]interface{}{"value": int(float64(v) * 1.0e-3)}, time.Now())
|
2021-10-04 15:23:43 +02:00
|
|
|
if err == nil {
|
2022-01-25 15:37:43 +01:00
|
|
|
switch {
|
|
|
|
case strings.Contains(name, "byte"):
|
|
|
|
y.AddMeta("unit", "Byte")
|
|
|
|
case strings.Contains(name, "pkt"):
|
|
|
|
y.AddMeta("unit", "Packets")
|
|
|
|
}
|
|
|
|
output <- y
|
2021-10-04 15:23:43 +02:00
|
|
|
}
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 17:47:08 +01:00
|
|
|
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *NetstatCollector) Close() {
|
2021-10-04 15:47:03 +02:00
|
|
|
m.init = false
|
2021-03-25 14:47:35 +01:00
|
|
|
}
|