-
Notifications
You must be signed in to change notification settings - Fork 438
/
exporter.go
312 lines (275 loc) · 9.52 KB
/
exporter.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
package ovnmonitor
import (
"os"
"os/exec"
"sync"
"time"
"github.com/greenpau/ovsdb"
"k8s.io/klog/v2"
"github.com/kubeovn/kube-ovn/pkg/util"
)
const metricNamespace = "kube_ovn"
var (
appName = "ovn-monitor"
isClusterEnabled = true
tryConnectCnt = 0
checkNbDbCnt = 0
checkSbDbCnt = 0
)
// Exporter collects OVN data from the given server and exports them using
// the prometheus metrics package.
type Exporter struct {
sync.RWMutex
Client *ovsdb.OvnClient
timeout int
pollInterval int
errors int64
errorsLocker sync.RWMutex
}
// OVNDBClusterStatus contains information about a cluster.
type OVNDBClusterStatus struct {
cid string
sid string
status string
role string
leader string
vote string
term float64
electionTimer float64
logIndexStart float64
logIndexNext float64
logNotCommitted float64
logNotApplied float64
connIn float64
connOut float64
connInErr float64
connOutErr float64
}
// NewExporter returns an initialized Exporter.
func NewExporter(cfg *Configuration) *Exporter {
e := Exporter{}
e.Client = ovsdb.NewOvnClient()
e.initParas(cfg)
return &e
}
func (e *Exporter) initParas(cfg *Configuration) {
e.timeout = cfg.PollTimeout
e.pollInterval = cfg.PollInterval
e.Client.Timeout = cfg.PollTimeout
e.Client.System.Hostname = os.Getenv("KUBE_NODE_NAME")
e.Client.System.RunDir = cfg.SystemRunDir
e.Client.Database.Vswitch.Name = cfg.DatabaseVswitchName
e.Client.Database.Vswitch.Socket.Remote = cfg.DatabaseVswitchSocketRemote
e.Client.Database.Vswitch.File.Data.Path = cfg.DatabaseVswitchFileDataPath
e.Client.Database.Vswitch.File.Log.Path = cfg.DatabaseVswitchFileLogPath
e.Client.Database.Vswitch.File.Pid.Path = cfg.DatabaseVswitchFilePidPath
e.Client.Database.Vswitch.File.SystemID.Path = cfg.DatabaseVswitchFileSystemIDPath
e.Client.Database.Northbound.Name = cfg.DatabaseNorthboundName
e.Client.Database.Northbound.Socket.Remote = cfg.DatabaseNorthboundSocketRemote
e.Client.Database.Northbound.Socket.Control = cfg.DatabaseNorthboundSocketControl
e.Client.Database.Northbound.File.Data.Path = cfg.DatabaseNorthboundFileDataPath
e.Client.Database.Northbound.File.Log.Path = cfg.DatabaseNorthboundFileLogPath
e.Client.Database.Northbound.File.Pid.Path = cfg.DatabaseNorthboundFilePidPath
e.Client.Database.Northbound.Port.Default = cfg.DatabaseNorthboundPortDefault
e.Client.Database.Northbound.Port.Ssl = cfg.DatabaseNorthboundPortSsl
e.Client.Database.Northbound.Port.Raft = cfg.DatabaseNorthboundPortRaft
e.Client.Database.Southbound.Name = cfg.DatabaseSouthboundName
e.Client.Database.Southbound.Socket.Remote = cfg.DatabaseSouthboundSocketRemote
e.Client.Database.Southbound.Socket.Control = cfg.DatabaseSouthboundSocketControl
e.Client.Database.Southbound.File.Data.Path = cfg.DatabaseSouthboundFileDataPath
e.Client.Database.Southbound.File.Log.Path = cfg.DatabaseSouthboundFileLogPath
e.Client.Database.Southbound.File.Pid.Path = cfg.DatabaseSouthboundFilePidPath
e.Client.Database.Southbound.Port.Default = cfg.DatabaseSouthboundPortDefault
e.Client.Database.Southbound.Port.Ssl = cfg.DatabaseSouthboundPortSsl
e.Client.Database.Southbound.Port.Raft = cfg.DatabaseSouthboundPortRaft
e.Client.Service.Vswitchd.File.Log.Path = cfg.ServiceVswitchdFileLogPath
e.Client.Service.Vswitchd.File.Pid.Path = cfg.ServiceVswitchdFilePidPath
e.Client.Service.Northd.File.Log.Path = cfg.ServiceNorthdFileLogPath
e.Client.Service.Northd.File.Pid.Path = cfg.ServiceNorthdFilePidPath
}
// StartConnection connect to database socket
func (e *Exporter) StartConnection() error {
if err := e.Client.Connect(); err != nil {
return err
}
klog.Infof("%s: exporter connect successfully", e.Client.System.Hostname)
return nil
}
// TryClientConnection try to connect to database socket after init exporter
func (e *Exporter) TryClientConnection() {
for {
if tryConnectCnt > 5 {
util.LogFatalAndExit(nil, "%s: ovn-monitor failed to reconnect db socket finally", e.Client.System.Hostname)
}
if err := e.StartConnection(); err != nil {
tryConnectCnt++
klog.Errorf("%s: ovn-monitor failed to reconnect db socket %v times", e.Client.System.Hostname, tryConnectCnt)
} else {
klog.Infof("%s: ovn-monitor reconnect db successfully", e.Client.System.Hostname)
break
}
time.Sleep(5 * time.Second)
}
}
var registerOvnMetricsOnce sync.Once
// StartOvnMetrics register and start to update ovn metrics
func (e *Exporter) StartOvnMetrics() {
registerOvnMetricsOnce.Do(func() {
registerOvnMetrics()
// OVN metrics updater
go e.ovnMetricsUpdate()
})
}
// ovnMetricsUpdate updates the ovn metrics for every 30 sec
func (e *Exporter) ovnMetricsUpdate() {
for {
e.exportOvnStatusGauge()
e.exportOvnLogFileSizeGauge()
e.exportOvnDBFileSizeGauge()
e.exportOvnRequestErrorGauge()
e.exportOvnDBStatusGauge()
e.exportOvnChassisGauge()
e.exportLogicalSwitchGauge()
e.exportLogicalSwitchPortGauge()
e.exportOvnClusterEnableGauge()
if isClusterEnabled {
e.exportOvnClusterInfoGauge()
}
time.Sleep(time.Duration(e.pollInterval) * time.Second)
}
}
// GetExporterName returns exporter name.
func GetExporterName() string {
return appName
}
func (e *Exporter) exportOvnStatusGauge() {
metricOvnHealthyStatus.Reset()
result := e.getOvnStatus()
for k, v := range result {
metricOvnHealthyStatus.WithLabelValues(e.Client.System.Hostname, k).Set(float64(v))
}
}
func (e *Exporter) exportOvnLogFileSizeGauge() {
metricLogFileSize.Reset()
components := []string{
"ovsdb-server-southbound",
"ovsdb-server-northbound",
"ovn-northd",
}
for _, component := range components {
file, err := e.Client.GetLogFileInfo(component)
if err != nil {
klog.Errorf("%s: log-file-%v", component, err)
e.IncrementErrorCounter()
continue
}
metricLogFileSize.WithLabelValues(e.Client.System.Hostname, file.Component, file.Path).Set(float64(file.Info.Size()))
}
}
func (e *Exporter) exportOvnDBFileSizeGauge() {
metricDBFileSize.Reset()
nbPath := e.Client.Database.Northbound.File.Data.Path
sbPath := e.Client.Database.Southbound.File.Data.Path
dirDbMap := map[string]string{
nbPath: "OVN_Northbound",
sbPath: "OVN_Southbound",
}
for dbFile, database := range dirDbMap {
fileInfo, err := os.Stat(dbFile)
if err != nil {
klog.Errorf("Failed to get the DB size for database %s: %v", database, err)
return
}
metricDBFileSize.WithLabelValues(e.Client.System.Hostname, database).Set(float64(fileInfo.Size()))
}
}
func (e *Exporter) exportOvnRequestErrorGauge() {
metricRequestErrorNums.WithLabelValues(e.Client.System.Hostname).Set(float64(e.errors))
}
func (e *Exporter) exportOvnChassisGauge() {
metricChassisInfo.Reset()
if vteps, err := e.Client.GetChassis(); err != nil {
klog.Errorf("%s: %v", e.Client.Database.Southbound.Name, err)
e.IncrementErrorCounter()
} else {
for _, vtep := range vteps {
metricChassisInfo.WithLabelValues(e.Client.System.Hostname, vtep.UUID, vtep.Name, vtep.IPAddress.String()).Set(1)
}
}
}
func (e *Exporter) exportLogicalSwitchGauge() {
resetLogicalSwitchMetrics()
e.setLogicalSwitchInfoMetric()
}
func (e *Exporter) exportLogicalSwitchPortGauge() {
resetLogicalSwitchPortMetrics()
e.setLogicalSwitchPortInfoMetric()
}
func (e *Exporter) exportOvnClusterEnableGauge() {
metricClusterEnabled.Reset()
isClusterEnabled, err := getClusterEnableState(e.Client.Database.Northbound.File.Data.Path)
if err != nil {
klog.Errorf("failed to get output of cluster status: %v", err)
}
if isClusterEnabled {
metricClusterEnabled.WithLabelValues(e.Client.System.Hostname, e.Client.Database.Northbound.File.Data.Path).Set(1)
} else {
metricClusterEnabled.WithLabelValues(e.Client.System.Hostname, e.Client.Database.Northbound.File.Data.Path).Set(0)
}
}
func (e *Exporter) exportOvnClusterInfoGauge() {
resetOvnClusterMetrics()
dirDbMap := map[string]string{
"nb": "OVN_Northbound",
"sb": "OVN_Southbound",
}
for direction, database := range dirDbMap {
clusterStatus, err := getClusterInfo(direction, database)
if err != nil {
klog.Errorf("Failed to get Cluster Info for database %s: %v", database, err)
return
}
e.setOvnClusterInfoMetric(clusterStatus, database)
}
}
func (e *Exporter) exportOvnDBStatusGauge() {
metricDBStatus.Reset()
dbList := []string{"OVN_Northbound", "OVN_Southbound"}
for _, database := range dbList {
ok, err := getDBStatus(database)
if err != nil {
klog.Errorf("Failed to get DB status for %s: %v", database, err)
return
}
if ok {
metricDBStatus.WithLabelValues(e.Client.System.Hostname, database).Set(1)
} else {
metricDBStatus.WithLabelValues(e.Client.System.Hostname, database).Set(0)
switch database {
case "OVN_Northbound":
checkNbDbCnt++
if checkNbDbCnt < 6 {
klog.Warningf("Failed to get OVN NB DB status for %v times", checkNbDbCnt)
return
} else {
klog.Warningf("Failed to get OVN NB DB status for %v times, ready to restore OVN DB", checkNbDbCnt)
checkNbDbCnt = 0
}
case "OVN_Southbound":
checkSbDbCnt++
if checkSbDbCnt < 6 {
klog.Warningf("Failed to get OVN SB DB status for %v times", checkSbDbCnt)
return
} else {
klog.Warningf("Failed to get OVN SB DB status for %v times, ready to restore OVN DB", checkSbDbCnt)
checkSbDbCnt = 0
}
}
output, err := exec.Command("/bin/bash", "/kube-ovn/restore-ovn-nb-db.sh").CombinedOutput()
if err != nil {
klog.Errorf("Failed to restore OVN DB, err %v", err)
}
klog.Infof("restore OVN DB %v, process output %v", database, string(output))
}
}
}