-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
118 lines (100 loc) · 2.72 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
package main
import (
"fmt"
"net/http"
"os"
"path"
"runtime"
"strings"
elasticsearch "github.com/elastic/go-elasticsearch/v7"
"github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/webdevops/alertmanager2es/config"
)
const (
Author = "webdevops.io"
)
var (
argparser *flags.Parser
opts config.Opts
// Git version information
gitCommit = "<unknown>"
gitTag = "<unknown>"
)
func main() {
initArgparser()
log.Infof("starting alertmanager2es v%s (%s; %s; by %v)", gitTag, gitCommit, runtime.Version(), Author)
log.Info(string(opts.GetJson()))
log.Infof("init exporter")
exporter := &AlertmanagerElasticsearchExporter{}
exporter.Init()
cfg := elasticsearch.Config{
Addresses: opts.Elasticsearch.Addresses,
Username: opts.Elasticsearch.Username,
Password: opts.Elasticsearch.Password,
APIKey: opts.Elasticsearch.ApiKey,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
exporter.ConnectElasticsearch(cfg, opts.Elasticsearch.Index)
// daemon mode
log.Infof("starting http server on %s", opts.ServerBind)
startHttpServer(exporter)
}
// init argparser and parse/validate arguments
func initArgparser() {
argparser = flags.NewParser(&opts, flags.Default)
_, err := argparser.Parse()
// check if there is an parse error
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println()
argparser.WriteHelp(os.Stdout)
os.Exit(1)
}
}
// verbose level
if opts.Logger.Verbose {
log.SetLevel(log.DebugLevel)
}
// debug level
if opts.Logger.Debug {
log.SetReportCaller(true)
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
s := strings.Split(f.Function, ".")
funcName := s[len(s)-1]
return funcName, fmt.Sprintf("%s:%d", path.Base(f.File), f.Line)
},
})
}
// json log format
if opts.Logger.LogJson {
log.SetReportCaller(true)
log.SetFormatter(&log.JSONFormatter{
DisableTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
s := strings.Split(f.Function, ".")
funcName := s[len(s)-1]
return funcName, fmt.Sprintf("%s:%d", path.Base(f.File), f.Line)
},
})
}
}
// start and handle prometheus handler
func startHttpServer(exporter *AlertmanagerElasticsearchExporter) {
// healthz
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, "Ok"); err != nil {
log.Error(err)
}
})
http.HandleFunc("/webhook", exporter.HttpHandler)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(opts.ServerBind, nil))
}