Files
go-sms-V2/scripts/cmpp-capture.sh
T

98 lines
3.6 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# cmpp-capture.sh — CMPP 联调抓包控制(Issue#3:客户连接报 -1 排查装备)
#
# 用法:
# scripts/cmpp-capture.sh start [端口] # 后台抓包(默认 7890;可 'host x.x.x.x' 过滤)
# scripts/cmpp-capture.sh stop # 停止
# scripts/cmpp-capture.sh status # 运行状态/文件/当前 7890 连接
# scripts/cmpp-capture.sh analyze [pcap] # 解析最新(或指定)pcap → CMPP 时间线
#
# 抓包口径:-i any(LINUX_SLL 链路层,分析器自适应)/ -s 0 全包(长短信 Submit ~3KB)
# / -C 50 -W 6 环形 50MB×6 防打爆 / -tttt.n 微秒时间戳
# 权限:sudo(sudoers NOPASSWD 已配);无 root 时提示降级方案(python 探针)。
set -euo pipefail
CAP_DIR="/data/go-sms-v2/logs/capture"
PID_FILE="$CAP_DIR/capture.pid"
LOG_FILE="$CAP_DIR/tcpdump-stderr.log"
ensure_dir() { mkdir -p "$CAP_DIR"; }
running() { pgrep -x tcpdump >/dev/null 2>&1; }
cmd_start() {
if running; then echo "已在运行(PID $(cat "$PID_FILE")),文件:"; ls -lh "$CAP_DIR"/*.pcap 2>/dev/null | tail -2; exit 0; fi
ensure_dir
local filter="${1:-7890}"
local f="port ${filter}"
# 传 host x.x.x.x 形式时原样作过滤器
[[ "$filter" == *" "* ]] && f="$filter"
local ts file
ts=$(date +%Y%m%d-%H%M%S)
file="$CAP_DIR/cmpp-$ts.pcap"
if ! sudo -n true 2>/dev/null; then
echo "ERROR: sudo 免密不可用,tcpdump 需 root(抓不了包;可用 python3 探针替代连通性探测)" >&2; exit 1
fi
# -Z root:tcpdump 4.x 写 pcap 默认降权 tcpdump 用户(目录无权→Permission denied)
sudo -n nohup tcpdump -Z root -i any -s 0 -C 50 -W 6 -tttt -n -U -w "$file" "$f" > "$LOG_FILE" 2>&1 &
sleep 1
local pid
pid=$(pgrep -x tcpdump | head -1 || true)
if [[ -z "$pid" ]]; then
echo "ERROR: tcpdump 启动失败:" >&2; cat "$LOG_FILE" >&2; exit 1
fi
echo "$pid" > "$PID_FILE"
echo "抓包已启动: PID=$pid 过滤器='$f'"
echo "文件: $file(环形 50MB×6)"
echo "停止: scripts/cmpp-capture.sh stop ;分析: scripts/cmpp-capture.sh analyze"
}
cmd_stop() {
if ! running; then echo "未在运行"; rm -f "$PID_FILE"; exit 0; fi
sudo -n pkill -x tcpdump 2>/dev/null || pkill -x tcpdump 2>/dev/null || true
sleep 1
rm -f "$PID_FILE"
echo "已停止。最近文件:"
ls -lht "$CAP_DIR"/*.pcap* 2>/dev/null | head -3
echo "分析: scripts/cmpp-capture.sh analyze"
}
cmd_status() {
if running; then
echo "运行中 PID=$(cat "$PID_FILE")"
else
echo "未在运行"
fi
ls -lht "$CAP_DIR"/*.pcap* 2>/dev/null | head -3 || echo "(无 pcap)"
echo "--- 当前 7890 连接(含出向)---"
ss -tn 2>/dev/null | grep ':7890' || echo "(无)"
tail -3 "$LOG_FILE" 2>/dev/null || true
}
cmd_analyze() {
local mode="${1:-}"
ensure_dir
# 环形切片 .pcapN:默认分析最新单片;all=cat 全组(按编号序)到临时文件合并分析
local f newest group
f=$(ls -t "$CAP_DIR"/*.pcap* 2>/dev/null | head -1 || true)
if [[ -z "$f" ]]; then echo "ERROR: 无 pcap 文件(先 start)" >&2; exit 1; fi
if [[ "$mode" == "all" ]]; then
group="${f%.pcap*}.pcap"
local merged="/tmp/cmpp-merged-$$.pcap"
cat "$group" "$group"[0-9]* 2>/dev/null > "$merged" || cat "$group"* > "$merged"
echo "(合并 $(ls "$group"* 2>/dev/null | wc -l) 个切片 → $merged)"
python3 /data/go-sms-v2/scripts/cmpp-analyze.py "$merged"
rm -f "$merged"
else
python3 /data/go-sms-v2/scripts/cmpp-analyze.py "$f"
fi
}
case "${1:-status}" in
start) shift || true; cmd_start "$@" ;;
stop) cmd_stop ;;
status) cmd_status ;;
analyze) shift || true; cmd_analyze "$@" ;;
*) echo "用法: $0 {start [端口|过滤式]|stop|status|analyze [pcap]}"; exit 1 ;;
esac