모니터링 스크립트 :: 에버노트

ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 모니터링 스크립트
    카테고리 없음 2018. 5. 18. 16:30
    #!/bin/sh
    #
    data_file=/proc/net/dev
    meminfo=/proc/meminfo
    if_dev1=eth1
    if_dev2=eth2
    if_ipaddr1=`ifconfig $if_dev1 | grep "inet addr" | cut -d: -f2 | awk '{print $1}'`
    if_ipaddr2=`ifconfig $if_dev1 | grep "inet addr" | cut -d: -f2 | awk '{print $1}'`
    interval=$1
     
    # check to system resources
    cpu_each=`grep 'processor' /proc/cpuinfo  | wc -l`
    mem_total=`grep "MemTotal" $meminfo | cut -d: -f2 | awk '{print $1}'`
    mem_t=$(echo "scale=0;$mem_total/1000" | bc)
    mem_dt="$mem_t MB"
    mem_free=`grep "MemFree" $meminfo | cut -d: -f2 | awk '{print $1}'`
    mem_usage=$(echo "scale=1;$mem_total-$mem_free" | bc)
    mem_u=$(echo "scale=0;$mem_usage/1000" | bc)
    mem_du="$mem_u MB"
     
    swap_total=`grep "SwapTotal" $meminfo | cut -d: -f2 | awk '{print $1}'`
    swap_free=`grep "SwapFree" $meminfo | cut -d: -f2 | awk '{print $1}'`
    swap_usage=$(echo "scale=1;$swap_total-$swap_free" | bc)
    swap_u=$(echo "scale=0;$swap_usage/1000" | bc)
    swap_du="$swap_u MB"
    swap_t=$(echo "scale=0;$swap_total/1000" | bc)
    swap_dt="$swap_t MB"

    if [ ! "$1" ]; then
        interval=1
    fi
     
    while :
    do
        clear
     
        # Received
        bps_r_1=`grep $if_dev1 $data_file | cut -d: -f2 | awk '{print $1}'`
        pkt_r_1=`grep $if_dev1 $data_file | cut -d: -f2 | awk '{print $2}'`
     
        # Trans
        bps_t_1=`grep $if_dev1 $data_file | cut -d: -f2 | awk '{print $9}'`
        pkt_t_1=`grep $if_dev1 $data_file | cut -d: -f2 | awk '{print $10}'`
     
        sleep $interval
     
        # Received
        bps_r_2=`grep $if_dev1 $data_file | cut -d: -f2 | awk '{print $1}'`
        pkt_r_2=`grep $if_dev1 $data_file | cut -d: -f2| awk '{print $2}'`
     
        # Trans
        bps_t_2=`grep $if_dev1 $data_file | cut -d: -f2 | awk '{print $9}'`
        pkt_t_2=`grep $if_dev1 $data_file | cut -d: -f2 | awk '{print $10}'`
        bps_r_3=$(echo "scale=3; $bps_r_2-$bps_r_1" | bc)
        bps_t_3=$(echo "scale=3; $bps_t_2-$bps_t_1" | bc)
        pkt_r_3=$(echo "scale=3; $pkt_r_2-$pkt_r_1" | bc)
        pkt_t_3=$(echo "scale=3; $pkt_t_2-$pkt_t_1" | bc)
     
        # BPS
        if [ $bps_r_3 -lt "1000" ]; then
            bps_r_4=$(echo $bps_r_3)
        # Kbps
        elif [ $bps_r_3 -gt "1000" -o $bps_r_3 -lt "1000000" ]; then
            bps_r_4=$(echo "scale=3; $bps_r_3 / 1000" | bc)
        # Mbsp
        elif [ $bps_r_3 -gt "1000000" -o $bps_r_3 -lt "1000000000" ]; then
            bps_r_4=$(echo "scale=3; $bps_r_3 / 1000000" | bc)
        else
        # Gbsp
            bps_r_4=$(echo "scale=3; $bps_r_3 / 1000000000" | bc)
        fi
     
        # bps
        if [ $bps_t_3 -lt "1000" ]; then
            bps_t_4=$(echo $bps_t_3)
        # kbps
        elif [ $bps_t_3 -gt "1000" -o $bps_t_3 -lt "1000000" ]; then
            bps_t_4=$(echo "scale=3; $bps_t_3 / 1000" | bc)
        # mbsp
        elif [ $bps_t_3 -gt "1000000" -o $bps_t_3 -lt "1000000000" ]; then
            bps_t_4=$(echo "scale=3; $bps_t_3 / 1000000" | bc)
        else
        # gbsp
            bps_t_4=$(echo "scale=3; $bps_t_3 / 1000000000" | bc)
        fi

        echo IP Addr : $if_ipaddr1
        echo Mem Usage : $mem_du / $mem_dt , Swap Usage : $swap_du / $swap_dt
        echo RX bps : $bps_r_4 bps , RX pps : $pkt_r_3  pps
        echo TX bps : $bps_t_4 bps , TX pps : $pkt_t_3 pps
        sleep 1
    done

    #출처 https://chonnom.com/bbs/board.php?bo_table=B23&wr_id=33&page=2

    댓글

Designed by Tistory.