This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
/
main.go
106 lines (92 loc) · 2.55 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
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"flag"
"fmt"
"io/ioutil"
"strconv"
"strings"
mp "github.com/mackerelio/go-mackerel-plugin-helper"
"github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/rpi"
"github.com/taiyoh/go-embd-bme280"
)
type SensorPlugin struct {
Prefix string
BME280 *bme280.BME280
}
func (s SensorPlugin) GraphDefinition() map[string](mp.Graphs) {
return map[string](mp.Graphs){
s.Prefix + ".temperature": mp.Graphs{
Label: "Sensor Temperature(℃)",
Unit: "float",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "cpu_temperature", Label: "CPU"},
mp.Metrics{Name: "bme280_temperature", Label: "BME280"},
},
},
s.Prefix + ".pressure": mp.Graphs{
Label: "Sensor Pressure(hPa)",
Unit: "float",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "bme280_pressure", Label: "BME280"},
},
},
s.Prefix + ".humidity": mp.Graphs{
Label: "Sensor Humidity(%)",
Unit: "float",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "bme280_humidity", Label: "BME280"},
},
},
}
}
func (s SensorPlugin) FetchMetrics() (map[string]interface{}, error) {
result := map[string]interface{}{"cpu_temperature": nil, "bme280_temperature": nil, "bme280_pressure": nil, "bme280_humidity": nil}
if cpuData, err := ioutil.ReadFile("/sys/class/thermal/thermal_zone0/temp"); err != nil {
return nil, fmt.Errorf("Failed to read CPU temperature: %s", err)
} else {
cpuTemp, err := strconv.ParseFloat(strings.TrimSpace(string(cpuData)), 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse CPU temperature: %s", err)
} else {
result["cpu_temperature"] = cpuTemp / 1000.0
}
}
bme280Data, err := s.BME280.Read()
if err != nil {
return nil, fmt.Errorf("Failed to read BME280 data: %s", err)
} else {
result["bme280_temperature"] = bme280Data[0]
result["bme280_pressure"] = bme280Data[1] / 100
result["bme280_humidity"] = bme280Data[2]
}
return result, nil
}
func main() {
// Parse arguments
optPrefix := flag.String("metric-key-prefix", "sensor", "Metric key prefix")
optTempfile := flag.String("tempfile", "", "Temp file name")
flag.Parse()
// Initialize BME280
if err := embd.InitI2C(); err != nil {
panic(err)
}
defer embd.CloseI2C()
bus := embd.NewI2CBus(1)
opt := bme280.NewOpt()
bme280, err := bme280.New(bus, opt)
if err != nil {
panic(err)
}
// Initialize helper
s := SensorPlugin{
Prefix: *optPrefix,
BME280: bme280,
}
helper := mp.NewMackerelPlugin(s)
helper.Tempfile = *optTempfile
if helper.Tempfile == "" {
helper.Tempfile = fmt.Sprintf("/tmp/mackerel-plugin-%s", *optPrefix)
}
helper.Run()
}