-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig.go
288 lines (250 loc) · 8.34 KB
/
config.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
/**
* Copyright 2024 Advanced Micro Devices, Inc. All rights reserved.
*
* 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 main
import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"github.com/joho/godotenv"
"gopkg.in/yaml.v3"
)
const (
DefaultListenerPort = "8080"
DefaultMetricsPort = "2112"
DefaultUseSSL = "false"
DefaultSeverityConfig = "Fatal,Critical,Informational"
NodeDrainPolicyFile = "nodeDrainPolicy.json"
)
type Config struct {
Information struct {
Updated string
Description string
}
SystemInformation struct {
ListenerIP string
ListenerPort string
UseSSL bool
MetricsPort int
}
CertificateDetails struct {
CertFile string
KeyFile string
}
SlurmToken string
SlurmControlNode string
SlurmUser string
SlurmScontrolPath string
SlurmDrainExcludeStr string
SubscriptionPayload SubscriptionPayload
RedfishServers []RedfishServer
TriggerEvents map[string]map[string][]EventInfo //map[Severity][MessageRegistry.MessageId][]EventInfo
PrometheusConfig PrometheusConfig
context *tls.Config
eventCount int
dataBuffer []byte
TlsTimeOut string
}
type EventInfo struct {
UniqueString string
Category string
Subcategory string
DrainReasonPrefix string
}
type TriggerEvent struct {
Severity string `json:"Severity"`
Action string `json:"Action"`
Message string `json:"Message"`
DrainReasonPrefix string `json:"DrainReasonPrefix"`
}
type TriggerEventsInfo struct {
Category string `json:"Category"`
Subcategory string `json:"Subcategory"`
MessageRegistry string `json:"MessageRegistry"`
MessageId string `json:"MessageId"`
UniqueString string `json:"UniqueString"`
Severity string `json:"Severity"`
DrainReasonPrefix string `json:"DrainReasonPrefix"`
Enable bool `json:"Enable"`
}
type PrometheusConfig struct {
Severity []string `json:"Severity"`
}
type target struct {
Targets []string `yaml:"targets"`
Labels map[string]string `yaml:"labels"`
}
func setupConfig(targetFile string) Config {
// Load .env file
err := godotenv.Load()
if err != nil {
log.Println("Failed to load .env file. Assuming environment variables are set.")
}
// Initialize Config object
var AppConfig Config
// Read environment variables
AppConfig.Information.Updated = os.Getenv("UPDATED")
AppConfig.Information.Description = os.Getenv("DESCRIPTION")
AppConfig.SystemInformation.ListenerIP = os.Getenv("LISTENER_IP")
// Read and parse LISTENER_PORT with a default value
listenerPort := os.Getenv("LISTENER_PORT")
if listenerPort == "" {
listenerPort = DefaultListenerPort
}
AppConfig.SystemInformation.ListenerPort = listenerPort
// Metrics Port Configuration
metricsPortStr := os.Getenv("METRICS_PORT")
if metricsPortStr == "" {
metricsPortStr = DefaultMetricsPort
}
metricsPort, err := strconv.Atoi(metricsPortStr)
if err != nil {
log.Fatalf("Failed to parse METRICS_PORT: %v", err)
}
AppConfig.SystemInformation.MetricsPort = metricsPort
// Read and parse USE_SSL with a default value
useSSLStr := os.Getenv("USE_SSL")
if useSSLStr == "" {
useSSLStr = DefaultUseSSL
}
useSSL, err := strconv.ParseBool(useSSLStr)
if err != nil {
log.Fatalf("Failed to parse USE_SSL: %v", err)
}
AppConfig.SystemInformation.UseSSL = useSSL
AppConfig.CertificateDetails.CertFile = os.Getenv("CERTFILE")
AppConfig.CertificateDetails.KeyFile = os.Getenv("KEYFILE")
AppConfig.SlurmToken = os.Getenv("SLURM_TOKEN")
AppConfig.SlurmControlNode = os.Getenv("SLURM_CONTROL_NODE")
AppConfig.SlurmUser = os.Getenv("SLURM_USER")
AppConfig.SlurmDrainExcludeStr = os.Getenv("SLURM_DRAIN_EXCLUDE_REASON_LIST")
AppConfig.SlurmScontrolPath = os.Getenv("SLURM_SCONTROL_PATH")
AppConfig.TlsTimeOut = os.Getenv("TLS_TIMEOUT")
subscriptionPayloadJSON := os.Getenv("SUBSCRIPTION_PAYLOAD")
if err := json.Unmarshal([]byte(subscriptionPayloadJSON), &AppConfig.SubscriptionPayload); err != nil {
log.Fatalf("Failed to parse SUBSCRIPTION_PAYLOAD: %v", err)
}
prometheusConfigJSON := os.Getenv("PROMETHEUS_CONFIG")
if prometheusConfigJSON != "" {
err = json.Unmarshal([]byte(prometheusConfigJSON), &AppConfig.PrometheusConfig)
if err != nil {
log.Fatalf("Failed to unmarshal PROMETHEUS_CONFIG: %v", err)
}
}
if prometheusConfigJSON == "" {
AppConfig.PrometheusConfig.Severity = strings.Split(DefaultSeverityConfig, ",")
}
// Read and parse the REDFISH_SERVERS environment variable
redfishServersJSON := os.Getenv("REDFISH_SERVERS")
if redfishServersJSON == "" {
log.Println("REDFISH_SERVERS environment variable is not set or is empty")
} else {
if err := json.Unmarshal([]byte(redfishServersJSON), &AppConfig.RedfishServers); err != nil {
log.Fatalf("Failed to parse REDFISH_SERVERS: %v", err)
}
}
// Read the node drain policy config file
nodeDrainPolicyConfig, err := os.ReadFile(NodeDrainPolicyFile)
if err != nil {
log.Fatalf("Failed to read: %v", NodeDrainPolicyFile)
}
triggerEventsInfo := []TriggerEventsInfo{}
err = json.Unmarshal(nodeDrainPolicyConfig, &triggerEventsInfo)
if err != nil {
log.Fatalf("Failed to unmarshal file: %v | err: %v", NodeDrainPolicyFile, err)
}
tInfoMap := map[string]map[string][]EventInfo{}
for _, evt := range triggerEventsInfo {
fmt.Printf("Trigger Event: %+v\n", evt)
if evt.Enable != true {
continue
}
eInfo := EventInfo{}
eInfo.Category = evt.Category
eInfo.Subcategory = evt.Subcategory
eInfo.DrainReasonPrefix = evt.DrainReasonPrefix
eInfo.UniqueString = evt.UniqueString
key := ""
if evt.MessageRegistry == "" {
key = evt.MessageId
} else {
key = evt.MessageRegistry + "." + evt.MessageId
}
if ee, ok := tInfoMap[evt.Severity]; !ok {
eInfoMap := map[string][]EventInfo{}
eInfoMap[key] = []EventInfo{eInfo}
tInfoMap[evt.Severity] = eInfoMap
} else {
ee[key] = append(ee[key], eInfo)
}
}
AppConfig.TriggerEvents = tInfoMap
for kk, tt := range AppConfig.TriggerEvents {
fmt.Println("Severity: ", kk)
for kkk, ttt := range tt {
fmt.Println("key: ", kkk)
fmt.Printf("event: %+v\n", ttt)
}
}
// Read and parse the REDFISH_SERVERS_COMMON_CONFIG environment variable
redfishServersCommonConfigJSON := os.Getenv("REDFISH_SERVERS_COMMON_CONFIG")
if redfishServersCommonConfigJSON == "" {
log.Println("redfishServersCommonConfigJSON environment variable is not set or is empty")
return AppConfig
}
redfishServersCommonConfig := RedfishServersCommongConfig{}
if err := json.Unmarshal([]byte(redfishServersCommonConfigJSON), &redfishServersCommonConfig); err != nil {
log.Fatalf("Failed to parse REDFISH_SERVERS_COMMON_CONFIG: %v", err)
}
if targetFile == "" {
log.Println("No target file provided")
return AppConfig
}
targetYamlFile, err := os.ReadFile(targetFile)
if err != nil {
log.Fatalf("Failed to read file: %v", targetFile)
}
targets := []target{}
err = yaml.Unmarshal(targetYamlFile, &targets)
if err != nil {
log.Fatalf("Error parsing target file: %v | err: %v", targetFile, err)
}
for _, t := range targets {
log.Println("target: ", t.Targets)
for _, hostName := range t.Targets {
// add this target to Redfish servers
server := RedfishServer{}
bmcHost := fmt.Sprintf(hostName+".%v", redfishServersCommonConfig.HostSuffix)
ips, err := net.LookupIP(bmcHost)
if err != nil || len(ips) == 0 {
log.Printf("[error] Couldn't get the IP for host: %v | ips: %v | err: %v", bmcHost, ips, err)
continue
}
log.Println("IPs: ", ips)
server.IP = fmt.Sprintf("https://%v", ips[0])
server.LoginType = "Session"
server.Username = redfishServersCommonConfig.UserName
server.Password = redfishServersCommonConfig.Password
server.SlurmNode = hostName
AppConfig.RedfishServers = append(AppConfig.RedfishServers, server)
}
}
return AppConfig
}