Files
dock/实时 history 监控

210 lines
6.0 KiB
Bash

#!/bin/bash
# 优化版实时命令监控脚本 - 中文显示 + 多IP查询源
# 版本: 3.3
set -e
### 配置区域 ###
LOG_DIR="/root/command_monitor_logs"
MAX_LOG_SIZE="1M"
MAX_LOG_FILES=50
LOG_ROTATE_INTERVAL=1800
MEMORY_LIMIT="512M"
CHECK_INTERVAL=300
BACKUP_DAYS=7
CLEANUP_INTERVAL=3600
### IP地理位置配置 - 多个备用源 ###
IP_API_SERVICES=("ipapi" "ipapi.co" "ipinfo.io" "ip-api.com" "whois.pconline.com.cn")
CACHE_IP_INFO=true
IP_CACHE_FILE="/tmp/ip_geo_cache.txt"
CACHE_EXPIRE=86400
### 颜色定义 ###
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'
### 全局变量 ###
SCRIPT_PID=$$
MONITOR_PID=""
LAST_CLEANUP=0
LAST_ROTATION=0
CURRENT_LOG=""
DAEMON_MODE=false
# 获取时间戳
timestamp() {
date '+%Y-%m-%d %H:%M:%S'
}
# 日志函数
log_message() {
local level="$1"
local message="$2"
local color="$GREEN"
case "$level" in
"ERROR") color="$RED" ;;
"WARN") color="$YELLOW" ;;
"INFO") color="$BLUE" ;;
"SUCCESS") color="$GREEN" ;;
"COMMAND") color="$CYAN" ;;
esac
if [ "$DAEMON_MODE" = true ]; then
echo -e "${color}[$(timestamp)] [$level] $message${NC}" >> "$CURRENT_LOG"
else
echo -e "${color}[$(timestamp)] [$level] $message${NC}" | tee -a "$CURRENT_LOG"
fi
}
# 英文转中文函数
english_to_chinese() {
local text="$1"
# 常见国家地区翻译
text=$(echo "$text" | sed -e 's/China/中国/g' -e 's/United States/美国/g' -e 's/Japan/日本/g' -e 's/Korea/韩国/g' \
-e 's/Russia/俄罗斯/g' -e 's/Germany/德国/g' -e 's/France/法国/g' -e 's/UK/英国/g' -e 's/Canada/加拿大/g' \
-e 's/Australia/澳大利亚/g' -e 's/Brazil/巴西/g' -e 's/India/印度/g')
echo "$text"
}
# 多IP查询源函数
get_ip_location_online() {
local ip="$1"
local location_info=""
for service in "${IP_API_SERVICES[@]}"; do
case $service in
"ipapi")
location_info=$(curl -s -m 3 "http://ip-api.com/json/$ip?fields=status,country,regionName,city,isp,as" 2>/dev/null || true)
;;
"ipapi.co")
location_info=$(curl -s -m 3 "https://ipapi.co/$ip/json/" 2>/dev/null || true)
;;
"ipinfo.io")
location_info=$(curl -s -m 3 "https://ipinfo.io/$ip" 2>/dev/null || true)
;;
"ip-api.com")
location_info=$(curl -s -m 3 "https://ip-api.com/json/$ip" 2>/dev/null || true)
;;
"whois.pconline.com.cn")
location_info=$(curl -s -m 3 "https://whois.pconline.com.cn/ipJson.jsp?ip=$ip&json=true" 2>/dev/null || true)
;;
*)
location_info=""
;;
esac
if [ -n "$location_info" ]; then
local country=$(echo "$location_info" | grep -o '"country":"[^"]*"' | cut -d'"' -f4)
local region=$(echo "$location_info" | grep -o '"regionName":"[^"]*"' | cut -d'"' -f4)
local city=$(echo "$location_info" | grep -o '"city":"[^"]*"' | cut -d'"' -f4)
local isp=$(echo "$location_info" | grep -o '"isp":"[^"]*"' | cut -d'"' -f4)
if [ -n "$country" ]; then
local result="$country"
[ -n "$region" ] && result="$result-$region"
[ -n "$city" ] && result="$result-$city"
[ -n "$isp" ] && result="$result($isp)"
echo "$result"
return 0
fi
fi
done
echo "未知位置"
return 1
}
# 获取客户端IP
get_client_ip() {
local ip="unknown"
if [ -n "$SSH_CLIENT" ]; then
ip=$(echo "$SSH_CLIENT" | awk '{print $1}')
elif [ -n "$SSH_CONNECTION" ]; then
ip=$(echo "$SSH_CONNECTION" | awk '{print $1}')
else
ip=$(who -m 2>/dev/null | awk '{print $5}' | sed 's/[()]//g' | head -1)
if [[ "$ip" == ":0" ]] || [[ "$ip" == ":1" ]] || [[ -z "$ip" ]]; then
ip="localhost"
fi
fi
echo "$ip"
}
# 日志轮转检查
check_log_rotation() {
local current_time=$(date +%s)
if [ $((current_time - LAST_ROTATION)) -ge $LOG_ROTATE_INTERVAL ]; then
log_rotation "time"
return 0
fi
if [ -f "$CURRENT_LOG" ]; then
local log_size=$(stat -c%s "$CURRENT_LOG" 2>/dev/null || echo 0)
if [ "$log_size" -gt 1048576 ]; then
log_rotation "size"
return 0
fi
fi
return 1
}
# 初始化日志系统
init_log_system() {
mkdir -p "$LOG_DIR"
CURRENT_LOG=$(generate_log_filename)
LATEST_LOG="$LOG_DIR/latest.log"
ln -sf "$CURRENT_LOG" "$LATEST_LOG" 2>/dev/null || true
LAST_ROTATION=$(date +%s)
log_message "INFO" "监控脚本启动 - PID: $$"
local client_ip=$(get_client_ip)
log_message "INFO" "客户端IP: $client_ip"
local location_info=$(get_ip_location "$client_ip")
log_message "INFO" "地理位置: $location_info"
log_message "INFO" "日志文件: $CURRENT_LOG"
log_message "INFO" "日志轮转: ${LOG_ROTATE_INTERVAL}秒或${MAX_LOG_SIZE}"
}
# 主监控函数
start_main_monitor() {
log_message "INFO" "启动主监控进程..."
while true; do
sleep 60
# 检查日志轮转条件
check_log_rotation
# 只监控内存使用
local mem_usage=$(free 2>/dev/null | awk 'NR==2{printf "%.2f", $3*100/$2}' || echo "0")
if (( $(echo "$mem_usage > 90" | bc -l 2>/dev/null) )); then
log_message "WARN" "内存使用率过高: ${mem_usage}%"
fi
# 检查磁盘空间
local disk_usage=$(df "$LOG_DIR" 2>/dev/null | awk 'NR==2{print $5}' | cut -d'%' -f1 || echo "0")
if [ "$disk_usage" -gt 90 ]; then
log_message "WARN" "磁盘使用率过高: ${disk_usage}%"
fi
done
}
# 显示使用说明
show_usage() {
echo -e "${GREEN}实时命令监控系统 v3.3