• 15项目实战_收集系统信息


    项目实战_收集系统信息

    脚本目的

    收集系统信息,用于分析系统资源性能瓶颈
    

    脚本功能

    1.查看CPU利用率与负载(top、vmstat、sar)
    
    2.查看磁盘、Inode利用率与I/O负载(df、iostst、iotop、sar、dstat)
    
    3.查看内存利用率(free、vmstat)
    
    4.查看TCP连接状态(netstat、ss)
    
    5.查看CPU与内存占用最高的10个进程(top、ps)
    
    6,查看网络流量(ifconfig、iftop、iptraf)
    

    脚本内容

    #!/bin/bash
    #show system information
    
    # 定义提示符
    PS3="Your choice is: "
    
    os_check(){
        # 获取包的安装方式
        if [ -e /etc/redhat-release ]; then
            # REDHAD=`cat /etc/redhat-release | cut -d' ' -f1`
            REDHAT=`cat /etc/redhat-release | awk '{print $1}'`
        else
            DEBIAN=`cat /etc/issue | cut -d' ' -f1`
        fi
        if [ "${REDHAT}" == "CentOS" -o "${REDHAT}" == "Red" ]; then
            P_M=yum
        elif [ "${DEBIAN}" == "Ubuntu" -o "${DEBIAN}" == "ubuntu" ]; then
            P_M=apt-get
        else
             echo "Operating system doen not support."
            exit 1
        fi
    }
    
    # 检查当前登录用户是否root
    if [ ${LOGNAME} != root ]; then
        echo "Please use the root account operation."
        exit 1
    fi
    
    # 检查命令是否安装
    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 "vmstat command not found,now the install."
        sleep 1
        os_check
        ${P_M} install sysstat -y
        echo "-----------------------------------------------------------------"
    fi
    
    if ! which ifconfig &>/dev/null; then
        echo "ifconfig command not found,now the install."
        sleep 1
        os_check
        ${P_M} install net-tools -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利用率与负载
                    echo "-----------------------------------------"
                    i=1
                    while [[ $i -lt 3 ]]
                    do
                        echo -e "33[32m  参考值${i}33[0m"
                        UTIL=`vmstat | awk '{if(NR==3){print 100-$(NF-2)"%"}}'`
                        USER=`vmstat | awk '{if(NR==3){print $(NF-4)"%"}}'`
                        SYS=`vmstat | awk '{if(NR==3){print $(NF-3)"%"}}'`
                        IOWAIT=`vmstat | awk '{if(NR==3){print $(NF-1)"%"}}'`
                        echo "Util: ${UTIL}"
                        echo "user use: ${USER}"
                        echo "System use: ${SYS}"
                        echo "I/O wait: ${IOWAIT}"
                        let i++
                        sleep 1
                    done
                    echo "-----------------------------------------"
                    break
                ;;
                disk_load)
                    #硬盘I/O负载
                    echo "-----------------------------------------"
                    i=1
                    while [[ $i -lt 3 ]]
                    do
                        echo -e "33[32m  参考值${i}33[0m"
                        UTIL=`iostat -x -k | awk '/^[v|s]/{OFS=": ";print $1,$NF"%"}'`
                        READ=`iostat -x -k | awk '/^[v|s]/{OFS=": ";print $1,$6"KB"}'`
                        WRITE=`iostat -x -k | awk '/^[v|s]/{OFS=": ";print $1,$7"KB"}'`
                        IOWAIT=`vmstat | awk '{if(NR==3){print $(NF-1)"%"}}'`
                        echo -e "Util:"
                        echo -e "${UTIL}"
                        echo -e "I/O Wait: ${IOWAIT}"
                        echo -e "Read/s:
    ${READ}"
                        echo -e "Write/s:
    ${WRITE}"
                        # let i++
                        i=$(($i+1))
                        sleep 1
                    done              
                    echo "-----------------------------------------"
                    break
                ;;
                disk_use)
                    #硬盘利用率
                    DISK_LOG=/tmp/disk_use.tmp
                    DISK_TOTAL=`fdisk -l | awk '/^Disk.*bytes/ && //dev/{print $2" ";printf "%d",$3;print "GB"}'`
                    USE_RATE=`df -h | awk '/^/dev/{print int($5)}'`
                    for i in ${USE_RATE}
                    do
                        if [ ${i} -gt 90 ]; then
                            PART=`df -h | awk '{if(int($5)=='''${i}'''){print $NF}}'`
                            echo "${PART} = ${i}%" >> ${DISK_LOG}
                        fi
                    done
                    echo "-----------------------------------------"
                    echo -e "Disk total:
    ${DISK_TOTAL}"
                    if [ -f "${DISK_LOG}" ]; then
                        echo "-----------------------------------------"
                        cat ${DISK_LOG}
                        echo "-----------------------------------------"
                        rm -rf ${DISK_LOG}
                    else
                        echo "-----------------------------------------"
                        echo "Disk use rate no than 90% of the partition."
                        echo "-----------------------------------------"
                    fi
                    break
                ;;
                disk_inode)
                    #硬盘inode利用率
                    INODE_LOG=/tmp/inode_use.tmp
                    INODE_USE=`df -i | awk '/^/dev/{print int($5)}'`
                    for i in ${INODE_USE}
                    do
                        if (( ${i} > 90 )); then
                            PART=`df -i | awk '{if(int($5)=='''${i}'''){print $NF}}'`
                            echo "${PART} = ${i}%" >> ${INODE_LOG}
                        fi
                    done
                    if [ -f "${INODE_LOG}" ]; then
                        echo "-----------------------------------------"
                        cat ${INODE_LOG}
                        echo "-----------------------------------------"
                        rm -rf ${INODE_LOG}
                    else
                        echo "-----------------------------------------"
                        echo "Inode use rate no than 90% of the partition."
                        echo "-----------------------------------------"
                    fi
                    break
                ;;
                mem_use)
                    #内存利用率
                    echo "-----------------------------------------"
                    MEM_TOTAL=`free -mh | awk '{if(NR==2){printf "%.1f",$2}}'`
                    USE=`free -mh | awk '{if(NR==2){printf "%.1f",$3}}'`
                    FREE=`free -mh | awk '{if(NR==2){printf "%.1f",$4}}'`
                    CACHE=`free -mh | awk '{if(NR==2){printf "%.1f",$6}}'`
                    echo -e "Total: ${MEM_TOTAL}"
                    echo -e "Use: ${USE}"
                    echo -e "Free: ${FREE}"
                    echo -e "Cache: ${CACHE}"
                    echo "-----------------------------------------"
                    break
                ;;
                tcp_status)
                    #网络连接状态
                    echo "-----------------------------------------"
                    COUNT=`ss -ant | awk '!/^State/{status[$1]++}END{for(i in status){print i,status[i]}}'`
                    echo -e "Tcp connection status:
    ${COUNT}"
                    echo "-----------------------------------------"
                    break
                ;;
                cpu_top10)
                    #占用CPU高的前10个进程
                    echo "-----------------------------------------"
                    CPU_LOG=/tmp/cpu_top.tmp
                    i=1
                    while [[ $i -le 3 ]]
                    do
                        #循环从11列开始(进程名)开始打印,如果i等于最后一行,就打印i的列,并换行,否则就打印i的列
                        ps aux | awk '{if($3>=0.1){{printf "PID: " $2 " CPU: " $3 "%--> "}for(i=11;i<=NF;i++){if(i==NF){printf $i"
    "}else{printf $i}}}}' | sort -k4 -nr | head -n 10 > ${CPU_LOG}
                        if [[ -n `cat ${CPU_LOG}` ]]; then
                            echo -e "33[32m 参考值${i}33[0m"
                            cat ${CPU_LOG}
                            > ${CPU_LOG}
                        else
                            echo "No process using the CPU."
                            break
                        fi
                        let i++
                        sleep 1
                    done            
                    echo "-----------------------------------------"
                    break
                ;;
                mem_top10)
                    #占用内存高的前10个进程
                    echo "-----------------------------------------"
                    MEM_LOG=/tmp/mem_top.tmp
                    i=1
                    while [[ $i -le 3 ]]
                    do
                        #循环从11列开始(进程名)开始打印,如果i等于最后一行,就打印i的列,并换行,否则就打印i的列
                        ps aux | awk '{if($4>=0.1){{printf "PID: " $2 " Memory: " $4 "%--> "}for(i=11;i<=NF;i++){if(i==NF){printf $i"
    "}else{printf $i}}}}' | sort -k4 -nr | head -n 10 > ${MEM_LOG}
                        if [[ -n `cat ${MEM_LOG}` ]]; then
                            echo -e "33[32m 参考值${i}33[0m"
                            cat ${MEM_LOG}
                            > ${MEM_LOG}
                        else
                            echo "No process using the Memory."
                            break
                        fi
                        let i++
                        sleep 1
                    done            
                    echo "-----------------------------------------"
                    break
                ;;
                traffic)
                    #查看网络流量
                    while : 
                    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 "Input format error or Don't have the card name,please input again."
                        fi
                    done
                    echo "-----------------------------------------"
                    echo -e "In ------ Out"
                    i=1
                    while [[ $i -le 3 ]]
                    do
                        # CentOS6和CentOS7 ifconfig输出进出流量信息位置不同
                        # CentOS6中RX与TX行号等于8
                        # CentOS7中RX行号是5,,TX行号是7
                        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==7){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==7){print $6}}'`
                        # 1M带宽==>1M=1024bit 计算下载速率的时候用的是KB/S(指的K字节(byte)每秒)1byte=8*bit
                        # 1M带宽的理论下载速度通常是1024/8=128KB/S
                        # 网卡的流量单位是byte 字节,所以要先除以1024转成Kbyte,然后转换成多少M带宽,就再除以128
                        RX=`awk 'BEGIN{printf "%.1f
    ",'$((${NEW_RX}-${OLD_RX}))'/1024/128}'`
                        TX=`awk 'BEGIN{printf "%.1f
    ",'$((${NEW_TX}-${OLD_TX}))'/1024/128}'`
                        echo "${RX}MB/s ${TX}MB/s"
                        let i++
                        sleep 1
                    done
                    echo "-----------------------------------------"
                    break
                ;;
                quit)
                    exit 0
                ;;
                *)
                    echo "-----------------------------------------"
                    echo "Please enter the right number."
                    echo "-----------------------------------------"
                    break
                ;;            
            esac
            
        done
    done
    
  • 相关阅读:
    extjs 获取Dom对象
    转: python requests的安装与简单运用
    转: python如何安装pip和easy_installer工具
    转: windows下面安装Python和pip终极教程
    JS Json数据转换
    转:永久解决火狐浏览器出现的flash版本更新问题
    python 字符编码 转换
    opencv输出图片像素值
    Mac中安装tensorflow(转)
    在linux和Mac中访问某个文件夹中所有的文件
  • 原文地址:https://www.cnblogs.com/ElegantSmile/p/12325799.html
Copyright © 2020-2023  润新知