-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
481 lines (421 loc) · 13.5 KB
/
Copy pathmain.go
File metadata and controls
481 lines (421 loc) · 13.5 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/yaml.v3"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
type PalladiumTestServerModel struct {
Emulator string `json:"Emulator"`
Hardware string `json:"Hardware"`
SystemStatus string `json:"SystemStatus"`
Clusters []struct {
ID int `json:"id"`
NumOfLD int `json:"numOfLD"`
Status string `json:"status"`
LogicDrawer []PalladiumTestServerModelLogicDrawer `json:"LogicDrawer"`
Board []struct {
ID int `json:"id"`
NumberOfDomains int `json:"NumberOfDomains"`
Status string `json:"Status"`
Domains []struct {
ID int `json:"id"`
Owner string `json:"Owner"`
Host string `json:"Host"`
Pid int `json:"pid"`
Design string `json:"Design"`
ElapsedTime string `json:"ElapsedTime"`
} `json:"Domains"`
} `json:"Board"`
} `json:"Clusters"`
AllTargetPods []struct {
RackID int `json:"RackId"`
Targets []struct {
ID string `json:"id"`
Type string `json:"type"`
Length int `json:"length"`
} `json:"Targets"`
} `json:"AllTargetPods"`
AvailableHDSB []struct {
RackID int `json:"RackId"`
Targets []string `json:"Targets"`
} `json:"AvailableHDSB"`
AvailableTpods []struct {
RackID int `json:"RackId"`
Targets []string `json:"Targets"`
} `json:"AvailableTpods"`
UnavailableHDSB []struct {
RackID int `json:"RackId"`
Targets []string `json:"Targets"`
} `json:"UnavailableHDSB"`
UnavailableTpods []struct {
RackID int `json:"RackId"`
Targets []string `json:"Targets"`
} `json:"UnavailableTpods"`
}
type PalladiumTestServerModelLogicDrawer struct {
ID int `json:"id"`
NumberOfDomains int `json:"NumberOfDomains"`
Status string `json:"Status"`
Domains []struct {
ID int `json:"id"`
Owner string `json:"Owner"`
Host string `json:"Host"`
Pid int `json:"pid"`
Design string `json:"Design"`
ElapsedTime string `json:"ElapsedTime"`
} `json:"Domains"`
}
type ZebuBoardModel struct {
Unit string `json:"unit"`
Module string `json:"module"`
System string `json:"system"`
Status string `json:"status"`
Session string `json:"session"`
User string `json:"user"`
Server string `json:"server"`
Type string `json:"type"`
}
type ZebuStatusModel struct {
Boards []ZebuBoardModel `json:"boards"`
Formatboards []struct {
Board string `json:"board"`
Status string `json:"status"`
} `json:"formatboards"`
Boards2 []struct {
Unit string `json:"unit"`
Module string `json:"module"`
System string `json:"system"`
Status string `json:"status"`
} `json:"boards2"`
Formatboards2 []struct {
Board string `json:"board"`
Status string `json:"status"`
} `json:"formatboards2"`
}
type ConfigModel struct {
MetricsInterval int `yaml:"metricsInterval"`
Palladium struct {
Name string `yaml:"name"`
Cmd string `yaml:"cmd"`
Racks []struct {
Name int `yaml:"name"`
ClusterIDs []int `yaml:"clusterIDs"`
BoardIDs []int `yaml:"boardIDs"`
} `yaml:"racks"`
} `yaml:"palladium"`
Zebu struct {
Name string `yaml:"name"`
Cmd string `yaml:"cmd"`
} `yaml:"zebu"`
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// get status data from txt for testing
func ReadPalladiumTestServerDataTxt() []byte {
wd, _ := os.Getwd()
filePath := "palladium-test_server.txt"
dat, _ := os.ReadFile(filepath.Join(wd, filePath))
return dat
}
// get status data from palladium command
func ReadPalladiumTestServerData(c ConfigModel) []byte {
cmd := exec.Command("bash", "-c", "export NO_PLATFORM_REL_CHECK=1;"+c.Palladium.Cmd)
data, _ := cmd.Output()
return data
}
func GetConfig() ConfigModel {
wd, _ := os.Getwd()
filePath := "config.yaml"
dat, _ := os.ReadFile(filepath.Join(wd, filePath))
config := ConfigModel{}
_ = yaml.Unmarshal(dat, &config)
return config
}
func GetPalladiumTestServerData(c ConfigModel, isTesting bool) PalladiumTestServerModel {
m := PalladiumTestServerModel{}
var data []byte
if isTesting {
data = ReadPalladiumTestServerDataTxt()
} else {
data = ReadPalladiumTestServerData(c)
}
_ = json.Unmarshal(data, &m)
return ReformatPalladiumTestServerModel(m)
}
func ReformatPalladiumTestServerModel(m PalladiumTestServerModel) PalladiumTestServerModel {
if m.Clusters[0].LogicDrawer == nil {
for i, _ := range m.Clusters {
m.Clusters[i].LogicDrawer = []PalladiumTestServerModelLogicDrawer{}
for j, _ := range m.Clusters[i].Board {
m.Clusters[i].LogicDrawer = append(m.Clusters[i].LogicDrawer, m.Clusters[i].Board[j])
}
}
}
return m
}
// get status data from txt for testing
func ReadZebuStatusDataTxt() []byte {
wd, _ := os.Getwd()
filePath := "zebu_zRscManager.txt"
dat, _ := os.ReadFile(filepath.Join(wd, filePath))
return dat
}
// get status data from zebu command
func ReadZebuStatusDataData(c ConfigModel) []byte {
cmd := exec.Command(c.Zebu.Cmd)
data, _ := cmd.Output()
return data
}
func GetZebuStatusData(c ConfigModel, isTesting bool) ([]string, []string) {
var data []byte
if isTesting {
data = ReadZebuStatusDataTxt()
} else {
data = ReadZebuStatusDataData(c)
}
lines := strings.Split(string(data), "\n")
var (
statusList []string
statusSmartZICEList []string
)
re := regexp.MustCompile(`[A-Z][0-9]+\.[A-Z][0-9]+\.[A-Z][0-9]+\s.*`)
re_smartZICE := regexp.MustCompile(`[A-Z][0-9]+\.smartZICE\.[A-Z][0-9]+\s.*`)
for _, line := range lines {
if re.MatchString(line) {
statusList = append(statusList, line)
}
if re_smartZICE.MatchString(line) {
statusSmartZICEList = append(statusSmartZICEList, line)
}
}
return statusList, statusSmartZICEList
}
func GetZebuStatusObjData(statusList []string) ZebuStatusModel {
m := ZebuStatusModel{}
for _, status := range statusList {
var (
session string
user string
server string
statusType string
)
statusCols := strings.Split(status, " ")
boardCols := strings.Split(statusCols[0], ".")
if len(statusCols) > 2 {
session = statusCols[2]
user = statusCols[3]
server = statusCols[4]
statusType = statusCols[5]
}
b := ZebuBoardModel{
Unit: boardCols[0],
Module: boardCols[1],
System: boardCols[2],
Status: statusCols[1],
Session: session,
User: user,
Server: server,
Type: statusType,
}
m.Boards = append(m.Boards, b)
}
return m
}
func GetPalladiumAvailableBoards(m PalladiumTestServerModel, requestNum int, c ConfigModel) ([]int, error) {
var boards []int
for clusterIdx, cluster := range m.Clusters {
for _, rack := range c.Palladium.Racks {
if clusterIdx == rack.ClusterIDs[0] {
boards = []int{}
}
}
for _, board := range cluster.LogicDrawer {
for domainIdx, domain := range board.Domains {
// domain free
if domainIdx == 0 && domain.Pid == 0 {
boards = append(boards, board.ID)
break
} else {
boards = []int{}
}
}
if len(boards) == requestNum {
return boards, nil
}
}
}
return []int{}, errors.New("boards not enough")
}
func ExportPalladiumMetrics(c ConfigModel, isTesting bool) {
palladium_status_gauge := promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "palladium_status",
Help: "palladium_status",
},
[]string{
"emulator",
},
)
palladium_cluster_status_gauge := promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "palladium_cluster_status",
Help: "palladium_cluster_status",
},
[]string{
"emulator", "rack", "cluster", "id",
},
)
palladium_cluster_board_status_gauge := promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "palladium_cluster_board_status",
Help: "palladium_cluster_board_status",
},
[]string{
"emulator", "rack", "cluster", "board", "id",
},
)
palladium_cluster_board_domain_status_gauge := promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "palladium_cluster_board_domain_status",
Help: "palladium_cluster_board_domain_status",
},
[]string{
"emulator", "rack", "cluster", "board", "domain", "owner", "host", "pid", "design", "id",
},
)
var rackId int
go func() {
for {
m := GetPalladiumTestServerData(c, isTesting)
palladium_status_gauge.Reset()
palladium_cluster_status_gauge.Reset()
palladium_cluster_board_status_gauge.Reset()
palladium_cluster_board_domain_status_gauge.Reset()
if m.SystemStatus == "ONLINE" {
palladium_status_gauge.With(
prometheus.Labels{"emulator": m.Emulator},
).Set(1)
} else {
palladium_status_gauge.With(
prometheus.Labels{"emulator": m.Emulator},
).Set(0)
}
for clusterIdx, cluster := range m.Clusters {
for _, rack := range c.Palladium.Racks {
if contains(rack.ClusterIDs, cluster.ID) {
rackId = rack.Name
if cluster.Status == "ONLINE" {
palladium_cluster_status_gauge.With(
prometheus.Labels{"emulator": m.Emulator, "rack": strconv.Itoa(rackId), "cluster": strconv.Itoa(clusterIdx), "id": m.Emulator + "-rack" + strconv.Itoa(rackId) + "-cluster" + strconv.Itoa(clusterIdx)},
).Set(1)
} else {
palladium_cluster_status_gauge.With(
prometheus.Labels{"emulator": m.Emulator, "rack": strconv.Itoa(rackId), "cluster": strconv.Itoa(clusterIdx), "id": m.Emulator + "-rack" + strconv.Itoa(rackId) + "-cluster" + strconv.Itoa(clusterIdx)},
).Set(0)
}
for _, board := range cluster.LogicDrawer {
if contains(rack.BoardIDs, board.ID) {
if board.Status == "ONLINE" {
palladium_cluster_board_status_gauge.With(
prometheus.Labels{"emulator": m.Emulator, "rack": strconv.Itoa(rackId), "cluster": strconv.Itoa(clusterIdx), "board": strconv.Itoa(board.ID), "id": m.Emulator + "-rack" + strconv.Itoa(rackId) + "-cluster" + strconv.Itoa(clusterIdx) + "-board" + strconv.Itoa(board.ID)},
).Set(1)
} else {
palladium_cluster_board_status_gauge.With(
prometheus.Labels{"emulator": m.Emulator, "rack": strconv.Itoa(rackId), "cluster": strconv.Itoa(clusterIdx), "board": strconv.Itoa(board.ID), "id": m.Emulator + "-rack" + strconv.Itoa(rackId) + "-cluster" + strconv.Itoa(clusterIdx) + "-board" + strconv.Itoa(board.ID)},
).Set(0)
}
for domainIdx, domain := range board.Domains {
// domain free
if domain.Pid == 0 {
palladium_cluster_board_domain_status_gauge.With(
prometheus.Labels{
"emulator": m.Emulator,
"rack": strconv.Itoa(rackId),
"cluster": strconv.Itoa(clusterIdx),
"board": strconv.Itoa(board.ID),
"domain": strconv.Itoa(domainIdx),
"id": m.Emulator + "-rack" + strconv.Itoa(rackId) + "-cluster" + strconv.Itoa(clusterIdx) + "-board" + strconv.Itoa(board.ID) + "-domain" + strconv.Itoa(domainIdx),
"owner": "NONE",
"host": "UNKNOWN",
"pid": strconv.Itoa(domain.Pid),
"design": "",
},
).Set(0)
} else {
elapsedTime := strings.Split(domain.ElapsedTime, "")
elapsedTime[2] = "h"
elapsedTime[5] = "m"
elapsedTimeStr := strings.Join(elapsedTime, "") + "s"
comp, _ := time.ParseDuration(elapsedTimeStr)
palladium_cluster_board_domain_status_gauge.With(
prometheus.Labels{
"emulator": m.Emulator,
"rack": strconv.Itoa(rackId),
"cluster": strconv.Itoa(clusterIdx),
"board": strconv.Itoa(board.ID),
"domain": strconv.Itoa(domainIdx),
"id": m.Emulator + "-rack" + strconv.Itoa(rackId) + "-cluster" + strconv.Itoa(clusterIdx) + "-board" + strconv.Itoa(board.ID) + "-domain" + strconv.Itoa(domainIdx),
"owner": domain.Owner,
"host": domain.Host,
"pid": strconv.Itoa(domain.Pid),
"design": domain.Design,
},
).Set(comp.Seconds())
}
}
}
}
}
}
}
time.Sleep(time.Duration(c.MetricsInterval) * time.Second)
}
}()
}
func main() {
metricsBoolArg := flag.Bool("metrics", false, "start prometheus metrics exporter server")
metricsPort := flag.Int("port", 27777, "prometheus metrics exporter port")
testFormatDataBoolArg := flag.Bool("test", false, "test format data")
emuTypeArg := flag.String("emu", "", "emu type, palladium or zebu")
emuBoardRequestNumArg := flag.Int("num", 1, "emu board request num")
flag.Parse()
c := GetConfig()
if *metricsBoolArg {
if *emuTypeArg == "palladium" {
ExportPalladiumMetrics(c, *testFormatDataBoolArg)
}
if *emuTypeArg == "zebu" {
}
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":"+strconv.Itoa(*metricsPort), nil)
} else {
if *emuTypeArg == "palladium" {
m := GetPalladiumTestServerData(c, *testFormatDataBoolArg)
boards, _ := GetPalladiumAvailableBoards(m, *emuBoardRequestNumArg, c)
// boards enough
bb, _ := json.Marshal(map[string]interface{}{"boards": boards})
fmt.Println(string(bb))
}
if *emuTypeArg == "zebu" {
l1, _ := GetZebuStatusData(c, *testFormatDataBoolArg)
m := GetZebuStatusObjData(l1)
bb, _ := json.Marshal(map[string]interface{}{"boards": m})
fmt.Println(string(bb))
}
}
}