-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (59 loc) · 2.26 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
package main
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
var (
url = kingpin.Flag("jenkins.url", "Jenkins server url.").Default("http://localhost:8080").String()
username = kingpin.Flag("jenkins.username", "Jenkins server username.").Default("admin").String()
password = kingpin.Flag("jenkins.password", "Jenkins server token/password.").Default("admin").String()
timeout = kingpin.Flag("jenkins.timeout", "Jenkins server connect timeout.").Default("30s").Duration()
runInterval = kingpin.Flag("run.interval", "Exporter collect metrics interval").Default("5m").Duration()
listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9118").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
)
log.AddFlags(kingpin.CommandLine)
kingpin.Version(version.Print("jenkins_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
log.Infoln("Starting jenkins_exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
exporter := NewExporter(*url, *username, *password, *runInterval, *timeout)
prometheus.MustRegister(exporter)
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Jenkins Exporter</title></head>
<body>
<h1>Jenkins Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
log.Infoln("Starting HTTP server on", *listenAddress)
srv := http.Server{
Addr: *listenAddress,
Handler: http.DefaultServeMux,
}
exporter.Run()
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal("http server failure")
}
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
rsig := <-sig
log.Infoln("Received signal %s, going to shutdown", rsig.String())
exporter.Stop()
srv.Shutdown(context.Background())
}