|
| 1 | +// Copyright 2023 the netexp authors. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +// TODO: |
| 5 | +// - Test netdev.*NetDev.Traffic + $HOST_PROC. |
| 6 | +// - Add proper logging. |
| 7 | +// - Test netdev's logging. |
| 8 | +// - Use layer8co/toolbox/container/ringbuf in netdev instead of []int64 once it's ready. |
| 9 | +// - Implement a generic bucketed pool in layer8co/toolbox and use that in rcu.*BufferRcu instead of sync.Pool. |
| 10 | +// - Move rcu to layer8co/toolbox. |
| 11 | + |
| 12 | +package main |
| 13 | + |
| 14 | +import ( |
| 15 | + "bytes" |
| 16 | + "flag" |
| 17 | + "fmt" |
| 18 | + "io" |
| 19 | + "net/http" |
| 20 | + "os" |
| 21 | + "regexp" |
| 22 | + "strings" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/layer8co/netexp/internal/metrics" |
| 26 | + "github.com/layer8co/netexp/internal/netdev" |
| 27 | + "github.com/layer8co/netexp/internal/rcu" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + appName = "netexp" |
| 32 | + helpText = "netexp is a Prometheus exporter that provides advanced network usage metrics." |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + listen = flag.String( |
| 37 | + "listen", |
| 38 | + ":9298", |
| 39 | + "address to listen on", |
| 40 | + ) |
| 41 | + ifaceRegexpFlag = flag.String( |
| 42 | + "iface-regexp", |
| 43 | + netdev.IfacePattern, |
| 44 | + "regexp to match network interface names", |
| 45 | + ) |
| 46 | + interval = flag.Duration( |
| 47 | + "interval", |
| 48 | + time.Second, |
| 49 | + "polling interval (e.g. 500ms, 1s)", |
| 50 | + ) |
| 51 | + burstWindowsFlag = flag.String( |
| 52 | + "burst-windows", |
| 53 | + "1s,5s", |
| 54 | + "comma-separated burst window durations", |
| 55 | + ) |
| 56 | + outputWindowsFlag = flag.String( |
| 57 | + "output-windows", |
| 58 | + "15s,30s,60s", |
| 59 | + "comma-separated output window durations", |
| 60 | + ) |
| 61 | +) |
| 62 | + |
| 63 | +var ( |
| 64 | + appRcu = rcu.NewBufferRcu() |
| 65 | + appNetDev *netdev.NetDev |
| 66 | + appMetrics *metrics.Metrics |
| 67 | +) |
| 68 | + |
| 69 | +func main() { |
| 70 | + |
| 71 | + flag.Usage = func() { |
| 72 | + fmt.Fprintf(flag.CommandLine.Output(), "%s\n\nUsage:\n", helpText) |
| 73 | + flag.PrintDefaults() |
| 74 | + } |
| 75 | + |
| 76 | + flag.Parse() |
| 77 | + |
| 78 | + ifaceRegexp, err := regexp.Compile(*ifaceRegexpFlag) |
| 79 | + if err != nil { |
| 80 | + die(fmt.Sprintf("-iface-regexp parse erorr: %s", err)) |
| 81 | + } |
| 82 | + |
| 83 | + appNetDev = netdev.New(ifaceRegexp.Match, func(fn func(io.Writer)) { |
| 84 | + b := new(bytes.Buffer) |
| 85 | + fn(b) |
| 86 | + fmt.Printf("%s\n", b.Bytes()) |
| 87 | + }) |
| 88 | + |
| 89 | + appMetrics = metrics.New(metrics.Config{ |
| 90 | + Interval: *interval, |
| 91 | + BurstWindows: mustGet(parseDurations(*burstWindowsFlag)), |
| 92 | + OutputWindows: mustGet(parseDurations(*outputWindowsFlag)), |
| 93 | + }) |
| 94 | + |
| 95 | + fmt.Printf("listening on %s\n", *listen) |
| 96 | + |
| 97 | + go func() { |
| 98 | + mustDo(gatherMetrics()) |
| 99 | + }() |
| 100 | + |
| 101 | + serveHttp() |
| 102 | +} |
| 103 | + |
| 104 | +func serveHttp() error { |
| 105 | + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 106 | + fmt.Fprintln(w, appName) |
| 107 | + }) |
| 108 | + http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) { |
| 109 | + appRcu.Read(func(b []byte) { |
| 110 | + w.Write(b) |
| 111 | + }) |
| 112 | + }) |
| 113 | + return http.ListenAndServe(*listen, nil) |
| 114 | +} |
| 115 | + |
| 116 | +func gatherMetrics() error { |
| 117 | + for ; true; <-time.Tick(*interval) { |
| 118 | + recv, trns, err := appNetDev.Traffic() |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + appRcu.Update(func(b []byte) ([]byte, error) { |
| 123 | + b = appMetrics.Step(recv, trns, b) |
| 124 | + b = append(b, '\n') |
| 125 | + return b, nil |
| 126 | + }) |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func parseDurations(s string) (out []time.Duration, err error) { |
| 132 | + for field := range strings.SplitSeq(s, ",") { |
| 133 | + field = strings.TrimSpace(field) |
| 134 | + d, err := time.ParseDuration(field) |
| 135 | + if err != nil { |
| 136 | + return nil, fmt.Errorf("could not parse duration %q: %w", field, err) |
| 137 | + } |
| 138 | + out = append(out, d) |
| 139 | + } |
| 140 | + return out, nil |
| 141 | +} |
| 142 | + |
| 143 | +func die(s string) { |
| 144 | + fmt.Println(s) |
| 145 | + os.Exit(1) |
| 146 | +} |
| 147 | + |
| 148 | +func mustDo(err error) { |
| 149 | + if err != nil { |
| 150 | + die(err.Error()) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func mustGet[T any](v T, err error) T { |
| 155 | + mustDo(err) |
| 156 | + return v |
| 157 | +} |
0 commit comments