# DOCUMENT:  Makefile
# VERSION:   $Revision: 1.3 $
# DATE:      $Date: 2007-09-02 20:25:42 $
# AUTHOR:    M.Beranek <marcus@beranek.de>
# COPYRIGHT: M.Beranek, http://www.beranek.de
# 
# This program is FREE SOFTWARE and may be copied and distributed
# as you like, as long as the above COPYRIGHT-notice stays untouched!
# 
# NOTE: Use at your own risk! 
#
# The author may not be held responsible for any damage to you or 
# your system caused by this program directly or indirectly.
# 
# You are allowed to modify this file, as long as you mark any
# modifications you have made.
# 


# directories:
INSTDIR=/usr/local/monitor
LOGDIR=/var/log/monitor
OUTPUTDIR=/var/log/monitor/summary

# user:
RUNAS=nobody

# ==============================================================================
# You should not need to not change anything below this line...
# But if you do anyway, don't blame me, if something wicked happens!
# ==============================================================================

export INSTDIR
export LOGDIR
export OUTPUTDIR
export RUNAS


# Binaries:
# rrdtool-binary
RRD=`which rrdtool`
# vmstat-binary:
VMSTAT=`which vmstat`
# cat-binary:
CAT=`which cat`
# install-binary:
INSTALL=`which install`

CRONDIR=/etc/cron.d

MONITOR_SOURCES=create_rrd_vm.sh draw_stats_vm.sh monitor_vmstat.sh parse_vm_output.pl
MONITOR_CONF=monitor_config.sh
MONITOR_CRON=monitorvmstat

all: clean install

clean: 
	if [ -f ${MONITOR_CONF} ]; then rm -f ${MONITOR_CONF}; fi
	if [ -f ${MONITOR_CRON} ]; then rm -f ${MONITOR_CRON}; fi

install: ${MONITOR_CONF} ${MONITOR_CRON} ${INSTDIR} copyfiles ${LOGDIR} creadterrd ${OUTPUTDIR} createcron

keepdata: clean ${MONITOR_CONF} ${MONITOR_CRON} ${INSTDIR} copyfiles ${LOGDIR} ${OUTPUTDIR} createcron

todo: 
	echo "todo"


# 0. Create Config "monitor_config.sh"
${MONITOR_CONF}: 
	${CAT} monitor_config_01.sh > ${MONITOR_CONF}
	echo "# Log-directory and -filenames:" >> ${MONITOR_CONF}
	echo "LOGDIR=${LOGDIR}" >> ${MONITOR_CONF}
	echo "" >> ${MONITOR_CONF}
	echo "# Output-directory for html and images:" >> ${MONITOR_CONF}
	echo "OUTPUTDIR=${OUTPUTDIR}" >> ${MONITOR_CONF}
	echo "" >> ${MONITOR_CONF}
	echo "# Binaries:" >> ${MONITOR_CONF}
	echo "# rrdtool-binary:" >> ${MONITOR_CONF}
	echo "RRD=${RRD}" >> ${MONITOR_CONF}
	echo "# vmstat-binary:" >> ${MONITOR_CONF}
	echo "VMSTAT=${VMSTAT}" >> ${MONITOR_CONF}
	echo "# cat-binary:" >> ${MONITOR_CONF}
	echo "CAT=${CAT}" >> ${MONITOR_CONF}
	echo "" >> ${MONITOR_CONF}
	${CAT} monitor_config_02.sh >> ${MONITOR_CONF}
	
# Create crontab-entry:
${MONITOR_CRON}:
	./monitor_vmstat.cron.sh > ${MONITOR_CRON}

# Create INSTDIR
${INSTDIR}: 
	${INSTALL} -d ${INSTDIR}

# Copy config-file and all scripts to INSTDIR
copyfiles:
	${INSTALL} ${MONITOR_SOURCES} ${INSTDIR}
	${INSTALL} ${MONITOR_CONF} ${INSTDIR}

# Create LOGDIR
${LOGDIR}: 
	${INSTALL} -o ${RUNAS} -d ${LOGDIR}

# Create RRD-Archive
creadterrd: 
	su ${RUNAS} -c "${INSTDIR}/create_rrd_vm.sh"

# Create OUTPUTDIR
${OUTPUTDIR}: 
	${INSTALL} -o ${RUNAS} -d ${OUTPUTDIR}

# Install cronjob
createcron: 
	${INSTALL} -m 644 ${MONITOR_CRON} ${CRONDIR}

# Uninstall:
uninstall:
	rm -rf ${INSTDIR}
	rm -rf ${LOGDIR}
	rm -rf ${OUTPUTDIR}
	rm -f ${CRONDIR}/${MONITOR_CRON}

