/
flowstats.go
96 lines (81 loc) · 2.3 KB
/
flowstats.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
// Copyright 2017 DigitalOcean.
//
// 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 ovs
import (
"errors"
"strconv"
"strings"
)
var (
// ErrInvalidFlowStats is returned when flow statistics from 'ovs-ofctl
// dump-aggregate' do not match the expected output format.
ErrInvalidFlowStats = errors.New("invalid flow statistics")
)
// FlowStats contains a variety of statistics about an Open vSwitch port,
// including its port ID and numbers about packet receive and transmit
// operations.
type FlowStats struct {
PacketCount uint64
ByteCount uint64
}
// UnmarshalText unmarshals a FlowStats from textual form.
func (f *FlowStats) UnmarshalText(b []byte) error {
// Make a copy per documentation for encoding.TextUnmarshaler.
s := string(b)
// Constants only needed within this method, to avoid polluting the
// package namespace with generic names
const (
packetCount = "packet_count"
byteCount = "byte_count"
flowCount = "flow_count"
)
// Find the index of packet count to find stats.
idx := strings.Index(s, packetCount)
if idx == -1 {
return ErrInvalidFlowStats
}
// Assume the last three fields are packets, bytes, and flows, in that order.
ss := strings.Fields(s[idx:])
fields := []string{
packetCount,
byteCount,
flowCount,
}
if len(ss) != len(fields) {
return ErrInvalidFlowStats
}
var values []uint64
for i := range ss {
// Split key from its integer value.
kv := strings.Split(ss[i], "=")
if len(kv) != 2 {
return ErrInvalidFlowStats
}
// Verify keys appear in expected order.
if kv[0] != fields[i] {
return ErrInvalidFlowStats
}
n, err := strconv.ParseUint(kv[1], 10, 64)
if err != nil {
return err
}
values = append(values, n)
}
*f = FlowStats{
PacketCount: values[0],
ByteCount: values[1],
// Flow count unused.
}
return nil
}