====== Cronjobs on Gandi AI to automatically check your disk usage ====== ===== Description ===== One of the most common reasons for support tickets on Gandi AI servers is because the customer doesn't realize that the server has run out of disk space. Here is a bash script you can run on your Gandi AI (or expert) VPS that will alert you when one of your attached disks has exceeded a specified disk usage. The following script checks all disks mounted on the server and verifies the space used on each, then sends an email to alert you if any disks have exceeded the quota. ===== Setup ===== Prerequisite: A PostFix mailserver must be installed for the email to be sent out. On Gandi AI servers, all you have to do is install the 'PostFix' module and configure it [[en:hosting:create-server:gandi-ai:mail-server-configuration|like this]]. To install the script, connect to the server via SSH with the ''admin'' user and the password you specified when you created the server. Then, create a file to contain the script in any directory where the ''admin'' user has write permissions. For example, I'll place my script in ''/srv/d_mydisk/scripts/'' (replace ''d_yourdiskname'' as appropriate). To create the directory and the file, run these commands: mkdir /srv/d_yourdiskname/scripts touch /srv/d_yourdiskname/scripts/checkdisk.sh Then, edit the file in order to place the script in it (replace ''vim'' with ''nano'' if you like a little pain with your text editing): vim /srv/d_yourdiskname/scripts/checkdisk.sh Then paste the script below. ===== Script contents ===== #!/bin/bash # Define the trigger threshold: # The script will alert you if any of your disks exceed this percentage # 'alert_quota=90' will alert you if any of your disks reach 90% full alert_quota=90 # Specify the email address to send the alert to: recipient="admin@example.net" # Default user@hostname # You can define the sender here if you want (optional) sender=`whoami`"@"`hostname` i=0 for disk in `df | grep /dev/xvd | awk -F" " '{print $1}' | cut -d/ -f3` do space_use=`df | grep $disk | awk -F" " '{print $5}' | cut -d% -f1` if [ "$space_use" -gt "$alert_quota" ] then i=$((i + 1)) over_quota["$i"]="$disk" fi done if [ ${#over_quota[*]} -gt 0 ] then subject="A disk has reached its quota on server `hostname`" message="Warning! The following disk(s) have exceeded your specified threashold of $alert_quota% used. \n\n" message+="Here are the details of the disk(s) in question:\n" for disk in ${over_quota[*]} do info_disk=(`df -h | grep $disk | awk -F" " '{print $6, $2, $3, $4, $5}'`) message+="\t|- Mount point: ${info_disk[O]} - Total space: ${info_disk[1]} - Used space : ${info_disk[2]} - Free space: ${info_disk[3]} - % Used space: ${info_disk[4]}" done message=`echo -e "$message"` # Send the email function fappend { echo "$2">>$1; } temp=".tempmail_"$RANDOM; rm -rf $temp; fappend $temp "From: $sender"; fappend $temp "To: $recipient"; fappend $temp "Subject: $subject"; fappend $temp "Content-Type: text/plain; charset=UTF-8; format=flowed"; fappend $temp "Content-Transfer-Encoding: 8bit"; fappend $temp ""; fappend $temp "$message"; fappend $temp ""; fappend $temp ""; cat $temp| /usr/sbin/sendmail -t; rm $temp; fi Save the file and exit the text editor. Lastly, make the file executable by running this command: chmod +x checkdisk.sh The only two important variables to configure are ''alert_quota'' and ''recipient'', which define the trigger point and the recipient of the alert email. It's a good idea to set the ''alert_quota'' variable to a null value at first to make sure the script works. You can manually test the script by running the command ''./checkdisk.sh'' from within the directory containing it. If you receive an email, it's working, so you can modify the variable to a reasonable value. ===== Automated disk checks with crontab ===== In order to be automatically alerted when one of your disks exceeds quota, we'll now set up a ''cron'' task which will execute the script at regular intervals. You can define the execution frequency according to your needs. To add a crontab, run the command ''crontab -e''. Then choose your desired frequency: Puis indiquez la fréquence, en fonction de vos préférences : # Once per hour @hourly /bin/bash /srv/d_yourdiskname/scripts/checkdisk.sh # Once per day @daily /bin/bash /srv/d_yourdiskname/scripts/checkdisk.sh # Once per week @weekly /bin/bash /srv/d_yourdiskname/scripts/checkdisk.sh For more information on cron jobs, see [[wp>Cron|this Wikipedia article]]. That's it! Once the cronjob has been configured, you will receive an email alert if any of your disks have reached the quota you've specified. The ''alert_quota'' is to be specified according to the size of your disks.