-
Notifications
You must be signed in to change notification settings - Fork 5
/
device.go
174 lines (138 loc) · 3.75 KB
/
device.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
package device
import (
"log"
"net"
"sync"
"time"
"github.com/PreetamJinka/sflow"
"github.com/PreetamJinka/snmp"
"github.com/PreetamJinka/cistern/state/flows"
"github.com/PreetamJinka/cistern/state/metrics"
"github.com/PreetamJinka/cistern/state/series"
)
type deviceType int
const (
TypeUnknown deviceType = 0
TypeNetwork deviceType = 1 << (iota - 1)
TypeLinux
TypeBSD
)
var (
descOid = snmp.MustParseOID(".1.3.6.1.2.1.1.1.0")
hostnameOid = snmp.MustParseOID(".1.3.6.1.2.1.1.5.0")
)
// A Device is an entity that sends flows or
// makes information available via SNMP.
type Device struct {
hostname string
desc string
ip net.IP
deviceType deviceType
snmpSession *snmp.Session
metricRegistry *metrics.MetricRegistry
topTalkers *flows.TopTalkers
Inbound chan sflow.Datagram
outbound chan series.Observation
}
// NewDevice returns a new Device with the given IP.
func NewDevice(address net.IP, outboundObservations chan series.Observation) *Device {
dev := &Device{
ip: address,
metricRegistry: metrics.NewMetricRegistry(),
Inbound: make(chan sflow.Datagram),
outbound: outboundObservations,
}
go dev.handleFlows()
return dev
}
func (d *Device) Discover() {
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
// Discover hostname
getRes, err := d.snmpSession.Get(hostnameOid)
if err != nil {
log.Printf("[SNMP %v] Could not get hostname: %v", d.ip, err)
return
}
if vbinds := getRes.Varbinds(); len(vbinds) > 0 {
hostnameStr, err := vbinds[0].GetStringValue()
if err != nil {
log.Printf("[SNMP %v] Invalid GetResponse for hostname: %v", d.ip, err)
return
}
d.hostname = hostnameStr
log.Printf("[SNMP %v] Discovered hostname %s", d.ip, hostnameStr)
}
}()
go func() {
defer wg.Done()
// Discover description
getRes, err := d.snmpSession.Get(descOid)
if err != nil {
log.Printf("[SNMP %v] Could not get description: %v", d.ip, err)
return
}
if vbinds := getRes.Varbinds(); len(vbinds) > 0 {
descStr, err := vbinds[0].GetStringValue()
if err != nil {
log.Printf("[SNMP %v] Invalid GetResponse for description: %v", d.ip, err)
return
}
d.desc = descStr
log.Printf("[SNMP %v] Discovered desc %s", d.ip, descStr)
}
}()
wg.Wait()
}
func (d *Device) Metrics() []metrics.MetricDefinition {
return d.metricRegistry.Metrics()
}
func (d *Device) IP() net.IP {
return d.ip
}
func (d *Device) Hostname() string {
return d.hostname
}
func (d *Device) TopTalkers() *flows.TopTalkers {
return d.topTalkers
}
func (d *Device) handleFlows() {
log.Printf("[Device %v] Handling flows", d.ip)
for dgram := range d.Inbound {
for _, sample := range dgram.Samples {
for _, record := range sample.GetRecords() {
d.processFlowRecord(record)
}
}
}
}
func (d *Device) processFlowRecord(r sflow.Record) {
switch r.(type) {
case sflow.HostCpuCounters:
d.processHostCPUCounters(r.(sflow.HostCpuCounters))
case sflow.HostMemoryCounters:
d.processHostMemoryCounters(r.(sflow.HostMemoryCounters))
case sflow.HostDiskCounters:
d.processHostDiskCounters(r.(sflow.HostDiskCounters))
case sflow.HostNetCounters:
d.processHostNetCounters(r.(sflow.HostNetCounters))
case sflow.GenericInterfaceCounters:
d.processGenericInterfaceCounters(r.(sflow.GenericInterfaceCounters))
case sflow.RawPacketFlow:
if d.topTalkers == nil {
d.topTalkers = flows.NewTopTalkers(time.Second * 30)
}
d.processRawPacketFlow(r.(sflow.RawPacketFlow))
}
}
func (d *Device) updateAndEmit(metric string, metricType metrics.MetricType, v interface{}) {
value := d.metricRegistry.Update(metric, metricType, v)
d.outbound <- series.Observation{
Source: d.ip.String(),
Metric: metric,
Timestamp: time.Now().Unix(),
Value: float64(value),
}
}