Skip to content

Commit 0993711

Browse files
author
Mehdi Yedes
committed
feat: add http endpoint for reloading config
The added endpoint `/-/reload` triggers a reload of the redfish_exporter configuration when receiving a PUT or POST request.
1 parent 4ecf7f5 commit 0993711

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,17 @@ curl http://<redfish_exporter host>:9610/redfish?target=10.36.48.24
5858
```
5959
or by pointing your favourite browser at this URL.
6060
61-
## prometheus configuration
61+
## Reloading Configuration
62+
```
63+
PUT /-/reload
64+
POST /-/reload
65+
```
66+
The `/-/reload` endpoint triggers a reload of the redfish_exporter configuration.
67+
500 will be returned when the reload fails.
68+
69+
Alternatively, a configuration reload can be triggered by sending `SIGHUP` to the redfish_exporter process as well.
70+
71+
## Prometheus Configuration
6272
6373
You can then setup [Prometheus][3] to scrape the target using
6474
something like this in your Prometheus configuration files:

main.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"io"
45
"net/http"
56
"os"
67
"os/signal"
@@ -45,6 +46,25 @@ func init() {
4546
rootLoggerCtx.Infof("version %s, build reversion %s, build branch %s, build at %s on host %s", Version, BuildRevision, BuildBranch, BuildTime, hostname)
4647
}
4748

49+
func reloadHandler(configLoggerCtx *alog.Entry) http.HandlerFunc {
50+
return func(w http.ResponseWriter, r *http.Request) {
51+
if r.Method == "POST" || r.Method == "PUT" {
52+
configLoggerCtx.Info("Triggered configuration reload from /-/reload HTTP endpoint")
53+
err := sc.ReloadConfig(*configFile)
54+
if err != nil {
55+
configLoggerCtx.WithError(err).Error("failed to reload config file")
56+
http.Error(w, "failed to reload config file", http.StatusInternalServerError)
57+
}
58+
configLoggerCtx.WithField("operation", "sc.ReloadConfig").Info("config file reloaded")
59+
60+
w.WriteHeader(http.StatusOK)
61+
io.WriteString(w, "Configuration reloaded successfully!")
62+
} else {
63+
http.Error(w, "Only PUT and POST methods are allowed", http.StatusBadRequest)
64+
}
65+
}
66+
}
67+
4868
// define new http handleer
4969
func metricsHandler() http.HandlerFunc {
5070
return func(w http.ResponseWriter, r *http.Request) {
@@ -136,7 +156,8 @@ func main() {
136156
}
137157
}()
138158

139-
http.Handle("/redfish", metricsHandler()) // Regular metrics endpoint for local Redfish metrics.
159+
http.Handle("/redfish", metricsHandler()) // Regular metrics endpoint for local Redfish metrics.
160+
http.Handle("/-/reload", reloadHandler(configLoggerCtx)) // HTTP endpoint for triggering configuration reload
140161
http.Handle("/metrics", promhttp.Handler())
141162

142163
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)