/
functions.go
118 lines (100 loc) · 2.79 KB
/
functions.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
package prometheus
import (
"context"
"fmt"
"github.com/kamushadenes/swayit/common"
"github.com/kamushadenes/swayit/config"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
pconfig "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"strconv"
"strings"
"time"
)
const (
okColor = "#2ECC40"
nokColor = "#FF4136"
)
func hasIssue(m *Metric) bool {
if sensitivity, ok := m.Labels["alert_sensitivity"]; ok {
switch sensitivity {
case "high":
if m.Value < 95 {
return true
}
case "medium":
if m.Value < 90 {
return true
}
case "low":
if m.Value < 75 {
return true
}
}
}
return false
}
func getMetrics(value model.Value) []*Metric {
var metrics []*Metric
lines := strings.Split(value.String(), "\n")
for _, l := range lines {
var m Metric
m.Labels = make(map[string]string)
m.Metric = strings.Split(l, "{")[0]
m.Value, _ = strconv.ParseInt(strings.Fields(strings.Split(l, "=>")[1])[0], 10, 64)
lblString := strings.Split(strings.Split(l, "{")[1], "}")[0]
lvs := strings.Split(lblString, ",")
for _, l := range lvs {
l = strings.TrimSpace(l)
fields := strings.Split(l, "=")
m.Labels[fields[0]] = fields[1]
}
metrics = append(metrics, &m)
}
return metrics
}
func run(w *common.WaybarOutput) error {
client, err := api.NewClient(api.Config{
Address: config.SwayItConfig.Prometheus.URL,
RoundTripper: pconfig.NewBasicAuthRoundTripper(config.SwayItConfig.Prometheus.Username, pconfig.Secret(config.SwayItConfig.Prometheus.Password), "", api.DefaultRoundTripper),
})
if err != nil {
return err
}
v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, _, err := v1api.Query(ctx, config.SwayItConfig.Prometheus.Query, time.Now())
if err != nil {
return err
}
metrics := getMetrics(result)
results := make(map[string]map[string]*Metric)
w.Text = fmt.Sprintf("<span color='%s'>\uf058</span>", okColor)
for _, v := range metrics {
if job, ok := v.Labels["job"]; ok {
if instance, ok := v.Labels["instance"]; ok {
if _, ok := results[job]; !ok {
results[job] = make(map[string]*Metric)
}
results[job][instance] = v
if hasIssue(v) {
w.Text = fmt.Sprintf("<span color='%s'>\uf071</span>", nokColor)
}
}
}
}
for job := range results {
w.Tooltip += fmt.Sprintf("<b>%s</b>\n", job)
for instance := range results[job] {
if hasIssue(results[job][instance]) {
w.Tooltip += fmt.Sprintf("\n%s: <b><span color='%s'>%d%%</span></b>", instance, nokColor, results[job][instance].Value)
} else {
w.Tooltip += fmt.Sprintf("\n%s: <span color='%s'>%d%%</span>", instance, okColor, results[job][instance].Value)
}
}
w.Tooltip += "\n\n"
}
return nil
}