This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
/
net.go
215 lines (190 loc) · 5.21 KB
/
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
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
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package psutil
import (
"fmt"
"net"
"strconv"
"time"
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
psutilnet "github.com/shirou/gopsutil/net"
)
var netIOCounterLabels = map[string]label{
"bytes_sent": label{
unit: "",
description: "",
},
"bytes_recv": label{
unit: "",
description: "",
},
"packets_sent": label{
unit: "",
description: "",
},
"packets_recv": label{
unit: "",
description: "",
},
"errin": label{
unit: "",
description: "",
},
"errout": label{
unit: "",
description: "",
},
"dropin": label{
unit: "",
description: "",
},
"dropout": label{
unit: "",
description: "",
},
}
func netIOCounters(nss []plugin.Namespace) ([]plugin.Metric, error) {
defer timeSpent(time.Now(), "netIOCounters")
// gather accumulated metrics for all interfaces
netsAll, err := psutilnet.IOCounters(false)
if err != nil {
return nil, err
}
// gather metrics per nic
netsNic, err := psutilnet.IOCounters(true)
if err != nil {
return nil, err
}
results := []plugin.Metric{}
for _, ns := range nss {
// set requested metric name from last namespace element
metricName := ns.Element(len(ns) - 1).Value
// check if requested metric is dynamic (requesting metrics for all nics)
if ns[3].Value == "*" {
for _, nic := range netsNic {
// prepare namespace copy to update value
// this will allow to keep namespace as dynamic (name != "")
dyn := make([]plugin.NamespaceElement, len(ns))
copy(dyn, ns)
dyn[3].Value = nic.Name
// get requested metric value
val, err := getNetIOCounterValue(&nic, metricName)
if err != nil {
return nil, err
}
tags, err := getInterfaceConfiguration(nic.Name)
if err != nil {
return nil, err
}
metric := plugin.Metric{
Namespace: dyn,
Data: val,
Timestamp: time.Now(),
Tags: tags,
Unit: netIOCounterLabels[metricName].unit,
}
results = append(results, metric)
}
} else {
stats := append(netsAll, netsNic...)
// find stats for interface name or all nics
stat := findNetIOStats(stats, ns[3].Value)
if stat == nil {
return nil, fmt.Errorf("Requested interface %s not found", ns[3].Value)
}
// get value for requested metric
val, err := getNetIOCounterValue(stat, metricName)
if err != nil {
return nil, err
}
var tags map[string]string
//for "all" interface there is no configuration
if ns[3].Value != "all" {
tags, err = getInterfaceConfiguration(ns[3].Value)
if err != nil {
return nil, err
}
}
metric := plugin.Metric{
Namespace: ns,
Data: val,
Tags: tags,
Timestamp: time.Now(),
Unit: netIOCounterLabels[metricName].unit,
}
results = append(results, metric)
}
}
return results, nil
}
func findNetIOStats(nets []psutilnet.IOCountersStat, name string) *psutilnet.IOCountersStat {
for _, net := range nets {
if net.Name == name {
return &net
}
}
return nil
}
func getNetIOCounterValue(stat *psutilnet.IOCountersStat, name string) (uint64, error) {
switch name {
case "bytes_sent":
return stat.BytesSent, nil
case "bytes_recv":
return stat.BytesRecv, nil
case "packets_sent":
return stat.PacketsSent, nil
case "packets_recv":
return stat.PacketsRecv, nil
case "errin":
return stat.Errin, nil
case "errout":
return stat.Errout, nil
case "dropin":
return stat.Dropin, nil
case "dropout":
return stat.Dropout, nil
default:
return 0, fmt.Errorf("Requested NetIOCounter statistic %s is not available", name)
}
}
func getNetIOCounterMetricTypes() ([]plugin.Metric, error) {
defer timeSpent(time.Now(), "getNetIOCounterMetricTypes")
mts := make([]plugin.Metric, 0)
for name, label := range netIOCounterLabels {
//metrics which are the sum for all available nics
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "net", "all", name),
Description: label.description,
Unit: label.unit,
})
//dynamic metrics representing any nic
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "net").
AddDynamicElement("interface_name", "network interface name").AddStaticElement(name),
Description: label.description,
Unit: label.unit,
})
}
return mts, nil
}
func getInterfaceConfiguration(ifaceName string) (map[string]string, error) {
interfaceConfig, err := net.InterfaceByName(ifaceName)
if err != nil {
return nil, err
}
tags := make(map[string]string)
tags["hardware_addr"] = string(interfaceConfig.HardwareAddr)
tags["mtu"] = strconv.Itoa(interfaceConfig.MTU)
return tags, nil
}