Skip to content

Commit dbcff4c

Browse files
committed
add /targets api endpoint
get list of configured and discovered targets and configuration Signed-off-by: Markus Blaschke <[email protected]>
1 parent 3ebfa53 commit dbcff4c

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ scrape_configs:
8080
HTTP Endpoints
8181
--------------
8282

83-
| Endpoint | Description |
84-
|------------|--------------------------------------------------------------------------------------------------------------------------|
85-
| `/metrics` | Default prometheus golang metrics |
86-
| `/probe` | Probe shelly plugs, uses mDNS servicediscovery to find Shelly plugs (must be run on host network) |
83+
| Endpoint | Description |
84+
|------------|---------------------------------------------------------------------------------------------------|
85+
| `/metrics` | Default prometheus golang metrics |
86+
| `/probe` | Probe shelly plugs, uses mDNS servicediscovery to find Shelly plugs (must be run on host network) |
87+
| `/targets` | List of configured and discovered targets as JSON |
8788

8889
Metrics
8990
-------

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func startHttpServer() {
8888

8989
mux.Handle("/metrics", promhttp.Handler())
9090
mux.HandleFunc("/probe", shellyProbeDiscovery)
91+
mux.HandleFunc("/targets", shellyProbeDiscoveryTargets)
9192

9293
srv := &http.Server{
9394
Addr: Opts.Server.Bind,

probe.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"net/http"
78
"strconv"
@@ -29,6 +30,35 @@ func newShellyProber(ctx context.Context, registry *prometheus.Registry, logger
2930
return sp
3031
}
3132

33+
func shellyProbeDiscoveryTargets(w http.ResponseWriter, r *http.Request) {
34+
registry := prometheus.NewRegistry()
35+
36+
contextLogger := buildContextLoggerFromRequest(r)
37+
38+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(30*float64(time.Second)))
39+
defer cancel()
40+
41+
sp := newShellyProber(ctx, registry, contextLogger)
42+
sp.UseDiscovery()
43+
44+
targets := sp.GetTargets()
45+
46+
body, err := json.Marshal(targets)
47+
if err != nil {
48+
contextLogger.Error(err)
49+
http.Error(w, fmt.Sprintf("unable to marshal targets to json: %s", err), http.StatusBadRequest)
50+
return
51+
}
52+
53+
w.Header().Set("Content-Type", "application/json")
54+
_, err = w.Write(body)
55+
if err != nil {
56+
contextLogger.Error(err)
57+
http.Error(w, err.Error(), http.StatusBadRequest)
58+
return
59+
}
60+
}
61+
3262
func shellyProbeDiscovery(w http.ResponseWriter, r *http.Request) {
3363
var err error
3464
var timeoutSeconds float64

0 commit comments

Comments
 (0)