-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (49 loc) · 1.37 KB
/
main.go
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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/chalko/sb-metrics/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"log"
"net/http"
"time"
)
var (
addr = flag.String("listen-address", ":8888", "The address to listen on for HTTP requests.")
timeout = flag.Duration(
"timeout",
30*time.Second,
"Timeout to scrape status screen")
)
func init() {
}
func main() {
c := client.NewCableModemClient("http://192.168.100.1", *timeout)
prometheus.MustRegister(c)
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/status.json", func(w http.ResponseWriter, r *http.Request) {
s, err := jsonStatus(c)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println(err)
http.Error(w, "my own error message", http.StatusInternalServerError)
}
fmt.Fprintf(w, string(s))
})
// The Handler function provides a default handler to expose metrics
// via an HTTP server. "/metrics" is the usual endpoint for that.
log.Printf("starting exporter on %q", *addr)
if err := http.ListenAndServe(*addr, mux); err != nil {
log.Fatalf("cannot start exporter: %s", err)
}
}
func jsonStatus(c *client.CableModemClient) ([]byte, error) {
s, err := c.GetModemStatus()
if err != nil {
return nil, err
}
return json.MarshalIndent(s, "", " ")
}