forked from influxdata/telegraf
/
ops_ctlr.go
199 lines (168 loc) · 5.11 KB
/
ops_ctlr.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
package ops_ctlr
import (
"encoding/json"
"fmt"
"log"
"strings"
"time"
"golang.org/x/crypto/ssh"
db "github.com/dancripe/libovsdb"
"github.com/mchuang3/telegraf"
"github.com/mchuang3/telegraf/plugins/inputs"
)
const measurement = "port_stats"
type OpsCtlrStats struct {
connected bool
client *ssh.Client
MgmtAddress string `toml:"mgmt_address"`
Username string `toml:"username"`
Password string `toml:"password"`
}
func connectDB(s *OpsCtlrStats) bool {
var config = &ssh.ClientConfig{
User: s.Username,
Auth: []ssh.AuthMethod{
ssh.Password(s.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
conn, err := ssh.Dial("tcp", s.MgmtAddress+":22", config)
if err != nil {
log.Printf("E! telegraf OPS-CTLR plugin client dial failed: %v", err)
return false
}
s.client = conn
s.connected = true
return true
}
func (s *OpsCtlrStats) Disconnect() {
if s.connected {
s.client.Close()
}
}
func (_ *OpsCtlrStats) Description() string {
return "Read metrics about Rack Controller managed OpenSwitch port statistics"
}
var sampleConfig = `
## Rack Controller Managed OpenSwitch statistics
## Switch Information
mgmt_address = "10.0.0.5"
username = "root"
password = ""
`
func (_ *OpsCtlrStats) SampleConfig() string {
return sampleConfig
}
func getPortRole(c *ssh.Client, port string) string {
session, err := c.NewSession()
if err != nil {
log.Printf("E! Failed to get new session: %v", err)
return ""
}
defer session.Close()
command := fmt.Sprintf("ovs-vsctl get Port %v external_ids", port)
cmdOut, err := session.Output(command)
if err != nil {
log.Printf("E! Failed to get port role - %v", err)
return ""
}
// Output of the command above is something like the following:
// {description="{\"role\":\"local\",\"svid\":3997,\"conn_type\":\"none\"}"}\n
// Need to get rid of the \ and prefix/suffix, leaving
// {"role":"local","svid":3997,"conn_type":"none"}
description := strings.TrimPrefix(string(cmdOut), `{description="`)
description = strings.TrimSuffix(description, "\"}\n")
description = strings.Replace(description, `\`, "", -1)
var roleInfo struct {
Role string `json:"role"`
}
if err := json.Unmarshal([]byte(description), &roleInfo); err == nil {
return roleInfo.Role
} else {
return ""
}
}
func (s *OpsCtlrStats) Gather(acc telegraf.Accumulator) error {
if !s.connected {
if !connectDB(s) {
return fmt.Errorf("Failed to connect to OpenSwitch")
}
}
session, err := s.client.NewSession()
if err != nil {
return fmt.Errorf("Failed to get new session: %v", err)
}
defer session.Close()
command := `ovsdb-client transact '["OpenSwitch",{"op":"select","table":"Interface","where":[],"columns":["name", "type", "link_state", "statistics"]}]'`
now := time.Now()
cmdOut, err := session.Output(command)
if err != nil {
return fmt.Errorf("Failed to get port stats - %v", err)
}
var data []map[string][]map[string]interface{}
if err := json.Unmarshal(cmdOut, &data); err != nil {
return fmt.Errorf("Port stats unmarshal err - %v", err)
}
for _, entry := range data {
if rows, ok := entry["rows"]; ok {
for _, row := range rows {
// Skip non-'system' interfaces, e.g. loopback, internal, etc.
if ifType, ok := row["type"]; ok {
if ifType.(string) != "system" {
continue
}
}
// Skip interfaces that are unlinked.
if link, ok := row["link_state"]; ok {
if link.(string) != "up" {
continue
}
}
ifName := row["name"].(string)
tags := map[string]string{
"port": ifName,
"role": getPortRole(s.client, ifName),
}
stats, ok := row["statistics"]
if !ok {
log.Printf("W! Interface %v statistics missing", ifName)
continue
}
var statsMap db.OvsMap
if bs, err := json.Marshal(stats.([]interface{})); err == nil {
if err = statsMap.UnmarshalJSON(bs); err == nil {
fields := map[string]interface{}{
"rx_packets": statsMap.GoMap["rx_packets"],
"rx_bytes": statsMap.GoMap["rx_bytes"],
"rx_errors": statsMap.GoMap["rx_errors"],
"tx_packets": statsMap.GoMap["tx_packets"],
"tx_bytes": statsMap.GoMap["tx_bytes"],
"tx_errors": statsMap.GoMap["tx_errors"],
"ipv4_uc_rx_packets": statsMap.GoMap["ipv4_uc_rx_packets"],
"ipv4_uc_tx_packets": statsMap.GoMap["ipv4_uc_tx_packets"],
"ipv4_mc_rx_packets": statsMap.GoMap["ipv4_mc_rx_packets"],
"ipv4_mc_tx_packets": statsMap.GoMap["ipv4_mc_tx_packets"],
"ipv6_uc_rx_packets": statsMap.GoMap["ipv6_uc_rx_packets"],
"ipv6_uc_tx_packets": statsMap.GoMap["ipv6_uc_tx_packets"],
"ipv6_mc_rx_packets": statsMap.GoMap["ipv6_mc_rx_packets"],
"ipv6_mc_tx_packets": statsMap.GoMap["ipv6_mc_tx_packets"],
}
acc.AddCounter(measurement, fields, tags, now)
} else {
log.Printf("E! Failed to unmarshal stats - %v", err)
}
} else {
log.Printf("E! Failed to convert stats into byte slice - %v", err)
}
}
}
}
return nil
}
func init() {
inputs.Add("ops-ctlr", func() telegraf.Input {
return &OpsCtlrStats{
connected: false,
}
})
}