-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_error_watch.sh
More file actions
executable file
·167 lines (138 loc) · 3.75 KB
/
interface_error_watch.sh
File metadata and controls
executable file
·167 lines (138 loc) · 3.75 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
#!/usr/bin/env bash
set -euo pipefail
# interface_error_watch.sh
# Samples interface rx/tx error and drop counters from /sys/class/net.
usage() {
cat <<'EOF'
Usage: ./interface_error_watch.sh [options]
Options:
--interval N Seconds between samples (default: 2)
--samples N Number of samples (default: 5)
--only IFACE Monitor only one interface (repeatable)
--exclude IFACE Exclude interface (repeatable)
--show-loopback Include loopback interface
-h, --help Show help
EOF
}
INTERVAL=2
SAMPLES=5
SHOW_LOOPBACK=false
ONLY_IFACES=()
EXCLUDE_IFACES=()
while [[ $# -gt 0 ]]; do
case "$1" in
--interval) INTERVAL="$2"; shift 2 ;;
--samples) SAMPLES="$2"; shift 2 ;;
--only) ONLY_IFACES+=("$2"); shift 2 ;;
--exclude) EXCLUDE_IFACES+=("$2"); shift 2 ;;
--show-loopback) SHOW_LOOPBACK=true; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown option: $1"; usage; exit 1 ;;
esac
done
if [[ ! -d /sys/class/net ]]; then
echo "/sys/class/net not found"
exit 1
fi
is_excluded_iface() {
local iface="$1"
local item
if [[ "$iface" == "lo" && "$SHOW_LOOPBACK" == false ]]; then
return 0
fi
if [[ "${#ONLY_IFACES[@]}" -gt 0 ]]; then
local in_only=false
for item in "${ONLY_IFACES[@]}"; do
if [[ "$iface" == "$item" ]]; then
in_only=true
break
fi
done
if [[ "$in_only" == false ]]; then
return 0
fi
fi
for item in "${EXCLUDE_IFACES[@]}"; do
if [[ "$iface" == "$item" ]]; then
return 0
fi
done
return 1
}
list_ifaces() {
local iface
for iface_path in /sys/class/net/*; do
iface="$(basename "$iface_path")"
if is_excluded_iface "$iface"; then
continue
fi
echo "$iface"
done
}
read_stat() {
local iface="$1"
local stat="$2"
local p="/sys/class/net/$iface/statistics/$stat"
if [[ -r "$p" ]]; then
cat "$p"
else
echo 0
fi
}
ifaces=()
while IFS= read -r iface; do
ifaces+=("$iface")
done < <(list_ifaces)
if [[ "${#ifaces[@]}" -eq 0 ]]; then
echo "No interfaces matched selection."
exit 1
fi
echo "Interface error watch timestamp: $(date -Iseconds)"
echo "interval=${INTERVAL}s samples=$SAMPLES"
echo "interfaces=${ifaces[*]}"
echo
declare -A prev_rx_err prev_tx_err prev_rx_drop prev_tx_drop
total_delta_events=0
for ((sample=1; sample<=SAMPLES; sample++)); do
echo "=== Sample $sample/$SAMPLES ==="
for iface in "${ifaces[@]}"; do
rx_err=$(read_stat "$iface" rx_errors)
tx_err=$(read_stat "$iface" tx_errors)
rx_drop=$(read_stat "$iface" rx_dropped)
tx_drop=$(read_stat "$iface" tx_dropped)
if [[ "$sample" -eq 1 ]]; then
drx_err=0
dtx_err=0
drx_drop=0
dtx_drop=0
else
drx_err=$(( rx_err - ${prev_rx_err[$iface]} ))
dtx_err=$(( tx_err - ${prev_tx_err[$iface]} ))
drx_drop=$(( rx_drop - ${prev_rx_drop[$iface]} ))
dtx_drop=$(( tx_drop - ${prev_tx_drop[$iface]} ))
fi
delta_sum=$(( drx_err + dtx_err + drx_drop + dtx_drop ))
if [[ "$delta_sum" -gt 0 ]]; then
total_delta_events=$(( total_delta_events + delta_sum ))
fi
printf '%-12s rx_err=%-8s tx_err=%-8s rx_drop=%-8s tx_drop=%-8s | delta rx_err=%-6s tx_err=%-6s rx_drop=%-6s tx_drop=%-6s\n' \
"$iface" "$rx_err" "$tx_err" "$rx_drop" "$tx_drop" "$drx_err" "$dtx_err" "$drx_drop" "$dtx_drop"
prev_rx_err[$iface]="$rx_err"
prev_tx_err[$iface]="$tx_err"
prev_rx_drop[$iface]="$rx_drop"
prev_tx_drop[$iface]="$tx_drop"
done
echo
if [[ "$sample" -lt "$SAMPLES" ]]; then
sleep "$INTERVAL"
fi
done
status="OK"
if [[ "$total_delta_events" -gt 0 ]]; then
status="WARN"
fi
echo "Summary: status=$status total_delta_events=$total_delta_events"
if [[ "$status" == "WARN" ]]; then
exit 2
fi
exit 0