forked from leannetworking/ovs-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
284 lines (252 loc) · 7.94 KB
/
handlers.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
package main
import (
"github.com/biwwy0/ovs-exporter/ovs"
"github.com/prometheus/client_golang/prometheus"
)
type OvsPromCollector struct {
ip string
port int
ovsReader ovs.OvsStatReader
}
var (
flowPacketsDesc = prometheus.NewDesc(
"flowPackets",
"The number of packets matched for the given OpenFlow entry.",
[]string{"match", "action", "table", "priority"},
nil)
flowBytesDesc = prometheus.NewDesc(
"flowBytes",
"The number of bytes matched for the given OpenFlow entry",
[]string{"match", "action", "table", "priority"},
nil)
flowAgeDesc = prometheus.NewDesc(
"flowAge",
"The number of seconds have passed since the given OpenFlow entry was created",
[]string{"match", "action", "table", "priority"},
nil)
flowIdleTimeDesc = prometheus.NewDesc(
"flowIdleTime",
"The number of seconds have passed since the last packet has seen for the given OpenFlow entry",
[]string{"match", "action", "table", "priority"},
nil)
portRxPacketsDesc = prometheus.NewDesc(
"portRxPackets",
"The number of packet that was recieved by a given port",
[]string{"port"},
nil)
portTxPackets = prometheus.NewDesc(
"portTxPackets",
"The number of packet that was sent by a given port",
[]string{"port"},
nil)
portRxBytesDesc = prometheus.NewDesc(
"portRxBytes",
"The number of bytes that was recieved by a given port",
[]string{"port"},
nil)
portTxBytes = prometheus.NewDesc(
"portTxBytes",
"The number of bytes that was sent by a given port",
[]string{"port"},
nil)
portRxDropsDesc = prometheus.NewDesc(
"portRxDrops",
"The number of packets that was dropped on receive side by a given port",
[]string{"port"},
nil)
portTxDropsDesc = prometheus.NewDesc(
"portTxDrops",
"The number of packets that was dropped on sending side by a given port",
[]string{"port"},
nil)
// groupPacketsDesc = prometheus.NewDesc(
// "groupPackets",
// "The number of packet that was sent by a given group",
// []string{"groupId", "groupType"},
// nil)
//
// groupBytesDesc = prometheus.NewDesc(
// "groupBytes",
// "The number of bytes that was sent by a given group",
// []string{"groupId", "groupType"},
// nil)
//
// groupDurationDesc = prometheus.NewDesc(
// "groupDuration",
// "The number of seconds passed since the group entry was added",
// []string{"groupId", "groupType"},
// nil)
//
// groupBucketPacketsDesc = prometheus.NewDesc(
// "groupBucketPackets",
// "The number of packet that was sent by a given group bucket",
// []string{"groupId", "groupType", "bucketActions"},
// nil)
//
// groupBucketBytesDesc = prometheus.NewDesc(
// "groupBucketBytes",
// "The number of bytes that was sent by a given group bucket",
// []string{"groupId", "groupType", "bucketActions"},
// nil)
//
urlParsingErrorDesc = prometheus.NewDesc(
"ovs_error",
"Error scraping target. Correct format is: http://<IP>:<Port>/metrics?target=<targetIP>",
nil, nil)
)
// Describe implements Prometheus.Collector.
func (c OvsPromCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- prometheus.NewDesc("dummy", "dummy", nil, nil)
}
// Collect implements Prometheus.Collector.
func (c OvsPromCollector) Collect(ch chan<- prometheus.Metric) {
if c.ip == "" {
ch <- prometheus.NewInvalidMetric(urlParsingErrorDesc, nil)
return
}
//Creating Prometheus compatible output for:
// - number of packets as "flowPackets" type Counter
// - number of bytes as "flowBytes" type Counter
// - age of the flow as "flowAge" type Gauge
// - idle time as "flowIdleTime" type Gauge
flowEntries, err := c.ovsReader.Flows(c.ip, c.port)
if err != nil {
ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("ovs_error", "Error parsing flow dump", nil, nil), err)
return
}
for _, entry := range flowEntries {
ch <- prometheus.MustNewConstMetric(
flowPacketsDesc,
prometheus.CounterValue,
float64(entry.Packets),
entry.Match,
entry.Action,
entry.Table,
entry.Priority)
ch <- prometheus.MustNewConstMetric(
flowBytesDesc,
prometheus.CounterValue,
float64(entry.Bytes),
entry.Match,
entry.Action,
entry.Table,
entry.Priority)
ch <- prometheus.MustNewConstMetric(
flowAgeDesc,
prometheus.GaugeValue,
float64(entry.Duration),
entry.Match,
entry.Action,
entry.Table,
entry.Priority)
ch <- prometheus.MustNewConstMetric(
flowIdleTimeDesc,
prometheus.GaugeValue,
float64(entry.IdleAge),
entry.Match,
entry.Action,
entry.Table,
entry.Priority)
}
portEntries, err := c.ovsReader.Ports(c.ip, c.port)
//if error was occured we return
if err != nil {
ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("ovs_error", "Error parsing port dump", nil, nil), err)
return
}
//if command was succesfull we further parse the output
//Creating Prometheus compatible output for every stat with PortNumber identifyer:
// - number of packets recieved by the given OpenFlow port as "portRxPackets" type Counter
// - number of packets sent by the given OpenFlow port as "portTxPackets" type Counter
// - number of bytes recieved by the given OpenFlow port as "portRxBytes" type Counter
// - number of bytes sent by the given OpenFlow port as "portTxBytes" type Counter
// - number of packet drops in recieve side by the given OpenFlow port as "portRxDrops" type Counter
// - number of packet drops in sending side by the given OpenFlow port as "portTxDrops" type Counter
for _, entry := range portEntries {
ch <- prometheus.MustNewConstMetric(
portRxPacketsDesc,
prometheus.CounterValue,
float64(entry.RxPackets),
entry.PortNumber)
ch <- prometheus.MustNewConstMetric(
portTxPackets,
prometheus.CounterValue,
float64(entry.TxPackets),
entry.PortNumber)
ch <- prometheus.MustNewConstMetric(
portRxBytesDesc,
prometheus.CounterValue,
float64(entry.RxBytes),
entry.PortNumber)
ch <- prometheus.MustNewConstMetric(
portTxBytes,
prometheus.CounterValue,
float64(entry.TxBytes),
entry.PortNumber)
ch <- prometheus.MustNewConstMetric(
portRxDropsDesc,
prometheus.CounterValue,
float64(entry.RxDrops),
entry.PortNumber)
ch <- prometheus.MustNewConstMetric(
portTxDropsDesc,
prometheus.CounterValue,
float64(entry.TxDrops),
entry.PortNumber)
}
// groupEntries, err := c.ovsReader.Groups(c.ip, c.port)
// //if error was occured we return
// if err != nil {
// ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("ovs_error", "Error parsing group dump", nil, nil), err)
// return
// }
//
// //Creating Prometheus compatible output for every group stat with groupId label:
// // - number of packets that was forwarded by a group rule as "groupPackets" type Counter
// // - number of bytes that was forwarded by a group rule as "groupBytes" type Counter
// // - number of second that passed since a group rule was added as "groupPackets" type Gauge
// // - number of packets that was forwarded by a bucket in a group rule as "groupBucketPackets" type Counter
// // - number of bytes that was forwarded by a bucket in a group rule as "groupBucketBytes" type Counter
//
// for _, entry := range groupEntries {
//
// ch <- prometheus.MustNewConstMetric(
// groupPacketsDesc,
// prometheus.CounterValue,
// float64(entry.Packets),
// entry.GroupId,
// entry.GroupType)
//
// ch <- prometheus.MustNewConstMetric(
// groupBytesDesc,
// prometheus.CounterValue,
// float64(entry.Bytes),
// entry.GroupId,
// entry.GroupType)
//
// ch <- prometheus.MustNewConstMetric(
// groupDurationDesc,
// prometheus.CounterValue,
// float64(entry.Duration),
// entry.GroupId,
// entry.GroupType)
//
// for _, bucket := range entry.Buckets {
// ch <- prometheus.MustNewConstMetric(
// groupBucketPacketsDesc,
// prometheus.CounterValue,
// float64(bucket.Packets),
// entry.GroupId,
// entry.GroupType,
// bucket.Actions)
//
// ch <- prometheus.MustNewConstMetric(
// groupBucketBytesDesc,
// prometheus.CounterValue,
// float64(bucket.Bytes),
// entry.GroupId,
// entry.GroupType,
// bucket.Actions)
// }
// }
}