Tutorial by Vinod Kumat

Unix df with ssh or df -h with help of ssh on remote system or disk monitoring in unix with df -h over ssh or expect command

Who to do the df -h with help of ssh on remote system

For achieving this we need:
1. ssh installed and configure in remote system.
2. expect installed in source system, which is going to connect remote system.


Logging into Unix system where you want to configure with respective user and password:
e.g:root/anyuser with password

create the appfordfoverssh.sh file with help of:
vi appfordfoverssh.sh then enter
A windows will appear with edit option:
pest the below content in that editopr:



# Environment Variables
SCRIPT_HOME_DIR=/home/icinga
SCRIPT_SRC="${SCRIPT_HOME_DIR}"/script_for_ssh_df
SCRIPT_FLE="${SCRIPT_SRC}"/sshdf.sh
MAX_VAL=95
 
#Checking the src dir if exist, if not then it will create
if [ ! -d "${SCRIPT_SRC}" ]; then
        mkdir ${SCRIPT_SRC}
fi

#This is for looping the all hosts configure in this

#host1
SSH_HOSTS[0]="user1@host1"
SSH_PWDS[0]="password1"

#host2
SSH_HOSTS[1]="user2@host2"
SSH_PWDS[1]="password2"
 
for i in 0 1
do
        echo "${SSH_HOSTS[i]}"
        # This section is only on for executing the df command with logging by ssh
        echo "#!/usr/bin/expect" > $SCRIPT_FLE
        echo "spawn /usr/bin/ssh \"${SSH_HOSTS[i]}\"" >> $SCRIPT_FLE
        echo "expect \"password:\"" >> $SCRIPT_FLE
        echo "send \"${SSH_PWDS[i]}\r\"" >> $SCRIPT_FLE
        echo "expect \"$ \"" >> $SCRIPT_FLE
        echo "send \"df -h\r\"" >> $SCRIPT_FLE
        echo "send \"exit\r\"" >> $SCRIPT_FLE
        echo "expect \"quit\r\"" >> $SCRIPT_FLE
        sleep 2
        chmod 755 $SCRIPT_FLE

        $SCRIPT_FLE > ${SCRIPT_SRC}/sshdf.txt

        cd ${SCRIPT_SRC}
        grep % sshdf.txt | sed 's/%//g' | awk '$5 > ${MAX_VAL} {print $1,$2,$3,$4,$5"%";}' | column -t | grep / > ${SCRIPT_SRC}/sshdf.txt

        if grep % sshdf.txt | sed 's/%//g' | awk '$5 > ${MAX_VAL} {print $1,$2,$3,$4,$5"%";}' | column -t | grep /  >/dev/null; then
                cat  ${SCRIPT_SRC}/sshdf.txt | mailx -s "Disk utilization are reaching more than the ${MAX_VAL}% in ${SSH_HOSTS[i]}" vinod.kumar@host.com,oryouremailid@host.com
        fi
done

chmod 775 appfordfoverssh.sh

Configure the cronttab -e option and sett:
0*/4 * * * /home/youruserid/appfordfoverssh.sh

Fill free do let me know if any disconnect in this post. I'll more than happy to help you!!!


No comments:

Post a Comment