-
Notifications
You must be signed in to change notification settings - Fork 1
/
tempest.go
225 lines (183 loc) · 6.22 KB
/
tempest.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
package main
import (
"encoding/json"
"fmt"
kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
line_metric_encoder "github.com/wz2b/telegraf-execd-toolkit/line-metric-encoder"
tlogger "github.com/wz2b/telegraf-execd-toolkit/telegraf-logger"
"io"
"net"
"os"
"time"
)
var config TempestAgentConfig
const DAYS = 24 * time.Hour
var klog kitlog.Logger
func main() {
logFactory, err := tlogger.NewTelegrafLoggerConfiguration(true)
if err != nil {
panic(err)
}
klog := logFactory.Create()
mp := line_metric_encoder.NewMetricEncoderPool()
sock, err := net.ListenPacket("udp", ":50222")
if err != nil {
level.Error(klog).Log("msg", "Unable to listen()", "error", err)
os.Exit(1)
}
byteBuf := make([]byte, 2000)
for {
n, _, err := sock.ReadFrom(byteBuf)
if err != nil {
level.Error(klog).Log("msg", "Could not read from socket", "error", err)
os.Exit(1)
}
bytes := byteBuf[:n]
/*
* Decode the envelope. This will tell us what type of message this is.
*/
var message TempestMessage
err = json.Unmarshal(bytes, &message)
if err != nil {
level.Warn(klog).Log("msg", "Error unmarshalling message", "error", err)
continue
}
if message.MessageType == "rapid_wind" {
writeRapidWind(mp, os.Stdout, bytes)
} else if message.MessageType == "hub_status" {
writeHubStatus(mp, os.Stdout, bytes)
} else if message.MessageType == "device_status" {
writeDeviceStatus(mp, os.Stdout, bytes)
} else if message.MessageType == "obs_st" {
writeStationObservation(mp, os.Stdout, bytes)
} else if message.MessageType == "evt_strike" {
writeLightningStrike(mp, os.Stdout, bytes)
} else {
klog.Log("msg",
fmt.Sprintf("Received: unknown message type %s\n", message.MessageType))
}
}
}
func writeLightningStrike(mp *line_metric_encoder.MetricEncoderPool, ostream *os.File, bytes []byte) {
var strike LightningStrikeEvent
err := json.Unmarshal(bytes, &strike)
if err != nil {
level.Debug(klog).Log("msg", "Unable to parse evt_strike", "error", err)
} else {
metric := mp.NewMetric("lightning_strike")
metric.AddTag("station", strike.SerialNumber)
metric.AddField("distance_km", strike.GetDistanceKm())
metric.AddField("energy", strike.GetStrikeEnergy())
_, err := metric.Write(ostream)
if err != nil {
level.Warn(klog).Log("msg", "unable to write evt_strike metric", "error", err)
}
}
}
func writeHubStatus(mp *line_metric_encoder.MetricEncoderPool, ostream io.Writer, bytes []byte) {
var hub HubStatus
err := json.Unmarshal(bytes, &hub)
if err != nil {
level.Warn(klog).Log("msg", "unable to parse hub status event", "error", err)
} else {
metric := mp.NewMetric("hub_status")
metric.WithTag("hub", hub.SerialNumber).WithField("seq", hub.Seq).WithField("rssi", hub.RSSI)
_, err := metric.Write(ostream)
if err != nil {
level.Warn(klog).Log("msg", "unable to write hub status metric", "error", err)
}
}
}
func writeRapidWind(mp *line_metric_encoder.MetricEncoderPool, ostream io.Writer, ByteBuf []byte) {
var wind RapidWind
err := json.Unmarshal(ByteBuf, &wind)
if err != nil {
level.Warn(klog).Log("msg", "unable to parse rapid wind event", "error", err)
} else {
metric := mp.NewMetric("wind")
v, e := wind.Speed()
metric.WithTag("hub", wind.HubSN)
metric.WithTag("station", wind.SerialNumber)
addFieldIfNoError(metric, "wind_speed", v, e)
v, e = wind.Direction()
addFieldIfNoError(metric, "wind_dir", v, e)
_, err := metric.Write(ostream)
if err != nil {
level.Warn(klog).Log("msg", "unable to write hub status metric", "error", err)
}
}
}
func addFieldIfNoError(metric *line_metric_encoder.WrappedMetric, key string, value interface{}, err error) {
if err != nil {
metric.WithField(key, value)
}
}
func writeStationObservation(mp *line_metric_encoder.MetricEncoderPool, ostream io.Writer, bytes []byte) {
var observation StationObservation
err := json.Unmarshal(bytes, &observation)
if err != nil {
level.Warn(klog).Log("msg", "unable to parse station observation event", "error", err)
return
}
now := time.Now()
numObs := observation.NumObservations()
for obs := 0; obs < numObs; obs++ {
t, err := observation.Time(obs)
if err != nil {
level.Warn(klog).Log("msg", "unable to parse timestamp from observation", "error", err)
return
}
if t == nil {
t = &now
}
metric := mp.NewMetric("observation").WithTime(*t)
metric.AddTag("station", observation.SerialNumber)
v, e := observation.AirTemp(obs)
addFieldIfNoError(metric, "temperature", v, e)
v, e = observation.RelativeHumidity(obs)
addFieldIfNoError(metric, "humidity", v, e)
v, e = observation.StationPressure(obs)
addFieldIfNoError(metric, "pressure", v, e)
v, e = observation.WindAvg(obs)
addFieldIfNoError(metric, "wind_spd", v, e)
v, e = observation.WindGust(obs)
addFieldIfNoError(metric, "wind_gust", v, e)
v, e = observation.WindLull(obs)
addFieldIfNoError(metric, "wind_lull", v, e)
v, e = observation.WindDir(obs)
addFieldIfNoError(metric, "wind_dir", v, e)
v, e = observation.RainPreviousMinute(obs)
addFieldIfNoError(metric, "rain_previous_min", v, e)
v, e = observation.LightningStrikeCount(obs)
addFieldIfNoError(metric, "lightning_strikes", v, e)
v, e = observation.UV(obs)
addFieldIfNoError(metric, "uv", v, e)
v, e = observation.Illuminance(obs)
addFieldIfNoError(metric, "illuminance", v, e)
v, e = observation.SolarRadiation(obs)
addFieldIfNoError(metric, "solar_radiation", v, e)
_, err = metric.Write(ostream)
if err != nil {
level.Warn(klog).Log("msg", "unable to write station observation metric", "error", err)
}
}
}
func writeDeviceStatus(mp *line_metric_encoder.MetricEncoderPool, ostream io.Writer, bytes []byte) {
var status DeviceStatus
err := json.Unmarshal(bytes, &status)
if err != nil {
level.Warn(klog).Log("msg", "unable to parse device status event", "error", err)
return
}
metric := mp.NewMetric("device_status")
metric.AddTag("station", status.SerialNumber)
metric.AddField("sensor_status", status.SensorStatus)
metric.AddField("rssi", status.RSSI)
metric.AddField("hub_rssi", status.HubRSSI)
metric.AddField("battery", status.Voltage)
_, err = metric.Write(ostream)
if err != nil {
level.Warn(klog).Log("msg", "unable to write device status metric", "error", err)
}
}