-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (51 loc) · 1.45 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
61
62
63
64
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/namsral/flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/aexel90/shelly_exporter/collector"
"github.com/aexel90/shelly_exporter/metric"
)
var (
flagAddress = flag.String("listen-address", "127.0.0.1:9784", "The address to listen on for HTTP requests.") //TODO port?
flagMetricsFile = flag.String("metrics-file", "shelly-metrics.json", "The JSON file with the metric definitions.")
flagTest = flag.Bool("test", false, "Test configured metrics")
)
func main() {
flag.Parse()
var metricsFile *metric.MetricsFile
err := readAndParseFile(*flagMetricsFile, &metricsFile)
if err != nil {
fmt.Println(err)
return
}
shellyCollector, err := collector.NewShellyCollector(metricsFile)
if err != nil {
fmt.Println(err)
return
}
if *flagTest {
shellyCollector.Test()
} else {
prometheus.MustRegister(shellyCollector)
http.Handle("/metrics", promhttp.Handler())
fmt.Printf("metrics available at http://%s/metrics\n", *flagAddress)
log.Fatal(http.ListenAndServe(*flagAddress, nil))
}
}
func readAndParseFile(file string, v interface{}) error {
jsonData, err := ioutil.ReadFile(file)
if err != nil {
return fmt.Errorf("error reading metric file: %v", err)
}
err = json.Unmarshal(jsonData, v)
if err != nil {
return fmt.Errorf("error parsing JSON: %v", err)
}
return nil
}