This repository was archived by the owner on Feb 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsystem.go
More file actions
358 lines (337 loc) · 11.9 KB
/
system.go
File metadata and controls
358 lines (337 loc) · 11.9 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright 2018 Paul Greenberg (greenpau@outlook.com)
//
// 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 ovsdb
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
// OvsDataFile stores information about the files related to OVS
// operations, e.g. log files, database files, etc.
type OvsDataFile struct {
Path string
Component string
Info os.FileInfo
Reader struct {
Offset int64
}
}
// OvsDaemon stores information about a process or database, together
// with associated log and process id files.
type OvsDaemon struct {
File struct {
Log OvsDataFile
Pid OvsDataFile
}
Process OvsProcess
Socket struct {
Control string
}
}
// GetSystemID TODO
func (cli *OvnClient) GetSystemID() error {
systemID, err := getSystemID(cli.Database.Vswitch.Client, cli.Database.Vswitch.Name, cli.Database.Vswitch.File.SystemID.Path)
if err != nil {
return err
}
cli.System.ID = systemID
return nil
}
// GetSystemID TODO
func (cli *OvsClient) GetSystemID() error {
systemID, err := getSystemID(cli.Database.Vswitch.Client, cli.Database.Vswitch.Name, cli.Database.Vswitch.File.SystemID.Path)
if err != nil {
return err
}
cli.System.ID = systemID
return nil
}
func getSystemID(client *Client, dbName string, filepath string) (string, error) {
var systemID string
var dbErr error
// First, try to query database if client is provided
if client != nil && dbName != "" {
query := fmt.Sprintf("SELECT external_ids FROM %s", dbName)
result, err := client.Transact(dbName, query)
if err == nil && len(result.Rows) > 0 {
col := "external_ids"
rowData, dataType, err := result.Rows[0].GetColumnValue(col, result.Columns)
if err == nil && dataType == "map[string]string" {
externalIDs := rowData.(map[string]string)
if dbSystemID, exists := externalIDs["system-id"]; exists && dbSystemID != "" {
systemID = dbSystemID
if len(systemID) > 253 {
return systemID, fmt.Errorf("system-id is greater than what the exporter currently allows: %d vs 253", len(systemID))
}
return systemID, nil
}
}
} else if err != nil {
dbErr = err
}
}
// Fallback to reading from file
file, err := os.Open(filepath)
if err != nil {
// If we also had a database error, return both
if dbErr != nil {
return "", fmt.Errorf("failed to get system-id from database (%s) and file (%s)", dbErr, err)
}
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
systemID = scanner.Text()
break
}
if err := scanner.Err(); err != nil {
if dbErr != nil {
return "", fmt.Errorf("failed to get system-id from database (%s) and file (%s)", dbErr, err)
}
return "", err
}
// vswitch.ovsschema does not limit system IDs to a particular length and a common
// ID to use is UUID (36 bytes). However, some tools use FQDNs for system-ids which
// are limited to 253 octets per RFC1035. Hence the current limit checked by the
// exporter is 253 bytes to avoid arbitrary length for system IDs and to have a sane limit.
if len(systemID) > 253 {
return systemID, fmt.Errorf("system-id is greater than what the exporter currently allows: %d vs 253", len(systemID))
}
return systemID, nil
}
func getVersionViaAppctl(sock string, timeout int) (string, error) {
cmd := "version"
app, err := NewClient(sock, timeout)
if err != nil {
return "", fmt.Errorf("failed to connect to socket %s: %s", sock, err)
}
r, err := app.query(cmd, nil)
app.Close()
if err != nil {
return "", fmt.Errorf("the '%s' command failed: %s", cmd, err)
}
response := r.String()
if response == "" {
return "", fmt.Errorf("the '%s' command returned no data", cmd)
}
return response, nil
}
func parseOvsVersion(versionStr string) string {
// Parse version from string like "ovs-vswitchd (Open vSwitch) 3.5.1"
re := regexp.MustCompile(`\(Open vSwitch\)\s+([\d.]+)`)
matches := re.FindStringSubmatch(versionStr)
if len(matches) > 1 {
return matches[1]
}
// Fallback: return the whole string
return strings.TrimSpace(versionStr)
}
func getSystemInfoFromOS() (string, string) {
// Read /etc/os-release to get system type and version
file, err := os.Open("/etc/os-release")
if err != nil {
return "", ""
}
defer file.Close()
var systemType, systemVersion string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "ID=") {
systemType = strings.Trim(strings.TrimPrefix(line, "ID="), "\"")
} else if strings.HasPrefix(line, "VERSION_ID=") {
systemVersion = strings.Trim(strings.TrimPrefix(line, "VERSION_ID="), "\"")
}
}
return systemType, systemVersion
}
func populateVersionFromAppctl(systemInfo map[string]string, sock string, timeout int, schema *Schema) {
// Get OVS version via ovs-appctl if missing from DB
if val, exists := systemInfo["ovs_version"]; !exists || val == "" {
versionStr, err := getVersionViaAppctl(sock, timeout)
if err == nil {
systemInfo["ovs_version"] = parseOvsVersion(versionStr)
} else {
systemInfo["ovs_version"] = "unknown"
}
}
// Get DB version from schema if missing from DB
if val, exists := systemInfo["db_version"]; !exists || val == "" {
if schema != nil && schema.Version != "" {
systemInfo["db_version"] = schema.Version
} else {
systemInfo["db_version"] = "unknown"
}
}
// Get system type and version from /etc/os-release if missing from DB
if val, exists := systemInfo["system_type"]; !exists || val == "" {
systemType, systemVersion := getSystemInfoFromOS()
if systemType != "" {
systemInfo["system_type"] = systemType
} else {
systemInfo["system_type"] = "unknown"
}
if systemVersion != "" {
systemInfo["system_version"] = systemVersion
} else {
systemInfo["system_version"] = "unknown"
}
} else if val, exists := systemInfo["system_version"]; !exists || val == "" {
_, systemVersion := getSystemInfoFromOS()
if systemVersion != "" {
systemInfo["system_version"] = systemVersion
} else {
systemInfo["system_version"] = "unknown"
}
}
}
func parseSystemInfo(systemID string, result Result) (map[string]string, error) {
systemInfo := make(map[string]string)
for _, row := range result.Rows {
col := "external_ids"
rowData, dataType, err := row.GetColumnValue(col, result.Columns)
if err != nil {
return systemInfo, fmt.Errorf("parsing '%s' failed: %s", col, err)
}
if dataType != "map[string]string" {
return systemInfo, fmt.Errorf("data type '%s' for '%s' column is unexpected in this context", dataType, col)
}
systemInfo = rowData.(map[string]string)
columns := []string{"ovs_version", "db_version", "system_type", "system_version"}
for _, col := range columns {
rowData, dataType, err = row.GetColumnValue(col, result.Columns)
if err != nil {
return systemInfo, fmt.Errorf("parsing '%s' failed: %s", col, err)
}
switch dataType {
case "string":
systemInfo[col] = rowData.(string)
case "[]string":
arr := rowData.([]string)
if len(arr) > 0 {
systemInfo[col] = arr[0]
} else {
systemInfo[col] = ""
}
default:
return systemInfo, fmt.Errorf("data type '%s' for '%s' column is unexpected in this context", dataType, col)
}
}
break //nolint:staticcheck
}
if dbSystemID, exists := systemInfo["system-id"]; exists {
if dbSystemID != systemID {
return systemInfo, fmt.Errorf("found 'system-id' mismatch %s (db) vs. %s (config)", dbSystemID, systemID)
}
} else {
return systemInfo, fmt.Errorf("no 'system-id' found")
}
// Set defaults for optional keys that may not be in external_ids
if _, exists := systemInfo["rundir"]; !exists {
systemInfo["rundir"] = "/var/run/openvswitch"
}
// Only hostname is truly required
requiredKeys := []string{"hostname"}
for _, key := range requiredKeys {
if _, exists := systemInfo[key]; !exists {
return systemInfo, fmt.Errorf("no mandatory '%s' found", key)
}
}
return systemInfo, nil
}
// GetSystemInfo returns a hash containing system information, e.g. `system_id`
// associated with the Open_vSwitch database.
func (cli *OvnClient) GetSystemInfo() error {
// Get system-id (tries database first, falls back to file)
systemID, err := getSystemID(cli.Database.Vswitch.Client, cli.Database.Vswitch.Name, cli.Database.Vswitch.File.SystemID.Path)
if err != nil {
return err
}
query := fmt.Sprintf("SELECT ovs_version, db_version, system_type, system_version, external_ids FROM %s", cli.Database.Vswitch.Name)
result, err := cli.Database.Vswitch.Client.Transact(cli.Database.Vswitch.Name, query)
if err != nil {
return fmt.Errorf("The '%s' query failed: %s", query, err)
}
if len(result.Rows) == 0 {
return fmt.Errorf("The '%s' query did not return any rows", query)
}
systemInfo, err := parseSystemInfo(systemID, result)
if err != nil {
return fmt.Errorf("The '%s' query returned results but erred: %s", query, err)
}
// Get schema for db_version
schema, _ := cli.Database.Vswitch.Client.GetSchema(cli.Database.Vswitch.Name)
// Ensure PID is read and socket path is updated before using control socket
if cli.Database.Vswitch.Process.ID == 0 {
p, pidErr := getProcessInfoFromFile(cli.Database.Vswitch.File.Pid.Path)
if pidErr == nil {
cli.Database.Vswitch.Process = p
}
}
cli.updateRefs()
// Query version information via ovs-appctl for fields not in DB (OVS 3.x+)
populateVersionFromAppctl(systemInfo, cli.Database.Vswitch.Socket.Control, cli.Timeout, &schema)
cli.System.ID = systemInfo["system-id"]
cli.System.RunDir = systemInfo["rundir"]
cli.System.Hostname = systemInfo["hostname"]
cli.System.Type = systemInfo["system_type"]
cli.System.Version = systemInfo["system_version"]
cli.Database.Vswitch.Version = systemInfo["ovs_version"]
cli.Database.Vswitch.Schema.Version = systemInfo["db_version"]
return nil
}
// GetSystemInfo returns a hash containing system information, e.g. `system_id`
// associated with the Open_vSwitch database.
func (cli *OvsClient) GetSystemInfo() error {
// Get system-id (tries database first, falls back to file)
systemID, err := getSystemID(cli.Database.Vswitch.Client, cli.Database.Vswitch.Name, cli.Database.Vswitch.File.SystemID.Path)
if err != nil {
return err
}
query := fmt.Sprintf("SELECT ovs_version, db_version, system_type, system_version, external_ids FROM %s", cli.Database.Vswitch.Name)
result, err := cli.Database.Vswitch.Client.Transact(cli.Database.Vswitch.Name, query)
if err != nil {
return fmt.Errorf("The '%s' query failed: %s", query, err)
}
if len(result.Rows) == 0 {
return fmt.Errorf("The '%s' query did not return any rows", query)
}
systemInfo, err := parseSystemInfo(systemID, result)
if err != nil {
return fmt.Errorf("The '%s' query returned results but erred: %s", query, err)
}
// Get schema for db_version
schema, _ := cli.Database.Vswitch.Client.GetSchema(cli.Database.Vswitch.Name)
// Ensure PID is read and socket path is updated before using control socket
if cli.Database.Vswitch.Process.ID == 0 {
p, pidErr := getProcessInfoFromFile(cli.Database.Vswitch.File.Pid.Path)
if pidErr == nil {
cli.Database.Vswitch.Process = p
}
}
cli.updateRefs()
// Query version information via ovs-appctl for fields not in DB (OVS 3.x+)
populateVersionFromAppctl(systemInfo, cli.Database.Vswitch.Socket.Control, cli.Timeout, &schema)
cli.System.ID = systemInfo["system-id"]
cli.System.RunDir = systemInfo["rundir"]
cli.System.Hostname = systemInfo["hostname"]
cli.System.Type = systemInfo["system_type"]
cli.System.Version = systemInfo["system_version"]
cli.Database.Vswitch.Version = systemInfo["ovs_version"]
cli.Database.Vswitch.Schema.Version = systemInfo["db_version"]
return nil
}