forked from hungnt1/netns-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
410 lines (328 loc) · 11 KB
/
collector.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package main
import (
"io/ioutil"
"net"
"os"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netns"
)
const (
NetnsPath = "/run/netns/"
InterfaceStatPath = "/sys/devices/virtual/net/"
ProcStatPath = "/proc/"
collectorNamespace = "netns"
collectorSubsystem = "network"
netnsLabel = "netns"
deviceLabel = "device"
router = "router"
hostname = "hostname"
deviceIP = "deviceIP"
)
type Collector struct {
logger logrus.FieldLogger
config *NetnsExporterConfig
netnsStatus *prometheus.Desc
intfStatus *prometheus.Desc
intfMetrics map[string]*prometheus.Desc
procMetrics map[string]*PrometheusProcMetric
}
type PrometheusProcMetric struct {
Config ProcMetric
Desc *prometheus.Desc
}
func NewCollector(config *NetnsExporterConfig, logger *logrus.Logger) *Collector {
// Add descriptions for netns count
netnsStatus := prometheus.NewDesc(
prometheus.BuildFQName(collectorNamespace, collectorSubsystem, "namespace"),
"Value is allways 1 for network namespace found",
[]string{netnsLabel, hostname},
nil,
)
// Add descriptions for interface adminStatus metric
intfStatus := prometheus.NewDesc(
prometheus.BuildFQName(collectorNamespace, collectorSubsystem, "up"),
"Value is 1 if operstate is 'up', 0 otherwise.",
[]string{netnsLabel, deviceLabel, router, hostname, deviceIP},
nil,
)
// Add descriptions for interface metrics
intfMetrics := make(map[string]*prometheus.Desc, len(config.InterfaceMetrics))
for _, metric := range config.InterfaceMetrics {
intfMetrics[metric] = prometheus.NewDesc(
prometheus.BuildFQName(collectorNamespace, collectorSubsystem, metric+"_total"),
"Interface statistics in the network namespace",
[]string{netnsLabel, deviceLabel, router, hostname, deviceIP},
nil,
)
}
// Add descriptions for proc metrics
procMetrics := make(map[string]*PrometheusProcMetric, len(config.InterfaceMetrics))
for metricName, metric := range config.ProcMetrics {
procMetrics[metricName] = &PrometheusProcMetric{
Config: metric,
Desc: prometheus.NewDesc(
prometheus.BuildFQName(collectorNamespace, collectorSubsystem, metricName+"_total"),
"Statistics from /proc filesystem in the network namespace",
[]string{netnsLabel},
nil,
),
}
}
return &Collector{
logger: logger.WithField("component", "collector"),
config: config,
netnsStatus: netnsStatus,
intfStatus: intfStatus,
intfMetrics: intfMetrics,
procMetrics: procMetrics,
}
}
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.netnsStatus
ch <- c.intfStatus
for _, desc := range c.intfMetrics {
ch <- desc
}
for _, metric := range c.procMetrics {
ch <- metric.Desc
}
}
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
// Limit the number of concurrent goroutines, because each will block an entire
// operating system thread. Maximum number of gorutines == number of CPU cores.
wg := NewLimitedWaitGroup(c.config.Threads)
startTime := time.Now()
// Get namespases files
nsFiles, err := ioutil.ReadDir(NetnsPath)
if err != nil {
c.logger.Errorf("Reading list of network nemaspaces failed: %s", err)
return
}
// Filter namespaces by regexp if namespace-filters declared in config
if (c.config.NamespacesFilter.BlacklistPattern != "") ||
(c.config.NamespacesFilter.WhitelistPattern != "") {
nsFiles = c.filterNsFiles(nsFiles)
}
c.logger.Debugf("Found %d namespaces", len(nsFiles))
c.logger.Debugf("Only %d parallel goroutines will be run", runtime.NumCPU())
// Get metrics from all of namespaces
for _, ns := range nsFiles {
wg.Add(1)
go c.getMetricsFromNamespace(ns.Name(), wg, ch)
}
wg.Wait()
c.logger.Debugf("collecting took %s for %d namespaces", time.Since(startTime), len(nsFiles))
}
func (c *Collector) getMetricsFromNamespace(namespace string, wg *LimitedWaitGroup, ch chan<- prometheus.Metric) {
defer wg.Done()
c.logger.Debugf("Start getting statistics for namespace %s", namespace)
// Lock the OS Thread so we don't accidentally switch namespaces
runtime.LockOSThread()
defer runtime.UnlockOSThread()
startTime := time.Now()
// Save current namespace
curNs, err := netns.Get()
if err != nil {
c.logger.Errorf("Get current namespace %s failed: %s", namespace, err)
return
}
defer curNs.Close()
defer netns.Set(curNs) //nolint:errcheck
// Switch namespace
ns, err := netns.GetFromName(namespace)
if err != nil {
c.logger.Errorf("Get net namespace by name %s failed: %s", namespace, err)
return
}
if err := netns.Set(ns); err != nil {
c.logger.Errorf("Change net namespace to %s failed: %s", namespace, err)
return
}
defer ns.Close()
// Say to the kernel that we will use separate context
if err := syscall.Unshare(syscall.CLONE_NEWNS); err != nil { //nolint:typecheck
c.logger.Errorf("Syscall unshare failed in namespace %s: %s", namespace, err)
return
}
ch <- prometheus.MustNewConstMetric(c.netnsStatus, prometheus.CounterValue, 1, namespace, c.getHostname())
// Don't let any mounts propagate back to the parent
// See: https://github.com/shemminger/iproute2/blob/6754e1d9783458550dce8d309efb4091ec8089a5/lib/namespace.c#L77
// and: https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt
if err := syscall.Mount("", "/", "none", syscall.MS_SLAVE|syscall.MS_REC, ""); err != nil { //nolint:typecheck
c.logger.Errorf("Mount root with rslave option failed in namepsace %s: %s", namespace, err)
return
}
// Mount sysfs from net nemaspace
if err := syscall.Mount(namespace, "/sys", "sysfs", 0, "ro"); err != nil { //nolint:typecheck
c.logger.Errorf("Mount /sys from the namespace failed in namespace: %s", namespace, err)
return
}
defer syscall.Unmount("/sys", syscall.MNT_DETACH) //nolint:errcheck,typecheck
// Parse interfaces statistics
ifFiles, err := ioutil.ReadDir(InterfaceStatPath)
if err != nil {
c.logger.Errorf("Reading sysfs directory for interface %s in namespace %s failed: %s", InterfaceStatPath, namespace, err)
return
}
// Filter device name by regexp if device-filters declared in config
if (c.config.DeviceFilter.BlacklistPattern != "") ||
(c.config.DeviceFilter.WhitelistPattern != "") {
ifFiles = c.filteriFFiles(ifFiles)
}
for _, ifFile := range ifFiles {
// We don't need to get stat for lo interface
if ifFile.Name() == "lo" {
continue
}
c.logger.Debugf("Start getting statistics for interface %s in namespace %s", ifFile.Name(), namespace)
// get ip address of device in namespace
device_addr, err := c.getIPfromNS(namespace, ifFile.Name())
if err != nil {
c.logger.Errorf("Failed to get IP of device: %s", ifFile.Name)
}
// parse routerID from namespace
routerID := strings.Replace(namespace, "qrouter-", "", -1)
// get current hostname
hostname := c.getHostname()
value, _ := c.getDeviceStatusMetricfromNS(namespace, ifFile.Name())
ch <- prometheus.MustNewConstMetric(c.intfStatus, prometheus.CounterValue, value, namespace, ifFile.Name(), routerID, hostname, device_addr)
for metricName, desc := range c.intfMetrics {
value := c.getMetricFromFile(namespace, InterfaceStatPath+ifFile.Name()+"/statistics/"+metricName)
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, value, namespace, ifFile.Name(), routerID, hostname, device_addr)
}
}
// Parse of /proc statistics
for _, metric := range c.procMetrics {
value := c.getMetricFromFile(namespace, ProcStatPath+metric.Config.FileName)
ch <- prometheus.MustNewConstMetric(metric.Desc, prometheus.CounterValue, value, namespace)
}
c.logger.Debugf("processing namespace %s took %s", namespace, time.Since(startTime))
}
func (c *Collector) getMetricFromFile(namespace, file string) float64 {
data, err := ioutil.ReadFile(file)
if err != nil {
c.logger.Errorf("Error while reading statistic file %s in namespace %s: %s", file, namespace, err)
return -1
}
stat, err := strconv.ParseFloat(strings.TrimSpace(string(data)), 64)
if err != nil {
c.logger.Printf("Error while parsing data from file %s in namespace %s: %s", file, namespace, err)
return -1
}
return stat
}
func (c *Collector) filterNsFiles(nsFiles []os.FileInfo) []os.FileInfo {
blacklistRegexp := c.config.NamespacesFilter.BlacklistRegexp
whitelistRegexp := c.config.NamespacesFilter.WhitelistRegexp
if blacklistRegexp.String() != "" {
tmp := make([]os.FileInfo, 0)
for _, ns := range nsFiles {
if !blacklistRegexp.MatchString(ns.Name()) {
tmp = append(tmp, ns)
}
}
nsFiles = tmp
}
if whitelistRegexp.String() != "" {
tmp := make([]os.FileInfo, 0)
for _, ns := range nsFiles {
if whitelistRegexp.MatchString(ns.Name()) {
tmp = append(tmp, ns)
}
}
nsFiles = tmp
}
return nsFiles
}
func (c *Collector) filteriFFiles(ifFiles []os.FileInfo) []os.FileInfo {
blacklistRegexp := c.config.DeviceFilter.BlacklistRegexp
whitelistRegexp := c.config.DeviceFilter.WhitelistRegexp
if blacklistRegexp.String() != "" {
tmp := make([]os.FileInfo, 0)
for _, ifD := range ifFiles {
if !blacklistRegexp.MatchString(ifD.Name()) {
tmp = append(tmp, ifD)
}
}
ifFiles = tmp
}
if whitelistRegexp.String() != "" {
tmp := make([]os.FileInfo, 0)
for _, ifD := range ifFiles {
if whitelistRegexp.MatchString(ifD.Name()) {
tmp = append(tmp, ifD)
}
}
ifFiles = tmp
}
return ifFiles
}
func (c *Collector) getHostname() string {
hostname, err := os.Hostname()
if err != nil {
c.logger.Debugf("Fail to get current hostname")
return ""
}
return hostname
}
func (c *Collector) getIPfromNS(namespace, device string) (IP string, err error) {
ns, err := netns.GetFromName(namespace)
if err != nil {
c.logger.Errorf("Failed to open namespace:", err)
return "nil", err
}
defer ns.Close()
netns.Set(ns)
defer netns.Set(netns.None())
// Get IP from specific ip adress
iface, err := net.InterfaceByName(device)
if err != nil {
c.logger.Errorf("Failed to retrieve interfaces:", err)
return "nil", err
}
addrs, err := iface.Addrs()
if err != nil {
c.logger.Errorf("Failed to retrieve addresses for interface", iface.Name, ":", err)
return "nil", err
}
if len(addrs) > 0 {
ip, _, err := net.ParseCIDR(addrs[0].String())
if err != nil {
c.logger.Errorf("Failed to parse IP address:", err)
return "nil", err
}
c.logger.Debugf("IP address %s %s in namespace %s", iface.Name, ip, namespace)
return ip.String(), nil
}
c.logger.Debugf("Interface %s no IP", iface.Name)
return "", nil
}
func (c *Collector) getDeviceStatusMetricfromNS(namespace, device string) (adminStatus float64, err error) {
ns, err := netns.GetFromName(namespace)
if err != nil {
c.logger.Errorf("Failed to open namespace:", err)
return 2, err
}
defer ns.Close()
netns.Set(ns)
defer netns.Set(netns.None())
iface, err := net.InterfaceByName(device)
if err != nil {
c.logger.Errorf("Failed to retrieve interfaces:", err)
return 2, err
}
// Get adminStatus
if iface.Flags&net.FlagUp != 0 {
// "Up" -> 1
return 1, nil
} else {
// "Down" -> 0
return 0, nil
}
}