-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
159 lines (130 loc) · 4.79 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
alertscollector "github.com/appuio/alerts_exporter/internal/alerts_collector"
"github.com/appuio/alerts_exporter/internal/healthcheck"
"github.com/appuio/alerts_exporter/internal/saauth"
openapiclient "github.com/go-openapi/runtime/client"
alertmanagerclient "github.com/prometheus/alertmanager/api/v2/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var listenAddr, healthListenAddr string
var host string
var withInhibited, withSilenced, withUnprocessed, withActive bool
var filters stringSliceFlag
var tlsCert, tlsCertKey, tlsCaCert, tlsServerName string
var tlsInsecure bool
var useTLS bool
var bearerToken string
var k8sBearerTokenAuth bool
func main() {
flag.StringVar(&listenAddr, "listen-addr", ":8080", "The addr to listen on")
flag.StringVar(&healthListenAddr, "health-listen-addr", ":8081", "The addr to listen on for the health check endpoint.")
flag.StringVar(&host, "host", "localhost:9093", "The host of the Alertmanager")
flag.BoolVar(&useTLS, "tls", false, "Use TLS when connecting to Alertmanager")
flag.StringVar(&tlsCert, "tls-cert", "", "Path to client certificate for TLS authentication")
flag.StringVar(&tlsCertKey, "tls-cert-key", "", "Path to client certificate key for TLS authentication")
flag.StringVar(&tlsCaCert, "tls-ca-cert", "", "Path to CA certificate. System certificates are used if not provided.")
flag.StringVar(&tlsServerName, "tls-server-name", "", "Server name to verify the hostname on the returned certificates. It must be a substring of either the Common Name or a Subject Alternative Name in the certificate. If empty, the hostname given in the address parameter is used.")
flag.BoolVar(&tlsInsecure, "insecure", false, "Disable TLS host verification")
flag.StringVar(&bearerToken, "bearer-token", "", "Bearer token to use for authentication")
flag.BoolVar(&k8sBearerTokenAuth, "k8s-bearer-token-auth", false, "Use Kubernetes service account bearer token for authentication")
flag.BoolVar(&withActive, "with-active", true, "Query for active alerts")
flag.BoolVar(&withInhibited, "with-inhibited", true, "Query for inhibited alerts")
flag.BoolVar(&withSilenced, "with-silenced", true, "Query for silenced alerts")
flag.BoolVar(&withUnprocessed, "with-unprocessed", true, "Query for unprocessed alerts")
flag.Var(&filters, "filter", "A list of Alertmanager matchers to filter alerts by. Multiple matchers are ANDed.\nUsage example: '--filter slo=\"true\" --filter severity=\"critical\"'")
flag.Parse()
opts := openapiclient.TLSClientOptions{
Certificate: tlsCert,
Key: tlsCertKey,
CA: tlsCaCert,
ServerName: tlsServerName,
}
if tlsInsecure {
opts.InsecureSkipVerify = true
opts.ServerName = ""
}
var schemes []string
if useTLS {
schemes = []string{"https"}
}
hc, err := openapiclient.TLSClient(opts)
if err != nil {
log.Fatal(err)
}
rt := openapiclient.NewWithClient(host, alertmanagerclient.DefaultBasePath, schemes, hc)
if bearerToken != "" {
rt.DefaultAuthentication = openapiclient.BearerToken(bearerToken)
}
if k8sBearerTokenAuth {
sa, err := saauth.NewServiceAccountAuthInfoWriter("", 0)
if err != nil {
log.Fatal(err)
}
defer sa.Stop()
rt.DefaultAuthentication = sa
}
ac := alertmanagerclient.New(rt, nil)
reg := prometheus.NewRegistry()
reg.MustRegister(&alertscollector.AlertsCollector{
AlertService: ac.Alert,
WithActive: &withActive,
WithSilenced: &withSilenced,
WithInhibited: &withInhibited,
WithUnprocessed: &withUnprocessed,
Filters: filters,
})
msm := http.NewServeMux()
msm.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
hsm := http.NewServeMux()
hsm.HandleFunc("/healthz", healthcheck.HealthCheck{GeneralService: ac.General}.HandleHealthz)
ms := &http.Server{
Addr: listenAddr,
Handler: msm,
}
hs := &http.Server{
Addr: healthListenAddr,
Handler: hsm,
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
go func() {
defer cancel()
log.Printf("Metrics: Listening on `%s`", listenAddr)
log.Println("Metrics:", ms.ListenAndServe())
}()
go func() {
defer cancel()
log.Printf("Healthz: Listening on `%s`", healthListenAddr)
log.Println("Healthz:", hs.ListenAndServe())
}()
var waitShutdown sync.WaitGroup
waitShutdown.Add(2)
go func() {
defer waitShutdown.Done()
<-ctx.Done()
ms.Shutdown(context.Background())
}()
go func() {
defer waitShutdown.Done()
<-ctx.Done()
hs.Shutdown(context.Background())
}()
waitShutdown.Wait()
}
type stringSliceFlag []string
func (f stringSliceFlag) String() string {
return fmt.Sprint([]string(f))
}
func (f *stringSliceFlag) Set(value string) error {
*f = append(*f, value)
return nil
}