-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_netstat.go
123 lines (109 loc) · 3.07 KB
/
monitor_netstat.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
package someta
import (
"context"
"encoding/json"
"fmt"
"log"
"sort"
"strings"
"time"
"github.com/shirou/gopsutil/net"
)
func init() {
registerMonitor("netstat", NewNetstatMonitor)
}
// NetstatMetadata encapsulates what it says
type NetstatMetadata struct {
Timestamp time.Time `json:"timestamp"`
Netstat map[string]net.IOCountersStat `json:"netstat"`
}
// NewNetstatMonitor creates and returns a new NetstatMonitor
func NewNetstatMonitor() MetadataGenerator {
return new(NetstatMonitor)
}
// NetstatMonitor collects network interface counters metadata
type NetstatMonitor struct {
Monitor
Data []NetstatMetadata `json:"data"`
ifacesMonitored []string
}
// DefaultConfig returns a default config or nil if no default
func (n *NetstatMonitor) DefaultConfig() *MonitorConf {
var ifnames []string
ifstatslice, _ := net.Interfaces()
for _, ifstat := range ifstatslice {
ifnames = append(ifnames, ifstat.Name)
}
conf := &MonitorConf{Kind: "netstat",
Interval: 1 * time.Second,
Device: ifnames,
}
return conf
}
// CheckConfig does some basic sanity checking on the configuration
func (n *NetstatMonitor) CheckConfig(name string, conf MonitorConf) {
if conf.Interval < time.Second*1 {
log.Fatalf("%s: interval %v too short", name, conf.Interval)
}
for _, devname := range conf.Device {
if !intfNames.isValid(devname) {
log.Fatalf("%s monitor: device %s doesn't exist; valid devices: %s", name, devname, strings.Join(intfNames.all(), ","))
}
}
}
// Init initializes a NetstatMonitor
func (n *NetstatMonitor) Init(name string, verbose bool, defaultInterval time.Duration, config MonitorConf) error {
n.CheckConfig(name, config)
n.baseInit(name, verbose, defaultInterval)
// no device names specified; monitor all devices
if len(config.Device) == 0 {
config.Device = intfNames.all()
}
n.ifacesMonitored = config.Device
if n.verbose {
plural := ""
if len(n.ifacesMonitored) > 1 {
plural = "s"
}
log.Printf("%s monitor: monitoring device%s %s\n", name, plural, strings.Join(n.ifacesMonitored, ","))
}
return nil
}
// Run runs the netstat monitor; this should be invoked in a goroutine
func (n *NetstatMonitor) Run(ctx context.Context) error {
ticker := time.NewTicker(n.interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
if n.verbose {
fmt.Printf("%s stopping\n", n.Name)
}
return nil
case t := <-ticker.C:
netstats, err := net.IOCounters(true)
countermap := make(map[string]net.IOCountersStat)
for _, ioc := range netstats {
idx := sort.SearchStrings(n.ifacesMonitored, ioc.Name)
if idx < len(n.ifacesMonitored) && n.ifacesMonitored[idx] == ioc.Name {
countermap[ioc.Name] = ioc
}
}
if err != nil {
log.Printf("%s: %v\n", n.Name, err)
} else {
n.mutex.Lock()
n.Data = append(n.Data, NetstatMetadata{t, countermap})
n.mutex.Unlock()
}
}
}
}
// Flush will write any current metadata to the writer
func (n *NetstatMonitor) Flush(encoder *json.Encoder) error {
n.mutex.Lock()
defer n.mutex.Unlock()
err := encoder.Encode(n)
n.Data = nil
return err
}