|
1 | 1 | #!/bin/sh |
2 | 2 |
|
3 | | -# 37-host-optimization.sh: Apply and validate network optimizations (ARP flux fix) |
| 3 | +# 37-host-optimization.sh: Detect ARP flux sysctl configuration. |
4 | 4 | # |
5 | | -# This script improves detection accuracy by ensuring proper ARP behavior. |
6 | | -# It attempts to apply sysctl settings and warns if not possible. |
| 5 | +# This script does not change host/kernel settings. |
7 | 6 |
|
8 | | -# --- Color Codes --- |
9 | | -RED=$(printf '\033[1;31m') |
10 | 7 | YELLOW=$(printf '\033[1;33m') |
11 | 8 | RESET=$(printf '\033[0m') |
12 | 9 |
|
13 | | -# --- Skip flag --- |
14 | | -if [ -n "${SKIP_OPTIMIZATIONS:-}" ]; then |
15 | | - exit 0 |
16 | | -fi |
17 | | - |
18 | | -# --- Helpers --- |
19 | | - |
20 | | -get_sysctl() { |
21 | | - sysctl -n "$1" 2>/dev/null || echo "unknown" |
22 | | -} |
23 | | - |
24 | | -set_sysctl_if_needed() { |
25 | | - key="$1" |
26 | | - expected="$2" |
27 | | - |
28 | | - current="$(get_sysctl "$key")" |
29 | | - |
30 | | - # Already correct |
31 | | - if [ "$current" = "$expected" ]; then |
32 | | - return 0 |
33 | | - fi |
34 | | - |
35 | | - # Try to apply |
36 | | - if sysctl -w "$key=$expected" >/dev/null 2>&1; then |
37 | | - return 0 |
38 | | - fi |
39 | | - |
40 | | - # Failed |
41 | | - return 1 |
42 | | -} |
43 | | - |
44 | | -# --- Apply Settings (best effort) --- |
45 | | - |
46 | 10 | failed=0 |
47 | 11 |
|
48 | | -set_sysctl_if_needed net.ipv4.conf.all.arp_ignore 1 || failed=1 |
49 | | -set_sysctl_if_needed net.ipv4.conf.all.arp_announce 2 || failed=1 |
50 | | -set_sysctl_if_needed net.ipv4.conf.default.arp_ignore 1 || failed=1 |
51 | | -set_sysctl_if_needed net.ipv4.conf.default.arp_announce 2 || failed=1 |
| 12 | +[ "$(sysctl -n net.ipv4.conf.all.arp_ignore 2>/dev/null || echo unknown)" = "1" ] || failed=1 |
| 13 | +[ "$(sysctl -n net.ipv4.conf.all.arp_announce 2>/dev/null || echo unknown)" = "2" ] || failed=1 |
52 | 14 |
|
53 | | -# --- Validate final state --- |
54 | | - |
55 | | -all_ignore="$(get_sysctl net.ipv4.conf.all.arp_ignore)" |
56 | | -all_announce="$(get_sysctl net.ipv4.conf.all.arp_announce)" |
57 | | - |
58 | | -# --- Warning Output --- |
59 | | - |
60 | | -if [ "$all_ignore" != "1" ] || [ "$all_announce" != "2" ]; then |
| 15 | +if [ "$failed" -eq 1 ]; then |
61 | 16 | >&2 printf "%s" "${YELLOW}" |
62 | | - >&2 cat <<EOF |
| 17 | + >&2 cat <<'EOF' |
63 | 18 | ══════════════════════════════════════════════════════════════════════════════ |
64 | | -⚠️ ATTENTION: ARP flux protection not enabled. |
65 | | -
|
66 | | - NetAlertX relies on ARP for device detection. Your system currently allows |
67 | | - ARP replies from incorrect interfaces (ARP flux), which may result in: |
68 | | -
|
69 | | - • False devices being detected |
70 | | - • IP/MAC mismatches |
71 | | - • Flapping device states |
72 | | - • Incorrect network topology |
73 | | -
|
74 | | - This is common when running in Docker or multi-interface environments. |
75 | | -
|
76 | | - ────────────────────────────────────────────────────────────────────────── |
77 | | - Recommended fix (Docker Compose): |
78 | | -
|
79 | | - sysctls: |
80 | | - net.ipv4.conf.all.arp_ignore: 1 |
81 | | - net.ipv4.conf.all.arp_announce: 2 |
82 | | -
|
83 | | - ────────────────────────────────────────────────────────────────────────── |
84 | | - Alternatively, apply on the host: |
| 19 | +⚠️ WARNING: ARP flux sysctls are not set. |
85 | 20 |
|
| 21 | + Expected values: |
86 | 22 | net.ipv4.conf.all.arp_ignore=1 |
87 | 23 | net.ipv4.conf.all.arp_announce=2 |
88 | 24 |
|
89 | | - Detection accuracy may be reduced until this is configured. |
| 25 | + Detection accuracy may be reduced until configured. |
| 26 | +
|
| 27 | + See: https://docs.netalertx.com/docker-troubleshooting/arp-flux-sysctls/ |
90 | 28 | ══════════════════════════════════════════════════════════════════════════════ |
91 | 29 | EOF |
92 | 30 | >&2 printf "%s" "${RESET}" |
|
0 commit comments