forked from greenpau/ovsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
191 lines (181 loc) · 6.3 KB
/
system.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
// Copyright 2018 Paul Greenberg ([email protected])
//
// 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"
)
// 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.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.File.SystemID.Path)
if err != nil {
return err
}
cli.System.ID = systemID
return nil
}
func getSystemID(filepath string) (string, error) {
var systemID string
file, err := os.Open(filepath)
if err != nil {
return systemID, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
systemID = scanner.Text()
break
}
if err := scanner.Err(); err != nil {
return systemID, 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 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)
}
if dataType != "string" {
return systemInfo, fmt.Errorf("data type '%s' for '%s' column is unexpected in this context", dataType, col)
}
systemInfo[col] = rowData.(string)
}
break
}
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")
}
requiredKeys := []string{"rundir", "hostname", "ovs_version", "db_version", "system_type", "system_version"}
for _, key := range requiredKeys {
if _, exists := systemInfo[key]; exists == false {
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 {
systemID, err := getSystemID(cli.Database.Vswitch.File.SystemID.Path)
if err != nil {
return err
}
cli.System.ID = systemID
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)
}
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 {
systemID, err := getSystemID(cli.Database.Vswitch.File.SystemID.Path)
if err != nil {
return err
}
cli.System.ID = systemID
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)
}
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
}