merge: add-health-endpoint-to-exporter

* Merge branch 'add-health-endpoint-to-exporter' into 'main'
* See merge request hectorjsmith/fail2ban-prometheus-exporter!110
* Merged by: Hector <hector@hjs.dev>
This commit is contained in:
Hector 2023-09-09 06:15:23 +00:00
commit 1333fde5ca
No known key found for this signature in database
6 changed files with 38 additions and 4 deletions

View file

@ -6,10 +6,10 @@ WORKDIR /app
# Copy compiled binary to release image # Copy compiled binary to release image
# (must build the binary before running docker build) # (must build the binary before running docker build)
COPY fail2ban_exporter /app/fail2ban_exporter COPY fail2ban_exporter /app/fail2ban_exporter
COPY health /app/health
# Setup a healthcheck # Setup a healthcheck
COPY health /app/health
RUN apk add curl RUN apk add curl
HEALTHCHECK --interval=10s --timeout=4s CMD /app/health HEALTHCHECK --interval=10s --timeout=4s --retries=3 CMD /app/health
ENTRYPOINT ["/app/fail2ban_exporter"] ENTRYPOINT ["/app/fail2ban_exporter"]

View file

@ -61,6 +61,22 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
c.collectErrorCountMetric(ch) c.collectErrorCountMetric(ch)
} }
func (c *Collector) IsHealthy() bool {
s, err := socket.ConnectToSocket(c.socketPath)
if err != nil {
log.Printf("error opening socket: %v", err)
c.socketConnectionErrorCount++
return false
}
pingSuccess, err := s.Ping()
if err != nil {
log.Printf("error pinging fail2ban server: %v", err)
c.socketRequestErrorCount++
return false
}
return pingSuccess
}
func printFail2BanServerVersion(socketPath string) { func printFail2BanServerVersion(socketPath string) {
s, err := socket.ConnectToSocket(socketPath) s, err := socket.ConnectToSocket(socketPath)
if err != nil { if err != nil {

View file

@ -44,7 +44,7 @@ func main() {
prometheus.MustRegister(textFileCollector) prometheus.MustRegister(textFileCollector)
if !appSettings.DryRunMode { if !appSettings.DryRunMode {
svrErr := server.StartServer(appSettings, textFileCollector) svrErr := server.StartServer(appSettings, f2bCollector, textFileCollector)
err := <-svrErr err := <-svrErr
log.Fatal(err) log.Fatal(err)
} else { } else {

2
health
View file

@ -5,4 +5,4 @@ if [ ! -z $F2B_WEB_LISTEN_ADDRESS ]; then
port=`echo $F2B_WEB_LISTEN_ADDRESS | cut -d ":" -f 2 -` port=`echo $F2B_WEB_LISTEN_ADDRESS | cut -d ":" -f 2 -`
fi fi
curl --fail localhost:$port/metrics || exit 1 curl --fail localhost:$port/health || exit 1

View file

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/f2b"
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/textfile" "gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/textfile"
) )
@ -31,3 +32,13 @@ func metricHandler(w http.ResponseWriter, r *http.Request, collector *textfile.C
promhttp.Handler().ServeHTTP(w, r) promhttp.Handler().ServeHTTP(w, r)
collector.WriteTextFileMetrics(w, r) collector.WriteTextFileMetrics(w, r)
} }
func healthHandler(w http.ResponseWriter, r *http.Request, collector *f2b.Collector) {
if collector.IsHealthy() {
w.WriteHeader(http.StatusOK)
w.Write([]byte("{\"healthy\":true}"))
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("{\"healthy\":false}"))
}
}

View file

@ -6,11 +6,13 @@ import (
"time" "time"
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg" "gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/cfg"
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/f2b"
"gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/textfile" "gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/collector/textfile"
) )
func StartServer( func StartServer(
appSettings *cfg.AppSettings, appSettings *cfg.AppSettings,
f2bCollector *f2b.Collector,
textFileCollector *textfile.Collector, textFileCollector *textfile.Collector,
) chan error { ) chan error {
http.HandleFunc("/", AuthMiddleware( http.HandleFunc("/", AuthMiddleware(
@ -23,6 +25,11 @@ func StartServer(
}, },
appSettings.AuthProvider, appSettings.AuthProvider,
)) ))
http.HandleFunc("/health",
func(w http.ResponseWriter, r *http.Request) {
healthHandler(w, r, f2bCollector)
},
)
log.Printf("metrics available at '%s'", metricsPath) log.Printf("metrics available at '%s'", metricsPath)
svrErr := make(chan error) svrErr := make(chan error)