-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdebug.go
More file actions
572 lines (507 loc) · 15.4 KB
/
debug.go
File metadata and controls
572 lines (507 loc) · 15.4 KB
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package main
import (
"cmp"
"html/template"
"maps"
"net/http"
"net/netip"
"regexp"
"slices"
"time"
"github.com/miekg/dns"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/andrew-d/proxmox-service-discovery/internal/buildtags"
"github.com/andrew-d/proxmox-service-discovery/internal/rghandlers"
)
// setupDebugHandlers sets up the HTTP debug server handlers
func (s *server) setupDebugHandlers() {
// Root page
s.debugMux.HandleFunc("/", s.handleDebugRoot)
// Config page
s.debugMux.HandleFunc("/config", s.handleDebugConfig)
// DNS records page
s.debugMux.HandleFunc("/dns", s.handleDebugDNS)
// Health check endpoint
s.debugMux.HandleFunc("/health", s.handleDebugHealth)
// Version endpoint
s.debugMux.HandleFunc("/version", s.handleDebugVersion)
// Prometheus metrics
s.debugMux.Handle("/metrics", promhttp.Handler())
}
// StartDebugServer starts the HTTP debug server if configured
func (s *server) StartDebugServer(rg *run.Group) {
if s.debugAddr == "" || s.debugStarted {
return
}
debugServer := &http.Server{
Addr: s.debugAddr,
Handler: s.debugMux,
}
logger.Info("starting HTTP debug server", "addr", s.debugAddr)
rg.Add(rghandlers.HTTPServer(debugServer))
s.debugStarted = true
}
// Template for the debug pages
// Base layout
const baseTmplStr = `
<!DOCTYPE html>
<html>
<head>
<title>{{.Title}} - Proxmox Service Discovery</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
:root {
--color-text: #333;
--color-heading: #2c3e50;
--color-link: #3498db;
--color-border: #eee;
--color-table-border: #ddd;
--color-light-bg: #f8f9fa;
--color-hover-bg: #f5f5f5;
--color-badge-bg: #eee;
--color-badge-text: white;
--color-info: #3498db;
--color-success: #2ecc71;
--color-warning: 243, 156, 18;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
line-height: 1.5;
margin: 20px;
color: var(--color-text);
}
h1, h2 { margin-top: 1em; color: var(--color-heading); }
nav {
margin: 20px 0;
padding: 10px 0;
border-bottom: 1px solid var(--color-border);
}
nav a {
margin-right: 15px;
color: var(--color-link);
text-decoration: none;
font-weight: 500;
}
nav a:hover { text-decoration: underline; }
table {
border-collapse: collapse;
width: 100%;
margin: 20px 0;
}
th, td {
text-align: left;
padding: 12px;
border-bottom: 1px solid var(--color-table-border);
}
tr.warning {
background-color: rgba(var(--color-warning), 0.7);
}
th {
background-color: var(--color-light-bg);
font-weight: 600;
}
tr:hover { background-color: var(--color-hover-bg); }
tr.warning:hover {
background-color: rgba(var(--color-warning), 0.9);
}
.container { max-width: 1200px; margin: 0 auto; }
.badge {
display: inline-block;
padding: 3px 7px;
font-size: 12px;
font-weight: 600;
border-radius: 3px;
background-color: var(--color-badge-bg);
}
.badge-info { background-color: var(--color-info); color: var(--color-badge-text); }
.badge-success { background-color: var(--color-success); color: var(--color-badge-text); }
.badge-warning { background-color: rgb(var(--color-warning)); color: var(--color-badge-text); }
pre {
background-color: var(--color-light-bg);
padding: 15px;
border-radius: 5px;
overflow-x: auto;
}
</style>
</head>
<body>
<div class="container">
<h1>{{.Title}}</h1>
<nav>
<a href="/">Home</a>
<a href="/config">Configuration</a>
<a href="/dns">DNS Records</a>
<a href="/metrics">Metrics</a>
<a href="/health">Health Check</a>
<a href="/version">Version</a>
</nav>
{{template "content" .}}
</div>
</body>
</html>
`
// Home page template
const homeTmplStr = `
{{define "content"}}
<p>Welcome to the debug interface for Proxmox Service Discovery.</p>
<p>Use the navigation links above to access different debug pages.</p>
<h2>Quick Status</h2>
<ul>
<li>DNS Zone: {{.Server.DnsZone}}</li>
<li>Proxmox Host: {{.Server.Host}}</li>
<li>Records: {{.RecordCount}} DNS entries</li>
<li>Last Updated: {{if .LastUpdated.IsZero}}Never{{else}}{{.LastUpdated.Format "2006-01-02 15:04:05"}}{{end}}</li>
<li>Version: {{.Version}}</li>
</ul>
{{end}}
`
// Configuration page template
const configTmplStr = `
{{define "content"}}
<h2>Service Configuration</h2>
<table>
<tr><th>Setting</th><th>Value</th></tr>
<tr><td>DNS Zone</td><td>{{.Server.DnsZone}}</td></tr>
<tr><td>Proxmox Host</td><td>{{.Server.Host}}</td></tr>
<tr><td>Debug Address</td><td>{{.Server.DebugAddr}}</td></tr>
{{if .Server.TLSNoVerify}}
<tr class="warning"><td>TLS No Verify</td><td>Enabled</td></tr>
{{end}}
</table>
<h2>Filter Configuration</h2>
<table>
<tr><th>Filter</th><th>Value</th></tr>
<tr><td>Filter Type</td><td>
{{if .FilterConfig.Type}}
{{.FilterConfig.Type}}
{{else}}
<em>All</em>
{{end}}
</td></tr>
<tr><td>Include Tags</td><td>
{{if .FilterConfig.IncludeTags}}
{{range .FilterConfig.IncludeTags}}
<span class="badge">{{.}}</span>
{{end}}
{{else}}
<em>None</em>
{{end}}
</td></tr>
<tr><td>Include Tag Regexes</td><td>
{{if .FilterConfig.IncludeTagsRe}}
{{range .FilterConfig.IncludeTagsRe}}
<span class="badge">{{.String}}</span>
{{end}}
{{else}}
<em>None</em>
{{end}}
</td></tr>
<tr><td>Exclude Tags</td><td>
{{if .FilterConfig.ExcludeTags}}
{{range .FilterConfig.ExcludeTags}}
<span class="badge">{{.}}</span>
{{end}}
{{else}}
<em>None</em>
{{end}}
</td></tr>
<tr><td>Exclude Tag Regexes</td><td>
{{if .FilterConfig.ExcludeTagsRe}}
{{range .FilterConfig.ExcludeTagsRe}}
<span class="badge">{{.String}}</span>
{{end}}
{{else}}
<em>None</em>
{{end}}
</td></tr>
<tr><td>Include CIDRs</td><td>
{{if .FilterConfig.IncludeCIDRs}}
{{range .FilterConfig.IncludeCIDRs}}
<span class="badge">{{.String}}</span>
{{end}}
{{else}}
<em>None</em>
{{end}}
</td></tr>
<tr><td>Exclude CIDRs</td><td>
{{if .FilterConfig.ExcludeCIDRs}}
{{range .FilterConfig.ExcludeCIDRs}}
<span class="badge">{{.String}}</span>
{{end}}
{{else}}
<em>None</em>
{{end}}
</td></tr>
</table>
{{end}}
`
// DNS records page template
const dnsTmplStr = `
{{define "content"}}
<h2>DNS Records <span class="badge badge-info">{{.Records | len}}</span></h2>
<table>
<tr>
<th>FQDN</th>
<th>Type</th>
<th>IP Addresses</th>
</tr>
{{range .Records}}
<tr>
<td>{{.FQDN}}</td>
<td>{{.Type}}</td>
<td>
{{range .Addresses}}
{{.}}<br>
{{end}}
</td>
</tr>
{{end}}
</table>
{{with .NoAddrs}}
<h2>FQDNs with No Addresses <span class="badge badge-warning">{{. | len}}</span></h2>
<p>
The following FQDNs have no associated IP addresses:
</p>
<table>
<tr>
<th>FQDN</th>
</tr>
{{range .}}
<tr>
<td>{{.}}</td>
</tr>
{{end}}
</table>
{{end}}
{{end}}
`
// Version page template
const versionTmplStr = `
{{define "content"}}
<h2>Version Information</h2>
<p>Current version: <span class="badge badge-info">{{.Version}}</span></p>
<p>Development mode: <span class="badge">{{.IsDev}}</span></p>
{{end}}
`
var (
// Create the base template
baseTemplate = template.Must(template.New("base").Parse(baseTmplStr))
// Add the content templates
homeTemplate = template.Must(template.Must(baseTemplate.Clone()).Parse(homeTmplStr))
configTemplate = template.Must(template.Must(baseTemplate.Clone()).Parse(configTmplStr))
dnsTemplate = template.Must(template.Must(baseTemplate.Clone()).Parse(dnsTmplStr))
versionTemplate = template.Must(template.Must(baseTemplate.Clone()).Parse(versionTmplStr))
)
// Data structures for templates
// baseTemplateData represents the common data for all templates
type baseTemplateData struct {
Title string
Version string
IsDev bool
}
// homeTemplateData represents the data for the home page template
type homeTemplateData struct {
baseTemplateData
Server serverInfo
RecordCount int
LastUpdated time.Time
}
// configTemplateData represents the data for the configuration page template
type configTemplateData struct {
baseTemplateData
Server serverInfo
FilterConfig filterConfigInfo
}
// dnsTemplateData represents the data for the DNS records page template
type dnsTemplateData struct {
baseTemplateData
Records []dnsRecordInfo
NoAddrs []string
}
// serverInfo represents server information for templates
type serverInfo struct {
Host string
DnsZone string
DebugAddr string
TLSNoVerify bool
}
// filterConfigInfo represents filter configuration for templates
type filterConfigInfo struct {
Type string
IncludeTags []string
IncludeTagsRe []*regexp.Regexp
ExcludeTags []string
ExcludeTagsRe []*regexp.Regexp
IncludeCIDRs []netip.Prefix
ExcludeCIDRs []netip.Prefix
}
// dnsRecordInfo represents a DNS record for templates
type dnsRecordInfo struct {
FQDN string
Type string
Addresses []string
}
// handleDebugRoot handles the debug server root page
func (s *server) handleDebugRoot(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
s.mu.RLock()
recordCount := len(s.records)
lastUpdated := s.lastInventoryUpdate
s.mu.RUnlock()
data := homeTemplateData{
baseTemplateData: baseTemplateData{
Title: "Debug Server",
Version: buildtags.Version,
IsDev: buildtags.IsDev,
},
Server: serverInfo{
Host: s.host,
DnsZone: s.dnsZone,
DebugAddr: s.debugAddr,
},
RecordCount: recordCount,
LastUpdated: lastUpdated,
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
if err := homeTemplate.Execute(w, data); err != nil {
logger.Error("error executing home template", "error", err)
}
}
// handleDebugConfig handles the configuration debug page
func (s *server) handleDebugConfig(w http.ResponseWriter, r *http.Request) {
// Determine if we're verifying TLS by poking into the http client we
// have to see if the flag is set.
//
// TODO: this is a bit abstraction-breaking and we should figure out a
// better way to do this.
tlsNoVerify := false
if s.httpc != nil {
if tr, ok := s.httpc.Transport.(*http.Transport); ok {
tlsNoVerify = tr.TLSClientConfig != nil && tr.TLSClientConfig.InsecureSkipVerify
}
}
data := configTemplateData{
baseTemplateData: baseTemplateData{
Title: "Configuration",
Version: buildtags.Version,
IsDev: buildtags.IsDev,
},
Server: serverInfo{
Host: s.host,
DnsZone: s.dnsZone,
DebugAddr: s.debugAddr,
TLSNoVerify: tlsNoVerify,
},
FilterConfig: filterConfigInfo{
Type: s.fc.Type,
IncludeTags: s.fc.IncludeTags,
IncludeTagsRe: s.fc.IncludeTagsRe,
ExcludeTags: s.fc.ExcludeTags,
ExcludeTagsRe: s.fc.ExcludeTagsRe,
IncludeCIDRs: s.fc.IncludeCIDRs,
ExcludeCIDRs: s.fc.ExcludeCIDRs,
},
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
if err := configTemplate.Execute(w, data); err != nil {
logger.Error("error executing config template", "error", err)
}
}
// handleDebugDNS handles the DNS records debug page
func (s *server) handleDebugDNS(w http.ResponseWriter, r *http.Request) {
s.mu.RLock()
defer s.mu.RUnlock()
// Create a sorted list of records
fqdns := slices.Collect(maps.Keys(s.records))
slices.Sort(fqdns)
var records []dnsRecordInfo
for _, fqdn := range fqdns {
rec := s.records[fqdn]
// Group answers by record type
recordTypes := make(map[string][]string)
for _, answer := range rec.Answers {
header := answer.Header()
recordType := dns.TypeToString[header.Rrtype]
// Extract the IP address based on the record type
var ipAddr string
switch header.Rrtype {
case dns.TypeA:
ipAddr = answer.(*dns.A).A.String()
case dns.TypeAAAA:
ipAddr = answer.(*dns.AAAA).AAAA.String()
default:
// Handle other record types if needed
ipAddr = "unknown"
}
recordTypes[recordType] = append(recordTypes[recordType], ipAddr)
}
// Create record info for each record type
for recordType, addresses := range recordTypes {
records = append(records, dnsRecordInfo{
FQDN: fqdn,
Type: recordType,
Addresses: addresses,
})
}
}
// Sort records to ensure we have a deterministic order
slices.SortFunc(records, func(a, b dnsRecordInfo) int {
return cmp.Or(
cmp.Compare(a.FQDN, b.FQDN),
cmp.Compare(a.Type, b.Type),
)
})
// For each record, sort the list of addresses as well
for i := range records {
slices.Sort(records[i].Addresses)
}
// Sort the list of FQDNs with no addresses
noAddrs := slices.Clone(s.noAddrs)
slices.Sort(noAddrs)
data := dnsTemplateData{
baseTemplateData: baseTemplateData{
Title: "DNS Records",
Version: buildtags.Version,
IsDev: buildtags.IsDev,
},
Records: records,
NoAddrs: noAddrs,
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
if err := dnsTemplate.Execute(w, data); err != nil {
logger.Error("error executing DNS template", "error", err)
}
}
// handleDebugHealth handles the health check endpoint
func (s *server) handleDebugHealth(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"status":"ok"}`))
}
// handleDebugVersion handles the version endpoint
func (s *server) handleDebugVersion(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Accept") == "application/json" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"version":"` + buildtags.Version + `"}`))
return
}
data := baseTemplateData{
Title: "Version Information",
Version: buildtags.Version,
IsDev: buildtags.IsDev,
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
if err := versionTemplate.Execute(w, data); err != nil {
logger.Error("error executing version template", "error", err)
}
}