forked from Lusitaniae/apache_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapache_exporter.go
98 lines (83 loc) · 3.52 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
// 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/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"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
listeningAddress = kingpin.Flag("telemetry.address", "Address on which to expose metrics.").Default(":9117").String()
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()
configFile = kingpin.Flag("web.config", "Path to config yaml file that can enable TLS or authentication.").Default("").String()
gracefulStop = make(chan os.Signal)
)
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,
}
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", "Starting Server: ", "listen_address", *listeningAddress)
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)
level.Info(logger).Log("msg", "Terminate apache-exporter on port:", "listen_address", *listeningAddress)
os.Exit(0)
}()
http.Handle(*metricsEndpoint, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Apache Exporter</title></head>
<body>
<h1>Apache Exporter</h1>
<p><a href='` + *metricsEndpoint + `'>Metrics</a></p>
</body>
</html>`))
})
//log.Fatal(http.ListenAndServe(*listeningAddress, nil))
server := &http.Server{Addr: *listeningAddress}
if err := web.ListenAndServe(server, *configFile, logger); err != nil {
level.Error(logger).Log("msg", "Listening error", "reason", err)
os.Exit(1)
}
}