monitor.sh
955 字节
#!/bin/bash
# 定义API的URL
API_URL="https://lh.admin.yu2le.com/api/monitor/server/upload"
# 获取系统性能信息的函数
get_system_performance() {
# 获取CPU使用率
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
# 获取内存使用率
MEMORY_USAGE=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
# 获取磁盘使用率
DISK_USAGE=$(df -h | grep '^/dev/' | grep -v 'boot' | awk '{print $5}' | tr -d '%' | paste -sd+ | bc)
# 获取系统连接数
CONNECTION_COUNT=$(netstat -an | grep ESTABLISHED | wc -l)
# 返回JSON格式的数据
echo "{\"cpu_usage\": \"$CPU_USAGE\", \"memory_usage\": \"$MEMORY_USAGE\", \"disk_usage\": \"$DISK_USAGE\", \"connection_count\": \"$CONNECTION_COUNT\"}"
}
# 获取当前系统性能数据
DATA=$(get_system_performance)
echo $DATA
# 使用curl将数据上传到指定的API
curl -X POST -H "Content-Type: application/json" -d "$DATA" "$API_URL"