-
Notifications
You must be signed in to change notification settings - Fork 443
/
exporter.go
197 lines (169 loc) · 5.32 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
package pinger
import (
"os"
"sync"
"time"
"github.com/greenpau/ovsdb"
"k8s.io/klog/v2"
)
const metricNamespace = "kube_ovn"
var (
appName = "ovs-monitor"
tryConnectCnt = 0
)
// Exporter collects OVS data from the given server and exports them using
// the prometheus metrics package.
type Exporter struct {
sync.RWMutex
Client *ovsdb.OvsClient
timeout int
pollInterval int
errors int64
errorsLocker sync.RWMutex
}
// NewExporter returns an initialized Exporter.
func NewExporter(cfg *Configuration) *Exporter {
e := Exporter{}
e.Client = ovsdb.NewOvsClient()
e.initParas(cfg)
if err := e.Client.GetSystemID(); err != nil {
klog.Errorf("%s failed to get system id: %s", appName, err)
}
if err := e.StartConnection(); err != nil {
klog.Errorf("%s failed to connect db properly: %s", appName, err)
go e.tryClientConnection()
}
return &e
}
func (e *Exporter) initParas(cfg *Configuration) {
e.timeout = cfg.PollTimeout
e.pollInterval = cfg.PollInterval
e.Client.Timeout = cfg.PollTimeout
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.Service.Vswitchd.File.Log.Path = cfg.ServiceVswitchdFileLogPath
e.Client.Service.Vswitchd.File.Pid.Path = cfg.ServiceVswitchdFilePidPath
e.Client.Service.OvnController.File.Log.Path = cfg.ServiceOvnControllerFileLogPath
e.Client.Service.OvnController.File.Pid.Path = cfg.ServiceOvnControllerFilePidPath
}
// StartConnection connect to database socket
func (e *Exporter) StartConnection() error {
if err := e.Client.Connect(); err != nil {
return err
}
if err := e.Client.GetSystemInfo(); err != nil {
return err
}
klog.Infof("%s: exporter connect successfully", e.Client.System.Hostname)
return nil
}
func (e *Exporter) tryClientConnection() {
for {
if tryConnectCnt > 5 {
klog.Errorf("%s: pinger failed to reconnect db socket finally", e.Client.System.ID)
break
}
if err := e.StartConnection(); err != nil {
tryConnectCnt++
klog.Errorf("%s: pinger failed to reconnect db socket %v times", e.Client.System.ID, tryConnectCnt)
} else {
if err := e.Client.GetSystemID(); err != nil {
klog.Errorf("%s failed to get system id: %s", appName, err)
} else {
klog.Infof("%s: pinger reconnect db successfully", e.Client.System.ID)
}
break
}
time.Sleep(5 * time.Second)
}
}
// ovsMetricsUpdate updates the ovs metrics
func (e *Exporter) ovsMetricsUpdate() {
e.exportOvsStatusGauge()
e.exportOvsInfoGauge()
e.exportOvsLogFileSizeGauge()
e.exportOvsDbFileSizeGauge()
e.exportOvsRequestErrorGauge()
e.exportOvsDpGauge()
e.exportOvsInterfaceGauge()
}
func (e *Exporter) exportOvsStatusGauge() {
metricOvsHealthyStatus.Reset()
result := e.getOvsStatus()
for k, v := range result {
if v {
metricOvsHealthyStatus.WithLabelValues(e.Client.System.Hostname, k).Set(1)
} else {
metricOvsHealthyStatus.WithLabelValues(e.Client.System.Hostname, k).Set(0)
}
}
}
func (e *Exporter) exportOvsInfoGauge() {
metricOvsInfo.Reset()
if err := e.Client.GetSystemInfo(); err != nil {
klog.Errorf("Failed to get System Info")
return
}
metricOvsInfo.WithLabelValues(e.Client.System.ID, e.Client.System.RunDir, e.Client.System.Hostname,
e.Client.System.Type, e.Client.System.Version, e.Client.Database.Vswitch.Version,
e.Client.Database.Vswitch.Schema.Version).Set(1)
}
func (e *Exporter) exportOvsLogFileSizeGauge() {
metricLogFileSize.Reset()
components := []string{
"ovsdb-server",
"ovs-vswitchd",
}
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) exportOvsDbFileSizeGauge() {
metricDbFileSize.Reset()
database := "OVS_DB"
fileInfo, err := os.Stat(e.Client.Database.Vswitch.File.Data.Path)
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) exportOvsRequestErrorGauge() {
metricRequestErrorNums.WithLabelValues(e.Client.System.Hostname).Set(float64(e.errors))
}
func (e *Exporter) exportOvsDpGauge() {
datapaths, err := e.getOvsDatapath()
if err != nil {
klog.Errorf("Failed to get the output of ovs-dpctl dump-dps: %v", err)
return
}
resetOvsDatapathMetrics()
for _, datapathName := range datapaths {
err = e.setOvsDpIfMetric(datapathName)
if err != nil {
klog.Errorf("failed to get datapath stats for %s %v", datapathName, err)
}
}
}
func (e *Exporter) exportOvsInterfaceGauge() {
intfs, err := e.getInterfaceInfo()
if err != nil {
klog.Errorf("Failed to get the output of ovs-vsctl list Interface: %v", err)
return
}
resetOvsInterfaceMetrics()
for _, intf := range intfs {
e.setOvsInterfaceMetric(intf)
}
}