forked from promhippie/jenkins_exporter
/
main.go
78 lines (57 loc) · 2.24 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/abarthol/jenkins_exporter/exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
_ "net/http/pprof"
)
var (
// showVersion is a flag to display the current version.
showVersion = flag.Bool("version", false, "Print version information")
// listenAddress defines the local address binding for the server.
listenAddress = flag.String("web.listen-address", ":9103", "Address to listen on for web interface and telemetry")
// metricsPath defines the path to access the metrics.
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path to expose metrics of the exporter")
// address defines the URL to access the Jenkins instance.
address = flag.String("jenkins.address", "", "Address where to reach Jenkins")
// username defines the username for the Jenkins authentication.
username = flag.String("jenkins.username", "", "Username to authenticate on Jenkins")
// password defines the password for the Jenkins authentication.
password = flag.String("jenkins.password", "", "Password to authenticate on Jenkins")
)
// init registers the collector version.
func init() {
prometheus.MustRegister(version.NewCollector("jenkins_exporter"))
}
// main simply initializes this tool.
func main() {
flag.Parse()
if *showVersion {
fmt.Fprintln(os.Stdout, version.Print("jenkins_exporter"))
os.Exit(0)
}
if *address == "" {
fmt.Fprintln(os.Stderr, "Please provide an address for Jenkins")
os.Exit(1)
}
log.Infoln("Starting Jenkins exporter", version.Info())
log.Infoln("Build context", version.BuildContext())
e := exporter.NewExporter(*address, *username, *password)
prometheus.MustRegister(e)
prometheus.Unregister(prometheus.NewGoCollector())
prometheus.Unregister(prometheus.NewProcessCollector(os.Getpid(), ""))
http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, *metricsPath, http.StatusMovedPermanently)
})
log.Infof("Listening on %s", *listenAddress)
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
log.Fatal(err)
}
}