Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ethflux
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
## Getting the code

```
go get github.com/DCSO/ethflux.git
cd ethflux
go get ./...
```

## Building
Expand All @@ -20,13 +21,17 @@ will produce an `ethflux` binary.
## Usage

```
Usage: ethflux [-help] [-quiet] [-measurement=string] <iface pattern>
Usage: ethflux [-help] [-quiet] [-hostname] [-measurement=string] [-sum-field=name=regex ...] <iface pattern>
-help
show help
-hostname
use os.Hostname() rather than FQDN
-measurement string
name of measurement to use in line (default "ethtool")
-quiet
do not show warnings
-sum-field value
add a derived field as name=regex, summing all fields matching regex (can be repeated)
```
The interface parameter supports regular expressions, e.g. `wl.*` would match `wlp4s0` etc.

Expand Down
52 changes: 50 additions & 2 deletions ethflux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,57 @@ import (
"net"
"os"
"regexp"
"strings"

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

// sumField describes a derived field that is computed as the sum of all
// existing fields whose name matches Pattern.
type sumField struct {
Name string
Pattern *regexp.Regexp
}

// sumFieldsFlag allows the -sum-field flag to be specified multiple times,
// each in the form name=regex.
type sumFieldsFlag []sumField

func (s *sumFieldsFlag) String() string {
parts := make([]string, len(*s))
for i, f := range *s {
parts[i] = fmt.Sprintf("%s=%s", f.Name, f.Pattern.String())
}
return strings.Join(parts, ",")
}

func (s *sumFieldsFlag) Set(value string) error {
idx := strings.Index(value, "=")
if idx < 0 {
return fmt.Errorf("invalid -sum-field value %q, expected format name=regex", value)
}
name := value[:idx]
pattern := value[idx+1:]
re, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("invalid regex in -sum-field %q: %w", value, err)
}
*s = append(*s, sumField{Name: name, Pattern: re})
return nil
}

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")
useHostname := flag.Bool("hostname", false, "use os.Hostname() rather than FQDN")
var sumFields sumFieldsFlag
flag.Var(&sumFields, "sum-field",
"add a derived field as name=regex, summing all fields matching regex (can be repeated)")
flag.Parse()

argsWithoutProg := flag.Args()
Expand All @@ -33,7 +71,7 @@ func main() {
}

if *help {
fmt.Fprintln(os.Stderr, "Usage: ethflux [-help] [-quiet] [-hostname] [-measurement=string] <iface pattern>")
fmt.Fprintln(os.Stderr, "Usage: ethflux [-help] [-quiet] [-hostname] [-measurement=string] [-sum-field=name=regex ...] <iface pattern>")
flag.PrintDefaults()
os.Exit(1)
}
Expand Down Expand Up @@ -77,6 +115,16 @@ func main() {
log.Fatal(err)
}

for _, sf := range sumFields {
var sum uint64
for k, v := range stats {
if sf.Pattern.MatchString(k) {
sum += v
}
}
stats[sf.Name] = sum
}

stringStats := make(map[string]string)
for k, v := range stats {
stringStats[k] = fmt.Sprintf("%d", v)
Expand Down
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/DCSO/ethflux

go 1.26.3

require (
github.com/DCSO/fluxline v0.0.0-20200907065040-78686e5e68f6
github.com/safchain/ethtool v0.7.0
github.com/sirupsen/logrus v1.9.4
)

require (
github.com/Showmax/go-fqdn v1.0.0 // indirect
golang.org/x/sys v0.38.0 // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/DCSO/fluxline v0.0.0-20200907065040-78686e5e68f6 h1:ILpYBsvnQwnM7PvDKoLjLx47tE0jPsR/oX3CvhuhJG4=
github.com/DCSO/fluxline v0.0.0-20200907065040-78686e5e68f6/go.mod h1:PvQC78r0wMuOsJ8Sl9isIEYR+00WKI/gliLjVYfDVjQ=
github.com/Showmax/go-fqdn v1.0.0 h1:0rG5IbmVliNT5O19Mfuvna9LL7zlHyRfsSvBPZmF9tM=
github.com/Showmax/go-fqdn v1.0.0/go.mod h1:SfrFBzmDCtCGrnHhoDjuvFnKsWjEQX/Q9ARZvOrJAko=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/safchain/ethtool v0.7.0 h1:rlJzfDetsVvT61uz8x1YIcFn12akMfuPulHtZjtb7Is=
github.com/safchain/ethtool v0.7.0/go.mod h1:MenQKEjXdfkjD3mp2QdCk8B/hwvkrlOTm/FD4gTpFxQ=
github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w=
github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=