#!/bin/bash

# sample command to purge collected stats older than 6 days in e.g. a crontab:
# find /var/tmp/stats -mindepth 1 -maxdepth 1 -type d -mtime +6 -print0 | xargs -r -0 rm -rf

trap "exit 1" INT
cd /var/tmp || exit 1
[ "`id -u`" = 0 ] || {
    echo "This must be run as root to be very useful. Aborting." >&2
    exit 1
}

while :
do
    eval `date '+date=%Y%m%d hour=%H minsec=%M%S'`
    dir=stats/$date/$hour/$minsec
    mkdir -p $dir
    pushd $dir || exit 1
    
    ps auxww | gzip -c > ps-auxww.gz
    w > w
    free > free
    vmstat > vmstat
    iostat > iostat
    lsof | gzip -c > lsof.gz

    popd
    sleep 30
done
