#! /bin/sh

#######################################################################
# Copyright (C) 2009 VMWare, Inc.
# All Rights Reserved
#######################################################################

# Cron script used to delete older netdump cores.

disk_usage() {
    du --block-size=$(( 1024 * 1024 )) --max-depth=0 ${NETDUMPER_DIR} | cut -f1
}

oldest_core() {
    find ${NETDUMPER_DIR} -type f -name "zdump*" -printf "%A@ %p\n" | sort -n | \
	awk '{ print $2; exit }'
}

if [ -f /etc/sysconfig/netdumper ]; then
   . /etc/sysconfig/netdumper

   NETDUMPER_DIR_MAX_MB=$(( $NETDUMPER_DIR_MAX_GB * 1024 ))

   if [ ! -d ${NETDUMPER_DIR} ]; then
       echo "error: NETDUMPER_DIR in /etc/sysconfig/netdumper is invalid"
       exit
   fi

   leash=0
   while [ $(disk_usage) -gt ${NETDUMPER_DIR_MAX_MB} ]; do
       filename=$(oldest_core)
       rm -f $filename

       leash=$(( $leash + 1 ))
       if [ $leash -gt 300 ]; then
	   echo "error: not making progress when removing cores..."
	   break
       fi
   done
fi
