#!/bin/bash

# 获取本地服务器IP地址
externalip=$(curl -s ipecho.net/plain;echo)
echo $externalip


# 获取cpu总核数
cpu_num=`grep -c "model name" /proc/cpuinfo`
echo $cpu_num


# 获取CPU利用率
# CPU时间计算公式:CPU_TIME=user+system+nice+idle+iowait+irq+softirq
# CPU使用率计算公式:cpu_usage=(idle2-idle1)/(cpu2-cpu1)*100
# 默认时间间隔
# CPU 核数为多核,则需获取到多个cpu的利用率
TIME_INTERVAL=1
cpunum=$(cat /proc/stat | grep cpu | wc -l)
j=$cpunum
while(( $j > 0 ))
do
i=1
LAST_CPU_INFO[i]=$(cat /proc/stat | grep cpu | awk NR==$i | awk '{print $2,$3,$4,$5,$6,$7,$8}')
LAST_SYS_IDLE[i]=$(echo ${LAST_CPU_INFO[i]} | awk '{print $4}')
LAST_TOTAL_CPU_T[i]=$(echo ${LAST_CPU_INFO[i]} | awk '{print $1+$2+$3+$4+$5+$6+$7}')
let 'i++'
let 'j--'
done

sleep ${TIME_INTERVAL}

j=$cpunum
while (( $j > 0 ))
do
i=1
NEXT_CPU_INFO[i]=$(cat /proc/stat | grep cpu | awk NR==$i | awk '{print $2,$3,$4,$5,$6,$7,$8}')
NEXT_SYS_IDLE[i]=$(echo ${NEXT_CPU_INFO[i]} | awk '{print $4}')
NEXT_TOTAL_CPU_T[i]=$(echo ${NEXT_CPU_INFO[i]} | awk '{print $1+$2+$3+$4+$5+$6+$7}')
let 'i++'
let 'j--'
done
j=$cpunum
while (( $j > 0 ))
do
i=1
# 系统空闲时间
SYSTEM_IDLE[i]=`echo ${NEXT_SYS_IDLE[i]} ${LAST_SYS_IDLE[i]} | awk '{print$1-$2}'`
# CPU总时间
TOTAL_TIME[i]=`echo ${NEXT_TOTAL_CPU_T[i]} ${LAST_TOTAL_CPU_T[i]} | awk '{print $1-$2}'`
CPU_USAGE[i]=`echo ${SYSTEM_IDLE[i]} ${TOTAL_TIME[i]} | awk '{printf "%.2f", 100-$1/$2*100}'`
echo "${CPU_USAGE[i]}%"
let 'i++'
let 'j--'
done
# *********************************************************************
# 获取磁盘信息
root_all=$(df -lh | grep -w '\/' | awk '{print $2}')
root_used=$(df -lh | grep -w '\/' | awk '{print $3}')
root_percent=$(df -lh | grep -w '\/' | awk '{print $5}')
echo "$root_all"
echo "$root_used"
echo "$root_percent"

# 获取内存信息
memery_used=$(free | awk 'NR==2' | awk '{print $3}')
memery_all=$(free | awk 'NR==2' | awk '{print $2}')
memery_percent=`echo ${memery_used} ${memery_all} | awk '{printf "%.2f", $1/$2*100}'`
echo "${memery_all}"
echo "${memery_used}"
echo "${memery_percent}%"

# 获取交换区信息
swap_used=$(free | awk 'NR==3' | awk '{print $3}')
swap_all=$(free | awk 'NR==3' | awk '{print $2}')
if [ $swap_used == 0 ]
then
swap_percent=0
else
swap_percent=`echo ${swap_used} ${swap_all} | awk '{printf "%.2f", $1/$2*100}'`
fi
echo "${swap_all}"
echo "${swap_used}"
echo "${swap_percent}%"

# 最大数据段长度
MAX_DATA_SEG=$(ulimit -a | grep '\-d' | awk '{print $6}')

#最大内存大小
MAX_MEM_SIZE=$(ulimit -a | grep '\-m' | awk '{print $6}')

#最大堆栈大小
MAX_STACK_SIZE=$(ulimit -a | grep '\-s' | awk '{print $5}')

#最大CPU时间
MAX_CPU_TIME=$(ulimit -a | grep '\-t' | awk '{print $5}')

#最大虚拟内存
MAX_VIRTUAL_MEM=$(ulimit -a | grep '\-v' | awk '{print $5}')

#最大进程数
MAX_USER_PROCESSES=$(ulimit -a | grep '\-u' | awk '{print $5}')

#最大文件打开数
MAX_OPEN_FILES=$(ulimit -a | grep '\-n' | awk '{print $4}')

echo $MAX_DATA_SEG
echo $MAX_MEM_SIZE
echo $MAX_STACK_SIZE
echo $MAX_CPU_TIME
echo $MAX_VIRTUAL_MEM
echo $MAX_USER_PROCESSES
echo $MAX_OPEN_FILES

修改后:

#!/bin/bash

# 获取本地服务器IP地址
#externalip=$(curl -s ipecho.net/plain;echo)
#echo $externalip


# 获取cpu总核数
cpu_num=`grep -c "model name" /proc/cpuinfo`
echo $cpu_num

#************************************************************************
# 获取CPU利用率
# CPU时间计算公式:CPU_TIME=user+system+nice+idle+iowait+irq+softirq
# CPU使用率计算公式:cpu_usage=(idle2-idle1)/(cpu2-cpu1)*100
# 默认时间间隔
# CPU 核数为多核,则需获取到多个cpu的利用率
TIME_INTERVAL=1
cpunum=$(cat /proc/stat | grep cpu | wc -l)
j=$cpunum
i=1
msg=$(cat /proc/stat)
while(( $j > 0 ))
do
LAST_CPU_INFO[i]=$(echo "$msg" | grep cpu | awk NR==$i | awk '{print $2,$3,$4,$5,$6,$7,$8}')
LAST_SYS_IDLE[i]=$(echo ${LAST_CPU_INFO[i]} | awk '{print $4}')
LAST_TOTAL_CPU_T[i]=$(echo ${LAST_CPU_INFO[i]} | awk '{print $1+$2+$3+$4+$5+$6+$7}')
let 'i++'
let 'j--'
done

sleep ${TIME_INTERVAL}

j=$cpunum
i=1
msg=$(cat /proc/stat)
while (( $j > 0 ))
do
NEXT_CPU_INFO[i]=$(echo "$msg" | grep cpu | awk NR==$i | awk '{print $2,$3,$4,$5,$6,$7,$8}')
NEXT_SYS_IDLE[i]=$(echo ${NEXT_CPU_INFO[i]} | awk '{print $4}')
NEXT_TOTAL_CPU_T[i]=$(echo ${NEXT_CPU_INFO[i]} | awk '{print $1+$2+$3+$4+$5+$6+$7}')
let 'i++'
let 'j--'
done
j=$cpunum
CPU_USAGE_ALL=""
i=1
while (( $j > 0 ))
do
# 系统空闲时间
SYSTEM_IDLE[i]=`echo ${NEXT_SYS_IDLE[i]} ${LAST_SYS_IDLE[i]} | awk '{print$1-$2}'`
# CPU总时间
TOTAL_TIME[i]=`echo ${NEXT_TOTAL_CPU_T[i]} ${LAST_TOTAL_CPU_T[i]} | awk '{print $1-$2}'`
CPU_USAGE[i]=`echo ${SYSTEM_IDLE[i]} ${TOTAL_TIME[i]} | awk '{printf "%.2f", 100-$1/$2*100}'`
CPU_USAGE_ALL="${CPU_USAGE_ALL},${CPU_USAGE[i]}"
let 'i++'
let 'j--'
done
echo "${CPU_USAGE_ALL}"
# *********************************************************************
# 获取所有磁盘信息
df_count=$(df -lh | wc -l)
desk_msg=''
i=2
while (( $df_count > 0 ))
do
temp_desk_msg=$(df -lh | awk NR==$i)
desk_msg="${desk_msg};${temp_desk_msg}"
let 'i++'
let 'df_count--'
done
# 获取磁盘读写kb
disk_r_kb=0
disk_w_kb=0
for tempdisk in `lsblk |grep -w 'disk' | awk '{print $1}'`
do
temp_disk_r_kb=$(iostat -k | grep -w $tempdisk | awk '{print $5}')
temp_disk_w_kb=$(iostat -k | grep -w $tempdisk | awk '{print $6}')
if [ $temp_disk_r_kb != '' ]
then
disk_r_kb=`expr $disk_r_kb + $temp_disk_r_kb`
fi
if [ $temp_disk_w_kb != '' ]
then
disk_w_kb=`expr $disk_w_kb + $temp_disk_w_kb`
fi
done

msg=$desk_msg,$disk_r_kb,$disk_w_kb
echo $msg
# 获取磁盘信息
disk_all=$(df -lh | grep -w '\/' | awk '{print $(NF-4)}')
disk_used=$(df -lh | grep -w '\/' | awk '{print $(NF-3)}')
disk_percent=$(df -lh | grep -w '\/' | awk '{print $(NF-1)}')
echo "$disk_all"
echo "$disk_used"
echo "${disk_percent%\%*}"
# 获取磁盘io
#disk_r=$(iostat | grep -w sda | awk '{print $3}')
#disk_w=$(iostat | grep -w sda | awk '{print $4}')
disk_r=0
disk_w=0
for tempdisk in `lsblk |grep -w 'disk' | awk '{print $1}'`
do
temp_disk_r=$(iostat | grep -w $tempdisk | awk '{print $3}')
temp_disk_w=$(iostat | grep -w $tempdisk | awk '{print $4}')
if [ $temp_disk_r != '' ]
then
disk_r=`echo "scale=2; $disk_r + $temp_disk_r" | bc | awk '{printf "%.2f", $0}'`
fi
if [ $temp_disk_w != '' ]
then
disk_w=`echo "scale=2; $disk_w + $temp_disk_w" | bc | awk '{printf "%.2f", $0}'`
fi
done
echo $disk_r
echo $disk_w

#**********************************************************************
# 获取内存信息
memery_used_k=$(free | grep 'Mem' | awk '{print $3}')
memery_all_k=$(free | grep 'Mem' | awk '{print $2}')
memery_percent=`echo ${memery_used_k} ${memery_all_k} | awk '{printf "%.2f", $1/$2*100}'`
echo "${memery_all_k}"
echo "${memery_used_k}"
echo "${memery_percent}"
#**********************************************************************
# 获取交换区信息
swap_used_k=$(free | grep 'Swap' | awk '{print $3}')
swap_all_k=$(free | grep 'Swap' | awk '{print $2}')
if [ $swap_used_k == 0 ]
then
swap_percent=0
else
swap_percent=`echo ${swap_used_k} ${swap_all_k} | awk '{printf "%.2f", $1/$2*100}'`
fi
echo "${swap_all_k}"
echo "${swap_used_k}"
echo "${swap_percent}"
#**********************************************************************
# 最大数据段长度
MAX_DATA_SEG=$(ulimit -a | grep '\-d' | awk '{print $6}')

#最大内存大小
MAX_MEM_SIZE=$(ulimit -a | grep '\-m' | awk '{print $6}')

#最大堆栈大小
MAX_STACK_SIZE=$(ulimit -a | grep '\-s' | awk '{print $5}')

#最大CPU时间
MAX_CPU_TIME=$(ulimit -a | grep '\-t' | awk '{print $5}')

#最大虚拟内存
MAX_VIRTUAL_MEM=$(ulimit -a | grep '\-v' | awk '{print $5}')

#最大进程数
MAX_USER_PROCESSES=$(ulimit -a | grep '\-u' | awk '{print $5}')

#最大文件打开数
MAX_OPEN_FILES=$(ulimit -a | grep '\-n' | awk '{print $4}')

echo $MAX_DATA_SEG
echo $MAX_MEM_SIZE
echo $MAX_STACK_SIZE
echo $MAX_CPU_TIME
echo $MAX_VIRTUAL_MEM
echo $MAX_USER_PROCESSES
echo $MAX_OPEN_FILES

注意:

编写脚本时,注意不同版本下的执行命令的不同、执行参数不同或者显示结果存在的不同

 

Logo

更多推荐