收集系统信息脚本目的:分析系统资源性能瓶颈
脚本功能
我们通过vmstat命令
[klaus@localhost ~]$ man vmstat | grep -C6 'CPU'
System
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.
CPU
These are percentages of total CPU time.
us: Time spent running non-kernel code. (user time, including nice time)
sy: Time spent running kernel code. (system time)
id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.
根据上面内容就可以完成功能
get_cpu_infor(){
local i=1
echo "-----------------------------------------------------------"
while [[ $i -le 3 ]];do
echo -e "\033[32m# Reference ${i}\033[0m"
systime=`vmstat | awk '{if (NR==3) print $14"%"}'`
used_time=`vmstat | awk '{if (NR==3) print 100-$15"%"}'`
IO_wait=`vmstat | awk '{if (NR==3) print $16"%"}'`
echo "Time spent running kernel code:$systime"
echo "Time spent used:$used_time,Time spent waiting for IO:$IO_wait"
sleep 2
let i++
done
echo "-----------------------------------------------------------"
}
同样,我们看看iostat命令,再通过man iostat查看对照信息
[klaus@localhost ~]$ iostat -x -k
Linux 2.6.32-754.3.5.el6.x86_64 (localhost.localdomain) 02/11/2020 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.40 0.00 0.46 0.01 0.00 99.14
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.12 1.14 0.29 0.59 4.41 6.94 25.80 0.00 0.90 0.46 1.12 0.52 0.05
根据上面的解读,可以完成磁盘负载
get_disk_load(){
local i=1
while [[ $i -le 3 ]];do
echo -e "\033[32m# DISK Partition Reference ${i}\033[0m"
IO_util=`iostat -x -k | awk '/^[v|s]/{OFS=": ";print $1,$NF"%"}'`
IO_read=`iostat -x -k | awk '/^[v|s]/{OFS=": ";print $1,$6"KB"}'`
IO_write=`iostat -x -k | awk '/^[v|s]/{OFS=": ";print $1,$7"KB"}'`
IO_wait=`vmstat | awk '{if(NR==3) print $16"%"}'`
echo -e "bandwidth utilization for the device:${IO_util}"
echo -e "Time spent waiting for IO:${IO_wait}"
echo -e "Read/s:$IO_read,Write/s:$IO_write"
i=$((i+1))
sleep 2
done
echo "-----------------------------------------------------------"
}
磁盘分区主要用的是iostat,实现细节如下,其中disk_use.log为日志文件
get_disk_part(){
DISK_log=/tmp/disk_use.log
DISK_total=`sudo fdisk -l|awk '/^Disk.*bytes/ && /dev/{print $2 $3"GB"}'`
DISK_userate=`df -h | awk '/^\/dev/{print int($5)}'`
local DISK_Threshold=50
if [ -n `cat $DISK_log` ];then
rm -rf $DISK_log
fi
touch $DISK_log
for i in $DISK_userate;do
if [ $i -gt $DISK_Threshold ];then
PART=`df -h |awk '{if(int($5)=='''$i''') print $6}'`
echo "$PART=${i}%">>$DISK_log
fi
done
echo -e "DISK total:${DISK_total}"
if [ -f $DISK_log ];then
echo "Warning, disk usage exceeds threshold $DISK_Threshold%!"
cat $DISK_log
fi
}
内存使用情况具体通过free -m命令,实现细节如下
get_mem_use(){
echo "-----------------------------------"
MEM_TOTAl=`free -m|awk '{if(NR==2)printf "%.2f",$2/1024}END{print"G"}'`
MEM_USED=`free -m|awk '{if(NR==2)printf "%.2f",$3/1024}END{print"G"}'`
MEM_FREE=`free -m|awk '{if(NR==2)printf "%.2f",$4/1024}END{print"G"}'`
MEM_CACHE=`free -m|awk '{if(NR==2)printf "%.2f",$NF/1024}END{print"G"}'`
echo -e "Total_memory:$MEM_TOTAl"
echo -e "Used:$MEM_USED;Free_memory:$MEM_FREE and Cache:$MEM_CACHE"
}
使用netsta或者ss都可以,具体实现细节如下
get_tcp_state(){
echo "-----------------------------------"
COUNT=`ss -ant|awk '!/state/{status[$1]++}END{for(i in status)print i,status[i]}'`
echo -e "TCP Connection status:\n$COUNT"
echo "-----------------------------------"
}
查看前10的CPU或者内存使用的命令具体用ps aux就可以了
get_cpu_top10(){
echo "-----------------CPU Top10 List-----------------"
CPU_LOG=/tmp/cpu_top.log
local i=1
while [[ $i -le 3 ]];do
ps aux | awk '{if($3>=0.1) print "PID:"$2" CPU:"$3"% -->" $11}'|sort -k3 -nr |head -10 >$CPU_LOG
if [[ -n `cat $CPU_LOG` ]];then
echo -e "\033[32m# CPU TOP10 Reference ${i}\033[0m""]]"
cat $CPU_LOG
else
echo "No process using the CPU"
break
fi
let i++
sleep 2
done
echo "------------------------------------------------"
}
get_mem_top10(){
echo "-----------------MeM Top10 List-----------------"
MEM_LOG=/tmp/mem_top.log
local i=1
while [[ $i -le 3 ]];do
ps aux | awk '{if($4>=0.1) print "PID:"$2" MEM:"$4"% -->" $11}'|sort -k4 -nr |head -10 >$MEM_LOG
if [[ -n `cat $MEM_LOG` ]];then
echo -e "\033[32m# MEM TOP10 Reference ${i}\033[0m""]]"
cat $MEM_LOG
else
echo "No process using the MEMORY"
break
fi
let i++
sleep 2
done
echo "------------------------------------------------"
}
网络流量统计这里需要针对某张单一网卡,使用的具体命令为ifconfig
get_network_traf(){
while true;do
read -p "Please enter the network card name(eth[0-9] or em[0-9]):" eth
if [ `ifconfig | grep -c "\<$eth\>"` -eq 1 ];then
break
else
echo "Incorrect name entered, the network card could not be found!"
fi
done
echo "------------------------------------------------"
local i=1
while [[ $i -le 3 ]];do
OLD_RX=`ifconfig eth0 |awk -F'[: ]+' '/bytes/{if(NR==8)print $4;else if(NR==5)print $6}'`
OLD_TX=`ifconfig eth0 |awk -F'[: ]+' '/bytes/{if(NR==8)print $9;else if(NR==5)print $6}'`
sleep 1
NEW_RX=`ifconfig eth0 |awk -F'[: ]+' '/bytes/{if(NR==8)print $4;else if(NR==5)print $6}'`
NEW_TX=`ifconfig eth0 |awk -F'[: ]+' '/bytes/{if(NR==8)print $9;else if(NR==5)print $6}'`
RX=`awk 'BEGIN{printf "%.8f\n",'$(($NEW_RX-$OLD_RX))'/1024/128}'`
TX=`awk 'BEGIN{printf "%.8f\n",'$(($NEW_TX-$OLD_TX))'/1024/128}'`
echo "RX:${RX} MB/s TX:${TX} MB/s"
let i++
sleep 1
done
}
8.实现效果
[klaus@localhost project]$ ./sys_info.sh
Non-root login for current user!
[sudo] password for klaus:
1) CPU/Disk_load 3) TCP_status 5) Network_traffic
2) Disk/Mem_use 4) CPU/Mem_top10 6) quit
Please input your choice[6 for quit]:1
-----------------------------------------------------------
# CPU Reference 1
Time spent running kernel code:0%
Time spent used:1%,Time spent waiting for IO:0%
# CPU Reference 2
Time spent running kernel code:0%
Time spent used:1%,Time spent waiting for IO:0%
# CPU Reference 3
Time spent running kernel code:0%
Time spent used:1%,Time spent waiting for IO:0%
-----------------------------------------------------------
# DISK Partition Reference 1
bandwidth utilization for the device:sda: 0.04%
Time spent waiting for IO:0%
Read/s:sda: 4.05KB,Write/s:sda: 6.83KB
# DISK Partition Reference 2
bandwidth utilization for the device:sda: 0.04%
Time spent waiting for IO:0%
Read/s:sda: 4.05KB,Write/s:sda: 6.83KB
# DISK Partition Reference 3
bandwidth utilization for the device:sda: 0.04%
Time spent waiting for IO:0%
Read/s:sda: 4.05KB,Write/s:sda: 6.83KB
-----------------------------------------------------------
Please input your choice[6 for quit]:2
DISK total:/dev/sda:21.5GB
Warning, disk usage exceeds threshold 50%!
/=51%
-----------------------------------
Total_memory:0.96G
Used:0.75G;Free_memory:0.21G and Cache:0.27G
Please input your choice[6 for quit]:3
-----------------------------------
TCP Connection status:
ESTAB 5
State 1
LISTEN 6
-----------------------------------
Please input your choice[6 for quit]:4
-----------------CPU Top10 List-----------------
# CPU TOP10 Reference 1]]
PID:93642 CPU:10.0% -->ps
PID:7 CPU:0.1% -->[events/0]
PID:2858 CPU:0.1% -->/usr/lib/vmware-tools/sbin64/vmtoolsd
PID:1501 CPU:0.1% -->/usr/sbin/vmtoolsd
# CPU TOP10 Reference 2]]
PID:93649 CPU:1.0% -->ps
PID:7 CPU:0.1% -->[events/0]
PID:2858 CPU:0.1% -->/usr/lib/vmware-tools/sbin64/vmtoolsd
PID:1501 CPU:0.1% -->/usr/sbin/vmtoolsd
# CPU TOP10 Reference 3]]
PID:7 CPU:0.1% -->[events/0]
PID:2858 CPU:0.1% -->/usr/lib/vmware-tools/sbin64/vmtoolsd
PID:1501 CPU:0.1% -->/usr/sbin/vmtoolsd
------------------------------------------------
-----------------MeM Top10 List-----------------
# MEM TOP10 Reference 1]]
PID:93670 MEM:0.1% -->awk
PID:93669 MEM:0.1% -->ps
PID:93504 MEM:0.1% -->/bin/bash
PID:93206 MEM:1.7% -->vim
PID:92323 MEM:0.1% -->-bash
PID:92322 MEM:0.1% -->sshd:
PID:92318 MEM:0.4% -->sshd:
PID:92246 MEM:0.1% -->-bash
PID:92245 MEM:0.1% -->sshd:
PID:92241 MEM:0.4% -->sshd:
# MEM TOP10 Reference 2]]
PID:93677 MEM:0.1% -->awk
PID:93676 MEM:0.1% -->ps
PID:93504 MEM:0.1% -->/bin/bash
PID:93206 MEM:1.7% -->vim
PID:92323 MEM:0.1% -->-bash
PID:92322 MEM:0.1% -->sshd:
PID:92318 MEM:0.4% -->sshd:
PID:92246 MEM:0.1% -->-bash
PID:92245 MEM:0.1% -->sshd:
PID:92241 MEM:0.4% -->sshd:
# MEM TOP10 Reference 3]]
PID:93684 MEM:0.1% -->awk
PID:93683 MEM:0.1% -->ps
PID:93504 MEM:0.1% -->/bin/bash
PID:93206 MEM:1.7% -->vim
PID:92323 MEM:0.1% -->-bash
PID:92322 MEM:0.1% -->sshd:
PID:92318 MEM:0.4% -->sshd:
PID:92246 MEM:0.1% -->-bash
PID:92245 MEM:0.1% -->sshd:
PID:92241 MEM:0.4% -->sshd:
------------------------------------------------
Please input your choice[6 for quit]:5
Please enter the network card name(eth[0-9] or em[0-9]):eth0
------------------------------------------------
RX:0.00000000 MB/s TX:0.00000000 MB/s
RX:0.00000000 MB/s TX:0.00000000 MB/s
RX:0.00000000 MB/s TX:0.00000000 MB/s
Please input your choice[6 for quit]:6
[klaus@localhost project]$
具体的完整代码在这里,大家也可以参考学习一下,主要是shell的一些用法