-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
151 lines (133 loc) · 4.19 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
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
)
const (
LOG_DEBUG = 0
LOG_INFO = 1
)
type PrometheusCachetParameters struct {
loglevel string
httpPort int
sslCert string
sslKey string
cachetRootCA string
cachetSkipVerifySsl bool
cachetURL string
cachetToken string
prometheusToken string
labelName string
squashIncident bool
}
// NewPrometheusCachetParameters is here to fetch all env variable or parameters
func NewPrometheusCachetParameters() *PrometheusCachetParameters {
p := &PrometheusCachetParameters{}
flag.StringVar(&p.prometheusToken, "prometheus_token", "", "token sent by Prometheus in the webhook configuration")
flag.StringVar(&p.cachetURL, "cachethq_url", "http://127.0.0.1/", "where to find CachetHQ")
flag.StringVar(&p.cachetToken, "cachethq_token", "", "token to send to CachetHQ")
flag.StringVar(&p.cachetRootCA, "cachethq_root_ca", "", "Root SSL CA to use against CachetHQ")
flag.BoolVar(&p.cachetSkipVerifySsl, "cachethq_skip_verify_ssl", false, "Dont check the SSL certificate of the https access to CachetHQ")
flag.StringVar(&p.loglevel, "log_level", "info", "log level: [info|debug]")
flag.StringVar(&p.sslCert, "ssl_cert_file", "", "to be used with ssl_key: enable https server")
flag.StringVar(&p.sslKey, "ssl_key_file", "", "to be used with ssl_cert: enable https server")
flag.StringVar(&p.labelName, "label_name", "alertname", "label to look for in Prometheus Alert info")
flag.IntVar(&p.httpPort, "http_port", 8080, "port to listen on")
flag.BoolVar(&p.squashIncident, "squash_incident", false, "do we want to merge down and up event into one incident")
flag.Parse()
// grab env variable (docker compliant)
if os.Getenv("PROMETHEUS_TOKEN") != "" {
p.prometheusToken = os.Getenv("PROMETHEUS_TOKEN")
}
if os.Getenv("CACHETHQ_URL") != "" {
p.cachetURL = os.Getenv("CACHETHQ_URL")
}
if os.Getenv("CACHETHQ_TOKEN") != "" {
p.cachetToken = os.Getenv("CACHETHQ_TOKEN")
}
if os.Getenv("CACHETHQ_ROOT_CA") != "" {
p.cachetRootCA = os.Getenv("CACHETHQ_ROOT_CA")
}
if os.Getenv("CACHETHQ_SKIP_VERIFY_SSL") == "true" {
p.cachetSkipVerifySsl = true
}
if os.Getenv("LOG_LEVEL") != "" {
p.loglevel = os.Getenv("LOG_LEVEL")
}
if os.Getenv("HTTP_PORT") != "" {
if port, err := strconv.Atoi(os.Getenv("HTTP_PORT")); err == nil {
p.httpPort = port
}
}
if os.Getenv("SSL_CERT_FILE") != "" {
p.sslCert = os.Getenv("SSL_CERT_FILE")
}
if os.Getenv("SSL_KEY_FILE") != "" {
p.sslKey = os.Getenv("SSL_KEY_FILE")
}
if os.Getenv("LABEL_NAME") != "" {
p.labelName = os.Getenv("LABEL_NAME")
}
if os.Getenv("SQUASH_INCIDENT") == "true" {
p.squashIncident = true
}
return p
}
type PrometheusCachetConfig struct {
PrometheusToken string
Cachet Cachet
LabelName string
LogLevel int
SquashIncident bool
}
func main() {
parameters := NewPrometheusCachetParameters()
caCertPool := x509.NewCertPool()
if parameters.cachetRootCA != "" {
caCert, err := ioutil.ReadFile(parameters.cachetRootCA)
if err != nil {
log.Fatal(err)
}
caCertPool.AppendCertsFromPEM(caCert)
}
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: parameters.cachetSkipVerifySsl,
},
},
}
config := PrometheusCachetConfig{
PrometheusToken: parameters.prometheusToken,
Cachet: NewCachetImpl(parameters.cachetURL, parameters.cachetToken, httpClient),
LabelName: parameters.labelName,
LogLevel: LOG_INFO,
SquashIncident: parameters.squashIncident,
}
config.LogLevel = LOG_INFO
if parameters.loglevel == "debug" {
config.LogLevel = LOG_DEBUG
}
router := PrepareGinRouter(&config)
server := &http.Server{
Addr: fmt.Sprintf(":%d", parameters.httpPort),
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if parameters.sslCert != "" && parameters.sslKey != "" {
log.Fatal(server.ListenAndServeTLS(parameters.sslCert, parameters.sslKey))
} else {
log.Fatal(server.ListenAndServe())
}
}