-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
482 lines (388 loc) · 13.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// main.go
package main
import (
"encoding/json"
"errors"
"flag"
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/cabify/gotoprom"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type AircraftList struct {
Messages float64 `json:"messages,int"`
Aircraft []Aircraft `json:"aircraft"`
}
type Aircraft struct {
Hex string `json:"hex"`
Flight string `json:"flight,omitempty"`
AltoBaro uint16 `json:"alt_baro,omitempty"`
AltoGeom uint16 `json:"alt_geom,omitempty"`
GroundSpeed float64 `json:"gs,omitempty"`
BaroRate int16 `json:"baro_rate,omitempty"`
Latitude float64 `json:"lat,omitempty"`
Longitude float64 `json:"lon,omitempty"`
NavHeading float64 `json:"nav_heading,omitempty"`
RSSi float64 `json:"rssi,omitempty"`
Category string `json:"category,omitempty"`
Emergency string `json:"emergency,omitempty"`
Messages float64 `json:"messages,omitempty"`
Seen float64 `json:"seen,omitempty"`
SeenPos float64 `json:"seen_pos,omitempty"`
Mlat []string `json:"mlat,omitempty"`
}
type Coordinate struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
var (
ReceiverLat float64
ReceiverLon float64
)
const radius = 6371.0e3
var myClient = &http.Client{Timeout: 10 * time.Second}
var directionLut = [17]string{"N", "NE", "NE", "E", "E", "SE", "SE", "S", "S", "SW", "SW", "W", "W", "NW", "NW", "N"}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func getJson(url string, target interface{}) error {
r, err := myClient.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
func degrees2radians(degrees float64) float64 {
return degrees * math.Pi / 180
}
func relativeAngle(lat1 float64, lng1 float64, lat2 float64, lng2 float64) float64 {
deg := math.Atan((lng2-lng1)/(lat2-lat1)) * (180 / math.Pi)
if lat2 == lat1 {
if lng2 > lng1 {
return 90
} else {
return 270
}
}
if lat2 > lat1 {
return math.Mod(360+deg, 360)
} else {
return 180 + deg
}
}
func distance(lat1 float64, lng1 float64, lat2 float64, lng2 float64) float64 {
degreesLat := degrees2radians(lat2 - lat1)
degreesLong := degrees2radians(lng2 - lng1)
a := (math.Sin(degreesLat/2)*math.Sin(degreesLat/2) +
math.Cos(degrees2radians(lat1))*
math.Cos(degrees2radians(lat2))*math.Sin(degreesLong/2)*
math.Sin(degreesLong/2))
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
d := radius * c
return d
}
func relativeDirection(angle float64) string {
if angle >= 360 {
angle = 0
}
index := int(math.Abs(angle) / 22.5)
direction := directionLut[index]
return direction
}
func aircraftMetrics(aircraftList AircraftList) {
aircraft := aircraftList.Aircraft
dump1090AltBaro.Reset()
dump1090Distance.Reset()
dump1090AltGeom.Reset()
dump1090BaroRate.Reset()
dump1090GroundSpeed.Reset()
dump1090NavHeading.Reset()
dump1090Rssi.Reset()
dump1090MaxRangeDirection.Reset()
dump1090MaxRange.Reset()
var threshold float64 = 15
var aircraft_observed int = 0
var aircraft_with_mlat int = 0
var aircraft_with_pos float64 = 0
var aircraft_max_range float64 = 0
aircraft_direction := make(map[string]int)
aircraft_direction_max_range := make(map[string]float64)
for d := range directionLut {
aircraft_direction[directionLut[d]] = 0
aircraft_direction_max_range[directionLut[d]] = 0
}
for _, s := range aircraft {
labels := prometheus.Labels{"flight": strings.TrimSpace(s.Flight), "hex": s.Hex}
if s.Seen < threshold {
aircraft_observed++
if s.SeenPos < threshold {
aircraft_with_pos++
if contains(s.Mlat, "lat") {
aircraft_with_mlat++
}
if s.Latitude != 0 {
dist := distance(ReceiverLat, ReceiverLon, s.Latitude, s.Longitude)
angle := relativeAngle(ReceiverLat, ReceiverLon, s.Latitude, s.Longitude)
direction := relativeDirection(angle)
log.Debug().
Str("Flight", s.Flight).
Float64("Ground Speed", s.GroundSpeed).
Float64("Distance", dist).
Float64("Angle", angle).
Str("Direction", direction).
Send()
aircraft_direction[direction]++
dump1090CountByDirection.With(prometheus.Labels{"direction": direction, "time_period": "latest"}).Set(float64(aircraft_direction[direction]))
if dist > float64(aircraft_direction_max_range[direction]) {
aircraft_direction_max_range[direction] = dist
dump1090MaxRangeDirection.With(prometheus.Labels{"direction": direction, "time_period": "latest"}).Set(dist)
}
if dist > aircraft_max_range {
// Set Max Range Metric
aircraft_max_range = dist
dump1090MaxRange.With(prometheus.Labels{"time_period": "latest"}).Set(dist)
}
dump1090Distance.With(labels).Set(dist)
}
}
}
dump1090AltBaro.With(labels).Set(float64(s.AltoBaro))
dump1090AltGeom.With(labels).Set(float64(s.AltoGeom))
dump1090BaroRate.With(labels).Set(float64(s.BaroRate))
dump1090GroundSpeed.With(labels).Set(float64(s.GroundSpeed))
dump1090NavHeading.With(labels).Set(float64(s.NavHeading))
dump1090Rssi.With(labels).Set(float64(s.RSSi))
}
metrics.RecentAircraftObserved(statLabels{TimePeriod: "latest"}).Set(float64(aircraft_observed))
dump1090Messages.With(prometheus.Labels{"time_period": "latest"}).Set(aircraftList.Messages)
dump1090CountWithMlat.With(prometheus.Labels{"time_period": "latest"}).Set(float64(aircraft_with_mlat))
dump1090CountWithPos.With(prometheus.Labels{"time_period": "latest"}).Set(float64(aircraft_with_pos))
}
func statMetrics(stats Statistics) {
defer func() {
if r := recover(); r != nil {
log.Error().
Str("func", "statMetrics").
Msgf("Recovered from panic. Error %s", r)
}
}()
m := make(map[string]SingleStat)
// m["last5minute"] = stats.Last_5
m["last1min"] = stats.Last_1
m["latest"] = stats.Latest
for key, value := range m {
minuteLabel := statLabels{
TimePeriod: key,
}
if key != "latest" {
dump1090Messages.With(prometheus.Labels{"time_period": key}).Set(value.Messages)
if len(value.Local.Accepted) > 0 {
metrics.LocalAccepted(minuteLabel).Set(value.Local.Accepted[0])
}
metrics.LocalSignalStrength(minuteLabel).Set(value.Local.SignalStrength)
metrics.LocalStrongSignal(minuteLabel).Set(value.Local.StrongSignals)
metrics.LocalPeakSignal(minuteLabel).Set(value.Local.PeakSignal)
metrics.LocalBad(minuteLabel).Set(value.Local.Bad)
metrics.LocalModeAc(minuteLabel).Set(value.Local.ModeAc)
metrics.LocalModes(minuteLabel).Set(value.Local.Modes)
metrics.LocalNoiseLevel(minuteLabel).Set(value.Local.Noise)
metrics.LocalSamplesDropped(minuteLabel).Set(value.Local.SamplesDropped)
metrics.LocalSamplesProcessed(minuteLabel).Set(value.Local.SamplesProcessed)
metrics.LocalUnknownIcao(minuteLabel).Set(value.Local.UnknownIcao)
metrics.CprLocalSpeed(minuteLabel).Set(value.Cpr.LocalSpeed)
metrics.CprGlobalSpeed(minuteLabel).Set(value.Cpr.GlobalSpeed)
if len(value.Remote.Accepted) > 0 {
metrics.RemoteAccepted(minuteLabel).Set(value.Remote.Accepted[0])
}
metrics.RemoteBad(minuteLabel).Set(value.Remote.Bad)
metrics.RemoteModeAc(minuteLabel).Set(value.Remote.ModeAc)
metrics.RemoteModes(minuteLabel).Set(value.Remote.Modes)
metrics.RemoteUnknownIcao(minuteLabel).Set(value.Remote.UnknownIcao)
metrics.TracksAll(minuteLabel).Set(value.Track.All)
metrics.TracksSingleMessage(minuteLabel).Set(value.Track.SingleMessage)
metrics.MessagesTotal(minuteLabel).Set(value.Messages)
}
metrics.CprAirborne(minuteLabel).Set(value.Cpr.Airborne)
metrics.CprFiltered(minuteLabel).Set(value.Cpr.Filtered)
metrics.CprGlobalBad(minuteLabel).Set(value.Cpr.GlobalBad)
metrics.CprGlobalOk(minuteLabel).Set(value.Cpr.GlobalOk)
metrics.CprGlobalRange(minuteLabel).Set(value.Cpr.GlobalRange)
metrics.CprGlobalSkipped(minuteLabel).Set(value.Cpr.GlobalSkipped)
metrics.CprLocalAircraftRelative(minuteLabel).Set(value.Cpr.LocalAircraftRelative)
metrics.CprLocalOk(minuteLabel).Set(value.Cpr.LocalOk)
metrics.CprLocalRange(minuteLabel).Set(value.Cpr.LocalRange)
metrics.CprLocalReceiverRelative(minuteLabel).Set(value.Cpr.LocalReceiverRelative)
metrics.CprLocalSkipped(minuteLabel).Set(value.Cpr.LocalSkipped)
metrics.CprSurface(minuteLabel).Set(value.Cpr.Surface)
metrics.CpuBackgroundMs(minuteLabel).Set(value.Cpu.Background)
metrics.CpuDemodMs(minuteLabel).Set(value.Cpu.Demod)
metrics.CpuReaderMs(minuteLabel).Set(value.Cpu.Reader)
}
}
func readAircraftFile(path string) {
var aircraft_path string = path + "aircraft.json"
_, err := url.ParseRequestURI(aircraft_path)
if err != nil {
if _, err := os.Stat(aircraft_path); errors.Is(err, os.ErrNotExist) {
// Do something because it doesn't exist
} else {
// Open the file
jsonFile, err := os.Open(aircraft_path)
// Increment the prom read metric
opsMetrics.AirCraftFileReads().Inc()
// Print the error if that happens.
if err != nil {
log.Error().Err(err).Msg("Error opening aircraft.json")
}
// defer file close
defer jsonFile.Close()
// read file
byteValue, _ := ioutil.ReadAll(jsonFile)
// Initialize list of aircraft
var aircraftList AircraftList
// Unmarshal to aircraft list
json.Unmarshal(byteValue, &aircraftList)
aircraftMetrics(aircraftList)
}
} else {
opsMetrics.AirCraftFileReads().Inc()
aircraftList := new(AircraftList)
getJson(aircraft_path, aircraftList)
aircraftMetrics(*aircraftList)
}
}
func readStatsFile(path string) {
var stats_path string = path + "stats.json"
_, err := url.ParseRequestURI(stats_path)
if err != nil {
if _, err := os.Stat(stats_path); errors.Is(err, os.ErrNotExist) {
// Do something because it doesn't exist
} else {
// Open the file
jsonFile, err := os.Open(stats_path)
// Increment the prom read metric
opsMetrics.StatsFileReads().Inc()
// Print the error if that happens.
if err != nil {
log.Error().Err(err).Msg("Error opening stats.json")
}
// defer file close
defer jsonFile.Close()
// read file
byteValue, _ := ioutil.ReadAll(jsonFile)
// Initialize list of aircraft
var stats Statistics
// Unmarshal to aircraft list
json.Unmarshal(byteValue, &stats)
statMetrics(stats)
}
} else {
opsMetrics.StatsFileReads().Inc()
stats := new(Statistics)
getJson(stats_path, stats)
statMetrics(*stats)
}
}
func readReceiverInfo(path string) {
var receiver_path string = path + "receiver.json"
_, err := url.ParseRequestURI(receiver_path)
if err != nil {
if _, err := os.Stat(receiver_path); errors.Is(err, os.ErrNotExist) {
// Do something because it doesn't exist
} else {
// Open the file
jsonFile, err := os.Open(receiver_path)
// Print the error if that happens.
if err != nil {
log.Error().Err(err).Msg("Error opening receiver.json")
}
// defer file close
defer jsonFile.Close()
// read file
byteValue, _ := ioutil.ReadAll(jsonFile)
// // Initialize list of coordinates
var cords Coordinate
// // Unmarshal to aircraft list
json.Unmarshal(byteValue, &cords)
ReceiverLat = cords.Lat
ReceiverLon = cords.Lon
}
} else {
cords := new(Coordinate)
getJson(receiver_path, cords)
ReceiverLat = cords.Lat
ReceiverLon = cords.Lon
}
}
func readFilesTicker(path string) {
aircraftTicker := time.NewTicker(5 * time.Second)
statsTicker := time.NewTicker(30 * time.Second)
readAircraftFile(path)
readStatsFile(path)
go func() {
for {
<-aircraftTicker.C
readAircraftFile(path)
}
}()
go func() {
for {
<-statsTicker.C
readStatsFile(path)
}
}()
}
func init() {
// reg := prometheus.NewRegistry()
gotoprom.MustInit(&metrics, "dump1090")
gotoprom.MustInit(&opsMetrics, "dump1090")
// Metrics have to be registered to be exposed:
prometheus.MustRegister(dump1090AltBaro)
prometheus.MustRegister(dump1090AltGeom)
prometheus.MustRegister(dump1090BaroRate)
prometheus.MustRegister(dump1090GroundSpeed)
prometheus.MustRegister(dump1090NavHeading)
prometheus.MustRegister(dump1090Rssi)
prometheus.MustRegister(dump1090Messages)
prometheus.MustRegister(dump1090Distance)
prometheus.MustRegister(dump1090MaxRangeDirection)
prometheus.MustRegister(dump1090MaxRange)
prometheus.MustRegister(dump1090CountByDirection)
prometheus.MustRegister(dump1090CountWithPos)
prometheus.MustRegister(dump1090CountWithMlat)
// prometheus.MustRegister(dump1090Observed)
}
func main() {
path := flag.String("path", "/run/dump1090-fa/", "Path to json files. Default /run/dump1090-fa/")
port := flag.String("port", "3000", "Port to expose metrics")
debug := flag.Bool("debug", false, "sets log level to debug")
flag.Parse()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
log.Info().Msg("Path to json files:" + *path)
log.Info().Msg("Listen Port:" + *port)
readReceiverInfo(*path)
go readFilesTicker(*path)
// go flightInit()
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":"+*port, nil); err != nil {
log.Fatal().Err(err).Msg("Startup failed")
}
}