-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
80 lines (64 loc) · 1.82 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
package main
import (
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
var (
filename = flag.String("f", "/var/log/auth.log", "ssh log file source")
prometheusPort = flag.String("m", ":2112", "prometheus port")
geolocate = flag.Bool("g", true, "geolocation service enabled/disabled")
debug = flag.Bool("d", false, "debug mode enabled")
)
const apiStackKey = "SSH2PROM_IPSTACK_ACCESSKEY"
func main() {
flag.Parse()
if *debug {
log.SetLevel(log.DebugLevel)
}
// Setup geolocation services
geolocationServices := []Geolocator{ipAPI{}}
apiStackAccessKey := os.Getenv(apiStackKey)
if apiStackAccessKey != "" {
geolocationServices = append(geolocationServices, apiStack{AccessKey: apiStackAccessKey})
}
geolocator := NewGeolocationProvider(geolocationServices...)
locatorOpt := geolocateOption{geolocator}
// Setup parser
parser := NewFailedConnEventParser()
// Setup reader
readerOpts := []ReaderOption{}
if *geolocate {
readerOpts = append(readerOpts, locatorOpt)
}
respChan := make(chan FailedConnEvent, 100)
errorChan := make(chan error, 100)
reader := NewFileReader(*filename, parser, respChan, errorChan, readerOpts...)
go reader.Start()
defer reader.Stop()
defer close(respChan)
defer close(errorChan)
// Setup prometheus reporter
rep := prometheusReporter{}
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(*prometheusPort, nil)
// Setup shutdown from OS signals
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case ev := <-respChan:
rep.Report(ev)
log.Debugf("Reported %v", ev)
case err := <-errorChan:
log.Debugf("Error %v", err)
case _ = <-sigs:
log.Debugf("Shutting down")
os.Exit(0)
}
}
}