Disk Check with Alert



This script checks the disk usage of servers and emails. sshpass is used only for pfsense for the rest servers ssh keys are used to connect.

#!/bin/bash
 DATE=`date +%Y-%m-%d`
 logfolder=/var/log/disk_usage/
 logfile=$logfolder/$DATE-disk-usage.log
 alertlogfile=$logfolder/$DATE-disk-usage-alert.log
 key=/example/keys/id_rsa_nagios
 sendemail=/example/scripts/sendEmail.pl
 SENDEMAIL=/example/scripts/sendEmail.pl
 MAIL_FROM="My Domain <automation@example.com>"
 MAIL_TO="admin@example.com"
 MAIL_HOST='example.com:587'
 MAILUSER=emailuser@example.com
 MAILPASS='emailuserpass'
 subject=$DATE-disk-usage-alert
 if [ ! -d $logfolder ]
 then
 mkdir -p $logfolder
 fi
 #addspace=`echo "" >> $logfile`
 ALERT=80
 IP=(192.168.0.10 192.168.0.20 192.168.0.30 192.168.0.40 192.168.0.50 192.168.0.60 192.168.0.10 192.168.0.70 192.168.0.10 192.168.0.80) # my server ips

function alert
 {
 grep -vE '^Filesystem|tmpfs|cdrom|devfs' | awk '{ print $5 " " $1 " " $6 }' | while read output;
 do
 #    echo output is $output for $Host
 usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f 1 )
 partition=$(echo $output | awk '{ print $2 }' )
 usem=$(echo $output | awk '{print $3}')
 # echo usep=$usep  usem=$usem partiton=$partition
 if [ $usep -ge $ALERT ]; then
 #echo "Running out of space \"$partition ($usep%)\" on $Host as on $DATE" |
 #mail -s "Alert: Almost out of disk space $usep" $ADMIN
 echo "Running out of space  \"$partition ($usep%) $usem\""  >> $alertlogfile
 fi
 done

}

## only for pfsense
 #:'
 echo "" >> $logfile
 Host=`sshpass -p PASSWD ssh -o StrictHostKeyChecking=no  -o 'KbdInteractiveDevices no'  root@192.168.0.1 hostname`
 echo "Hostname is $Host"
 echo "            $Host" >> $logfile
 echo "" >> $logfile
 sshpass -p PASSWD ssh -o StrictHostKeyChecking=no -o 'KbdInteractiveDevices no'  root@192.168.0.1 df -P -h >> $logfile
 sshpass -p PASSWD ssh -o StrictHostKeyChecking=no -o 'KbdInteractiveDevices no' root@192.168.0.1 df -P -h | alert
 echo "-----------------------------------------------------------------------" >> $logfile
 #'

#exit

function disk-check_local
 {
 ip=$1
 echo $ip
 echo "" >> $logfile
 Host=`ssh -o StrictHostKeyChecking=no -i "$key" restricted@$ip hostname`
 echo "Hostname is $Host"
 echo "            $Host" >> $logfile
 echo "            $Host" >> $alertlogfile
 echo "" >> $logfile
 echo "" >> $alertlogfile
 ssh -o StrictHostKeyChecking=no -i "$key" restricted@$ip df -P -h >> $logfile
 ssh -o StrictHostKeyChecking=no -i "$key" restricted@$ip df -P -h | alert
 echo "-----------------------------------------------------------------------" >> $logfile
 echo "-----------------------------------------------------------------------" >> $alertlogfile
 }

for i in ${IP[@]}; do
 echo "processing $ip"
 disk-check_local $i
 #  echo "done with $ip"
 done

#exit
 cat $logfile
 cat $alertlogfile
 report=`cat $alertlogfile`

$SENDEMAIL -f "$MAIL_FROM" -t $MAIL_TO -s $MAIL_HOST -xu $MAILUSER -xp $MAILPASS -u "$subject" -m "$report" -a $logfile

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy This Password *

* Type Or Paste Password Here *

4,474 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>