- CPU空闲率
- 系统总内存
- 系统已使用内存
- 系统可用内存
- 硬盘交换分区占用
- centos-root 分区信息
- centos-home 分区信息
- 网络使用情况
- 负载平均值
- 系统中运行的所有进程总数
- 正在运行的Java进程
- 正在使用的数据库和版本(MySQL,SQL Server)
#!/bin/bash
echo "---------------------------------------------------------"
echo "CPU空闲率:"
top -bn1 | grep "Cpu(s)" | \
sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \
awk '{print 100 - $1"%"}'
echo "---------------------------------------------------------"
echo "系统总内存:"
free -m | awk '/Mem:/ {print $2 " MB"}'
echo "---------------------------------------------------------"
echo "系统已使用内存:"
free -m | awk '/Mem:/ {printf "%.2f%%\n", $3*100/$2 }'
echo "---------------------------------------------------------"
echo "系统可用内存:"
free -m | awk '/Mem:/ {printf "%.2f%%\n", $7*100/$2 }'
echo "---------------------------------------------------------"
echo "硬盘交换分区占用:"
SWAP=$(free -m | grep Swap)
SWAP_TOTAL=$(echo $SWAP | awk '{print $2}')
SWAP_USED=$(echo $SWAP | awk '{print $3}')
if [ "$SWAP_TOTAL" -eq 0 ]; then
echo "无交换分区"
else
SWAP_USED_PERCENT=$(awk "BEGIN {printf \"%.2f\", ($SWAP_USED/$SWAP_TOTAL)*100}")
echo "${SWAP_USED_PERCENT}%"
fi
echo "---------------------------------------------------------"
echo "Root 分区信息:"
df -h /dev/mapper/centos-root | awk 'NR==2{print "总大小: " $2, "已使用: " $3, "剩余: " $4, "使用率: " $5}'
echo "---------------------------------------------------------"
echo "Home 分区信息:"
df -h /dev/mapper/centos-home | awk 'NR==2{print "总大小: " $2, "已使用: " $3, "剩余: " $4, "使用率: " $5}'
echo "---------------------------------------------------------"
echo "网络使用情况:"
cat /proc/net/dev | awk 'NR>2{printf "%s: Received: %s MB, Transmitted: %s MB\n", $1, $2/1024/1024, $10/1024/1024}'
echo "---------------------------------------------------------"
echo "负载平均值:"
uptime | awk -F'[a-z]:' '{ print $2}'
echo "---------------------------------------------------------"
echo "系统中运行的所有进程的总数:"
ps -e | wc -l
echo "---------------------------------------------------------"
echo "正在运行的Java进程:"
ps -ef | grep java | grep -v grep | awk '{
java_bin = ""; java_dir = ""; jar_file = "";
for (i=1; i<=NF; i++) {
if ($i ~ /-D(catalina.base|catalina.home)=/) {
split($i, path, "=");
java_dir = path[2];
} else if ($i ~ /\.jar/) {
jar_file = $i;
} else if ($i ~ /\/java$/) {
java_bin = $i;
}
}
if (java_bin != "" && java_dir != "") {
print java_bin " -> " java_dir "/temp";
} else if (java_bin != "" && jar_file != "") {
print java_bin " -> " jar_file;
}
}'
echo "---------------------------------------------------------"
if command -v mysql &> /dev/null
then
echo "MySQL版本:"
mysql -V
else
echo "MySQL未安装"
fi
if command -v sqlcmd &> /dev/null
then
echo "SQL Server版本:"
sqlcmd -Q "SELECT @@VERSION" -h -1
else
echo "SQL Server未安装"
fi