Compare commits

..

No commits in common. "6f2a73f46e0625cc4a38074a02a1bbfd02f09de6" and "12f983b16d8dc3a5ca8cb60ed82a57b98334d8a8" have entirely different histories.

7 changed files with 37 additions and 207 deletions

View file

@ -1,5 +1,3 @@
USERNAME=***** USERNAME=*****
HUE_URL=192.168.178.34 HUE_URL=192.168.178.34
LISTEN_ADDRESS=127.0.0.1:9773 METRICS_FILE=******
AUTH_USER=user
AUTH_PASS=password

View file

@ -1,18 +0,0 @@
name: Docker Image CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build the Docker image
run: docker build . --file Dockerfile --tag hue_exporter:$(date +%s)

View file

@ -9,7 +9,6 @@ RUN go build -o /hue_exporter
FROM alpine:latest FROM alpine:latest
WORKDIR / WORKDIR /
COPY --from=build /hue_exporter /hue_exporter COPY --from=build /hue_exporter /hue_exporter
COPY hue_metrics.json ./
EXPOSE 9773 EXPOSE 9773
ENTRYPOINT [ "sh", "-c", "/hue_exporter -username ${USERNAME} -hue-url ${HUE_URL} -metrics-file hue_metrics.json -listen-address ${LISTEN_ADDRESS} -auth.user ${AUTH_USER} -auth.pass ${AUTH_PASS}" ] ENTRYPOINT [ "sh", "-c", "/hue_exporter -username ${USERNAME} -hue-url ${HUE_URL} -metrics-file ${METRICS_FILE} " ]

View file

@ -4,13 +4,13 @@ This exporter exports some variables from Philips Hue Bridge
(https://www.philips-hue.com) (https://www.philips-hue.com)
to prometheus. to prometheus.
## Build ## Building
go get github.com/aexel90/hue_exporter/ go get github.com/aexel90/hue_exporter/
cd $GOPATH/src/github.com/aexel90/hue_exporter cd $GOPATH/src/github.com/aexel90/hue_exporter
go install go install
## Execute ## Running
How to create a user for your bridge is described here: https://developers.meethue.com/develop/get-started-2/ How to create a user for your bridge is described here: https://developers.meethue.com/develop/get-started-2/
@ -34,12 +34,6 @@ Usage:
-username string -username string
The username token having bridge access The username token having bridge access
## Docker
cp .env.template .env
vi .env
docker-compose up -d --build
## Example execution ## Example execution
### Running within prometheus: ### Running within prometheus:
@ -82,6 +76,4 @@ Usage:
Dashboard ID is 13645. Dashboard ID is 13645.
https://grafana.com/grafana/dashboards/13645
![Grafana](https://raw.githubusercontent.com/aexel90/hue_exporter/main/grafana/screenshot.jpg) ![Grafana](https://raw.githubusercontent.com/aexel90/hue_exporter/main/grafana/screenshot.jpg)

View file

@ -1,14 +1,13 @@
version: '3.4' version: '3.4'
services: services:
hue_exporter: stravakomootsync:
image: hue_exporter image: hue_exporter
build: build:
context: . context: .
dockerfile: ./Dockerfile dockerfile: ./Dockerfile.multistage
restart: unless-stopped restart: unless-stopped
container_name: hue_exporter container_name: hue_exporter
#network_mode: "host"
ports: ports:
- 9773:9773 - 9773:9773
env_file: env_file:

View file

@ -1,47 +1,4 @@
{ {
"__inputs": [
{
"name": "DS_PROMETHEUS",
"label": "Prometheus",
"description": "",
"type": "datasource",
"pluginId": "prometheus",
"pluginName": "Prometheus"
}
],
"__elements": {},
"__requires": [
{
"type": "grafana",
"id": "grafana",
"name": "Grafana",
"version": "9.2.1"
},
{
"type": "panel",
"id": "graph",
"name": "Graph (old)",
"version": ""
},
{
"type": "datasource",
"id": "prometheus",
"name": "Prometheus",
"version": "1.0.0"
},
{
"type": "panel",
"id": "stat",
"name": "Stat",
"version": ""
},
{
"type": "panel",
"id": "table",
"name": "Table",
"version": ""
}
],
"annotations": { "annotations": {
"list": [ "list": [
{ {
@ -68,14 +25,14 @@
"fiscalYearStartMonth": 0, "fiscalYearStartMonth": 0,
"gnetId": 13645, "gnetId": 13645,
"graphTooltip": 0, "graphTooltip": 0,
"id": null, "id": 4,
"links": [], "links": [],
"liveNow": false, "liveNow": false,
"panels": [ "panels": [
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
@ -118,14 +75,9 @@
"text": {}, "text": {},
"textMode": "auto" "textMode": "auto"
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "hue_bridge_info", "expr": "hue_bridge_info",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -140,7 +92,7 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
@ -183,13 +135,9 @@
"text": {}, "text": {},
"textMode": "auto" "textMode": "auto"
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"expr": "hue_bridge_info", "expr": "hue_bridge_info",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -204,7 +152,7 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
@ -247,14 +195,9 @@
"text": {}, "text": {},
"textMode": "auto" "textMode": "auto"
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "hue_bridge_info", "expr": "hue_bridge_info",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -269,7 +212,7 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
@ -312,14 +255,9 @@
"text": {}, "text": {},
"textMode": "auto" "textMode": "auto"
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "hue_bridge_info", "expr": "hue_bridge_info",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -334,7 +272,7 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
@ -378,14 +316,9 @@
"text": {}, "text": {},
"textMode": "auto" "textMode": "auto"
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "hue_bridge_info", "expr": "hue_bridge_info",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -400,7 +333,7 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
@ -443,14 +376,9 @@
"text": {}, "text": {},
"textMode": "auto" "textMode": "auto"
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "hue_bridge_info", "expr": "hue_bridge_info",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -465,7 +393,7 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
@ -509,14 +437,9 @@
"text": {}, "text": {},
"textMode": "value" "textMode": "value"
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "hue_bridge_info", "expr": "hue_bridge_info",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -531,7 +454,7 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"description": "", "description": "",
"fieldConfig": { "fieldConfig": {
@ -574,14 +497,9 @@
"text": {}, "text": {},
"textMode": "auto" "textMode": "auto"
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "sum(hue_light_state)", "expr": "sum(hue_light_state)",
"format": "table", "format": "table",
"hide": false, "hide": false,
@ -598,7 +516,7 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"description": "", "description": "",
"fieldConfig": { "fieldConfig": {
@ -641,14 +559,9 @@
"text": {}, "text": {},
"textMode": "auto" "textMode": "auto"
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "count(hue_light_state)-sum(hue_light_state)", "expr": "count(hue_light_state)-sum(hue_light_state)",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -673,7 +586,7 @@
"dashes": false, "dashes": false,
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"decimals": 1, "decimals": 1,
"fill": 0, "fill": 0,
@ -706,7 +619,7 @@
"alertThreshold": false "alertThreshold": false
}, },
"percentage": false, "percentage": false,
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"pointradius": 2, "pointradius": 2,
"points": false, "points": false,
"renderer": "flot", "renderer": "flot",
@ -716,15 +629,9 @@
"steppedLine": false, "steppedLine": false,
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "10^((hue_sensor_lightlevel-1)/10000)", "expr": "10^((hue_sensor_lightlevel-1)/10000)",
"interval": "", "interval": "",
"legendFormat": "{{name}}", "legendFormat": "{{name}}",
"range": true,
"refId": "A" "refId": "A"
} }
], ],
@ -774,7 +681,7 @@
"dashes": false, "dashes": false,
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"decimals": 1, "decimals": 1,
"fill": 0, "fill": 0,
@ -807,7 +714,7 @@
"alertThreshold": false "alertThreshold": false
}, },
"percentage": false, "percentage": false,
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"pointradius": 2, "pointradius": 2,
"points": false, "points": false,
"renderer": "flot", "renderer": "flot",
@ -817,15 +724,9 @@
"steppedLine": false, "steppedLine": false,
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "hue_sensor_temperature/100", "expr": "hue_sensor_temperature/100",
"interval": "", "interval": "",
"legendFormat": "{{name}}", "legendFormat": "{{name}}",
"range": true,
"refId": "A" "refId": "A"
} }
], ],
@ -866,7 +767,7 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
@ -1022,14 +923,9 @@
} }
] ]
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "hue_sensor_info{type=~\"ZLL.*\"}", "expr": "hue_sensor_info{type=~\"ZLL.*\"}",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -1082,9 +978,8 @@
{ {
"datasource": { "datasource": {
"type": "prometheus", "type": "prometheus",
"uid": "${DS_PROMETHEUS}" "uid": "nLstfzWgz"
}, },
"description": "",
"fieldConfig": { "fieldConfig": {
"defaults": { "defaults": {
"color": { "color": {
@ -1277,14 +1172,9 @@
} }
] ]
}, },
"pluginVersion": "9.2.1", "pluginVersion": "8.5.3",
"targets": [ "targets": [
{ {
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "hue_light_info", "expr": "hue_light_info",
"format": "table", "format": "table",
"instant": true, "instant": true,
@ -1339,20 +1229,20 @@
} }
], ],
"refresh": "5m", "refresh": "5m",
"schemaVersion": 37, "schemaVersion": 36,
"style": "dark", "style": "dark",
"tags": [], "tags": [],
"templating": { "templating": {
"list": [] "list": []
}, },
"time": { "time": {
"from": "now-30m", "from": "now-6h",
"to": "now" "to": "now"
}, },
"timepicker": {}, "timepicker": {},
"timezone": "", "timezone": "",
"title": "Philips Hue", "title": "Philips Hue",
"uid": "X7uYULiRk", "uid": "X7uYULiRk",
"version": 1, "version": 6,
"weekStart": "" "weekStart": ""
} }

32
main.go
View file

@ -21,31 +21,12 @@ var (
flagUsername = flag.String("username", "", "The username token having bridge access") 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.") flagAddress = flag.String("listen-address", "127.0.0.1:9773", "The address to listen on for HTTP requests.")
flagMetricsFile = flag.String("metrics-file", "hue_metrics.json", "The JSON file with the metric definitions.") flagMetricsFile = flag.String("metrics-file", "hue_metrics.json", "The JSON file with the metric definitions.")
authUser = flag.String("auth.user", "", "Username for basic auth.")
authPass = flag.String("auth.pass", "", "Password for basic auth. Enables basic auth if set.")
flagTest = flag.Bool("test", false, "Test configured metrics") flagTest = flag.Bool("test", false, "Test configured metrics")
flagCollect = flag.Bool("collect", false, "Collect all available metrics") flagCollect = flag.Bool("collect", false, "Collect all available metrics")
flagCollectFile = flag.String("collect-file", "", "The JSON file where to store collect results") flagCollectFile = flag.String("collect-file", "", "The JSON file where to store collect results")
) )
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
}
func main() { func main() {
flag.Parse() flag.Parse()
@ -74,18 +55,7 @@ func main() {
hueCollector.Test() hueCollector.Test()
} else { } else {
prometheus.MustRegister(hueCollector) prometheus.MustRegister(hueCollector)
handler := promhttp.Handler() http.Handle("/metrics", 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)
fmt.Printf("metrics available at http://%s/metrics\n", *flagAddress) fmt.Printf("metrics available at http://%s/metrics\n", *flagAddress)
log.Fatal(http.ListenAndServe(*flagAddress, nil)) log.Fatal(http.ListenAndServe(*flagAddress, nil))
} }