forked from kumina/postfix_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
104 lines (90 loc) · 2.56 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
package main
import (
"context"
"log"
"net/http"
"os"
_ "embed"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promslog"
"github.com/prometheus/exporter-toolkit/web"
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
)
var (
version string
commit string
date string
builtBy string
//go:embed VERSION
fallbackVersion string
)
func main() {
var (
ctx = context.Background()
app = kingpin.New("postfix_exporter", "Prometheus metrics exporter for postfix")
versionFlag = app.Flag("version", "Print version information").Bool()
toolkitFlags = kingpinflag.AddFlags(app, ":9154")
metricsPath = app.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
postfixShowqPath = app.Flag("postfix.showq_path", "Path at which Postfix places its showq socket.").Default("/var/spool/postfix/public/showq").String()
logUnsupportedLines = app.Flag("log.unsupported", "Log all unsupported lines.").Bool()
)
InitLogSourceFactories(app)
kingpin.MustParse(app.Parse(os.Args[1:]))
if version == "" {
version = fallbackVersion
}
if *versionFlag {
os.Stdout.WriteString(version)
os.Exit(0)
}
versionString := "postfix_exporter " + version
if commit != "" {
versionString += " (" + commit + ")"
}
if date != "" {
versionString += " built on " + date
}
if builtBy != "" {
versionString += " by: " + builtBy
}
log.Print(versionString)
logSrc, err := NewLogSourceFromFactories(ctx)
if err != nil {
log.Fatalf("Error opening log source: %s", err)
}
defer logSrc.Close()
exporter, err := NewPostfixExporter(
*postfixShowqPath,
logSrc,
*logUnsupportedLines,
)
if err != nil {
log.Fatalf("Failed to create PostfixExporter: %s", err)
}
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, promhttp.Handler())
lc := web.LandingConfig{
Name: "Postfix Exporter",
Description: "Prometheus exporter for postfix metrics",
Version: versionString,
Links: []web.LandingLinks{
{
Address: *metricsPath,
Text: "Metrics",
},
},
}
lp, err := web.NewLandingPage(lc)
if err != nil {
log.Fatalf("Failed to create landing page: %s", err)
}
http.Handle("/", lp)
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()
go exporter.StartMetricCollection(ctx)
server := &http.Server{}
logger := promslog.New(&promslog.Config{})
log.Fatal(web.ListenAndServe(server, toolkitFlags, logger))
}