Update 实时 history 监控
This commit is contained in:
@@ -1,100 +1,82 @@
|
|||||||
# 创建超级简单的监控系统
|
# 创建极简监控系统
|
||||||
cat > /usr/local/bin/watchcmd << 'EOF'
|
cat > /tmp/simple_monitor.sh << 'EOF'
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
LOG="/root/watch.log"
|
LOG_FILE="/tmp/command_monitor.log"
|
||||||
PID="/tmp/watch.pid"
|
PID_FILE="/tmp/monitor_simple.pid"
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
start)
|
start)
|
||||||
# 停止其他监控
|
echo "启动极简监控..."
|
||||||
pkill -f "cmdwatch"
|
|
||||||
pkill -f "monitor"
|
|
||||||
pkill -f "mt"
|
|
||||||
pkill -f "mon"
|
|
||||||
|
|
||||||
# 设置实时history
|
# 设置实时history
|
||||||
echo 'export PROMPT_COMMAND="history -a; history -c; history -r"' >> ~/.bashrc
|
echo 'export PROMPT_COMMAND="history -a; history -c; history -r"' >> ~/.bashrc
|
||||||
source ~/.bashrc
|
source ~/.bashrc
|
||||||
|
|
||||||
# 启动监控
|
# 启动监控进程
|
||||||
(
|
(
|
||||||
echo "监控启动: $(date)" > "$LOG"
|
echo "=== 监控启动: $(date) ===" > "$LOG_FILE"
|
||||||
declare -A sizes
|
declare -A sizes
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
for user in /home/* /root; do
|
for user_dir in /home/* /root; do
|
||||||
[ -d "$user" ] || continue
|
[ -d "$user_dir" ] || continue
|
||||||
history_file="$user/.bash_history"
|
user=$(basename "$user_dir")
|
||||||
|
history_file="$user_dir/.bash_history"
|
||||||
[ -f "$history_file" ] || continue
|
[ -f "$history_file" ] || continue
|
||||||
|
|
||||||
user_name=$(basename "$user")
|
|
||||||
current=$(stat -c%s "$history_file" 2>/dev/null || echo 0)
|
current=$(stat -c%s "$history_file" 2>/dev/null || echo 0)
|
||||||
last=${sizes["$user_name"]:-0}
|
last=${sizes["$user"]:-0}
|
||||||
|
|
||||||
if [ "$current" -gt "$last" ]; then
|
if [ "$current" -gt "$last" ]; then
|
||||||
cmd=$(tail -n 1 "$history_file" 2>/dev/null | tr -d '\000-\037')
|
cmd=$(tail -n 1 "$history_file" 2>/dev/null)
|
||||||
if [ -n "$cmd" ] && [ ${#cmd} -gt 1 ]; then
|
if [ -n "$cmd" ] && [ ${#cmd} -gt 1 ]; then
|
||||||
case "$cmd" in
|
case "$cmd" in
|
||||||
ls|cd|pwd|ll|history|exit|clear|watchcmd|".")
|
ls|cd|pwd|ll|history|exit|clear|".") continue ;;
|
||||||
continue
|
|
||||||
;;
|
|
||||||
*)
|
*)
|
||||||
ip="unknown"
|
ip="unknown"
|
||||||
[ -n "$SSH_CLIENT" ] && ip=$(echo "$SSH_CLIENT" | awk '{print $1}')
|
[ -n "$SSH_CLIENT" ] && ip=$(echo "$SSH_CLIENT" | awk '{print $1}')
|
||||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $user_name: $cmd (from: $ip)" >> "$LOG"
|
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $user: $cmd (from: $ip)" >> "$LOG_FILE"
|
||||||
sizes["$user_name"]=$current
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
sizes["$user"]=$current
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
sleep 1
|
sleep 2
|
||||||
done
|
done
|
||||||
) &
|
) &
|
||||||
echo $! > "$PID"
|
echo $! > "$PID_FILE"
|
||||||
echo "监控已启动"
|
echo "监控已启动 (PID: $!)"
|
||||||
;;
|
echo "查看日志: tail -f $LOG_FILE"
|
||||||
stop)
|
|
||||||
pkill -f "watchcmd"
|
|
||||||
rm -f "$PID"
|
|
||||||
echo "监控已停止"
|
|
||||||
;;
|
;;
|
||||||
view)
|
view)
|
||||||
if [ -f "$LOG" ]; then
|
if [ -f "$LOG_FILE" ]; then
|
||||||
tail -f "$LOG"
|
tail -f "$LOG_FILE"
|
||||||
else
|
else
|
||||||
echo "暂无日志"
|
echo "暂无日志,请先启动监控: $0 start"
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
status)
|
stop)
|
||||||
if [ -f "$PID" ] && ps -p $(cat "$PID") >/dev/null 2>&1; then
|
if [ -f "$PID_FILE" ]; then
|
||||||
echo "监控运行中 (PID: $(cat "$PID"))"
|
kill $(cat "$PID_FILE") 2>/dev/null
|
||||||
|
rm -f "$PID_FILE"
|
||||||
|
echo "监控已停止"
|
||||||
else
|
else
|
||||||
echo "监控未运行"
|
echo "监控未运行"
|
||||||
rm -f "$PID"
|
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
install)
|
|
||||||
# 设置开机启动
|
|
||||||
(crontab -l 2>/dev/null; echo "@reboot /usr/local/bin/watchcmd start >/dev/null 2>&1") | crontab -
|
|
||||||
# 设置别名
|
|
||||||
echo "alias wc='watchcmd view'" >> ~/.bashrc
|
|
||||||
source ~/.bashrc
|
|
||||||
# 启动
|
|
||||||
watchcmd start
|
|
||||||
echo "安装完成! 使用 'wc' 查看监控"
|
|
||||||
;;
|
|
||||||
*)
|
*)
|
||||||
echo "使用: watchcmd [start|stop|view|status|install]"
|
echo "使用方法: $0 {start|view|stop}"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
chmod +x /usr/local/bin/watchcmd
|
chmod +x /tmp/simple_monitor.sh
|
||||||
|
|
||||||
# 安装并测试
|
# 启动极简监控
|
||||||
watchcmd install
|
/tmp/simple_monitor.sh start
|
||||||
|
|
||||||
# 测试
|
# 测试
|
||||||
wc
|
echo "test_simple_$(date +%s)" >> ~/.bash_history
|
||||||
|
sleep 2
|
||||||
|
tail -5 /tmp/command_monitor.log
|
||||||
|
|||||||
Reference in New Issue
Block a user