forked from JimZAH/DVSMON
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
231 lines (197 loc) · 5.22 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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/gocolly/colly/v2"
)
/* Store the calls locally */
var (
monitor Monitor
)
type Call struct {
Num string `json:"num"`
Date string `json:"date"`
Name string `json:"name"`
Call string `json:"call"`
Id string `json:"id"`
Sec string `json:"sec"`
Slot string `json:"slot"`
Talkgroup string `json:"talkgroup"`
}
/* Monitor data */
type Monitor struct {
Calls []Call
Config Config
Cache_stale bool
Last_access time.Time
Mu sync.Mutex
Stats Stats
Uptime time.Time
Users Users
User_name map[string]string
User_update time.Time
}
/* Store API Stats */
type Stats struct {
Cache bool `json:"stale_cache"`
Hits uint64 `json:"hits"`
Refresh uint64 `json:"refresh"`
Uptime uint64 `json:"uptime"`
}
/*
User data from radioid.net database
*/
type Users struct {
Users []struct {
First_name string `json:"fname"`
Name string `json:"name"`
Country string `json:"country"`
Callsign string `json:"callsign"`
City string `json:"city"`
Surname string `json:"surname"`
Radio_id uint32 `json:"radio_id"`
Id int `json:"id"`
State string `json:"state"`
} `json:"users"`
}
type Config struct {
Last_access time.Duration `json:"last_access"`
Page string `json:"page"`
Reload time.Duration `json:"reload"`
Users string `json:"users"`
Users_reload int64 `json:"users_reload"`
}
/* Pull data from radioid.net user dump */
func nameLookup(config *Config) {
/* Only update cache if timer has expired or we have no data
if we fail just silently return */
if time.Since(monitor.User_update) >= time.Second*time.Duration(config.Users_reload) || len(monitor.Users.Users) == 0 {
monitor.User_update = time.Now()
client := &http.Client{}
reqs, err := http.NewRequest("GET", config.Users, nil)
if err != nil {
return
}
reqs.Header.Add("Content-Type", "application/json")
resp, err := client.Do(reqs)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
if err := json.Unmarshal(body, &monitor.Users); err != nil {
fmt.Println(err)
}
for _, u := range monitor.Users.Users {
monitor.User_name[strconv.Itoa(u.Id)] = u.Name
}
}
}
func req(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
monitor.Mu.Lock()
monitor.Last_access = time.Now()
monitor.Stats.Hits++
/* Wait for new data */
if monitor.Cache_stale {
for {
monitor.Mu.Unlock()
time.Sleep(time.Millisecond * 10)
monitor.Mu.Lock()
if !monitor.Cache_stale {
break
}
}
}
json.NewEncoder(w).Encode(monitor.Calls)
monitor.Mu.Unlock()
}
func getStats(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
monitor.Mu.Lock()
monitor.Stats.Uptime = uint64(time.Since(monitor.Uptime)) / 100_000_000_0
json.NewEncoder(w).Encode(monitor.Stats)
monitor.Mu.Unlock()
}
func serv() {
srv := &http.Server{
Addr: ":8181",
}
http.HandleFunc("/monitor", req)
http.HandleFunc("/monitor/stats", getStats)
srv.ListenAndServe()
}
/* Scrape the dashboard */
func scrape(config *Config, monitor *Monitor) {
var new_calls []Call
c := colly.NewCollector()
nameLookup(config)
c.OnHTML("table > tbody", func(h *colly.HTMLElement) {
h.ForEach("tr", func(_ int, el *colly.HTMLElement) {
if el.ChildText("td:nth-child(1)") != "" {
this_call := Call{Num: el.ChildText("td:nth-child(1)"), Date: el.ChildText("td:nth-child(3)"), Call: el.ChildText("td:nth-child(8)"), Id: el.ChildText("td:nth-child(7)"), Sec: el.ChildText("td:nth-child(4)"), Slot: el.ChildText("td:nth-child(10)"), Talkgroup: el.ChildText("td:nth-child(11)")}
this_call.Name = monitor.User_name[this_call.Id]
new_calls = append(new_calls, this_call)
}
})
})
c.Visit(config.Page)
monitor.Mu.Lock()
monitor.Calls = new_calls
monitor.Stats.Refresh++
monitor.Mu.Unlock()
}
func (config *Monitor) updateCheck() bool {
monitor.Mu.Lock()
monitor.Cache_stale = time.Since(monitor.Last_access) >= time.Minute*monitor.Config.Last_access
monitor.Stats.Cache = monitor.Cache_stale
monitor.Mu.Unlock()
return monitor.Cache_stale
}
func main() {
cFileL := "./dvsmon.conf"
if len(os.Args) > 2 {
if !strings.ContainsAny(os.Args[1], "\\!@#$%^&*():;'\"|<>?") {
cFileL = os.Args[1]
}
}
cFile, err := os.ReadFile(cFileL)
if err != nil {
fmt.Println("Can't open config file! Expecting .dvsmon.conf: ", err)
os.Exit(-1)
}
if err := json.Unmarshal(cFile, &monitor.Config); err != nil {
fmt.Println("Trouble parsing config file: ", err)
}
monitor.Last_access = time.Now()
last_update := time.Now()
monitor.Uptime = time.Now()
monitor.User_update = time.Now()
monitor.User_name = make(map[string]string)
/* Serve the API service */
go serv()
for {
time.Sleep(time.Millisecond * 256)
/* If we're idle don't scrape */
if monitor.updateCheck() {
continue
}
if time.Since(last_update) >= time.Second*monitor.Config.Reload {
last_update = time.Now()
go scrape(&monitor.Config, &monitor)
}
}
}