Skip to content

Commit 07b4601

Browse files
authored
feat(packetloss): add option to enable DNS resolution in MTR tests (#172)
1 parent 5fd40ee commit 07b4601

6 files changed

Lines changed: 39 additions & 16 deletions

File tree

cmd/netronome/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func runServer(cmd *cobra.Command, args []string) error {
238238
// create packet loss service if enabled
239239
if cfg.PacketLoss.Enabled {
240240
// We'll set the actual broadcaster after creating the server
241-
packetLossService = speedtest.NewPacketLossService(db, notifier, nil, cfg.PacketLoss.MaxConcurrentMonitors, cfg.PacketLoss.PrivilegedMode)
241+
packetLossService = speedtest.NewPacketLossService(db, notifier, nil, cfg.PacketLoss.MaxConcurrentMonitors, cfg.PacketLoss.PrivilegedMode, cfg.PacketLoss.MTREnableDNS)
242242
}
243243

244244
// Create monitor service variable

config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ default_interval = 3600
4747
default_packet_count = 10
4848
max_concurrent_monitors = 10
4949
privileged_mode = true
50+
mtr_enable_dns = false
5051

5152
[monitor]
5253
enabled = true

internal/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ type PacketLossConfig struct {
131131
DefaultPacketCount int `toml:"default_packet_count" env:"PACKETLOSS_DEFAULT_PACKET_COUNT"`
132132
MaxConcurrentMonitors int `toml:"max_concurrent_monitors" env:"PACKETLOSS_MAX_CONCURRENT_MONITORS"`
133133
PrivilegedMode bool `toml:"privileged_mode" env:"PACKETLOSS_PRIVILEGED_MODE"`
134+
MTREnableDNS bool `toml:"mtr_enable_dns" env:"PACKETLOSS_MTR_ENABLE_DNS"`
134135
RestoreMonitorsOnStartup bool `toml:"restore_monitors_on_startup" env:"PACKETLOSS_RESTORE_MONITORS_ON_STARTUP"`
135136
}
136137

@@ -276,6 +277,7 @@ func New() *Config {
276277
DefaultPacketCount: 10,
277278
MaxConcurrentMonitors: 10,
278279
PrivilegedMode: true,
280+
MTREnableDNS: false,
279281
RestoreMonitorsOnStartup: false,
280282
},
281283
Agent: AgentConfig{
@@ -586,6 +588,11 @@ func (c *Config) loadPacketLossFromEnv() {
586588
c.PacketLoss.PrivilegedMode = privileged
587589
}
588590
}
591+
if v := getEnv("PACKETLOSS_MTR_ENABLE_DNS"); v != "" {
592+
if enableDNS, err := strconv.ParseBool(v); err == nil {
593+
c.PacketLoss.MTREnableDNS = enableDNS
594+
}
595+
}
589596
if v := getEnv("PACKETLOSS_RESTORE_MONITORS_ON_STARTUP"); v != "" {
590597
if restore, err := strconv.ParseBool(v); err == nil {
591598
c.PacketLoss.RestoreMonitorsOnStartup = restore
@@ -863,6 +870,9 @@ func (c *Config) WriteToml(w io.Writer) error {
863870
if _, err := fmt.Fprintf(w, "privileged_mode = %v # Use privileged ICMP mode for better MTR support (requires root/sudo)\n", cfg.PacketLoss.PrivilegedMode); err != nil {
864871
return err
865872
}
873+
if _, err := fmt.Fprintf(w, "mtr_enable_dns = %v # Enable DNS resolution in MTR tests to show hostnames instead of IPs\n", cfg.PacketLoss.MTREnableDNS); err != nil {
874+
return err
875+
}
866876

867877
// Monitor section
868878
if _, err := fmt.Fprintln(w, ""); err != nil {

internal/speedtest/mtr_unix.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,25 @@ func killMTRProcessGroup(pid int) error {
4444

4545
// buildMTRArgs builds Unix-specific MTR arguments
4646
// On Unix, we can use the -j flag for JSON output directly
47-
func buildMTRArgs(host string, packetCount int, privilegedMode bool) ([]string, string, error) {
47+
func buildMTRArgs(host string, packetCount int, privilegedMode bool, enableDNS bool) ([]string, string, error) {
4848
args := []string{
49-
"-4", // Force IPv4
50-
"-j", // JSON output
51-
"-c", fmt.Sprintf("%d", packetCount), // Number of cycles
52-
"-i", "1", // 1 second interval
53-
"--no-dns", // Skip DNS resolution for speed
54-
host,
49+
"-4", // Force IPv4
50+
"-j", // JSON output
51+
"-c", fmt.Sprintf("%d", packetCount), // Number of cycles
52+
"-i", "1", // 1 second interval
5553
}
56-
54+
55+
if !enableDNS {
56+
args = append(args, "--no-dns")
57+
}
58+
59+
args = append(args, host)
60+
5761
// Add UDP mode if not privileged
5862
if !privilegedMode {
5963
args = append([]string{"-u"}, args...)
6064
}
61-
65+
6266
// Return empty string to indicate Unix doesn't need parsing
6367
return args, "", nil
6468
}

internal/speedtest/mtr_windows.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,22 @@ func killMTRProcessGroup(pid int) error {
6767
// buildMTRArgs builds Windows-specific MTR arguments
6868
// Windows MTR doesn't support -j for JSON output, so we use -r for report mode
6969
// and -w for wide format, then capture stdout and parse it
70-
func buildMTRArgs(host string, packetCount int, privilegedMode bool) ([]string, string, error) {
70+
func buildMTRArgs(host string, packetCount int, privilegedMode bool, enableDNS bool) ([]string, string, error) {
7171
args := []string{
7272
"-4", // Force IPv4
7373
"-r", // Report mode
7474
"-w", // Wide report, don't truncate hostnames
75-
"-n", // No DNS lookups (numeric output only)
75+
}
76+
77+
if !enableDNS {
78+
args = append(args, "-n") // No DNS lookups (numeric output only)
79+
}
80+
81+
args = append(args,
7682
"-c", fmt.Sprintf("%d", packetCount), // Number of cycles
7783
"-i", "1", // 1 second interval
7884
"-t", "2", // 2 second timeout per hop to prevent hanging on unresponsive hops
79-
}
85+
)
8086

8187
// Add UDP mode if not privileged (Windows MTR defaults to ICMP in privileged mode)
8288
if !privilegedMode {

internal/speedtest/packetloss.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ type PacketLossService struct {
5151
}
5252
maxConcurrent int
5353
privilegedMode bool
54+
enableDNS bool
5455
}
5556

5657
// NewPacketLossService creates a new packet loss monitoring service
57-
func NewPacketLossService(db database.Service, notifier *notifications.Notifier, broadcast func(types.PacketLossUpdate), maxConcurrent int, privilegedMode bool) *PacketLossService {
58+
func NewPacketLossService(db database.Service, notifier *notifications.Notifier, broadcast func(types.PacketLossUpdate), maxConcurrent int, privilegedMode bool, enableDNS bool) *PacketLossService {
5859
if maxConcurrent <= 0 {
5960
maxConcurrent = 10
6061
}
@@ -69,6 +70,7 @@ func NewPacketLossService(db database.Service, notifier *notifications.Notifier,
6970
broadcast: broadcast,
7071
maxConcurrent: maxConcurrent,
7172
privilegedMode: privilegedMode,
73+
enableDNS: enableDNS,
7274
}
7375
}
7476

@@ -1007,7 +1009,7 @@ func (s *PacketLossService) runMTRTest(monitor *PacketLossMonitor) (*probing.Sta
10071009
defer cancel()
10081010

10091011
// Build platform-specific MTR command arguments
1010-
args, platformFlag, err := buildMTRArgs(monitor.Host, monitor.PacketCount, s.privilegedMode)
1012+
args, platformFlag, err := buildMTRArgs(monitor.Host, monitor.PacketCount, s.privilegedMode, s.enableDNS)
10111013
if err != nil {
10121014
return nil, fmt.Errorf("failed to build MTR arguments: %w", err)
10131015
}
@@ -1174,7 +1176,7 @@ func (s *PacketLossService) runMTRTest(monitor *PacketLossMonitor) (*probing.Sta
11741176
Msg("MTR privileged mode failed, trying UDP mode")
11751177

11761178
// Rebuild args with UDP mode for retry
1177-
retryArgs, retryPlatformFlag, buildErr := buildMTRArgs(monitor.Host, monitor.PacketCount, false)
1179+
retryArgs, retryPlatformFlag, buildErr := buildMTRArgs(monitor.Host, monitor.PacketCount, false, s.enableDNS)
11781180
if buildErr != nil {
11791181
return nil, fmt.Errorf("failed to build retry MTR arguments: %w", buildErr)
11801182
}

0 commit comments

Comments
 (0)