-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (138 loc) · 4.38 KB
/
main.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
//go:generate protoc --proto_path=. --descriptor_set_out=./alert/alert.protoset --include_imports ./alert/alert.proto
//go:generate protoc --proto_path=. -I/usr/include -I. -Ivendor --go_out=plugins=grpc:. ./alert/alert.proto
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/cyrinux/waybar-livestatus/client"
"github.com/cyrinux/waybar-livestatus/helpers"
"github.com/cyrinux/waybar-livestatus/lql"
"github.com/cyrinux/waybar-livestatus/server"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func formatData(
hAlerts *lql.AlertStruct,
sAlerts *lql.AlertStruct,
config *helpers.CONFIG) (wOutput helpers.WaybarOutput) {
// format
globalClass := "ok"
var text, tooltip, icon string
// test and format
var hostAlertsCount = int(hAlerts.Count)
if hostAlertsCount > 0 {
icon = fmt.Sprintf(" %s ", config.HostPrefix)
text += fmt.Sprintf("%s%d", icon, hAlerts.Count)
tooltip += fmt.Sprintf("<b>Hosts: %d</b>\n\n%s", hAlerts.Count, hAlerts.Items)
globalClass = hAlerts.Class
}
var serviceAlertsCount = int(sAlerts.Count)
if serviceAlertsCount > 0 {
if hostAlertsCount > 0 {
tooltip += "\n\n"
}
tooltip += fmt.Sprintf("<b>Services: %d</b>\n\n%s", sAlerts.Count, sAlerts.Items)
if len(strings.TrimSpace(text)) > 0 {
text += " | "
}
icon = fmt.Sprintf(" %s ", strings.TrimSpace(config.ServicePrefix))
text += fmt.Sprintf("%s%d", icon, sAlerts.Count)
globalClass += sAlerts.Class
}
if len(strings.TrimSpace(text)) == 0 {
icon = fmt.Sprintf(" %s ", strings.TrimSpace(config.OkPrefix))
text = icon
}
// on error
if hAlerts.Error != nil || sAlerts.Error != nil {
icon = fmt.Sprintf(" %s ", strings.TrimSpace(config.ErrorPrefix))
text = icon
tooltip = "Can't connect to the livestatus server"
globalClass = "error"
}
tooltip = strings.TrimRight(tooltip, "\n")
if len(strings.TrimSpace(text)) == 0 {
text = strings.TrimSpace(text)
} else {
text = strings.TrimRight(text, "\n")
}
// waybar output
wOutput = helpers.WaybarOutput{
Text: text,
Tooltip: tooltip,
Class: globalClass,
Count: hostAlertsCount + serviceAlertsCount,
}
return
}
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
// get config
var config = helpers.GetConfig()
// start the client and exit
if config.Client {
err := client.Start(config)
if err != nil {
log.Error().Msgf("%v", err)
os.Exit(1)
}
os.Exit(0)
}
// start cron based auto-pause system
helpers.AutoPause(config)
// create channels and start goroutines
hostAlerts, serviceAlerts := make(chan lql.AlertStruct), make(chan lql.AlertStruct)
notificationsChannel, serverChannel := make(chan *helpers.Alert, 10), make(chan []*helpers.Alert)
hAlerts, sAlerts := new(lql.AlertStruct), new(lql.AlertStruct)
if config.HostsOnly && !config.ServicesOnly || (!config.ServicesOnly && !config.HostsOnly) {
go lql.GetItems("hosts", config, hostAlerts, notificationsChannel, serverChannel)
}
if (config.ServicesOnly && !config.HostsOnly) || (!config.ServicesOnly && !config.HostsOnly) {
go lql.GetItems("services", config, serviceAlerts, notificationsChannel, serverChannel)
}
// Handle Signal
go helpers.SignalHandler()
// notification channel
if config.Popup {
go helpers.SendNotification(notificationsChannel, config)
}
// keep version of previous output
var wOutput, previousWOutput helpers.WaybarOutput
// Start gRPC server
go server.GRPCListen(serverChannel, config)
log.Debug().Msgf("Refresh rate: %d seconds, long refresh: %d seconds", config.Refresh, config.LongRefresh)
// main loop
for {
select {
case *sAlerts = <-serviceAlerts:
log.Debug().Msgf("received %d service alerts", sAlerts.Count)
case *hAlerts = <-hostAlerts:
log.Debug().Msgf("received %d hosts alerts", hAlerts.Count)
default:
// nothing receive, sleep a little
time.Sleep(500 * time.Millisecond)
}
// save previous waybar output
previousWOutput = wOutput
// new waybar content
wOutput = formatData(hAlerts, sAlerts, config)
if helpers.Pause {
wOutput.Class = "pause"
wOutput.Text = fmt.Sprintf(" %s ", config.PausePrefix)
wOutput.Tooltip = "Paused, click to resume "
wOutput.Count = 0
}
// convert in JSON
jsonOutput, err := json.Marshal(wOutput)
if err != nil {
log.Error().Msgf("%v", err)
}
// Finally print the expected waybar JSON
if wOutput != previousWOutput {
fmt.Println(string(jsonOutput))
}
}
}