-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdump1090-exporter.go
155 lines (134 loc) · 4.87 KB
/
dump1090-exporter.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"time"
geohash "github.com/mmcloughlin/geohash"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type dump900AircraftStruct struct {
Now float64 `json:"now"`
Messages int `json:"messages"`
Aircraft []struct {
Hex string `json:"hex,omitempty"`
Flight string `json:"flight,omitempty"`
AltBaro int `json:"alt_baro,omitempty"`
AltGeom int `json:"alt_geom,omitempty"`
Altitude int `json:"altitude,omitempty"`
Gs float64 `json:"gs,omitempty"`
Ias int `json:"ias,omitempty"`
Tas int `json:"tas,omitempty"`
Mach float64 `json:"mach,omitempty"`
Track float64 `json:"track,omitempty"`
TrackRate float64 `json:"track_rate,omitempty"`
Roll float64 `json:"roll,omitempty"`
MagHeading float64 `json:"mag_heading,omitempty"`
BaroRate int `json:"baro_rate,omitempty"`
GeomRate int `json:"geom_rate,omitempty"`
Squawk string `json:"squawk,omitempty"`
Category string `json:"category,omitempty"`
NavQnh float64 `json:"nav_qnh,omitempty"`
NavAltitudeMcp int `json:"nav_altitude_mcp,omitempty"`
Lat float64 `json:"lat,omitempty"`
Lon float64 `json:"lon,omitempty"`
Nic int `json:"nic,omitempty"`
Rc int `json:"rc,omitempty"`
SeenPos float64 `json:"seen_pos,omitempty"`
Version int `json:"version,omitempty"`
NacP int `json:"nac_p,omitempty"`
NacV int `json:"nac_v,omitempty"`
Sil int `json:"sil,omitempty"`
SilType string `json:"sil_type,omitempty"`
Mlat []interface{} `json:"mlat,omitempty"`
Tisb []interface{} `json:"tisb,omitempty"`
Messages int `json:"messages,omitempty"`
Seen float64 `json:"seen,omitempty"`
Rssi float64 `json:"rssi,omitempty"`
Emergency string `json:"emergency,omitempty"`
NavHeading float64 `json:"nav_heading,omitempty"`
NicBaro int `json:"nic_baro,omitempty"`
Gva int `json:"gva,omitempty"`
Sda int `json:"sda,omitempty"`
} `json:"aircraft"`
}
var addr = flag.String("listen-address", ":9467",
"The address to listen on for HTTP requests.")
func main() {
flag.Parse()
trackingACcount := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "dump1090_ac_tracking_now_count",
Help: "Count of tracking ACs in realtime",
})
prometheus.MustRegister(trackingACcount)
catchedMessages := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "dump1090_messages_catched_count",
Help: "Total messages catched with dump1090",
})
prometheus.MustRegister(catchedMessages)
var trackingACs *prometheus.GaugeVec
trackingACs = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dump1090_ac_tracking_flights",
Help: "Tracking ACs",
},
[]string{"flight", "geohash", "lat", "long", "altitude", "squawk"},
)
aircraftFile := os.Getenv("AIRCRAFT_JSON")
if len(aircraftFile) > 0 {
fmt.Println("AIRCRAFT_JSON:", aircraftFile)
} else {
aircraftFile = "/run/dump1090-fa/aircraft.json"
}
go func() {
for {
aircraftFile, err := os.Open(aircraftFile)
if err != nil {
log.Fatalln(err)
}
defer aircraftFile.Close()
byteValue, _ := io.ReadAll(aircraftFile)
var aircrafts dump900AircraftStruct
err = json.Unmarshal(byteValue, &aircrafts)
if err != nil {
log.Println("Error unmarshaling aircraft.json")
}
trackingACs.Reset()
trackingACcount.Set(0)
for _, aircraft := range aircrafts.Aircraft {
if len(aircraft.Flight) > 0 {
trackingACcount.Inc()
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
log.Fatal(err)
}
aircraftFlight := reg.ReplaceAllString(aircraft.Flight, "")
geoHash := geohash.Encode(aircraft.Lat, aircraft.Lon)
alt := aircraft.AltGeom
if aircraft.Altitude > 0 {
alt = aircraft.Altitude
}
trackingACs.WithLabelValues(aircraftFlight, geoHash, fmt.Sprintf("%2.6f", aircraft.Lat), fmt.Sprintf("%2.6f", aircraft.Lon), fmt.Sprintf("%d", alt), aircraft.Squawk).Set(1)
prometheus.Register(trackingACs)
}
}
catchedMessages.Set(float64(aircrafts.Messages))
time.Sleep(1000 * time.Millisecond)
aircraftFile.Close()
}
}()
http.Handle("/metrics", promhttp.Handler())
log.Printf("Starting web server at %s\n", *addr)
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Printf("http.ListenAndServer: %v\n", err)
}
}