This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetatmo-munin-plugin.go
347 lines (325 loc) · 11.1 KB
/
netatmo-munin-plugin.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"fmt"
"os"
"strconv"
netatmo "github.com/exzz/netatmo-api-go"
toml "github.com/BurntSushi/toml"
)
type configGlobalStruct struct {
ClientID, ClientSecret, Username, Password (string)
Sensors (configSensorsStruct)
}
type configSensorsStruct struct {
CO2, Noise, Pressure, Humidity, Temperature, WindStrength, Rain, WindAngle, BatteryPercent, WifiStatus, RFStatus (bool)
}
var configGlobal (configGlobalStruct)
type modulesDataStruct struct {
name (string)
data (float64)
}
var modulesData = make(map[string][]modulesDataStruct)
var DisplayConfig = false
func main() {
cfg_file := ""
if _, err := os.Stat("/etc/munin/netatmo.cfg"); err == nil {
cfg_file = "/etc/munin/netatmo.cfg"
}
if _, err := os.Stat("netatmo.cfg"); err == nil {
cfg_file = "netatmo.cfg"
}
if cfg_file == "" {
fmt.Println("Configuration file not found")
os.Exit(1)
}
configGlobal.Sensors = configSensorsStruct{true, true, true, true, true, true, true, false, true, false, true}
if _, err := toml.DecodeFile(cfg_file, &configGlobal); err != nil {
fmt.Printf("Cannot parse config file: %s\n", err)
os.Exit(1)
}
if (configGlobal.ClientID == "" || configGlobal.ClientSecret == "" || configGlobal.Username == "" || configGlobal.Password == "") {
fmt.Println("Wrong configuration")
os.Exit(1)
}
if (configGlobal.ClientID == "ClientID" || configGlobal.ClientSecret == "ClientSecret" || configGlobal.Username == "Username" || configGlobal.Password == "Password") {
fmt.Println("Do not forget change example configuration settings :)")
os.Exit(1)
}
//help
if (len(os.Args) == 2 && os.Args[1] == "help") {
fmt.Println("Netatmo Munin PlugIn")
fmt.Println("https://github.com/NightMan-1/netatmo-munin-go")
fmt.Println("(c)2017 Sergey Gurinovich")
os.Exit(0)
}
//connect NetAtmo
n, err := netatmo.NewClient(netatmo.Config{
ClientID: configGlobal.ClientID,
ClientSecret: configGlobal.ClientSecret,
Username: configGlobal.Username,
Password: configGlobal.Password,
})
if err != nil {
fmt.Println("Cannot connect to NetAtmo server: ", err)
os.Exit(1)
}
dc, err := n.Read()
if err != nil {
fmt.Println("Cannot read NetAtmo data: ", err)
os.Exit(1)
}
//parce NetAtmo data
for _, station := range dc.Stations() {
//fmt.Printf("Station : %v\n", station.StationName)
for _, module := range station.Modules() {
//fmt.Printf("\tModule : %v\n", module.ModuleName)
_, data := module.Data()
for dataType, value := range data {
//fmt.Printf("\t\t%s : %v\n", dataType, value)
if (dataType == "LastMesure") {
continue
}
t, _ := strconv.ParseFloat(fmt.Sprintf("%v", value), 64)
if (len(dc.Stations()) > 1){
modulesData[dataType] = append(modulesData[dataType], modulesDataStruct{module.ModuleName + " (" + station.StationName + ")", t})
}else{
modulesData[dataType] = append(modulesData[dataType], modulesDataStruct{module.ModuleName, t})
}
}
_, info := module.Info()
for dataType, value := range info {
//fmt.Printf("\t\t%s : %v\n", dataType, value)
t, _ := strconv.ParseFloat(fmt.Sprintf("%v", value), 64)
if (len(dc.Stations()) > 1){
modulesData[dataType] = append(modulesData[dataType], modulesDataStruct{module.ModuleName + " (" + station.StationName + ")", t})
}else{
modulesData[dataType] = append(modulesData[dataType], modulesDataStruct{module.ModuleName, t})
}
}
}
}
if (len(os.Args) == 2 && os.Args[1] == "config") { DisplayConfig = true }
for m_type, m_data := range modulesData { // type name + module data
switch m_type {
case "BatteryPercent":
if (configGlobal.Sensors.BatteryPercent) {
fmt.Println("multigraph netatmo_battery")
if (DisplayConfig){
fmt.Println("graph_title Battery")
fmt.Println("graph_vlabel percent")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("battery_%v.label %v\n", key, value.name)
fmt.Printf("battery_%v.draw LINE1\n", key)
}
}else{
for key, value := range m_data {
fmt.Printf("battery_%v.value %.0f\n", key, value.data)
}
}
}
case "WifiStatus":
if (configGlobal.Sensors.BatteryPercent) {
fmt.Println("multigraph netatmo_WifiStatus")
if (DisplayConfig){
fmt.Println("graph_title Wifi status")
//fmt.Println("graph_vlabel level")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("WifiStatus_%v.label %v\n", key, value.name)
fmt.Printf("WifiStatus_%v.draw LINE1\n", key)
}
}else{
for key, value := range m_data {
fmt.Printf("WifiStatus_%v.value %.0f\n", key, value.data)
}
}
}
case "RFStatus":
if (configGlobal.Sensors.BatteryPercent) {
fmt.Println("multigraph netatmo_RFStatus")
if (DisplayConfig){
fmt.Println("graph_title RF status")
//fmt.Println("graph_vlabel level")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("RFStatus_%v.label %v\n", key, value.name)
fmt.Printf("RFStatus_%v.draw LINE1\n", key)
}
}else{
for key, value := range m_data {
fmt.Printf("RFStatus_%v.value %.0f\n", key, value.data)
}
}
}
case "CO2":
if (configGlobal.Sensors.CO2) {
fmt.Println("multigraph netatmo_co2")
if (DisplayConfig){
fmt.Println("graph_title CO₂")
fmt.Println("graph_vlabel ppm")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("co2_%v.label %v\n", key, value.name)
fmt.Printf("co2_%v.warning 1000\n", key)
fmt.Printf("co2_%v.critical 1500\n", key)
fmt.Printf("co2_%v.draw LINE1\n", key)
}
}else{
for key, value := range m_data {
fmt.Printf("co2_%v.value %.0f\n", key, value.data)
}
}
}
case "Noise":
if (configGlobal.Sensors.Noise) {
fmt.Println("multigraph netatmo_noise")
if (DisplayConfig){
fmt.Println("graph_title Sonometer (noise level)")
fmt.Println("graph_vlabel dB")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("noise_%v.label %v\n", key, value.name)
fmt.Printf("noise_%v.draw LINE1\n", key)
}
}else{
for key, value := range m_data {
fmt.Printf("noise_%v.value %.0f\n", key, value.data)
}
}
}
case "Pressure":
if (configGlobal.Sensors.Pressure) {
fmt.Println("multigraph netatmo_pressure")
if (DisplayConfig){
fmt.Println("graph_title Pressure")
fmt.Println("graph_vlabel mmHg")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("pressure_%v.label %v\n", key, value.name)
fmt.Printf("pressure_%v.draw LINE1\n", key)
}
}else{
for key, value := range m_data {
fmt.Printf("pressure_%v.value %00.2f\n", key, value.data*0.75006375541921)
}
}
}
case "Humidity":
if (configGlobal.Sensors.Humidity) {
fmt.Println("multigraph netatmo_humidity")
if (DisplayConfig){
fmt.Println("graph_title Humidity")
fmt.Println("graph_vlabel percent")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("hum_%v.label %v\n", key, value.name)
fmt.Printf("hum_%v.min 0\n", key)
fmt.Printf("hum_%v.max 100\n", key)
fmt.Printf("hum_%v.draw LINE1\n", key)
}
}else{
for key, value := range m_data {
fmt.Printf("hum_%v.value %.0f\n", key, value.data)
}
}
}
case "Temperature":
if (configGlobal.Sensors.Temperature) {
fmt.Println("multigraph netatmo_temp")
if (DisplayConfig){
fmt.Println("graph_title Temperature")
fmt.Println("graph_vlabel °C")
fmt.Println("graph_category netatmo")
fmt.Println("graph_scale no")
for key, value := range m_data {
fmt.Printf("temp_%v.label %v\n", key, value.name)
fmt.Printf("temp_%v.draw LINE1\n", key)
}
}else{
for key, value := range m_data {
fmt.Printf("temp_%v.value %.1f\n", key, value.data)
}
}
}
case "WindStrength":
if (configGlobal.Sensors.WindStrength) {
fmt.Println("multigraph netatmo_wind")
if (DisplayConfig){
fmt.Println("graph_title Wind speed")
fmt.Println("graph_vlabel km/h")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("wind_speed_%v.label %v (speed)\n", key, value.name)
fmt.Printf("wind_gust_%v.label %v (gust)\n", key, value.name)
fmt.Printf("wind_gust_%v.draw LINE1\n", key)
fmt.Printf("wind_speed_%v.draw LINE1\n", key)
}
}else{
for key, _ := range m_data {
fmt.Printf("wind_speed_%v.value %.0f\n", key, modulesData["WindStrength"][key].data)
fmt.Printf("wind_gust_%v.value %.0f\n", key, modulesData["GustStrength"][key].data)
}
}
}
case "Rain1Day":
if (configGlobal.Sensors.Rain) {
fmt.Println("multigraph netatmo_rain")
if (DisplayConfig){
fmt.Println("graph_title Amount of rain")
fmt.Println("graph_vlabel mm")
fmt.Println("graph_scale no")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("rain_daily_%v.label %v (daily)\n", key, value.name)
fmt.Printf("rain_hourly_%v.label %v (hourly)\n", key, value.name)
fmt.Printf("rain_%v.label %v (last rain measured)\n", key, value.name)
//fmt.Printf("rain_daily_%v.type DDERIVE\n", key)
//fmt.Printf("rain_hourly_%v.type DDERIVE\n", key)
//fmt.Printf("rain_%v.type DDERIVE\n", key)
fmt.Printf("rain_daily_%v.draw AREA\n", key)
fmt.Printf("rain_hourly_%v.draw AREA\n", key)
fmt.Printf("rain_%v.draw AREA\n", key)
//fmt.Printf("rain_daily_%v.draw LINE1\n", key)
//fmt.Printf("rain_hourly_%v.draw LINE1\n", key)
//fmt.Printf("rain_%v.draw LINE1\n", key)
//fmt.Printf("rain_daily_%v.min 0.00\n", key)
//fmt.Printf("rain_hourly_%v.min 0.00\n", key)
//fmt.Printf("rain_%v.min 0.00\n", key)
}
}else{
for key, _ := range m_data {
fmt.Printf("rain_daily_%v.value %.2f\n", key, modulesData["Rain1Day"][key].data)
fmt.Printf("rain_hourly_%v.value %.2f\n", key, modulesData["Rain1Hour"][key].data)
fmt.Printf("rain_%v.value %.2f\n", key, modulesData["Rain"][key].data)
}
}
}
case "WindAngle":
if (configGlobal.Sensors.WindAngle) {
fmt.Println("multigraph netatmo_wind_angl")
if (DisplayConfig){
fmt.Println("graph_title Wind direction")
fmt.Println("graph_vlabel degrees")
fmt.Println("graph_category netatmo")
for key, value := range m_data {
fmt.Printf("wind_angle_%v.label %v\n", key, value.name)
fmt.Printf("gust_angle_%v.label %v (gust)\n", key, value.name)
fmt.Printf("wind_angle_%v.min 0\n", key)
fmt.Printf("wind_angle_%v.max 360\n", key)
fmt.Printf("gust_angle_%v.min 0\n", key)
fmt.Printf("gust_angle_%v.max 360\n", key)
fmt.Printf("gust_angle_%v.draw LINE1\n", key)
fmt.Printf("wind_angle_%v.draw LINE1\n", key)
}
}else{
for key, _ := range m_data {
fmt.Printf("wind_angle_%v.value %v\n", key, modulesData["WindAngle"][key].data)
fmt.Printf("gust_angle_%v.value %v\n", key, modulesData["GustAngle"][key].data)
}
}
}
}
fmt.Println("")
}
}