forked from Setheck/smartthings-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (65 loc) · 2.1 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/kelseyhightower/envconfig"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/setheck/smartthings-exporter/smartthings"
)
var (
Version = "dev"
Built = ""
Commit = ""
Banner = strings.ReplaceAll(`
_ _ _ _ _
___ _ __ ___ __ _ _ __| |_| |_| |__ (_)_ __ __ _ ___ _____ ___ __ ___ _ __| |_ ___ _ __
/ __| '_ q _ \ / _q | '__| __| __| '_ \| | '_ \ / _q / __|_____ / _ \ \/ / '_ \ / _ \| '__| __/ _ \ '__|
\__ \ | | | | | (_| | | | |_| |_| | | | | | | | (_| \__ \_____| __/> <| |_) | (_) | | | || __/ |
|___/_| |_| |_|\__,_|_| \__|\__|_| |_|_|_| |_|\__, |___/ \___/_/\_\ .__/ \___/|_| \__\___|_|
|___/ |_|`, "q", "`")
)
type Configuration struct {
Port int `envconfig:"PORT" default:"9119"`
ApiToken string `envconfig:"API_TOKEN"`
}
var (
ver = flag.Bool("version", false, "print version and exit")
)
func main() {
flag.Parse()
fmt.Println(Banner)
fmt.Println("version:", Version)
fmt.Println(" built:", Built)
fmt.Println(" commit:", Commit)
if *ver {
os.Exit(0)
}
log.Println("starting up")
var config Configuration
if err := envconfig.Process("STE", &config); err != nil {
log.Fatal(err)
}
log.Println("creating client")
client := smartthings.NewDefaultClient(config.ApiToken)
if _, err := client.ListDevices(context.Background()); err != nil {
log.Fatal("failed to initialize client:", err)
}
log.Println("creating collector")
collector := NewCollector(client)
prometheus.MustRegister(collector)
http.HandleFunc("/", rootHandler)
http.Handle("/metrics", promhttp.Handler())
addr := fmt.Sprintf("0.0.0.0:%d", config.Port)
log.Println("starting server on", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
// TODO:(smt) default page with info and usage etc...
fmt.Fprint(w, "OK")
}