#!/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