hue_exporter/main.go

106 lines
2.9 KiB
Go
Raw Normal View History

2020-12-22 18:24:23 +01:00
package main
import (
2020-12-29 14:10:47 +01:00
"encoding/json"
2020-12-22 18:24:23 +01:00
"fmt"
2020-12-29 14:10:47 +01:00
"io/ioutil"
2020-12-22 18:24:23 +01:00
"log"
"net/http"
"github.com/namsral/flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/aexel90/hue_exporter/collector"
2020-12-28 17:29:44 +01:00
"github.com/aexel90/hue_exporter/hue"
2020-12-29 14:10:47 +01:00
"github.com/aexel90/hue_exporter/metric"
2020-12-22 18:24:23 +01:00
)
var (
2020-12-29 14:10:47 +01:00
flagBridgeURL = flag.String("hue-url", "", "The URL of the bridge")
flagUsername = flag.String("username", "", "The username token having bridge access")
flagAddress = flag.String("listen-address", "127.0.0.1:9773", "The address to listen on for HTTP requests.")
2020-12-29 14:24:09 +01:00
flagMetricsFile = flag.String("metrics-file", "hue_metrics.json", "The JSON file with the metric definitions.")
2023-10-17 21:28:30 +02:00
authUser = flag.String("auth.user", "", "Username for basic auth.")
authPass = flag.String("auth.pass", "", "Password for basic auth. Enables basic auth if set.")
2020-12-22 18:24:23 +01:00
2020-12-29 15:14:36 +01:00
flagTest = flag.Bool("test", false, "Test configured metrics")
flagCollect = flag.Bool("collect", false, "Collect all available metrics")
2020-12-28 17:29:44 +01:00
flagCollectFile = flag.String("collect-file", "", "The JSON file where to store collect results")
2020-12-22 18:24:23 +01:00
)
2023-10-17 21:28:30 +02:00
type basicAuthHandler struct {
handler http.HandlerFunc
user string
password string
}
func (h *basicAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
user, password, ok := r.BasicAuth()
if !ok || password != h.password || user != h.user {
w.Header().Set("WWW-Authenticate", "Basic realm=\"metrics\"")
http.Error(w, "Invalid username or password", http.StatusUnauthorized)
return
}
h.handler(w, r)
return
}
2020-12-22 18:24:23 +01:00
func main() {
flag.Parse()
2020-12-29 14:10:47 +01:00
var metricsFile *metric.MetricsFile
2020-12-28 17:29:44 +01:00
// collect mode
if *flagCollect {
hue.CollectAll(*flagBridgeURL, *flagUsername, *flagCollectFile)
return
}
2020-12-29 14:10:47 +01:00
err := readAndParseFile(*flagMetricsFile, &metricsFile)
if err != nil {
fmt.Println(err)
return
}
hueCollector, err := collector.NewHueCollector(*flagBridgeURL, *flagUsername, metricsFile)
2020-12-22 18:24:23 +01:00
if err != nil {
fmt.Println(err)
return
}
if *flagTest {
hueCollector.Test()
2020-12-28 17:29:44 +01:00
} else {
prometheus.MustRegister(hueCollector)
2023-10-17 21:28:30 +02:00
handler := promhttp.Handler()
if *authUser != "" || *authPass != "" {
if *authUser == "" || *authPass == "" {
log.Fatal("You need to specify -auth.user and -auth.pass to enable basic auth")
}
handler = &basicAuthHandler{
handler: promhttp.Handler().ServeHTTP,
user: *authUser,
password: *authPass,
}
}
http.Handle("/metrics", handler)
2020-12-28 17:29:44 +01:00
fmt.Printf("metrics available at http://%s/metrics\n", *flagAddress)
log.Fatal(http.ListenAndServe(*flagAddress, nil))
2020-12-22 18:24:23 +01:00
}
}
2020-12-29 14:10:47 +01:00
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
}