-
Notifications
You must be signed in to change notification settings - Fork 146
/
apache_exporter.go
103 lines (90 loc) · 3.38 KB
/
apache_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
102
103
// Copyright (c) 2015 neezgee
//
// Licensed under the MIT license: https://opensource.org/licenses/MIT
// Permission is granted to use, copy, modify, and redistribute the work.
// Full license information available in the project LICENSE file.
//
package main
import (
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/Lusitaniae/apache_exporter/collector"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
"github.com/prometheus/exporter-toolkit/web/kingpinflag"
)
var (
metricsEndpoint = kingpin.Flag("telemetry.endpoint", "Path under which to expose metrics.").Default("/metrics").String()
scrapeURI = kingpin.Flag("scrape_uri", "URI to apache stub status page.").Default("http://localhost/server-status/?auto").String()
hostOverride = kingpin.Flag("host_override", "Override for HTTP Host header; empty string for no override.").Default("").String()
insecure = kingpin.Flag("insecure", "Ignore server certificate if using https.").Bool()
toolkitFlags = kingpinflag.AddFlags(kingpin.CommandLine, ":9117")
gracefulStop = make(chan os.Signal, 1)
customHeaders = kingpin.Flag("custom_headers", "Adds custom headers to the collector.").StringMap()
)
func main() {
promlogConfig := &promlog.Config{}
// Parse flags
flag.AddFlags(kingpin.CommandLine, promlogConfig)
kingpin.HelpFlag.Short('h')
kingpin.Version(version.Print("apache_exporter"))
kingpin.Parse()
logger := promlog.New(promlogConfig)
// listen to termination signals from the OS
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
signal.Notify(gracefulStop, syscall.SIGHUP)
signal.Notify(gracefulStop, syscall.SIGQUIT)
config := &collector.Config{
ScrapeURI: *scrapeURI,
HostOverride: *hostOverride,
Insecure: *insecure,
CustomHeaders: *customHeaders,
}
exporter := collector.NewExporter(logger, config)
prometheus.MustRegister(exporter)
prometheus.MustRegister(version.NewCollector("apache_exporter"))
level.Info(logger).Log("msg", "Starting apache_exporter", "version", version.Info())
level.Info(logger).Log("msg", "Build context", "build", version.BuildContext())
level.Info(logger).Log("msg", "Collect from: ", "scrape_uri", *scrapeURI)
// listener for the termination signals from the OS
go func() {
level.Info(logger).Log("msg", "listening and wait for graceful stop")
sig := <-gracefulStop
level.Info(logger).Log("msg", "caught sig: %+v. Wait 2 seconds...", "sig", sig)
time.Sleep(2 * time.Second)
os.Exit(0)
}()
http.Handle(*metricsEndpoint, promhttp.Handler())
landingConfig := web.LandingConfig{
Name: "Apache Exporter",
Description: "Prometheus exporter for Apache HTTP server metrics",
Version: version.Info(),
Links: []web.LandingLinks{
{
Address: *metricsEndpoint,
Text: "Metrics",
},
},
}
landingPage, err := web.NewLandingPage(landingConfig)
if err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
http.Handle("/", landingPage)
server := &http.Server{}
if err := web.ListenAndServe(server, toolkitFlags, logger); err != nil {
level.Error(logger).Log("err", err)
os.Exit(1)
}
}