Advertisement
Guest User

warn_disk_usage.sh

a guest
Dec 12th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.30 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #    This script is used to send an e-mail notification if a certain partition
  4. #    is running out of space. The message will be delivered if either the disk
  5. #    usage in partition $PART is bigger than $MAXPERC or the amount of free
  6. #    space is smaller than $MINFREE.
  7. #
  8. #    Copyright (C) 2013, Henning Hollermann, laclaro@mail.com
  9. #
  10. #    This program is free software: you can redistribute it and/or modify
  11. #    it under the terms of the GNU General Public License as published by
  12. #    the Free Software Foundation, either version 3 of the License, or
  13. #    (at your option) any later version.
  14. #
  15. #    This program is distributed in the hope that it will be useful,
  16. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. #    GNU General Public License for more details.
  19. #
  20. #    You should have received a copy of the GNU General Public License
  21. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  22.  
  23. # percentage of disk usage, where mail should be sent
  24. MAXPERC=95 # percent
  25. # minimum free space, below e-mail should be sent
  26. MINFREE=15 # GB
  27. # mail destination
  28. MAIL_DESTINATION=admin@domain.com
  29. # surveilled partition
  30. PART=partition_name
  31.  
  32. # free space in bytes
  33. FREE=`df |grep $PART | awk '{ print $4 }'`
  34.  
  35. # percentage of disk space used and df-info line
  36. if [ -x /usr/bin/pydf ]; then
  37.   USEPERC=`pydf --bw |grep $PART | awk '{ printf("%.0f",$5) }'`
  38.   DF_LINE=`pydf --bw|awk '
  39.    BEGIN { print "  Disk space" };
  40.    /'$PART'/{print "  "$7 "\t" $3 "/" $2 " (" $5 "%)\tfree: "$4 "\n  " $6};
  41.    END { print ""}'`
  42. else
  43.   USEPERC=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`
  44.   DF_LINE=`df -h|awk '
  45.     BEGIN { print "  Disk space" };
  46.     /Size|'$PART'/{print "  "$2 "\t" $3 "\t" $4 "\t" $5 "\t" $6};
  47.     END { print ""}'`
  48. fi
  49. echo $FREE $(($MINFREE*1000000))
  50. # if conditions met, send e-mail
  51. if [ $USEPERC -gt $MAXPERC -o $FREE -lt $(($MINFREE*1000000)) ]; then
  52.   echo -e "
  53. Hello,\n
  54. this message was automatically created by $0.
  55. Partition $PART is running out of space (${USEPERC}% used).\n
  56. $DF_LINE\n
  57. You may clear the trash bin to free some space.\n
  58. `cat /tmp/motd-trashes`\n
  59. Best wishes, the administrator
  60. " | mail -s "$PART is running out of space" $MAIL_DESTINATION
  61.  
  62. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement