File: ethflux.go

package info (click to toggle)
ethflux 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 72 kB
  • sloc: makefile: 14
file content (83 lines) | stat: -rw-r--r-- 1,611 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main

// ethflux
// Copyright (c) 2018, DCSO GmbH

import (
	"bytes"
	"flag"
	"fmt"
	"net"
	"os"
	"regexp"

	"github.com/DCSO/fluxline"
	log "github.com/Sirupsen/logrus"
	"github.com/safchain/ethtool"
)

func main() {

	msmt := flag.String("measurement", "ethtool",
		"name of measurement to use in line")
	help := flag.Bool("help", false, "show help")
	quiet := flag.Bool("quiet", false, "do not show warnings")
	flag.Parse()

	argsWithoutProg := flag.Args()

	ifs, err := net.Interfaces()
	if err != nil {
		log.Fatal(err)
	}

	if *help {
		fmt.Fprintln(os.Stderr, "Usage: ethflux [-help] [-quiet] [-measurement=string] <iface pattern>")
		flag.PrintDefaults()
		os.Exit(1)
	}
	pattern := ".*"
	if len(argsWithoutProg) > 0 {
		pattern = argsWithoutProg[0]
	}

	ethHandle, err := ethtool.NewEthtool()
	if err != nil {
		log.Fatal(err)
	}
	for _, intf := range ifs {
		if len(pattern) > 0 {
			match, err := regexp.MatchString(pattern, intf.Name)
			if err != nil {
				log.Fatal(err)
			}
			if !match {
				continue
			}
		}
		stats, err := ethHandle.Stats(intf.Name)
		if err != nil {
			if !*quiet {
				log.Warn(err)
			}
			continue
		}
		drivername, err := ethHandle.DriverName(intf.Name)
		if err != nil {
			log.Fatal(err)
		}

		stringStats := make(map[string]string)
		for k, v := range stats {
			stringStats[k] = fmt.Sprintf("%d", v)
		}
		stringStats["driver"] = fmt.Sprintf(`"%s"`, drivername)
		tags := make(map[string]string)
		tags["interface"] = intf.Name

		var b bytes.Buffer
		enc := fluxline.NewEncoder(&b)
		enc.EncodeMap(*msmt, stringStats, tags)
		fmt.Println(b.String())
	}
}