Guest User

Untitled

a guest
Jan 23rd, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # This is a script to log (and eventually monitor) diskusage on a filesystem.
  5. # It will write **THE USER SPECIFIED FILESYSTEM** disk usage to a logfile.
  6. # The logfile will never grow larger than MAX_LINES number of lines.
  7. #
  8. # To be used together with a cron job and optionally a GNUPLOT.
  9. #
  10.  
  11. # **SPECIFY THE FILESYSTEM YOU WANT TO MONITOR**
  12. # (Running df on the system will reveal all currently mounted filesystems)
  13. FILE_SYSTEM="/dev/disk1s1"
  14.  
  15. ## This var holds the name of the logfile
  16. FILE_NAME="example.log"
  17.  
  18. # This var holds the name of a temporary logfile
  19. TEMP_FILE_NAME=${FILE_NAME}.temp
  20.  
  21. # Format Current Date Time
  22. CURRENT_DATETIME=$(date "+%Y-%m-%d %H:%M:%S %Z")
  23.  
  24. # Extract SIZE, USED, AVAILABLE and CAPACITY for the given filesystem
  25. # and append to file
  26. df -H | awk -v datetime="$CURRENT_DATETIME" -v filesystem="$FILE_SYSTEM" \
  27. '$1 == filesystem {print datetime, $2, $3, $4, $5}' >> ${FILE_NAME}
  28.  
  29. # If diskusage file does not exist, something went wrong. Better exit.
  30. if [ ! -f "$FILE_NAME" ]; then
  31. exit 1
  32. fi
  33.  
  34.  
  35. #
  36. # Make sure this file doesn't grow larger than MAX_LINES lines.
  37. # Delete oldest entries.
  38. #
  39.  
  40. # Maximum number of lines the logfile may hold
  41. MAX_LINES=10000
  42.  
  43. # Number of lines the logfile currently holds
  44. CURRENT_LINES=$(cat ${FILE_NAME} | wc -l)
  45.  
  46. if [ "$CURRENT_LINES" -gt "$MAX_LINES" ]; then
  47.  
  48. # Tail MAX_LINES to tempfile and (force) copy back to original logfile
  49.  
  50. tail -n${MAX_LINES} ${FILE_NAME} > ${TEMP_FILE_NAME}
  51. cp -f ${TEMP_FILE_NAME} ${FILE_NAME}
  52. rm -f ${TEMP_FILE_NAME}
  53.  
  54. fi
  55.  
  56. exit 0
Add Comment
Please, Sign In to add comment