-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathi_net.go
101 lines (90 loc) · 2.3 KB
/
i_net.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
package main
import (
"regexp"
"fmt"
"os/exec"
"strings"
"strconv"
"time"
"log"
)
type NetIOStats struct {
Interfaces []string
}
func (_ *NetIOStats) Description() string {
return "Read metrics about network interface usage"
}
var netSampleConfig = `
## By default, telegraf gathers stats from any up interface (excluding loopback)
## Setting interfaces will tell it to gather these explicit interfaces,
## regardless of status.
##
# interfaces = ["eth0"]
`
func (_ *NetIOStats) SampleConfig() string {
return netSampleConfig
}
func (s *NetIOStats) Gather(acc Accumulator) error {
interfaces := map[string]string{}
if len(s.Interfaces) > 0 {
for _, value := range s.Interfaces {
interfaces[value] = ""
}
} else {
c1, err := exec.Command("ifconfig", "-a").CombinedOutput()
if err != nil {
return fmt.Errorf("error getting NetIOStat info: %s", err.Error())
}
rows := strings.Split(string(c1), "\n")
rows = rows[0:len(rows)-1]
for _, row := range rows {
idx := strings.Index(row, ": ")
if idx > 0 {
interfaces[row[0:idx]] = ""
}
}
}
for inet, _ := range interfaces {
output, err := exec.Command("kstat", "-p", fmt.Sprintf("::%s", inet)).CombinedOutput()
if err != nil {
log.Printf("D! Error getting NetIO (kstat) info: %s\n", err.Error())
continue
}
s := string(output)
if s != "" {
fields := map[string]interface{}{}
tags := map[string]string{
"interface": inet,
}
stats := strings.Split(s, "\n")
stats = stats[0: len(stats)-1]
for _, row := range stats {
data := strings.Fields(row)
reg := regexp.MustCompile(".*:.*:.*:")
field := reg.ReplaceAllString(data[0], "${1}")
switch field {
case "obytes":
fields["bytes_sent"], _ = strconv.ParseInt(data[1], 10, 0)
break
case "rbytes":
fields["bytes_recv"], _ = strconv.ParseInt(data[1], 10, 0)
break
case "opackets":
fields["packets_sent"], _ = strconv.ParseInt(data[1], 10, 0)
break
case "ipackets":
fields["packets_recv"], _ = strconv.ParseInt(data[1], 10, 0)
break
case "ierrors":
fields["err_in"], _ = strconv.ParseInt(data[1], 10, 0)
break
case "oerrors":
fields["err_out"], _ = strconv.ParseInt(data[1], 10, 0)
break
}
}
acc.AddGauge("net", fields, tags, time.Now())
}
}
return nil
}