forked from nzin/prometheus-cachethq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (81 loc) · 2.36 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
)
const (
LOG_DEBUG = 0
LOG_INFO = 1
)
type PrometheusCachetConfig struct {
PrometheusToken string
CachetURL string
CachetToken string
LabelName string
LogLevel int
}
func main() {
var config PrometheusCachetConfig
var loglevel string
var httpPort int
var sslCert string
var sslKey string
flag.StringVar(&config.PrometheusToken, "prometheus_token", "", "token sent by Prometheus in the webhook configuration")
flag.StringVar(&config.CachetURL, "cachethq_url", "http://127.0.0.1/", "where to find CachetHQ")
flag.StringVar(&config.CachetToken, "cachethq_token", "", "token to send to CachetHQ")
flag.StringVar(&loglevel, "log_level", "info", "log level: [info|debug]")
flag.StringVar(&sslCert, "ssl_cert_file", "", "to be used with ssl_key: enable https server")
flag.StringVar(&sslKey, "ssl_key_file", "", "to be used with ssl_cert: enable https server")
flag.StringVar(&config.LabelName, "label_name", "alertname", "label to look for in Prometheus Alert info")
flag.IntVar(&httpPort, "http_port", 8080, "port to listen on")
flag.Parse()
// grab env variable (docker compliant)
if os.Getenv("PROMETHEUS_TOKEN") != "" {
config.PrometheusToken = os.Getenv("PROMETHEUS_TOKEN")
}
if os.Getenv("CACHETHQ_URL") != "" {
config.CachetURL = os.Getenv("CACHETHQ_URL")
}
if os.Getenv("CACHETHQ_TOKEN") != "" {
config.CachetToken = os.Getenv("CACHETHQ_TOKEN")
}
if os.Getenv("LOG_LEVEL") != "" {
loglevel = os.Getenv("LOG_LEVEL")
}
if os.Getenv("HTTP_PORT") != "" {
if port, err := strconv.Atoi(os.Getenv("HTTP_PORT")); err == nil {
httpPort = port
}
}
if os.Getenv("SSL_CERT_FILE") != "" {
sslCert = os.Getenv("SSL_CERT_FILE")
}
if os.Getenv("SSL_KEY_FILE") != "" {
sslKey = os.Getenv("SSL_KEY_FILE")
}
if os.Getenv("LABEL_NAME") != "" {
config.LabelName = os.Getenv("LABEL_NAME")
}
config.LogLevel = LOG_INFO
if loglevel == "debug" {
config.LogLevel = LOG_DEBUG
}
router := PrepareGinRouter(&config)
server := &http.Server{
Addr: fmt.Sprintf(":%d", httpPort),
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if sslCert != "" && sslKey != "" {
log.Fatal(server.ListenAndServeTLS(sslCert, sslKey))
} else {
log.Fatal(server.ListenAndServe())
}
}