-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerts.go
100 lines (86 loc) · 3.58 KB
/
alerts.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
// This sheet gets alerts from default Prometheus instance installed with OpenShift Monitoring Operator
package main
import (
b64 "encoding/base64"
"strings"
"time"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/tidwall/gjson"
)
func createAlertSheet() {
var csvHeader = []string{"Cluster", "Alert", "Severity", "Instance", "StartTime", "EndTime", "Message", "State"}
var csvData []string
var startTime time.Time
var duration time.Duration
var xR int = 1
var sheetName string = "Alerts"
var apiurl string
var secret, token string
var status int
// Initialize Excel sheet
index := xf.NewSheet(sheetName)
xf.SetActiveSheet(index)
xf.SetSheetRow(sheetName, "A1", &csvHeader)
info.Printf("%s: Section started\n", sheetName)
startTime = time.Now()
for i := 0; i < len(cfg.Clusters); i++ {
if !cfg.Clusters[i].Enable {
continue
}
boolCheck, msg := checkClusterAPI(cfg.Clusters[i].BaseURL, cfg.Clusters[i].Token)
if !boolCheck {
erro.Printf("%s: %s: %s\n", sheetName, cfg.Clusters[i].Name, msg)
continue
}
info.Printf("%s: Working on %s\n", sheetName, cfg.Clusters[i].Name)
// Get secret name of prometheus-k8s service account
apiurl = cfg.Clusters[i].BaseURL + "/api/v1/namespaces/openshift-monitoring/serviceaccounts/prometheus-k8s"
body, _ := getRest(apiurl, cfg.Clusters[i].Token)
items := gjson.GetBytes(body, "secrets")
items.ForEach(func(key, value gjson.Result) bool {
x := gjson.Get(value.String(), `name`)
if strings.Contains(x.String(), "token") {
secret = x.String()
return false
}
return true
})
// Get token from secret resource
apiurl = cfg.Clusters[i].BaseURL + "/api/v1/namespaces/openshift-monitoring/secrets/" + secret
body, status = getRest(apiurl, cfg.Clusters[i].Token)
if status == 403 {
warn.Println(yellow("Service Account used for OCinfo doesn't have permission to read secrets. Alerts and Prometheus metrics will not be displayed"))
cfg.Clusters[i].PromToken = ""
continue
}
r := gjson.GetBytes(body, `data.token`)
tb, _ := b64.StdEncoding.DecodeString(r.String())
token = string(tb)
cfg.Clusters[i].PromToken = token
// Get alerts from Prometheus with the token
apiurl = "https://alertmanager-main-openshift-monitoring.apps." + strings.TrimLeft(strings.Split(cfg.Clusters[i].BaseURL, ":")[1], "//api.") + "/api/v1/alerts"
body, _ = getRest(apiurl, cfg.Clusters[i].PromToken)
// Loop in alerts data and export data into Excel file
items = gjson.GetBytes(body, "data")
items.ForEach(func(key, value gjson.Result) bool {
vars := gjson.GetMany(value.String(), `labels.alertname`, `labels.severity`, `labels.instance`, `startsAt`, `endsAt`, `annotations.message`, `status.state`)
csvData = nil
csvData = append(csvData, cfg.Clusters[i].Name) // Cluster Name
csvData = append(csvData, vars[0].String()) // Alert Name
csvData = append(csvData, vars[1].String()) // Alert Severity
csvData = append(csvData, vars[2].String()) // Alert Instance Name
csvData = append(csvData, vars[3].String()) // Alert Start Time
csvData = append(csvData, vars[4].String()) // Alert End Time
csvData = append(csvData, vars[5].String()) // Alert Message
csvData = append(csvData, vars[6].String()) // Alert State
xR++
cell, _ := excelize.CoordinatesToCellName(1, xR)
xf.SetSheetRow(sheetName, cell, &csvData)
return true
})
}
formatTable(sheetName, len(csvHeader))
conditionalFormat5(sheetName, "C", []string{"critical", "warning", "info", "undefined", "none"})
duration = time.Since(startTime)
info.Printf("%s: Section ended in %s\n", sheetName, duration)
}