Update 实时 history 监控

This commit is contained in:
2025-10-21 23:36:36 +08:00
committed by GitHub
parent c8a47b04da
commit b5d3520bc1

View File

@@ -1,7 +1,7 @@
#!/bin/bash
# 稳定版实时命令监控脚本 - 带离线IP库支持
# 版本: 3.2
# 优化版实时命令监控脚本 - 中文显示 + 多IP查询源
# 版本: 3.3
set -e
@@ -11,11 +11,17 @@ MAX_LOG_SIZE="1M"
MAX_LOG_FILES=50
LOG_ROTATE_INTERVAL=1800
MEMORY_LIMIT="512M"
CPU_LIMIT=90
# 移除CPU限制只保留内存监控
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'
@@ -33,243 +39,6 @@ LAST_ROTATION=0
CURRENT_LOG=""
DAEMON_MODE=false
# 简单离线IP库 - 中国常见IP段
declare -A IP_LOCAL_DB=(
["1."]="中国-APNIC"
["14."]="中国-APNIC"
["27."]="中国-APNIC"
["36."]="中国-电信"
["39."]="中国-电信"
["42."]="中国-电信"
["49."]="中国-电信"
["58."]="中国-电信"
["59."]="中国-电信"
["60."]="中国-电信"
["61."]="中国-电信"
["106."]="中国-电信"
["110."]="中国-电信"
["111."]="中国-电信"
["112."]="中国-电信"
["113."]="中国-电信"
["114."]="中国-电信"
["115."]="中国-电信"
["116."]="中国-电信"
["117."]="中国-电信"
["118."]="中国-电信"
["119."]="中国-电信"
["120."]="中国-电信"
["121."]="中国-电信"
["122."]="中国-电信"
["123."]="中国-电信"
["124."]="中国-电信"
["125."]="中国-电信"
["139."]="中国-电信"
["140."]="中国-电信"
["171."]="中国-电信"
["175."]="中国-电信"
["180."]="中国-电信"
["182."]="中国-电信"
["183."]="中国-电信"
["202."]="中国-电信"
["203."]="中国-电信"
["210."]="中国-电信"
["218."]="中国-电信"
["219."]="中国-电信"
["220."]="中国-电信"
["221."]="中国-电信"
["222."]="中国-电信"
["223."]="中国-电信"
["14.16."]="中国-广东-电信"
["14.17."]="中国-广东-电信"
["14.18."]="中国-广东-电信"
["14.19."]="中国-广东-电信"
["27.16."]="中国-湖北-电信"
["27.17."]="中国-湖北-电信"
["27.18."]="中国-湖北-电信"
["27.19."]="中国-湖北-电信"
["36.32."]="中国-浙江-电信"
["36.33."]="中国-浙江-电信"
["36.34."]="中国-浙江-电信"
["36.35."]="中国-浙江-电信"
["36.36."]="中国-浙江-电信"
["36.37."]="中国-浙江-电信"
["36.38."]="中国-浙江-电信"
["36.39."]="中国-浙江-电信"
["39.128."]="中国-云南-电信"
["39.129."]="中国-云南-电信"
["39.130."]="中国-云南-电信"
["39.131."]="中国-云南-电信"
["42.48."]="中国-湖南-电信"
["42.49."]="中国-湖南-电信"
["42.50."]="中国-湖南-电信"
["42.51."]="中国-湖南-电信"
["42.52."]="中国-湖南-电信"
["42.53."]="中国-湖南-电信"
["42.54."]="中国-湖南-电信"
["42.55."]="中国-湖南-电信"
["42.56."]="中国-湖南-电信"
["42.57."]="中国-湖南-电信"
["42.58."]="中国-湖南-电信"
["42.59."]="中国-湖南-电信"
["42.60."]="中国-湖南-电信"
["42.61."]="中国-湖南-电信"
["42.62."]="中国-湖南-电信"
["42.63."]="中国-湖南-电信"
["49.64."]="中国-江苏-电信"
["49.65."]="中国-江苏-电信"
["49.66."]="中国-江苏-电信"
["49.67."]="中国-江苏-电信"
["49.68."]="中国-江苏-电信"
["49.69."]="中国-江苏-电信"
["49.70."]="中国-江苏-电信"
["49.71."]="中国-江苏-电信"
["49.72."]="中国-江苏-电信"
["49.73."]="中国-江苏-电信"
["49.74."]="中国-江苏-电信"
["49.75."]="中国-江苏-电信"
["49.76."]="中国-江苏-电信"
["49.77."]="中国-江苏-电信"
["49.78."]="中国-江苏-电信"
["49.79."]="中国-江苏-电信"
["49.80."]="中国-江苏-电信"
["49.81."]="中国-江苏-电信"
["49.82."]="中国-江苏-电信"
["49.83."]="中国-江苏-电信"
["49.84."]="中国-江苏-电信"
["49.85."]="中国-江苏-电信"
["49.86."]="中国-江苏-电信"
["49.87."]="中国-江苏-电信"
["49.88."]="中国-江苏-电信"
["49.89."]="中国-江苏-电信"
["49.90."]="中国-江苏-电信"
["49.91."]="中国-江苏-电信"
["49.92."]="中国-江苏-电信"
["49.93."]="中国-江苏-电信"
["49.94."]="中国-江苏-电信"
["49.95."]="中国-江苏-电信"
["49.96."]="中国-江苏-电信"
["49.97."]="中国-江苏-电信"
["49.98."]="中国-江苏-电信"
["49.99."]="中国-江苏-电信"
["58.16."]="中国-贵州-电信"
["58.17."]="中国-贵州-电信"
["58.18."]="中国-贵州-电信"
["58.19."]="中国-贵州-电信"
["58.20."]="中国-湖南-电信"
["58.21."]="中国-湖南-电信"
["58.22."]="中国-湖南-电信"
["58.23."]="中国-湖南-电信"
["58.24."]="中国-江苏-电信"
["58.25."]="中国-上海-电信"
["58.26."]="中国-福建-电信"
["58.27."]="中国-福建-电信"
["58.28."]="中国-福建-电信"
["58.29."]="中国-福建-电信"
["58.30."]="中国-上海-电信"
["58.31."]="中国-上海-电信"
["58.32."]="中国-上海-电信"
["58.33."]="中国-上海-电信"
["58.34."]="中国-上海-电信"
["58.35."]="中国-上海-电信"
["58.36."]="中国-上海-电信"
["58.37."]="中国-上海-电信"
["58.38."]="中国-上海-电信"
["58.39."]="中国-上海-电信"
["58.40."]="中国-上海-电信"
["58.41."]="中国-上海-电信"
["58.42."]="中国-上海-电信"
["58.43."]="中国-上海-电信"
["58.44."]="中国-上海-电信"
["58.45."]="中国-上海-电信"
["58.46."]="中国-上海-电信"
["58.47."]="中国-上海-电信"
["58.48."]="中国-上海-电信"
["58.49."]="中国-上海-电信"
["58.50."]="中国-上海-电信"
["58.51."]="中国-上海-电信"
["58.52."]="中国-上海-电信"
["58.53."]="中国-上海-电信"
["58.54."]="中国-上海-电信"
["58.55."]="中国-上海-电信"
["58.56."]="中国-上海-电信"
["58.57."]="中国-上海-电信"
["58.58."]="中国-上海-电信"
["58.59."]="中国-上海-电信"
["58.60."]="中国-广东-电信"
["58.61."]="中国-广东-电信"
["58.62."]="中国-广东-电信"
["58.63."]="中国-广东-电信"
["59.32."]="中国-广东-电信"
["59.33."]="中国-广东-电信"
["59.34."]="中国-广东-电信"
["59.35."]="中国-广东-电信"
["59.36."]="中国-广东-电信"
["59.37."]="中国-广东-电信"
["59.38."]="中国-广东-电信"
["59.39."]="中国-广东-电信"
["59.40."]="中国-广东-电信"
["59.41."]="中国-广东-电信"
["59.42."]="中国-广东-电信"
["59.43."]="中国-广东-电信"
["59.44."]="中国-广东-电信"
["59.45."]="中国-广东-电信"
["59.46."]="中国-广东-电信"
["59.47."]="中国-广东-电信"
["59.48."]="中国-广东-电信"
["59.49."]="中国-广东-电信"
["59.50."]="中国-广东-电信"
["59.51."]="中国-广东-电信"
["59.52."]="中国-广东-电信"
["59.53."]="中国-广东-电信"
["59.54."]="中国-广东-电信"
["59.55."]="中国-广东-电信"
["59.56."]="中国-广东-电信"
["59.57."]="中国-广东-电信"
["59.58."]="中国-广东-电信"
["59.59."]="中国-广东-电信"
["59.60."]="中国-广东-电信"
["59.61."]="中国-广东-电信"
["59.62."]="中国-广东-电信"
["59.63."]="中国-广东-电信"
["59.64."]="中国-广东-电信"
["59.65."]="中国-广东-电信"
["59.66."]="中国-广东-电信"
["59.67."]="中国-广东-电信"
["59.68."]="中国-广东-电信"
["59.69."]="中国-广东-电信"
["59.70."]="中国-广东-电信"
["59.71."]="中国-广东-电信"
["59.72."]="中国-广东-电信"
["59.73."]="中国-广东-电信"
["59.74."]="中国-广东-电信"
["59.75."]="中国-广东-电信"
["59.76."]="中国-广东-电信"
["59.77."]="中国-广东-电信"
["59.78."]="中国-广东-电信"
["59.79."]="中国-广东-电信"
["59.80."]="中国-广东-电信"
["59.81."]="中国-广东-电信"
["59.82."]="中国-广东-电信"
["59.83."]="中国-广东-电信"
["59.84."]="中国-广东-电信"
["59.85."]="中国-广东-电信"
["59.86."]="中国-广东-电信"
["59.87."]="中国-广东-电信"
["59.88."]="中国-广东-电信"
["59.89."]="中国-广东-电信"
["59.90."]="中国-广东-电信"
["59.91."]="中国-广东-电信"
["59.92."]="中国-广东-电信"
["59.93."]="中国-广东-电信"
["59.94."]="中国-广东-电信"
["59.95."]="中国-广东-电信"
["59.96."]="中国-广东-电信"
["59.97."]="中国-广东-电信"
["59.98."]="中国-广东-电信"
["59.99."]="中国-广东-电信"
["113.194."]="中国-江西-电信"
)
# 获取时间戳
timestamp() {
date '+%Y-%m-%d %H:%M:%S'
@@ -296,56 +65,106 @@ log_message() {
fi
}
# 离线IP地理位置查询
get_ip_location_offline() {
local ip="$1"
# 英文转中文函数
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' \
-e 's/Beijing/北京/g' \
-e 's/Shanghai/上海/g' \
-e 's/Guangzhou/广州/g' \
-e 's/Shenzhen/深圳/g' \
-e 's/Hangzhou/杭州/g' \
-e 's/Nanjing/南京/g' \
-e 's/Wuhan/武汉/g' \
-e 's/Chengdu/成都/g' \
-e 's/Xi'an/西安/g' \
-e 's/Chongqing/重庆/g' \
-e 's/Tianjin/天津/g' \
-e 's/Suzhou/苏州/g' \
-e 's/Zhengzhou/郑州/g' \
-e 's/Changsha/长沙/g' \
-e 's/Hefei/合肥/g' \
-e 's/Nanchang/南昌/g' \
-e 's/Fuzhou/福州/g' \
-e 's/Jinan/济南/g' \
-e 's/Taiyuan/太原/g' \
-e 's/Hohhot/呼和浩特/g' \
-e 's/Shijiazhuang/石家庄/g' \
-e 's/Harbin/哈尔滨/g' \
-e 's/Changchun/长春/g' \
-e 's/Jilin/吉林/g' \
-e 's/Dalian/大连/g' \
-e 's/Qingdao/青岛/g' \
-e 's/Ningbo/宁波/g' \
-e 's/Xiamen/厦门/g' \
-e 's/Kunming/昆明/g' \
-e 's/Guiyang/贵阳/g' \
-e 's/Nanning/南宁/g' \
-e 's/Haikou/海口/g' \
-e 's/Urumqi/乌鲁木齐/g' \
-e 's/Lanzhou/兰州/g' \
-e 's/Xining/西宁/g' \
-e 's/Yinchuan/银川/g' \
-e 's/Lhasa/拉萨/g' \
-e 's/Hong Kong/香港/g' \
-e 's/Macau/澳门/g' \
-e 's/Taiwan/台湾/g' \
-e 's/Jiangxi/江西/g' \
-e 's/Zhejiang/浙江/g' \
-e 's/Jiangsu/江苏/g' \
-e 's/Guangdong/广东/g' \
-e 's/Fujian/福建/g' \
-e 's/Hunan/湖南/g' \
-e 's/Hubei/湖北/g' \
-e 's/Henan/河南/g' \
-e 's/Hebei/河北/g' \
-e 's/Shandong/山东/g' \
-e 's/Shanxi/山西/g' \
-e 's/Shaanxi/陕西/g' \
-e 's/Sichuan/四川/g' \
-e 's/Yunnan/云南/g' \
-e 's/Guizhou/贵州/g' \
-e 's/Liaoning/辽宁/g' \
-e 's/Jilin/吉林/g' \
-e 's/Heilongjiang/黑龙江/g' \
-e 's/Anhui/安徽/g' \
-e 's/Gansu/甘肃/g' \
-e 's/Qinghai/青海/g' \
-e 's/Telecom/电信/g' \
-e 's/Unicom/联通/g' \
-e 's/Mobile/移动/g' \
-e 's/Network/网络/g' \
-e 's/Communications/通信/g' \
-e 's/Company/公司/g' \
-e 's/Corporation/集团/g' \
-e 's/Limited/有限公司/g' \
-e 's/CHINA169/中国联通/g' \
-e 's/ChinaNet/中国电信/g' \
-e 's/CMNET/中国移动/g')
# 检查内网IP
if [[ "$ip" == "192.168."* ]] || [[ "$ip" == "10."* ]] || [[ "$ip" == "172."* ]]; then
echo "内网IP"
return 0
fi
if [[ "$ip" == "127.0.0.1" ]] || [[ "$ip" == "localhost" ]] || [[ "$ip" == "unknown" ]]; then
echo "本机"
return 0
fi
# 在离线数据库中查找
for prefix in "${!IP_LOCAL_DB[@]}"; do
if [[ "$ip" == "$prefix"* ]]; then
echo "${IP_LOCAL_DB[$prefix]}"
return 0
fi
done
# 根据IP段判断国家
local first_octet=$(echo "$ip" | cut -d. -f1)
case $first_octet in
1|14|27|36|39|42|49|58|59|60|61|106|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|139|140|171|175|180|182|183|202|203|210|218|219|220|221|222|223)
echo "中国-未知地区"
;;
8|12|13|23|24|32|34|35|37|38|40|45|47|50|52|54|63|64|65|66|67|68|69|70|71|72|73|74|75|76|96|97|98|99|104|107|108|128|129|130|131|132|134|135|136|137|138|142|143|144|146|147|148|149|150|152|153|155|156|157|158|159|160|161|162|164|165|166|167|168|169|170|172|173|174|176|184|192|198|199|200|202|203|204|205|206|207|208|209)
echo "美国"
;;
5|31|37|46|62|77|78|79|80|81|82|83|84|85|86|87|88|89|90|91|92|93|94|95)
echo "欧洲"
;;
103|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|126|133|180|210|211|218|219|220|221)
echo "亚太地区"
;;
*)
echo "未知位置"
;;
esac
echo "$text"
}
# 在线IP地理位置查询
# 多IP查询源函数
get_ip_location_online() {
local ip="$1"
local location_info=""
# 方法1: ip-api.com (最可靠)
local location_info=$(curl -s -m 3 "http://ip-api.com/json/$ip?fields=status,country,regionName,city,isp" 2>/dev/null || true)
# 方法1: ip-api.com (国际,返回英文)
location_info=$(curl -s -m 3 "http://ip-api.com/json/$ip?fields=status,country,regionName,city,isp,as" 2>/dev/null || true)
if [ -n "$location_info" ] && echo "$location_info" | grep -q '"status":"success"'; then
local country=$(echo "$location_info" | grep -o '"country":"[^"]*"' | cut -d'"' -f4)
local region=$(echo "$location_info" | grep -o '"regionName":"[^"]*"' | cut -d'"' -f4)
@@ -362,15 +181,27 @@ get_ip_location_online() {
fi
fi
# 方法2: 使用ipapi.co
# 方法2: 国内IP查询接口 (返回中文)
location_info=$(curl -s -m 3 "https://whois.pconline.com.cn/ipJson.jsp?ip=$ip&json=true" 2>/dev/null || true)
if [ -n "$location_info" ]; then
local addr=$(echo "$location_info" | grep -o '"addr":"[^"]*"' | cut -d'"' -f4)
if [ -n "$addr" ] && [ "$addr" != "保留地址" ]; then
echo "$addr"
return 0
fi
fi
# 方法3: ipapi.co (国际)
location_info=$(curl -s -m 3 "https://ipapi.co/$ip/json/" 2>/dev/null || true)
if [ -n "$location_info" ]; then
local country=$(echo "$location_info" | grep -o '"country_name":"[^"]*"' | cut -d'"' -f4)
local region=$(echo "$location_info" | grep -o '"region":"[^"]*"' | cut -d'"' -f4)
local city=$(echo "$location_info" | grep -o '"city":"[^"]*"' | cut -d'"' -f4)
local isp=$(echo "$location_info" | grep -o '"org":"[^"]*"' | 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"
@@ -378,6 +209,42 @@ get_ip_location_online() {
fi
fi
# 方法4: ipinfo.io (国际)
location_info=$(curl -s -m 3 "https://ipinfo.io/$ip" 2>/dev/null || true)
if [ -n "$location_info" ]; then
local country=$(echo "$location_info" | grep -o '"country":"[^"]*"' | cut -d'"' -f4)
local region=$(echo "$location_info" | grep -o '"region":"[^"]*"' | cut -d'"' -f4)
local city=$(echo "$location_info" | grep -o '"city":"[^"]*"' | cut -d'"' -f4)
local isp=$(echo "$location_info" | grep -o '"org":"[^"]*"' | 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
# 方法5: 淘宝IP库 (国内)
location_info=$(curl -s -m 3 "http://ip.taobao.com/service/getIpInfo.php?ip=$ip" 2>/dev/null || true)
if [ -n "$location_info" ] && echo "$location_info" | grep -q '"code":0'; then
local country=$(echo "$location_info" | grep -o '"country":"[^"]*"' | cut -d'"' -f4)
local region=$(echo "$location_info" | grep -o '"region":"[^"]*"' | 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
return 1
}
@@ -385,29 +252,85 @@ get_ip_location_online() {
get_ip_location() {
local ip="$1"
# 先尝试在线查询
if command -v curl &> /dev/null; then
local online_result=$(get_ip_location_online "$ip")
if [ -n "$online_result" ] && [ "$online_result" != "null" ]; then
echo "$online_result"
return 0
# 检查内网IP
if [[ "$ip" == "192.168."* ]] || [[ "$ip" == "10."* ]] || [[ "$ip" == "172."* ]]; then
echo "内网IP"
return 0
fi
if [[ "$ip" == "127.0.0.1" ]] || [[ "$ip" == "localhost" ]] || [[ "$ip" == "unknown" ]]; then
echo "本机"
return 0
fi
# 检查缓存
if [ "$CACHE_IP_INFO" = true ] && [ -f "$IP_CACHE_FILE" ]; then
local cached_info=$(grep "^$ip|" "$IP_CACHE_FILE" | head -1)
if [ -n "$cached_info" ]; then
local cache_time=$(echo "$cached_info" | cut -d'|' -f2)
local current_time=$(date +%s)
if [ $((current_time - cache_time)) -lt $CACHE_EXPIRE ]; then
local cached_location=$(echo "$cached_info" | cut -d'|' -f3-)
# 缓存的内容也要转中文
english_to_chinese "$cached_location"
return 0
fi
fi
fi
# 在线查询失败,使用离线数据库
local offline_result=$(get_ip_location_offline "$ip")
echo "$offline_result"
# 在线查询
local location_info=""
if command -v curl &> /dev/null; then
location_info=$(get_ip_location_online "$ip")
fi
# 如果查询失败,使用简单判断
if [ -z "$location_info" ] || [ "$location_info" = "null" ]; then
# 根据IP段简单判断
local first_octet=$(echo "$ip" | cut -d. -f1)
case $first_octet in
1|14|27|36|39|42|49|58|59|60|61|106|110|111|112|113|114|115|116|117|118|119|120|121|122|123|124|125|139|140|171|175|180|182|183|202|203|210|218|219|220|221|222|223)
location_info="中国-未知地区"
;;
*)
location_info="未知位置"
;;
esac
fi
# 转换为中文
location_info=$(english_to_chinese "$location_info")
# 缓存结果
if [ "$CACHE_IP_INFO" = true ]; then
mkdir -p "$(dirname "$IP_CACHE_FILE")"
grep -v "^$ip|" "$IP_CACHE_FILE" 2>/dev/null > "${IP_CACHE_FILE}.tmp" || true
echo "$ip|$(date +%s)|$location_info" >> "${IP_CACHE_FILE}.tmp"
mv "${IP_CACHE_FILE}.tmp" "$IP_CACHE_FILE" 2>/dev/null || true
fi
echo "$location_info"
}
# 获取城市名称
# 获取城市名称(修复版)
get_city_name() {
local ip="$1"
local location=$(get_ip_location "$ip")
# 从位置信息中提取城市名
if [[ "$location" == *"-"* ]]; then
# 格式: 中国-江西-南昌(中国联通)
local city=$(echo "$location" | awk -F'-' '{print $NF}' | sed 's/(.*//')
if [ -n "$city" ] && [ "$city" != "未知位置" ] && [ "$city" != "未知地区" ]; then
if [ -n "$city" ] && [ "$city" != "未知位置" ] && [ "$city" != "未知地区" ] && [ "$city" != "本机" ]; then
# 只保留中文、英文、数字,其他字符替换为下划线
echo "$city" | sed 's/[^a-zA-Z0-9\u4e00-\u9fff]/_/g'
else
echo "unknown"
fi
elif [[ "$location" == *" "* ]]; then
# 格式: 中国 江西 南昌 联通
local city=$(echo "$location" | awk '{print $3}')
if [ -n "$city" ] && [ "$city" != "未知位置" ] && [ "$city" != "未知地区" ] && [ "$city" != "本机" ]; then
echo "$city" | sed 's/[^a-zA-Z0-9\u4e00-\u9fff]/_/g'
else
echo "unknown"
@@ -435,7 +358,7 @@ get_client_ip() {
echo "$ip"
}
# 生成唯一日志文件名
# 生成唯一日志文件名(修复版)
generate_log_filename() {
local client_ip=$(get_client_ip)
local city_name=$(get_city_name "$client_ip")
@@ -443,7 +366,7 @@ generate_log_filename() {
local counter=1
local base_name=""
if [ "$city_name" != "unknown" ]; then
if [ "$city_name" != "unknown" ] && [ -n "$city_name" ]; then
base_name="monitor_${client_ip//./_}_${city_name}_${log_date}"
else
base_name="monitor_${client_ip//./_}_${log_date}"
@@ -504,10 +427,10 @@ init_log_system() {
log_message "INFO" "日志轮转: ${LOG_ROTATE_INTERVAL}秒或${MAX_LOG_SIZE}"
# 显示查询方式
if [[ "$location_info" == *"离线"* ]] || [[ "$location_info" == *"未知"* ]]; then
log_message "WARN" "使用离线IP数据库查询"
if [[ "$location_info" == *"未知"* ]]; then
log_message "WARN" "IP查询使用备用方案"
else
log_message "SUCCESS" "在线IP查询成功"
log_message "SUCCESS" "IP查询成功"
fi
}
@@ -525,6 +448,118 @@ log_rotation() {
log_message "INFO" "新日志文件已创建"
}
# 资源监控函数移除CPU限制
monitor_resources() {
local check_count=0
while true; do
sleep 60
# 检查日志轮转条件
check_log_rotation
# 只监控内存使用移除CPU监控
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 ] 2>/dev/null; then
log_message "WARN" "磁盘使用率过高: ${disk_usage}%"
fi
# 定期系统检查
((check_count++))
if [ $((check_count * 60)) -ge $CHECK_INTERVAL ]; then
perform_system_check
check_count=0
fi
# 定期清理旧日志
local current_time=$(date +%s)
if [ $((current_time - LAST_CLEANUP)) -ge $CLEANUP_INTERVAL ]; then
cleanup_old_logs
LAST_CLEANUP=$current_time
fi
done
}
# 系统健康检查
perform_system_check() {
log_message "INFO" "=== 系统健康检查 ==="
# 内存信息
local mem_info=$(free -h 2>/dev/null || echo "无法获取内存信息")
log_message "INFO" "内存使用: $mem_info"
# 磁盘信息
local disk_info=$(df -h "$LOG_DIR" 2>/dev/null | tail -1 || echo "无法获取磁盘信息")
log_message "INFO" "磁盘使用: $disk_info"
# 日志文件信息
local log_count=$(find "$LOG_DIR" -name "monitor_*.log" 2>/dev/null | wc -l)
local total_log_size=$(du -sh "$LOG_DIR" 2>/dev/null | cut -f1 || echo "0")
log_message "INFO" "日志文件数: $log_count, 总大小: $total_log_size"
log_message "INFO" "=== 检查完成 ==="
}
# 清理旧日志
cleanup_old_logs() {
log_message "INFO" "开始清理旧日志..."
# 按时间清理
local deleted_count=$(find "$LOG_DIR" -name "monitor_*.log" -mtime "+$BACKUP_DAYS" -delete -print 2>/dev/null | wc -l)
if [ "$deleted_count" -gt 0 ]; then
log_message "INFO" "已删除 $deleted_count 个过期日志文件"
fi
# 按数量清理(保留最新的)
local log_count=$(find "$LOG_DIR" -name "monitor_*.log" 2>/dev/null | wc -l)
if [ "$log_count" -gt "$MAX_LOG_FILES" ]; then
local files_to_delete=$((log_count - MAX_LOG_FILES))
find "$LOG_DIR" -name "monitor_*.log" -type f -printf '%T@ %p\n' 2>/dev/null | \
sort -n 2>/dev/null | head -n "$files_to_delete" | cut -d' ' -f2- | while read file; do
rm -f "$file" 2>/dev/null && log_message "INFO" "删除旧日志: $(basename "$file")"
done
fi
log_message "SUCCESS" "日志清理完成"
}
# 配置实时history
configure_realtime_history() {
log_message "INFO" "配置实时命令记录..."
for user_dir in /home/* /root; do
if [ -d "$user_dir" ]; then
local user=$(basename "$user_dir")
local bashrc="$user_dir/.bashrc"
if [ -f "$bashrc" ]; then
if ! grep -q "REAL_TIME_HISTORY" "$bashrc"; then
cat >> "$bashrc" << 'EOF'
# REAL_TIME_HISTORY - 实时命令记录配置
export PROMPT_COMMAND='history -a; history -c; history -r'
export HISTTIMEFORMAT='%F %T '
export HISTSIZE=10000
export HISTFILESIZE=20000
shopt -s histappend
export HISTCONTROL=ignoredups:erasedups
EOF
log_message "SUCCESS" "已为用户 $user 配置实时命令记录"
else
log_message "INFO" "用户 $user 已配置实时记录"
fi
fi
fi
done
}
# 主监控函数
start_main_monitor() {
log_message "INFO" "启动主监控进程..."
@@ -607,13 +642,8 @@ start_background_monitor() {
log_message "INFO" "后台监控进程启动 - PID: $$"
echo $$ > "/tmp/command_monitor.pid"
# 启动资源监控(简化版)
(
while true; do
sleep 60
check_log_rotation
done
) &
# 启动资源监控
monitor_resources &
start_main_monitor
@@ -637,40 +667,9 @@ start_background_monitor() {
fi
}
# 配置实时history
configure_realtime_history() {
log_message "INFO" "配置实时命令记录..."
for user_dir in /home/* /root; do
if [ -d "$user_dir" ]; then
local user=$(basename "$user_dir")
local bashrc="$user_dir/.bashrc"
if [ -f "$bashrc" ]; then
if ! grep -q "REAL_TIME_HISTORY" "$bashrc"; then
cat >> "$bashrc" << 'EOF'
# REAL_TIME_HISTORY - 实时命令记录配置
export PROMPT_COMMAND='history -a; history -c; history -r'
export HISTTIMEFORMAT='%F %T '
export HISTSIZE=10000
export HISTFILESIZE=20000
shopt -s histappend
export HISTCONTROL=ignoredups:erasedups
EOF
log_message "SUCCESS" "已为用户 $user 配置实时命令记录"
else
log_message "INFO" "用户 $user 已配置实时记录"
fi
fi
fi
done
}
# 显示使用说明
show_usage() {
echo -e "${GREEN}实时命令监控系统 v3.2${NC}"
echo -e "${GREEN}实时命令监控系统 v3.3${NC}"
echo "用法: $0 [选项]"
echo
echo "选项:"
@@ -680,9 +679,10 @@ show_usage() {
echo " -h, --help 显示此帮助信息"
echo
echo "特性:"
echo " - 离线IP数据库支持"
echo " - 中文地理位置显示"
echo " - 多IP查询源支持"
echo " - 无CPU使用限制"
echo " - 智能日志轮转"
echo " - 唯一日志文件名"
echo
echo "示例:"
echo " $0 -d # 后台运行监控"