Add Prometheus config section
This commit is contained in:
parent
3e416925da
commit
e8428677a4
5 changed files with 65 additions and 7 deletions
|
|
@ -1,13 +1,27 @@
|
|||
package prometheus
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/aceberg/WatchYourLAN/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
func Handler(appConfig *models.Conf) func(c *gin.Context) {
|
||||
h := promhttp.Handler()
|
||||
return func(c *gin.Context) {
|
||||
if !appConfig.PrometheusEnable {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
h.ServeHTTP(c.Writer, c.Request)
|
||||
}
|
||||
}
|
||||
|
||||
var up *prometheus.GaugeVec
|
||||
|
||||
func NewMetrics() {
|
||||
|
|
@ -18,6 +32,10 @@ func NewMetrics() {
|
|||
}, []string{"ip", "iface", "name", "mac", "known"})
|
||||
}
|
||||
|
||||
func RemoveMetrics() {
|
||||
up = nil
|
||||
}
|
||||
|
||||
// Add a Prometheus metric
|
||||
func Add(oneHist models.Host) {
|
||||
if up == nil {
|
||||
|
|
|
|||
|
|
@ -109,3 +109,15 @@ func saveInfluxHandler(c *gin.Context) {
|
|||
|
||||
c.Redirect(http.StatusFound, "/config")
|
||||
}
|
||||
|
||||
func savePrometheusHandler(c *gin.Context) {
|
||||
enable := c.PostForm("enable")
|
||||
|
||||
appConfig.PrometheusEnable = enable == "on"
|
||||
|
||||
conf.Write(appConfig)
|
||||
|
||||
slog.Info("Writing new config to " + appConfig.ConfPath)
|
||||
|
||||
c.Redirect(http.StatusFound, "/config")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ func updateRoutines() {
|
|||
|
||||
if appConfig.PrometheusEnable {
|
||||
prometheus.NewMetrics()
|
||||
} else {
|
||||
prometheus.RemoveMetrics()
|
||||
}
|
||||
|
||||
quitScan = make(chan bool)
|
||||
|
|
|
|||
|
|
@ -179,6 +179,33 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md mt-4 mb-4">
|
||||
<div class="card border-primary">
|
||||
<div class="card-header">Prometheus config</div>
|
||||
<div class="card-body table-responsive">
|
||||
<table class="table table-borderless">
|
||||
<form action="/config_prometheus/" method="post">
|
||||
<tr>
|
||||
<td>Enable</td>
|
||||
<td>
|
||||
<div class="form-check form-switch">
|
||||
{{ if .Config.PrometheusEnable }}
|
||||
<input class="form-check-input" type="checkbox" name="enable" checked>
|
||||
{{ else }}
|
||||
<input class="form-check-input" type="checkbox" name="enable">
|
||||
{{ end }}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><button type="submit" class="btn btn-primary">Save</button></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</form>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card border-primary mt-4 mb-4">
|
||||
<div class="card-header">
|
||||
About (<a href="https://github.com/aceberg/WatchYourLAN/releases/tag/{{ .Version }}" target="_blank">v{{ .Version }}</a>)
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ import (
|
|||
|
||||
"github.com/aceberg/WatchYourLAN/internal/check"
|
||||
"github.com/aceberg/WatchYourLAN/internal/conf"
|
||||
"github.com/aceberg/WatchYourLAN/internal/prometheus"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
// Gui - start web server
|
||||
|
|
@ -60,13 +60,12 @@ func Gui(dirPath, nodePath string) {
|
|||
router.GET("/host/:id", hostHandler) // host.go
|
||||
router.GET("/config/", configHandler) // config.go
|
||||
|
||||
router.POST("/config/", saveConfigHandler) // config.go
|
||||
router.POST("/config_settings/", saveSettingsHandler) // config.go
|
||||
router.POST("/config_influx/", saveInfluxHandler) // config.go
|
||||
router.POST("/config/", saveConfigHandler) // config.go
|
||||
router.POST("/config_settings/", saveSettingsHandler) // config.go
|
||||
router.POST("/config_influx/", saveInfluxHandler) // config.go
|
||||
router.POST("/config_prometheus/", savePrometheusHandler) // config.go
|
||||
|
||||
if appConfig.PrometheusEnable {
|
||||
router.GET("/metrics", gin.WrapH(promhttp.Handler()))
|
||||
}
|
||||
router.GET("/metrics", prometheus.Handler(&appConfig))
|
||||
|
||||
err := router.Run(address)
|
||||
check.IfError(err)
|
||||
|
|
|
|||
Loading…
Reference in a new issue