#!/bin/sh
# Copyright (c) 2005  Morettoni Luca <luca@morettoni.net>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id: cachesize.sh,v 1.1.1.1 2003/09/26 10:00:30 luca Exp $

# Please run dnscache log with (stats on separate file):
# #!/bin/sh
# exec setuidgid Gdnslog multilog t /var/log/dnscache/ \
#  '-*' '+* stats * * * *' =/var/log/dnscache/stats.log
#
# run this script every 24 hour, at the first run it store only
# cache status, after it sent to $EMAIL cache report!

### begin of config vars
# main log directory
LOG_DIR=/var/log/dnscache/

# stats log file (see log/run file above)
LOG_FILE=${LOG_DIR}/stats.log

# motion log file
LOG_MOTION=${LOG_DIR}/motion.log

# cache average file
AVG_FILE=${LOG_DIR}/cache.avg

# average days
MAX_SAMPLE=15

# dnscache service path
SERVICE=/var/service/dnscache

# email address where reports cache motion stats
EMAIL=${1-root}

# ratio reference value (see: http://cr.yp.to/djbdns/cachesize.html)
# default: 3 days
REF_RATIO="3.0"

# set to 1 for debugging mode (no info are saved)
DEBUG=0

### end of config vars

# path to application, check befor use
DC=/usr/bin/dc
SVSTAT=/usr/local/bin/svstat
HEAD=/usr/bin/head
CUT=/usr/bin/cut
HEAD=/usr/bin/head
AWK=/usr/bin/awk
STAT=/usr/bin/stat

# run from shell? force debug
[ -t 0 ] && DEBUG=1

# read cache size and current motion
CACHE_SIZE=`cat ${SERVICE}/env/CACHESIZE`
MOTION=`${HEAD} -n1 ${LOG_FILE} | ${AWK} '{ print $4; }'`

# read old motion (save it and exit if no old motion is present)
if [ -f ${LOG_MOTION} ]; then
  OLD_MOTION=`cat ${LOG_MOTION}`
else
  [ ${DEBUG} = 0 ] && echo ${MOTION} > ${LOG_MOTION}
  exit 1
fi

# seconds since last run
eval $(${STAT} -s ${LOG_MOTION})
NOW=`date "+%s"`
TIME_RATIO=`echo "3k ${NOW} ${st_mtime} - 86400 / p" | ${DC}`

# save current motion
[ ${DEBUG} = 0 ] && echo ${MOTION} > ${LOG_MOTION}

# no activity? exit!
[ ${MOTION} = ${OLD_MOTION} ] && exit 0

# dnscache restarted?
if [ ${MOTION} -lt ${OLD_MOTION} ]; then
  [ ${DEBUG} = 0 ] && rm -f ${AVG_FILE}
  exit 0
fi

# mumble mumble...
SQ=`${HEAD} -n1 ${LOG_FILE} | ${AWK} '{ print "3k "$4" "$3" / p"; }' | ${DC}`
#RATIO=`echo "3k ${MOTION} ${OLD_MOTION} - ${CACHE_SIZE} r / p" | ${DC}`
RATIO=`echo "3k ${MOTION} ${OLD_MOTION} - ${CACHE_SIZE} r / ${TIME_RATIO} * p" | ${DC}`
CACHE=` echo "3k ${REF_RATIO} ${RATIO} / ${CACHE_SIZE} * p" | ${DC} | ${CUT} -d"." -f1`
STATUS=`${SVSTAT} ${SERVICE} | ${CUT} -d' ' -f2-`

VALUE=${CACHE}
AVG=${VALUE}
SAMPLE=1
OLD_SAMPLE=1

if [ -f ${AVG_FILE} ]; then
  OLD_SAMPLE=`${AWK} '{ print $1 }' ${AVG_FILE}`
  OLD_AVG=`${AWK} '{ print $2 }' ${AVG_FILE}`
  SAMPLE=`echo "${OLD_SAMPLE} 1 + p" | ${DC}`
  AVG=`echo "${OLD_SAMPLE} ${OLD_AVG} * ${VALUE} + ${SAMPLE} / p" | ${DC}`

  [ ${SAMPLE} -gt ${MAX_SAMPLE} ] && SAMPLE=1
fi

[ ${DEBUG} = 0 ] && echo "${SAMPLE} ${AVG}" > ${AVG_FILE}

# send report
cat <<MAIL | mail -s "dnscache report" ${EMAIL}
dnscache status for `hostname`

current cache size:  ${CACHE_SIZE}
best cache size:     ${CACHE}
avg best cache size: ${AVG} (${OLD_SAMPLE} samples)
one day cache ratio: ${RATIO}
query/motion ratio:  ${SQ}
dnscache status:     ${STATUS}

--
Developed by Luca Morettoni - <luca@morettoni.net>
See more at http://morettoni.net
MAIL

