-
Notifications
You must be signed in to change notification settings - Fork 4
/
authlog_exporter.go
101 lines (97 loc) · 2.95 KB
/
authlog_exporter.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
package main
import (
"os"
"os/signal"
"path/filepath"
"syscall"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
"github.com/woblerr/authlog_exporter/promexporter"
)
var version = "unknown"
func main() {
var (
authlogPath = kingpin.Flag(
"auth.log",
"Path to auth.log.",
).Default("/var/log/auth.log").String()
webPath = kingpin.Flag(
"web.endpoint",
"Endpoint used for metrics.",
).Default("/metrics").String()
webAdditionalToolkitFlags = kingpinflag.AddFlags(kingpin.CommandLine, ":9991")
geodbPath = kingpin.Flag(
"geo.db",
"Path to geoIP database file.",
).Default("").String()
geodbLang = kingpin.Flag(
"geo.lang",
"Output language format.",
).Default("en").String()
geodbTimeout = kingpin.Flag(
"geo.timeout",
"Timeout in seconds for waiting response from geoIP database API.",
).Default("2").Int()
geodbType = kingpin.Flag(
"geo.type",
"Type of geoIP database: db, url.",
).Default("").String()
geodbURL = kingpin.Flag(
"geo.url",
"URL for geoIP database API.",
).Default("https://reallyfreegeoip.org/json/").String()
metricHideIP = kingpin.Flag(
"metric.hideip",
"Set this flag to hide IPs in the output and therefore drastically reduce the amount of metrics published.",
).Bool()
metricHideUser = kingpin.Flag(
"metric.hideuser",
"Set this flag to hide user accounts in the output and therefore drastically reduce the amount of metrics published.",
).Bool()
)
// Set logger config.
promlogConfig := &promlog.Config{}
// Add flags log.level and log.format from promlog package.
flag.AddFlags(kingpin.CommandLine, promlogConfig)
// Add short help flag.
kingpin.HelpFlag.Short('h')
// Load command line arguments.
kingpin.Parse()
// Setup signal catching.
sigs := make(chan os.Signal, 1)
// Catch listed signals.
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// Set logger.
logger := promlog.New(promlogConfig)
// Method invoked upon seeing signal.
go func(logger log.Logger) {
s := <-sigs
level.Warn(logger).Log(
"msg", "Stopping exporter",
"name", filepath.Base(os.Args[0]),
"signal", s)
os.Exit(1)
}(logger)
level.Info(logger).Log(
"msg", "Starting exporter",
"name", filepath.Base(os.Args[0]),
"version", version,
)
// Setup parameters for exporter.
promexporter.SetExporterParams(*authlogPath, *webPath, *webAdditionalToolkitFlags, *metricHideIP, *metricHideUser)
level.Info(logger).Log(
"mgs", "Use exporter parameters",
"authlog", *authlogPath,
"endpoint", *webPath,
"config.file", *webAdditionalToolkitFlags.WebConfigFile,
"hideip", *metricHideIP,
"hideuser", *metricHideUser,
)
promexporter.SetGeodbPath(*geodbType, *geodbPath, *geodbLang, *geodbURL, *geodbTimeout, logger)
// Start exporter.
promexporter.Start(logger)
}