forked from MauveSoftware/ilo_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
105 lines (87 loc) · 3.16 KB
/
main.go
File metadata and controls
105 lines (87 loc) · 3.16 KB
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
105
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/MauveSoftware/ilo4_exporter/chassis"
"github.com/MauveSoftware/ilo4_exporter/client"
"github.com/MauveSoftware/ilo4_exporter/system"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"github.com/sirupsen/logrus"
)
const version string = "0.3.0"
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("web.listen-address", ":9545", "Address on which to expose metrics and web interface.")
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
username = flag.String("api.username", "", "Username")
password = flag.String("api.password", "", "Password")
maxConcurrentRequests = flag.Uint("api.max-concurrent-requests", 4, "Maximum number of requests sent against API concurrently")
)
func init() {
flag.Usage = func() {
fmt.Println("Usage: ilo4_exporter [ ... ]\n\nParameters:")
fmt.Println()
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
if *showVersion {
printVersion()
os.Exit(0)
}
startServer()
}
func printVersion() {
fmt.Println("ilo4_exporter")
fmt.Printf("Version: %s\n", version)
fmt.Println("Author(s): Daniel Czerwonk")
fmt.Println("Copyright: 2020, Mauve Mailorder Software GmbH & Co. KG, Licensed under MIT license")
fmt.Println("Metric exporter for HP iLO4")
}
func startServer() {
logrus.Infof("Starting iLO4 exporter (Version: %s)", version)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>iLO4 Exporter (Version ` + version + `)</title></head>
<body>
<h1>iLO4 Exporter by Mauve Mailorder Software</h1>
<h2>Example</h2>
<p>Metrics for host 172.16.0.200</p>
<p><a href="` + *metricsPath + `?host=172.16.0.200">` + r.Host + *metricsPath + `?host=172.16.0.200</a></p>
<h2>More information</h2>
<p><a href="https://github.com/MauveSoftware/ilo4_exporter">github.com/MauveSoftware/ilo4_exporter</a></p>
</body>
</html>`))
})
http.HandleFunc(*metricsPath, errorHandler(handleMetricsRequest))
logrus.Infof("Listening for %s on %s", *metricsPath, *listenAddress)
logrus.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func errorHandler(f func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := f(w, r)
if err != nil {
logrus.Errorln(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
func handleMetricsRequest(w http.ResponseWriter, r *http.Request) error {
host := r.URL.Query().Get("host")
if host == "" {
return fmt.Errorf("no host defined")
}
reg := prometheus.NewRegistry()
cl := client.NewClient(host, *username, *password, client.WithMaxConcurrentRequests(*maxConcurrentRequests), client.WithInsecure())
reg.MustRegister(system.NewCollector(cl))
reg.MustRegister(chassis.NewCollector(cl))
promhttp.HandlerFor(reg, promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(w, r)
return nil
}