Repository navigation
Expand file tree
/
Copy pathplugin.go
More file actions
162 lines (144 loc) · 4.46 KB
/
Copy pathplugin.go
File metadata and controls
162 lines (144 loc) · 4.46 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"os"
"regexp"
mp "github.com/mackerelio/go-mackerel-plugin"
"github.com/monitoring-forge/saferio"
"github.com/prometheus/procfs"
)
var tooOldDuration = 600.0 // seconds
type LinuxNetDevPlugin struct {
ignoreInterfaces string
ignoreInterfacesRegexp *regexp.Regexp
workDir string
}
func (u LinuxNetDevPlugin) GraphDefinition() map[string]mp.Graphs {
graphdef := func(label string, metrics ...mp.Metrics) mp.Graphs {
return mp.Graphs{
Label: label,
Unit: mp.UnitInteger,
Metrics: metrics,
}
}
metricsdef := func(name, label string) mp.Metrics {
return mp.Metrics{
Name: name,
Label: label,
Stacked: false,
}
}
return map[string]mp.Graphs{
"linux-netdev.errors.#": graphdef("Linux NetDev errors per sec",
metricsdef("tx", "transmit errors encountered"),
metricsdef("rx", "receive errors encountered"),
),
"linux-netdev.dropped.#": graphdef("Linux NetDev dropped packets per sec",
metricsdef("tx", "packets dropped while transmitting"),
metricsdef("rx", "packets dropped while receiving"),
),
"linux-netdev.pps.#": graphdef("Linux NetDev packets per sec",
metricsdef("tx", "packets transmitted"),
metricsdef("rx", "packets received"),
),
}
}
func (u LinuxNetDevPlugin) interfaceMetrics() (map[string]procfs.NetDevLine, error) {
return u.interfaceMetricsFS("/proc")
}
func (u LinuxNetDevPlugin) interfaceMetricsFS(mountPoint string) (map[string]procfs.NetDevLine, error) {
res := map[string]procfs.NetDevLine{}
pfs, err := procfs.NewFS(mountPoint)
if err != nil {
return res, err
}
netdev, err := pfs.NetDev()
if err != nil {
return res, err
}
cur := map[string]procfs.NetDevLine{}
for _, i := range netdev {
if i.Name == "lo" {
continue
}
if u.ignoreInterfaces != "" && u.ignoreInterfacesRegexp.MatchString(i.Name) {
continue
}
cur[i.Name] = i
}
return cur, nil
}
func calcdiff(cur, prev uint64) float64 {
if cur < prev {
return 0
}
return float64(cur - prev)
}
func (u LinuxNetDevPlugin) calcMetrics(timeDiff float64, curMetrics, prevMetrics map[string]procfs.NetDevLine) map[string]float64 {
res := map[string]float64{}
allTxErrors := 0.0
allTxDropped := 0.0
allRxErrors := 0.0
allRxDropped := 0.0
for k, c := range curMetrics {
p, ok := prevMetrics[k]
if !ok {
continue
}
// tx_errors
txErrors := calcdiff(c.TxErrors, p.TxErrors)
res[fmt.Sprintf("linux-netdev.errors.%s.tx", c.Name)] = txErrors / timeDiff
allTxErrors += txErrors
// tx_dropped
txDropped := calcdiff(c.TxDropped, p.TxDropped)
res[fmt.Sprintf("linux-netdev.dropped.%s.tx", c.Name)] = txDropped / timeDiff
allTxDropped += txDropped
// rx_errors
rxErrors := calcdiff(c.RxErrors, p.RxErrors)
res[fmt.Sprintf("linux-netdev.errors.%s.rx", c.Name)] = rxErrors / timeDiff
allRxErrors += rxErrors
// rx_dropped
rxDropped := calcdiff(c.RxDropped, p.RxDropped)
res[fmt.Sprintf("linux-netdev.dropped.%s.rx", c.Name)] = rxDropped / timeDiff
allRxDropped += rxDropped
// tx_packets
txPackets := calcdiff(c.TxPackets, p.TxPackets)
res[fmt.Sprintf("linux-netdev.pps.%s.tx", c.Name)] = txPackets / timeDiff
// tx_packets
rxPackets := calcdiff(c.RxPackets, p.RxPackets)
res[fmt.Sprintf("linux-netdev.pps.%s.rx", c.Name)] = rxPackets / timeDiff
}
res["linux-netdev.errors.all.tx"] = allTxErrors / timeDiff
res["linux-netdev.dropped.all.tx"] = allTxDropped / timeDiff
res["linux-netdev.errors.all.rx"] = allRxErrors / timeDiff
res["linux-netdev.dropped.all.rx"] = allRxDropped / timeDiff
return res
}
func (u LinuxNetDevPlugin) FetchMetrics() (map[string]float64, error) {
curMetrics, err := u.interfaceMetrics()
if err != nil {
return map[string]float64{}, err
}
path := statFile()
if !saferio.FileExists(u.workDir, path) {
if err := writeStats(u.workDir, path, curMetrics); err != nil {
return map[string]float64{}, fmt.Errorf("failed to write initial stats: %w", err)
}
fmt.Fprintf(os.Stderr, "Notice: first time execution command\n")
return map[string]float64{}, nil
}
defer func() {
if err := writeStats(u.workDir, path, curMetrics); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}()
timeDiff, prevMetrics, err := readStats(u.workDir, path)
if err != nil {
return map[string]float64{}, fmt.Errorf("failed to read previous stats: %w", err)
}
if prevMetrics == nil {
return map[string]float64{}, fmt.Errorf("previous stats contain no interfaces")
}
res := u.calcMetrics(timeDiff, curMetrics, prevMetrics)
return res, nil
}