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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
)
type Iface struct {
Name string
RxBytes uint64
RxPackets uint64
RxErrs uint64
TxBytes uint64
TxPackets uint64
TxErrs uint64
}
// human readable bits 'n bytes per second
// assumes bytes input.
func humanBps(bps float64, raw bool, bits bool) string {
prefixes := []string{"", "k", "M", "G", "T", "P", "E", "Z", "Y"}
unit := "B/s"
if bits {
bps *= 8
unit = "b/s"
}
if raw {
return fmt.Sprintf("%.2f", bps)
}
prefix := 0
for prefix+1 < len(prefixes) && bps >= 1000 {
bps /= 1000
prefix++
}
return fmt.Sprintf("%.2f %s%s", bps, prefixes[prefix], unit)
}
func main() {
bitFlag := flag.Bool("b", false, "render bits not bytes")
intervalFlag := flag.String("d", "1s", "update interval (e.g. 500ms, 1s, 2s)")
pathFlag := flag.String("f", "/proc/net/dev", "path")
interfaceFlag := flag.String("i", "", "interface to report on")
rawFlag := flag.Bool("r", false, "show raw values, not human")
versionFlag := flag.Bool("V", false, "show version information")
flag.Parse()
if *versionFlag {
fmt.Println("fsbm-go version 1.0")
os.Exit(0)
}
interval, err := time.ParseDuration(*intervalFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid duration (%s): %v", *intervalFlag, err)
os.Exit(1)
}
if _, err := os.Stat(*pathFlag); err != nil {
fmt.Fprintf(os.Stderr, "cannot access %s: %v\n", *pathFlag, err)
os.Exit(1)
}
limitIfaces := false
requestedIfaces := make(map[string]struct{})
if *interfaceFlag != "" {
for _, name := range strings.Split(*interfaceFlag, ",") {
requestedIfaces[name] = struct{}{}
}
if len(requestedIfaces) > 0 {
limitIfaces = true
}
}
ifaces := make(map[string]Iface)
var prevTime time.Time
for {
openTime := time.Now()
file, err := os.Open(*pathFlag) // For read access.
if err != nil {
fmt.Fprintf(os.Stderr, "cannot access %s: %v\n", *pathFlag, err)
os.Exit(1)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
name, rest, found := strings.Cut(line, ":")
if !found {
continue
}
if limitIfaces {
_, ok := requestedIfaces[name]
if !ok {
continue
}
}
fields := strings.Fields(rest)
if len(fields) != 16 {
fmt.Fprintf(os.Stderr, "cannot parse %s", line)
os.Exit(1)
}
cur := Iface{
Name: name,
}
cur.RxBytes, _ = strconv.ParseUint(fields[0], 10, 64) // RxBytes uint64
cur.RxPackets, _ = strconv.ParseUint(fields[1], 10, 64) // RxPackets uint64
cur.RxErrs, _ = strconv.ParseUint(fields[2], 10, 64) // RxErrs uint64
cur.TxBytes, _ = strconv.ParseUint(fields[8], 10, 64) // TxBytes uint64
cur.TxPackets, _ = strconv.ParseUint(fields[9], 10, 64) // TxPackets uint64
cur.TxErrs, _ = strconv.ParseUint(fields[10], 10, 64) // TxErrs uint64
prev, exists := ifaces[name]
if exists {
// deltas
timeDiff := openTime.Sub(prevTime).Seconds()
rxRate := float64(cur.RxBytes-prev.RxBytes) / timeDiff
txRate := float64(cur.TxBytes-prev.TxBytes) / timeDiff
fmt.Printf("%s: (%s, %s) ", name, humanBps(rxRate, *rawFlag, *bitFlag), humanBps(txRate, *rawFlag, *bitFlag))
}
ifaces[name] = cur
}
fmt.Printf("\n")
file.Close()
prevTime = openTime
time.Sleep(interval)
}
}
|