-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (77 loc) · 2.26 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
yaml "gopkg.in/yaml.v2"
)
const version string = "0.1"
type List struct {
Names map[string]string
}
var (
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("listen-address", ":9330", "Address on which to expose metrics.")
metricsPath = flag.String("path", "/metrics", "Path under which to expose metrics.")
device = flag.Int("device", 22, "Sensor type, either 11 or 22 for DHT11/DHT22")
nameFile = flag.String("names", "names.yaml", "File mapping GPIOs to names")
list List
)
func init() {
flag.Usage = func() {
fmt.Println("Usage: dht_exporter [ ... ]\n\nParameters:")
fmt.Println()
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
if *showVersion {
printVersion()
os.Exit(0)
}
filename, _ := filepath.Abs(*nameFile)
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal("Can't read names file")
}
err = yaml.Unmarshal(yamlFile, &list)
if err != nil {
log.Fatal("Can't read names file")
}
startServer()
}
func printVersion() {
fmt.Println("dht_exporter")
fmt.Printf("Version: %s\n", version)
}
func startServer() {
log.Infof("Starting DHT exporter (Version: %s)\n", version)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>DHT Exporter (Version ` + version + `)</title></head>
<body>
<h1>DHT Exporter</h1>
<p><a href="` + *metricsPath + `">Metrics</a></p>
<h2>More information:</h2>
<p><a href="https://github.com/l3akage/dht_exporter">github.com/l3akage/dht_exporter</a></p>
</body>
</html>`))
})
http.HandleFunc(*metricsPath, handleMetricsRequest)
log.Infof("Listening for %s on %s\n", *metricsPath, *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
func handleMetricsRequest(w http.ResponseWriter, r *http.Request) {
reg := prometheus.NewRegistry()
reg.MustRegister(&dhtCollector{})
promhttp.HandlerFor(reg, promhttp.HandlerOpts{
ErrorLog: log.NewErrorLogger(),
ErrorHandling: promhttp.ContinueOnError}).ServeHTTP(w, r)
}