-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (45 loc) · 1.41 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
package main
import (
"fmt"
"log"
"os"
"sync"
"github.com/sonlis/lxc-bootdisdk-usage-alerter/internal/alerting"
"github.com/sonlis/lxc-bootdisdk-usage-alerter/internal/lxc"
)
func main() {
var gotifyToken, gotifyHost string
if gotifyToken = os.Getenv("GOTIFY_TOKEN"); gotifyToken == "" {
log.Fatalf("GOTIFY_TOKEN env variable not set")
}
if gotifyHost = os.Getenv("GOTIFY_HOST"); gotifyHost == "" {
log.Fatalf("GOTIFY_HOST env variable not set")
}
lxcs, err := lxc.List()
if err != nil {
log.Fatalf("Error listing lxcs: %v", err)
}
if lxcs == nil {
log.Printf("No lxc running")
os.Exit(0)
}
var wg sync.WaitGroup
for _, lxContainer := range lxcs {
wg.Add(1)
go func(lxContainer lxc.Lxc) {
defer wg.Done()
usage, err := lxContainer.GetStorageUsage()
if err != nil {
log.Printf("Error getting storage usage for lxc %s: %v", lxContainer.Name, err)
}
if usage > 89 {
message := fmt.Sprintf("Used storage is at %f%%", usage)
err = alerting.AlertServiceUnealthy(lxContainer.Name, message, gotifyToken, gotifyHost)
if err != nil {
log.Fatalf("Error sending alert for lxc %s: %v", lxContainer.Name, err)
}
}
}(lxContainer)
}
wg.Wait()
}