Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # This is a script to log (and eventually monitor) diskusage on a filesystem.
- # It will write **THE USER SPECIFIED FILESYSTEM** disk usage to a logfile.
- # The logfile will never grow larger than MAX_LINES number of lines.
- #
- # To be used together with a cron job and optionally a GNUPLOT.
- #
- # **SPECIFY THE FILESYSTEM YOU WANT TO MONITOR**
- # (Running df on the system will reveal all currently mounted filesystems)
- FILE_SYSTEM="/dev/disk1s1"
- ## This var holds the name of the logfile
- FILE_NAME="example.log"
- # This var holds the name of a temporary logfile
- TEMP_FILE_NAME=${FILE_NAME}.temp
- # Format Current Date Time
- CURRENT_DATETIME=$(date "+%Y-%m-%d %H:%M:%S %Z")
- # Extract SIZE, USED, AVAILABLE and CAPACITY for the given filesystem
- # and append to file
- df -H | awk -v datetime="$CURRENT_DATETIME" -v filesystem="$FILE_SYSTEM" \
- '$1 == filesystem {print datetime, $2, $3, $4, $5}' >> ${FILE_NAME}
- # If diskusage file does not exist, something went wrong. Better exit.
- if [ ! -f "$FILE_NAME" ]; then
- exit 1
- fi
- #
- # Make sure this file doesn't grow larger than MAX_LINES lines.
- # Delete oldest entries.
- #
- # Maximum number of lines the logfile may hold
- MAX_LINES=10000
- # Number of lines the logfile currently holds
- CURRENT_LINES=$(cat ${FILE_NAME} | wc -l)
- if [ "$CURRENT_LINES" -gt "$MAX_LINES" ]; then
- # Tail MAX_LINES to tempfile and (force) copy back to original logfile
- tail -n${MAX_LINES} ${FILE_NAME} > ${TEMP_FILE_NAME}
- cp -f ${TEMP_FILE_NAME} ${FILE_NAME}
- rm -f ${TEMP_FILE_NAME}
- fi
- exit 0
Add Comment
Please, Sign In to add comment