Linux/unix shell script monitors disk free space

Source: Internet
Author: User
Tags disk usage

A shell script that monitors the disk's free space under Linux is essential for a system administrator or DBA. Here is a sample of the shell script that monitors disk space space for your reference.

1. Free space for monitoring disk shell script

[Python]View PlainCopyprint?
  1. [Email protected]:~/dba_scripts/custom/bin> more ck_fs_space.sh
  2. #!/bin/bash
  3. # ------------------------------------------------------------------------------+
  4. # CHECK FILE sysytem SPACE by THRESHOLD |
  5. # Filename:ck_fs_space.sh |
  6. # Desc: |
  7. # The script to check for file system space by threshold |
  8. # Once usage of the disk beyond the threshold, a mail alert would be sent. |
  9. # Deploy it by crontab. e.g. per-min |
  10. # Usage: |
  11. #./ck_fs_space.sh <percent> </filesystem [/filesystem2]> |
  12. #                                                                                   |  
  13. # Author:robinson |
  14. # blog:http://blog.csdn.net/robinson_0612 |
  15. # ------------------------------------------------------------------------------+
  16. #
  17. # -------------------------------
  18. # Set Environment here
  19. # ------------------------------
  20. If [-f ~/.bash_profile]; Then
  21. . ~/.bash_profile
  22. Fi
  23. Export host= ' hostname '
  24. Export MAIL_DIR=/USERS/ROBIN/DBA_SCRIPTS/SENDEMAIL-V1. About
  25. Export mail_list=' [email protected] '
  26. Export mail_fm=' [email protected] '
  27. Tmpfile=/tmp/ck_fs_space.txt
  28. Alert=n
  29. # --------------------------------
  30. # Check The parameter
  31. # --------------------------------
  32. max=$1
  33. if [! ${2}]; then
  34. echo "No filesystems specified."
  35. echo "Usage:ck_fs_space.sh//u01"
  36. Exit 1
  37. Fi
  38. # --------------------------------
  39. # Start to check the disk space
  40. # --------------------------------
  41. While [ "${2}"]
  42. Do
  43. Percent= ' Df-p ${2} | tail-1 | awk ' {print $} ' | cut-d'% '-f1 '
  44. if [ "${percent}"-ge "${max}"]; Then
  45. Alert=y
  46. Break
  47. Fi
  48. Shift
  49. Done
  50. # ------------------------------------------------------------------------
  51. # When a partition is above the threshold then send mail with DF output
  52. # ------------------------------------------------------------------------
  53. if [! "${alert}" = ' n '];then
  54. Df-h > $tmpfile
  55. mail_sub="Disk usage beyond the threshold ${max} on ${host}."
  56. $mail _dir/sendemail-u ${mail_sub}-F $mail _fm-t $mail _list-o Message-file=${tmpfile}
  57. Fi
  58. Exit

2. Script description
A, the script uses the SendEmail tool to send messages.
b, the use of the way is "Usage:ck_fs_space.sh//u01".
C. A while loop is used in the script to determine, on a per-partition basis, whether the free space for all the specified partitions exceeds the threshold.
D. Send the message for exceeding the threshold and attach the usage of disk space on the current server.

Ext.: http://blog.csdn.net/leshami/article/details/8893943

Linux/unix shell script monitors disk free space

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.