-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (76 loc) · 2.41 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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"runtime"
log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)
const (
handlerWebSrvPort = "9999"
version = "0.0.1"
sapHandlerName = "/hooks-sap"
defautlHandlerName = "/hooks-default"
)
var (
// the time the binary was built
buildDate string
// global --help flag
helpFlag *bool
// global --version flag
versionFlag *bool
)
func showVersion() {
fmt.Printf("version %s\nbuilt with %s %s/%s %s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH, buildDate)
os.Exit(0)
}
func init() {
flag.StringP("port", "p", handlerWebSrvPort, "The port number to listen on for http alerts event")
flag.StringP("alertserver", "a", "", "prometheus alertmanager server IP")
flag.StringP("log-level", "l", "info", "log level of the handler")
flag.StringP("config", "c", "", "The path to a custom configuration file. NOTE: it must be in yaml format.")
flag.CommandLine.SortFlags = false
helpFlag = flag.BoolP("help", "h", false, "show this help message")
versionFlag = flag.BoolP("version", "v", false, "show version and build information")
}
// default handler. this is where the alerts witch doesn't match anything goes
func defaultHandler(_ http.ResponseWriter, req *http.Request) {
// read body json from Prometheus alertmanager
decoder := json.NewDecoder(req.Body)
var alerts PromAlert
err := decoder.Decode(&alerts)
if err != nil {
log.Warnln(err)
}
// the default handler for moment does nothing
}
func main() {
flag.Parse()
switch {
case *helpFlag:
flag.Usage()
os.Exit(0)
case *versionFlag:
fmt.Printf("version %s\nbuilt with %s %s/%s %s\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH, buildDate)
os.Exit(0)
}
configFile, err := Config(flag.CommandLine)
if err != nil {
log.Fatalf("Could not initialize config: %s", err)
}
portWebHandler := configFile.GetString("port")
log.Infof("[SETUP]: starting handler on port: %s", portWebHandler)
log.Infof("[SETUP]: logLevel set to \"%s\" level", configFile.GetString("log-level"))
// register the various handler
h := new(HanaDiskFull)
// make sure we run only 1 handler until it finish.
http.HandleFunc(sapHandlerName, h.handlerHanaDiskFull)
http.HandleFunc(defautlHandlerName, defaultHandler)
// TODO: serve webserver (future https)
err = http.ListenAndServe(fmt.Sprintf(":%s", portWebHandler), nil)
if err != nil {
log.Fatalln(err)
}
}