I have tried writing a shell script which alerts admin when the disk usage reaches up to 70%. I want write this script in python
#!/bin/bash
ADMIN="admin@myaccount.com"
INFORM=70
df -H | grep -vE ‘^Filesystem|tmpfs|cdrom’ | awk ‘{ print $5 ” ” $6 }’ | while read output;
do
use=$(echo $output | awk ‘{ print $1}’ | cut -d’%’ -f1 )
partition=$(echo $output | awk ‘{ print $2 }’ )
if [ $use -ge $INFORM ]; then
echo “Running out of space \”$partition ($use%)\” on $(hostname) as on $(date)” |
mail -s “DISK SPACE ALERT: $(hostname)” $ADMIN
fi
done
The easiest(understandable) approach for you would be to run the
dfcommand on an external process and extract the details from the returned output.To execute a shell command in Python, you need to use
subprocessmodule. You can usesmtplibmodule to send emails to the admin.I cooked up a small script that should do the job of filtering the filesystems you don’t need to monitor, does some string manipulation to pull out the filesystem and % used values and prints out when the usage exceeds the threshold.
The output of the executed script would be something like this: