-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (62 loc) · 2.29 KB
/
Copy pathmain.go
File metadata and controls
79 lines (62 loc) · 2.29 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
// SPDX-License-Identifier: BSD-3-Clause
// Package main parses the CLI flags and starts the various components
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/NETWAYS/alertmanager-icinga-bridge/internal/api"
"github.com/NETWAYS/alertmanager-icinga-bridge/internal/config"
"github.com/NETWAYS/alertmanager-icinga-bridge/internal/gc"
"github.com/NETWAYS/alertmanager-icinga-bridge/internal/icinga2"
"github.com/alecthomas/kong"
)
var (
// These get filled at build time with the proper values.
version = "development"
commit = "HEAD"
)
// buildVersion creates a string that contains the executable's version
func buildVersion() string {
result := version
if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
return result
}
func main() {
var cli config.CLI
// Create and parse CLI flags
kong.Parse(&cli,
kong.Name("alertmanager-icinga-bridge"),
kong.Description(`The Alertmanager to Icinga bridge can receive alerts from the Prometheus Alertmanager's generic webhook receiver and creates Icinga Services for these alerts.`),
kong.Vars{"version": buildVersion()},
)
// Central logger that we pass to the components
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: config.MapLogLevel(cli.Loglevel)}))
slog.SetDefault(logger)
cfg, errConfig := config.NewConfigFromCLI(&cli)
if errConfig != nil {
logger.Error("Could not load configuration", "component", "main", "error", errConfig.Error())
os.Exit(1)
}
// Create a central context to propagate a shutdown
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
// Create Icinga Client
icingaClient := icinga2.NewClient(cfg, logger)
logger.Info("Starting alertmanager-icinga-bridge", "version", version, "commit", commit, "component", "main")
// Create and start the Service Garbage Collector
garbagecol := gc.NewGarbageCollector(cfg, logger, icingaClient)
garbagecol.Start(ctx)
// Create and start the API Listener
//nolint: noinlineerr
if err := api.NewListener(cfg, logger, icingaClient).Run(ctx); err != nil {
logger.Error("Listener has finished with an error", "component", "main", "error", err.Error())
} else {
logger.Info("Listener has finished", "component", "main")
}
}