Mini Shell

Direktori : /proc/self/root/opt/sharedrads/
Upload File :
Current File : //proc/self/root/opt/sharedrads/check_io

#!/bin/bash
#========================================================================================
# check_io | batch iotop color display / logging | 3/09/2012 | JacobN@inmotionhosting.com
#========================================================================================
# check_io ----------- Logs to /var/log/sa/rads/diskio/
# check_io --process - Processes logs into /home/userna5/.imh/IO_YY-MM-DD_MM_HH
# check_io --remove -- Removes all IO_ logs
#========================================================================================
# Load functions + banners
if [ -f /opt/sharedrads/radsfunctions.sh ]; then
    source /opt/sharedrads/radsfunctions.sh
else
    source /opt/dedrads/radsfunctions.sh
fi

# Path to iotop binary
if [ -f /usr/bin/iotop ]; then
        IOTOP=/usr/bin/iotop
elif [ -f /sbin/iotop ]; then
        IOTOP=/sbin/iotop
fi

# Make sure we have iotop
if [ ! -f $IOTOP ]; then
        echo;echo "ERROR: /usr/bin/iotop does not exist on this machine";echo;exit 1
fi

# Setup colors

reset="\033[0m"; red="\033[31;1;1m"; red_="\033[31;3;3m"; green="\033[32;1;1m"; green_="\033[32;3;3m"; white="\033[29;1;1m";

echo -en `radsbanner` $green_ "check_io v2.0" $reset; echo

function run()
{
echo -e "$green==============================  Read > 300K/s < 1,000K/s   ==============================$reset"
echo -e "$green_==============================  Read > 1,000K/s            ==============================$reset"
echo -e "$red============================== Write > 300K/s < 1,000K/s  ===============================$reset"
echo -e "$red_============================== Write > 1,000K/s           ===============================$reset"

if [ ! -d "/var/log/sa/rads/diskio" ]
then
        mkdir -p /var/log/sa/rads/diskio/logs
fi

$IOTOP -bokt | grep -v "DISK" | awk '{ READ=$5; WRITE=$7 } { if ( READ > 300 && READ < 1000 ) print "\033[32;1;1m" $0 "\033[0m" | "tee -a /var/log/sa/rads/diskio/logs/READ_low"; else if ( READ > 1000) print "\033[32;3;3m" $0 "\033[0m" | "tee -a /var/log/sa/rads/diskio/logs/READ_high"; else if ( WRITE > 300 && WRITE < 1000 ) print "\033[31;1;1m" $0 "\033[0m" | "tee -a /var/log/sa/rads/diskio/logs/WRITE_low"; else if ( WRITE > 1000) print "\033[31;3;3m" $0 "\033[0m" | "tee -a /var/log/sa/rads/diskio/logs/WRITE_high"}'
}

function process(){

find /var/log/sa/rads/diskio/logs -type f -exec cat {} \; | egrep -v "mailnull|mysql|nobody|root|none" | awk -F"/[0-9] " '{print $2}' | awk '{print $1}' | sort | uniq > /var/log/sa/rads/diskio/USERS

for user in `cat /var/log/sa/rads/diskio/USERS`
do

        if [ ! -d "/home/"$user"/.imh" ]
        then
                mkdir -p /home/$user/.imh
        fi

        grep $user /var/log/sa/rads/diskio/logs/READ_high > /home/$user/.imh/$user"_READ_high"
        grep $user /var/log/sa/rads/diskio/logs/READ_low > /home/$user/.imh/$user"_READ_low"
        grep $user /var/log/sa/rads/diskio/logs/WRITE_high > /home/$user/.imh/$user"_WRITE_high"
        grep $user /var/log/sa/rads/diskio/logs/WRITE_low > /home/$user/.imh/$user"_WRITE_low"

        cat /home/$user/.imh/$user_* | sed 's/1m/1m /' | sed 's/3m/3m /' | sed 's/:/ /g' | sort -nk2 -nk3 -nk4 | awk '{print $1,$2":"$3":"$4, substr($0, index($0,$5))}' > /home/$user/.imh/IO_`date +%F_%H_%M`
done

rm -rf /var/log/sa/rads/diskio/logs/READ_high
rm -rf /var/log/sa/rads/diskio/logs/READ_low
rm -rf /var/log/sa/rads/diskio/logs/WRITE_high
rm -rf /var/log/sa/rads/diskio/logs/WRITE_low

echo

for user in `cat /var/log/sa/rads/diskio/USERS`
do
        echo -n $user" "
        echo -n `cat /home/$user/.imh/$user"_READ_high" | wc -l`" "
        echo -n `cat /home/$user/.imh/$user"_READ_low" | wc -l`" "
        echo -n `cat /home/$user/.imh/$user"_WRITE_high" | wc -l`" "
        echo -n `cat /home/$user/.imh/$user"_WRITE_low" | wc -l`" "
        echo -n `cat /home/$user/.imh/$user* | wc -l`" "
        echo
done > DUMP

for user in `cat /var/log/sa/rads/diskio/USERS`
do
        rm -rf /home/$user/.imh/$user"_READ_high"
        rm -rf /home/$user/.imh/$user"_READ_low"
        rm -rf /home/$user/.imh/$user"_WRITE_high"
        rm -rf /home/$user/.imh/$user"_WRITE_low"
done

echo
echo -e "$white===========================$green_ Top 5 READ >1K/s $reset$white=======================|==========================$green Top 5 READ > 300K/s $reset$white===================$reset"
paste <(echo -e "Username R>1K R<1K W>1K W<1K [Total]" | awk '{printf "\033[29;1;1m%-8s\033[0m %1s \033[32;3;3m%10s\033[0m %10s %10s %10s %10s\n", $1," ",$2,$3,$4,$5,$6}'; \
cat DUMP | sort -nk2 | tail -5 | awk '{printf "\033[29;1;1m%-8s\033[0m %1s \033[32;3;3m%10s\033[0m %10s %10s %10s %10s\n", $1," ",$2,$3,$4,$5,$6}') \
<(echo -e "Username R>1K R<1K W>1K W<1K [Total]" | awk '{printf "\033[29;1;1m%-8s\033[0m %10s \033[32;1;1m%10s\033[0m %10s %10s %10s\n", $1,$2,$3,$4,$5,$6}'; \
cat DUMP | sort -nk3 | tail -5 | awk '{printf "\033[29;1;1m%-8s\033[0m %10s\033[32;1;1m%10s\033[0m %10s %10s %10s\n", $1,$2,$3,$4,$5,$6}')
echo

echo -e "$white===========================$red_ Top 5 WRITE >1K/s $reset$white=======================|=========================$red Top 5 WRITE > 300K/s $reset$white==================$reset"
paste <(echo -e "Username R>1K R<1K W>1K W<1K [Total]" | awk '{printf "\033[29;1;1m%-8s\033[0m %10s %10s %1s \033[31;3;3m%10s\033[0m%10s %10s\n", $1,$2,$3," ",$4,$5,$6}'; \
cat DUMP | sort -nk4 | tail -5 | awk '{printf "\033[29;1;1m%-8s\033[0m %10s %10s %1s \033[31;3;3m%10s\033[0m %10s %10s\n", $1,$2,$3," ",$4,$5,$6}') \
<(echo -e "Username R>1K R<1K W>1K W<1K [Total]" | awk '{printf "\033[29;1;1m%-8s\033[0m %10s %10s %10s \033[31;1;1m%10s\033[0m%10s\n", $1,$2,$3,$4,$5,$6}'; \
cat DUMP | sort -nk5 | tail -5 | awk '{printf "\033[29;1;1m%-8s\033[0m %10s %10s %10s \033[31;1;1m%10s\033[0m %10s\n", $1,$2,$3,$4,$5,$6}')
echo

echo -e "$white=========================== Top 10 TOTAL ==============================================================================================$reset"
echo -e "Username R>1K R<1K W>1K W<1K [Total]" | awk '{printf "\033[29;1;1m%-8s\033[0m %10s %10s %10s %10s \033[29;1;1m%10s\033[0m\n", $1,$2,$3,$4,$5,$6}'
cat DUMP | sort -nk6 | tail -10 | awk '{printf "\033[29;1;1m%-8s\033[0m %10s %10s %10s %10s \033[29;1;1m%10s\033[0m\n", $1,$2,$3,$4,$5,$6}'
rm -rf DUMP
}

function remove() {

echo "On this server `hostname` there are `ls -f /home/*/.imh/IO_* | wc -l` IO_ logs to remove"

read -p "Ok to remove? [y/n]" yn
case $yn in [Yy]* ) rm -rv /home/*/.imh/IO_*;; [Nn]* ) exit;; * ) echo "Please answer y or n.";; esac

echo "All IO_ files wiped, thank you come again"

}

case $1 in
--process) process ;;
--remove) remove ;;
*) run ;;
esac

Zerion Mini Shell 1.0