#!/bin/bash # # netdrawcopy -- 2007-12-10, last edit 2008-09-30 # # make a midnight copy of netdraw.png, called from crontab at a minute # after midnight, keep a list of most recent images, update the netdraw # web page, update the monthly summary image and copy the monthly image # on the first of each month # # last sample time was 23:59, so we processing yesterday's stuff # # Copyright (C) 2007,2008 Grant Coady GPLv2 # # 2008-09-30 # change .conf reader to use case, cleaner. still need to rearrange data # fixup to be done before chart draw # # 2008-09-06 # update the web page after processing completed, trial a bash based .conf # reader first, those multiple awk calls look bad ;) # # 2008-05-17 # repair and trim the data sample file, perhaps redraw yesterday's chart # DOT_CONF="/etc/netdraw.conf" while read key value rest do case $key in codepath) CODEPATH=$value;; htmlpath) IMAGEPATH=$value;; imagfile) IMAGEFILE=$value;; daycopylog) COPYLOG=$value;; daylogmax) COPYMAX=$value;; copytmp) COPYTMP=$value;; moncopylog) MONCOPYLOG=$value;; monlogmax) MONCOPYMAX=$value;; datapath) DATAPATH=$value;; datafile) DATAFILE="$DATAPATH/$value";; sampletime) SAMPLTIM=$value;; samplemax) SAMPLMAX=$value;; *) continue;; esac done < $DOT_CONF DATESTAMP=$(date -d yesterday +%F) IMAGENEW="netdraw-$DATESTAMP.png" cd $IMAGEPATH || exit 1 cp -f $IMAGEFILE $IMAGENEW # daily snapshot file echo $IMAGENEW >> $COPYLOG # add to log # limit length of daily snapshots log sort -r $COPYLOG | uniq | head -$COPYMAX > $COPYTMP && \ mv -f $COPYTMP $COPYLOG # remove old daily snapshots ls netdraw-*png > $COPYTMP cat $COPYTMP $COPYLOG | sort | uniq -u | xargs rm -f # perhaps do monthly processing MONTHDAY=$(date +%d) # 2008-03-01 really fix last month summary if [ $MONTHDAY -eq 1 ]; then # month start? create last month's summary image IMAGENEW="netdrawmon-$DATESTAMP.png" $CODEPATH/netdrawmon -v show_date="$DATESTAMP" echo $IMAGENEW >> $MONCOPYLOG # limit length of monthly snapshots log sort -r $MONCOPYLOG | uniq | head -$COPYMAX > $COPYTMP && \ mv -f $COPYTMP $MONCOPYLOG # remove old monthly snapshots ls netdrawmon-*png > $COPYTMP cat $COPYTMP $MONCOPYLOG | sort | uniq -u | xargs rm -f fi # update the web page $CODEPATH/netdrawhtml # fill in any missing samples with zero data and trim sample file, notice # the illegal sample time filter, change this to suit if you are sampling # other than at hh:?4 or hh:?9 minutes in the cron job cp -f $DATAFILE $DATAFILE.bkp # data record format: 2008-05-17.05:14 1193463140 109699700 # this is broken, cannot handle switch from daylight savings to normal time #awk -v sampletime=$SAMPLTIM -v flagfile="$DATAFILE.flag" ' #{ # split($1, a, "[-.:]") # # m = a[5] % 10; if (!(m == 4 || m == 9)) { next } # drop bad samples # # datetime = mktime(a[1]" "a[2]" "a[3]" "a[4]" "a[5]" 30") # if (last_datetime && datetime - last_datetime > sampletime) { # last_datetime += sampletime # while(last_datetime < datetime) { # print strftime("%F.%H:%M", last_datetime) " 0 0" # last_datetime += sampletime # print strftime("%F", last_datetime) > flagfile # } # } # print # last_datetime = datetime #} #' $DATAFILE > $COPYTMP && tail -$SAMPLMAX $COPYTMP > $DATAFILE && rm $COPYTMP # trim sample file, data recovery is manual until the above is fixed to use # epoch time mv $DATAFILE $COPYTMP && tail -$SAMPLMAX $COPYTMP > $DATAFILE && rm $COPYTMP # perhaps redraw daily summary chart/s, we do this here as we now have all # the expected hour marker records in the sample data file #if [ -r "$DATAFILE.flag" ]; then # uniq $DATAFILE.flag > $COPYTMP # while read end_date crap; do # $CODEPATH/netdraw end_date="$end_date" # done < $COPYTMP # rm $DATAFILE.flag $COPYTMP #fi # end