-
-
Save genothomas/f3476122e7c224659adfb3a1b0441aad to your computer and use it in GitHub Desktop.
Ever craved for a tool to monitor what files are growing faster than other ones? Just run it by cron: diskhogs.sh /var/lib/libvirt/images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Checking the spool directory | |
SPOOL=/var/spool/diskhogs | |
if [ ! -e "${SPOOL}" ]; then | |
mkdir -p "${SPOOL}" | |
fi | |
if [ ! -d "${SPOOL}" ]; then | |
echo "There are no ${SPOOL} directory" >&2 | |
exit 1 | |
fi | |
if [ -z "${1}" ]; then | |
DIR=. | |
else | |
DIR="${1}" | |
fi | |
FILES=$(find "${DIR}" -type f) | |
TIME=$(date +%s) | |
if [ -z "${TIME}" ]; then | |
echo "Can't determine current time" >&2 | |
exit 1 | |
fi | |
for FILE in ${FILES}; do | |
SIZE=$(ls -nl ${FILE} | awk '{ print $5 }') | |
if [ -z "${SIZE}" ]; then | |
echo "Can't determine size of the ${FILE} file" >&2 | |
continue | |
fi | |
sqlite3 "${SPOOL}/db" "INSERT INTO sizes VALUES ('${FILE}', '${TIME}', '${SIZE}');" | |
if [ ${?} -ne 0 ]; then | |
continue | |
fi | |
done | |
for PERIOD in 60 300 600 1800 3600 86400; do | |
TIME_WAS=$((${TIME} - ${PERIOD})) | |
( | |
echo "*** Since $(date --date="@${TIME_WAS}") (${PERIOD} seconds ago) ***" | |
sqlite3 \ | |
"${SPOOL}/db" \ | |
"SELECT MAX(size) - MIN(size) AS mm, name | |
FROM sizes | |
WHERE time >= '${TIME_WAS}' | |
GROUP BY name | |
ORDER BY mm | |
;" | |
) > "${SPOOL}/report_${PERIOD}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment