-
Notifications
You must be signed in to change notification settings - Fork 434
/
util.go
314 lines (285 loc) · 10.3 KB
/
util.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package ovnmonitor
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"sync/atomic"
"k8s.io/klog/v2"
)
// IncrementErrorCounter increases the counter of failed queries to OVN server.
func (e *Exporter) IncrementErrorCounter() {
e.errorsLocker.Lock()
defer e.errorsLocker.Unlock()
atomic.AddInt64(&e.errors, 1)
}
func (e *Exporter) getOvnStatus() map[string]int {
result := make(map[string]int)
// get ovn-northbound status
cmdstr := "ovs-appctl -t /var/run/ovn/ovnnb_db.ctl cluster/status OVN_Northbound"
cmd := exec.Command("sh", "-c", cmdstr)
output, err := cmd.CombinedOutput()
if err != nil {
klog.Errorf("get ovn-northbound status failed, err %v", err)
result["ovsdb-server-northbound"] = 0
}
result["ovsdb-server-northbound"] = parseDbStatus(string(output))
// get ovn-southbound status
cmdstr = "ovs-appctl -t /var/run/ovn/ovnsb_db.ctl cluster/status OVN_Southbound"
cmd = exec.Command("sh", "-c", cmdstr)
output, err = cmd.CombinedOutput()
if err != nil {
klog.Errorf("get ovn-southbound status failed, err %v", err)
result["ovsdb-server-southbound"] = 0
}
result["ovsdb-server-southbound"] = parseDbStatus(string(output))
// get ovn-northd status
pid, err := os.ReadFile("/var/run/ovn/ovn-northd.pid")
if err != nil {
klog.Errorf("read ovn-northd pid failed, err %v", err)
result["ovn-northd"] = 0
} else {
cmdstr := "ovs-appctl -t /var/run/ovn/ovn-northd." + strings.Trim(string(pid), "\n") + ".ctl status"
klog.V(3).Infof("cmd is %v", cmdstr)
cmd := exec.Command("sh", "-c", cmdstr)
output, err := cmd.CombinedOutput()
if err != nil {
klog.Errorf("get ovn-northd status failed, err %v", err)
result["ovn-northd"] = 0
}
if len(strings.Split(string(output), ":")) != 2 {
result["ovn-northd"] = 0
} else {
status := strings.TrimSpace(strings.Split(string(output), ":")[1])
if status == "standby" {
result["ovn-northd"] = 2
} else if status == "active" {
result["ovn-northd"] = 1
}
}
}
return result
}
func getClusterEnableState(dbName string) (bool, error) {
cmdstr := fmt.Sprintf("ovsdb-tool db-is-clustered %s", dbName)
cmd := exec.Command("sh", "-c", cmdstr)
_, err := cmd.CombinedOutput()
if err != nil {
return false, err
}
return true, nil
}
func (e *Exporter) setLogicalSwitchInfoMetric() {
lsws, err := e.Client.GetLogicalSwitches()
if err != nil {
klog.Errorf("%s: %v", e.Client.Database.Southbound.Name, err)
e.IncrementErrorCounter()
} else {
for _, lsw := range lsws {
metricLogicalSwitchInfo.WithLabelValues(e.Client.System.Hostname, lsw.UUID, lsw.Name).Set(1)
metricLogicalSwitchPortsNum.WithLabelValues(e.Client.System.Hostname, lsw.UUID, lsw.Name).Set(float64(len(lsw.Ports)))
if len(lsw.Ports) > 0 {
for _, p := range lsw.Ports {
metricLogicalSwitchPortBinding.WithLabelValues(e.Client.System.Hostname, lsw.UUID, p, lsw.Name).Set(1)
}
}
if len(lsw.ExternalIDs) > 0 {
for k, v := range lsw.ExternalIDs {
metricLogicalSwitchExternalIDs.WithLabelValues(e.Client.System.Hostname, lsw.UUID, k, v, lsw.Name).Set(1)
}
}
metricLogicalSwitchTunnelKey.WithLabelValues(e.Client.System.Hostname, lsw.UUID, lsw.Name).Set(float64(lsw.TunnelKey))
}
}
}
func (e *Exporter) setLogicalSwitchPortInfoMetric() {
lswps, err := e.Client.GetLogicalSwitchPorts()
if err != nil {
klog.Errorf("%s: %v", e.Client.Database.Southbound.Name, err)
e.IncrementErrorCounter()
} else {
for _, port := range lswps {
metricLogicalSwitchPortInfo.WithLabelValues(e.Client.System.Hostname, port.UUID, port.Name, port.ChassisUUID,
port.LogicalSwitchName, port.DatapathUUID, port.PortBindingUUID, port.MacAddress.String(), port.IPAddress.String()).Set(1)
metricLogicalSwitchPortTunnelKey.WithLabelValues(e.Client.System.Hostname, port.UUID, port.LogicalSwitchName, port.Name).Set(float64(port.TunnelKey))
}
}
}
func getClusterInfo(direction, dbName string) (*OVNDBClusterStatus, error) {
clusterStatus := &OVNDBClusterStatus{}
var err error
cmdstr := fmt.Sprintf("ovs-appctl -t /var/run/ovn/ovn%s_db.ctl cluster/status %s", direction, dbName)
cmd := exec.Command("sh", "-c", cmdstr)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to retrieve cluster/status info for database %s: %v", dbName, err)
}
for _, line := range strings.Split(string(output), "\n") {
idx := strings.Index(line, ":")
if idx == -1 {
continue
}
switch line[:idx] {
case "Cluster ID":
// the value is of the format `45ef (45ef51b9-9401-46e7-810d-6db0fc344ea2)`
clusterStatus.cid = strings.Trim(strings.Fields(line[idx+2:])[1], "()")
case "Server ID":
clusterStatus.sid = strings.Trim(strings.Fields(line[idx+2:])[1], "()")
case "Status":
clusterStatus.status = line[idx+2:]
case "Role":
clusterStatus.role = line[idx+2:]
case "Term":
if value, err := strconv.ParseFloat(line[idx+2:], 64); err == nil {
clusterStatus.term = value
}
case "Leader":
clusterStatus.leader = line[idx+2:]
case "Vote":
clusterStatus.vote = line[idx+2:]
case "Election timer":
if value, err := strconv.ParseFloat(line[idx+2:], 64); err == nil {
clusterStatus.electionTimer = value
}
case "Log":
// the value is of the format [2, 1108]
values := strings.Split(strings.Trim(line[idx+2:], "[]"), ", ")
if value, err := strconv.ParseFloat(values[0], 64); err == nil {
clusterStatus.logIndexStart = value
}
if value, err := strconv.ParseFloat(values[1], 64); err == nil {
clusterStatus.logIndexNext = value
}
case "Entries not yet committed":
if value, err := strconv.ParseFloat(line[idx+2:], 64); err == nil {
clusterStatus.logNotCommitted = value
}
case "Entries not yet applied":
if value, err := strconv.ParseFloat(line[idx+2:], 64); err == nil {
clusterStatus.logNotApplied = value
}
case "Connections":
// The value could be nil
if len(line[idx+1:]) != 0 {
// the value is of the format `->0000 (->56d7) <-46ac <-56d7`
var connIn, connOut, connInErr, connOutErr float64
for _, conn := range strings.Fields(line[idx+2:]) {
if strings.HasPrefix(conn, "->") {
connOut++
} else if strings.HasPrefix(conn, "<-") {
connIn++
} else if strings.HasPrefix(conn, "(->") {
connOutErr++
} else if strings.HasPrefix(conn, "(<-") {
connInErr++
}
}
clusterStatus.connIn = connIn
clusterStatus.connOut = connOut
clusterStatus.connInErr = connInErr
clusterStatus.connOutErr = connOutErr
}
}
}
return clusterStatus, nil
}
func (e *Exporter) setOvnClusterInfoMetric(c *OVNDBClusterStatus, dbName string) {
metricClusterRole.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid, c.role).Set(1)
metricClusterStatus.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid, c.status).Set(1)
metricClusterTerm.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.term)
if c.leader == "self" {
metricClusterLeaderSelf.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(1)
} else {
metricClusterLeaderSelf.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(0)
}
if c.vote == "self" {
metricClusterVoteSelf.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(1)
} else {
metricClusterVoteSelf.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(0)
}
metricClusterElectionTimer.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.electionTimer)
metricClusterNotCommittedEntryCount.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.logNotCommitted)
metricClusterNotAppliedEntryCount.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.logNotApplied)
metricClusterLogIndexStart.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.logIndexStart)
metricClusterLogIndexNext.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.logIndexNext)
metricClusterInConnTotal.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.connIn)
metricClusterOutConnTotal.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.connOut)
metricClusterInConnErrTotal.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.connInErr)
metricClusterOutConnErrTotal.WithLabelValues(e.Client.System.Hostname, dbName, c.sid, c.cid).Set(c.connOutErr)
}
func parseDbStatus(output string) int {
var status string
var result int
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.Contains(line, "Role:") {
status = strings.TrimSpace(strings.Split(line, ":")[1])
break
}
}
switch status {
case "leader":
result = 1
case "follower":
result = 2
default:
result = 0
}
return result
}
func getDBStatus(dbName string) (bool, error) {
var cmdstr string
var result bool
switch dbName {
case "OVN_Northbound":
cmdstr = fmt.Sprintf("ovn-appctl -t /var/run/ovn/ovnnb_db.ctl ovsdb-server/get-db-storage-status %s", dbName)
case "OVN_Southbound":
cmdstr = fmt.Sprintf("ovn-appctl -t /var/run/ovn/ovnsb_db.ctl ovsdb-server/get-db-storage-status %s", dbName)
}
cmd := exec.Command("sh", "-c", cmdstr)
output, err := cmd.CombinedOutput()
if err != nil {
klog.Errorf("get ovn-northbound status failed, err %v", err)
return false, err
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "status: ok") {
result = true
break
}
if strings.Contains(line, "ovsdb error") {
result = false
break
}
}
return result, nil
}
func resetLogicalSwitchMetrics() {
metricLogicalSwitchInfo.Reset()
metricLogicalSwitchPortsNum.Reset()
metricLogicalSwitchPortBinding.Reset()
metricLogicalSwitchExternalIDs.Reset()
metricLogicalSwitchTunnelKey.Reset()
}
func resetLogicalSwitchPortMetrics() {
metricLogicalSwitchPortInfo.Reset()
metricLogicalSwitchPortTunnelKey.Reset()
}
func resetOvnClusterMetrics() {
metricClusterRole.Reset()
metricClusterStatus.Reset()
metricClusterTerm.Reset()
metricClusterLeaderSelf.Reset()
metricClusterVoteSelf.Reset()
metricClusterElectionTimer.Reset()
metricClusterNotCommittedEntryCount.Reset()
metricClusterNotAppliedEntryCount.Reset()
metricClusterLogIndexStart.Reset()
metricClusterLogIndexNext.Reset()
metricClusterInConnTotal.Reset()
metricClusterOutConnTotal.Reset()
metricClusterInConnErrTotal.Reset()
metricClusterOutConnErrTotal.Reset()
}