基本框架
This commit is contained in:
104
scripts/diag/entrypath/lib/common.sh
Normal file
104
scripts/diag/entrypath/lib/common.sh
Normal file
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
now() { date '+%Y-%m-%d %H:%M:%S'; }
|
||||
say() { echo "[$(now)] $*"; }
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
用法:
|
||||
entrypath.sh <command> [选项]
|
||||
entrypath.sh [选项] # 等价于 run
|
||||
|
||||
命令:
|
||||
run 完整检查(默认)
|
||||
preflight 仅检查本地依赖与参数环境
|
||||
capture 强制开启所有抓包/trace能力后执行 run
|
||||
analyze --log <path> 离线分析日志文件
|
||||
|
||||
通用选项:
|
||||
--worker-host <user@host> 远端 worker SSH 主机(默认 jack@192.168.2.62)
|
||||
--client-host <user@host> 远端客户端 SSH 主机(可选,用于自动发起 curl)
|
||||
--client-ip <ip> 第三方客户端 IP(默认 192.168.2.63)
|
||||
--lb-ip <ip> 待排查 LB 节点 IP(默认 192.168.2.62)
|
||||
--worker-ssh-key <path> worker SSH 私钥路径(默认 ~/.ssh/id_ed25519_k3s_diag_worker)
|
||||
--client-ssh-key <path> 客户端 SSH 私钥路径(默认 ~/.ssh/id_ed25519_k3s_diag_client)
|
||||
--ssh-key <path> 兼容别名,等同 --worker-ssh-key
|
||||
--remote-check <y|n> 是否启用远端检查(默认 n,交互可覆盖)
|
||||
--capture-mode <y|n> 抓包模式(worker ens18,默认 n)
|
||||
--capture-seconds <n> 抓包持续秒数(默认 12)
|
||||
--nft-trace-mode <y|n> nft trace 模式(worker,默认 n)
|
||||
--nft-trace-seconds <n> nft trace 持续秒数(默认 8)
|
||||
--return-trace-mode <y|n> 回包链路跟踪(ylc61/ylc62,默认 n)
|
||||
--return-trace-seconds <n> 回包链路跟踪持续秒数(默认 10)
|
||||
--pod-netns-trace-mode <y|n> Traefik Pod netns 抓包(ylc61,默认 n)
|
||||
--pod-netns-trace-seconds <n> Traefik Pod netns 抓包持续秒数(默认同 return-trace-seconds)
|
||||
--non-interactive 非交互模式(需配合上面参数)
|
||||
--log <path> 仅 analyze 子命令使用
|
||||
-h, --help 显示帮助
|
||||
EOF
|
||||
}
|
||||
|
||||
run_cmd() {
|
||||
local desc="$1"
|
||||
shift
|
||||
echo
|
||||
echo "===== ${desc} ====="
|
||||
"$@" || true
|
||||
}
|
||||
|
||||
require_cmd() {
|
||||
local c="$1"
|
||||
if ! command -v "$c" >/dev/null 2>&1; then
|
||||
echo "[ERR] missing command: $c"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
read_default() {
|
||||
local prompt="$1"
|
||||
local def="$2"
|
||||
local out
|
||||
printf "%s [%s]: " "$prompt" "$def" >&2
|
||||
read -r out
|
||||
echo "${out:-$def}"
|
||||
}
|
||||
|
||||
extract_pkts_for_target() {
|
||||
local table="$1"
|
||||
local chain="$2"
|
||||
local target="$3"
|
||||
sudo iptables ${table:+-t "$table"} -L "$chain" -n -v -x 2>/dev/null \
|
||||
| awk -v t="$target" '$3==t {print $1; exit}'
|
||||
}
|
||||
|
||||
extract_first_jump_target() {
|
||||
local table="$1"
|
||||
local chain="$2"
|
||||
sudo iptables ${table:+-t "$table"} -S "$chain" 2>/dev/null \
|
||||
| awk '/-j KUBE-SEP-/{for(i=1;i<=NF;i++) if($i=="-j"){print $(i+1); exit}}'
|
||||
}
|
||||
|
||||
count_tcpdump_flag() {
|
||||
local file="$1"
|
||||
local flag="$2"
|
||||
if [[ ! -f "$file" ]]; then
|
||||
echo 0
|
||||
return 0
|
||||
fi
|
||||
awk -v f="$flag" 'BEGIN{c=0} index($0,f){c++} END{print c}' "$file"
|
||||
}
|
||||
|
||||
init_defaults() {
|
||||
COMMAND="run"
|
||||
ANALYZE_LOG=""
|
||||
WORKER_HOST="jack@192.168.2.62"
|
||||
CLIENT_HOST=""
|
||||
CLIENT_IP="192.168.2.63"
|
||||
LB_IP="192.168.2.62"
|
||||
WORKER_SSH_KEY=""
|
||||
CLIENT_SSH_KEY=""
|
||||
DEFAULT_WORKER_SSH_KEY="${HOME}/.ssh/id_ed25519_k3s_diag_worker"
|
||||
DEFAULT_CLIENT_SSH_KEY="${HOME}/.ssh/id_ed25519_k3s_diag_client"
|
||||
DO_REMOTE_ARG=""
|
||||
NON_INTERACTIVE="0"
|
||||
}
|
||||
Reference in New Issue
Block a user