作者简介:
陈志珂(头条号:强扭的瓜不好吃)目前就职于中国最大的安卓应用软件公司,任高级工程师,现在公司任php开发工程师,python开发工程师,高级运维工程师,公众号“铅笔学园”运维内容合作作者之一。**
铅笔学园:IT资源分享|知识分享,做初级程序员的指明灯**
shell脚本实例-shell 分析系统瓶颈脚本
#!/usr/bin/bash
PS3=”Your choice is: [10 for quit]”
#检查是那个系统
os_check() {
if [ -e /etc/redhat-release ]; then
REDHAT=cat /etc/redhat-release |cut -d' ' -f1
else
DEBIAN=cat /etc/issue |cut -d' ' -f1
fi
if [ “ REDHAT"=="CentOS"−o" R E D H A T "==" C e n t O S " − o " REDHAT” == “Red” ]; then
P_M=yum
elif [ “ DEBIAN"=="Ubuntu"−o" D E B I A N "==" U b u n t u " − o " DEBIAN” == “ubutnu” ]; then
P_M=apt-get
else
Operating system does not support.
exit 1
fi
}
#判断是否是root用哦过户
if [ $LOGNAME != root ]; then
echo “Please use the root account operation.”
exit 1
fi
#查看cpu工具是否安装
if ! which vmstat &>/dev/null; then
echo “vmstat command not found, now the install.”
sleep 1
os_check
$P_M install procps -y
echo “———————————————————————–”
fi
#查看磁盘使用率的工具是否安装
if ! which iostat &>/dev/null; then
echo “iostat command not found, now the install.”
sleep 1
os_check
$P_M install sysstat -y
echo “———————————————————————–”
fi
while true; do
select input in cpu_load disk_load disk_use disk_inode mem_use tcp_status cpu_top10 mem_top10 traffic quit; do
case $input in
cpu_load)
#CPU usage and load
echo “—————————————”
i=1
while [[ $i -le 3 ]]; do
echo -e “[32m Reference value${i}[0m”
UTIL=vmstat |awk '{if(NR==3)print 100-$15"%"}'
USER=vmstat |awk '{if(NR==3)print $13"%"}'
SYS=vmstat |awk '{if(NR==3)print $14"%"}'
IOWAIT=vmstat |awk '{if(NR==3)print $16"%"}'
echo “Util: $UTIL”
echo “User use: $USER”
echo “System use: $SYS”
echo “I/O wait: $IOWAIT”
i=