#!/bin/sh

# No copyright is claimed on this script. Change it, use it,
# distribute it as you see fit.


# configurable variables

RRDDIR=/tmp


# automatically generated variables

GAUGES="active-db-connections db-connections http-connections imap-connections internal-connections memory-used other-connections pop3-connections query-queue-length smtp-connections total-db-connections"
COUNTERS="anonymous-logins injection-errors login-failures messages-injected messages-sent messages-submitted queries-executed queries-failed successful-logins unparsed-messages"


# other variables

TMP=`mktemp /tmp/rrdglue-$$-XXXXXXXX`


# code.

# 1. Fetch the data.

telnet localhost 17220 2>/dev/null | egrep '^[a-z0-9]+(-[a-z0-9]+)*( [1-9][0-9]*:[0-9]+)*$' > $TMP

# 2. Make any missing RRD files

cd $RRDDIR

export GAUGES COUNTERS
for t in COUNTER GAUGE; do
    for a in $(eval $(echo echo '$'$t'S')) ; do
        [ -f ${a}.rrd ] || \
            rrdtool create ${a}.rrd -s 1 \
                DS:`echo $a | tr - _ | cut -c-19`:$t:30:0:2147483647 \
                RRA:AVERAGE:0.5:120:2880 \
                RRA:AVERAGE:0.5:3600:672 \
                RRA:AVERAGE:0.5:86400:720
            # Store averages:
            # Per two minutes for the last four days (2880 bi-minutes)
            # Per hour for the last four weeks (672 hours)
            # Per day for the last two years (inaccurately 720 days)
            # If you also want maxima, you need MAX lines as well

            # The heartbeat is set to 30. Archiveopteryx will skip up
            # to 28 seconds of equal values, so a heartbeat of 30 is
            # the smallest safe value.

            # The data period is set to one second. If it's changed to
            # something bigger (sich as 30 or 60), the rrd files will
            # be smaller, but the data less exact.
    done
done

# 3. Update each file.

while read a ; do
    FILE=`echo $a | cut '-d ' -f1`.rrd
    LIMIT=`rrdtool info $FILE | awk '/last_update = /{print $3}'`
    echo $a | tr : ' ' | awk '{printf "update %s.rrd", $1; for (i=2; i<NF; i+=2) if ($i>'$LIMIT') printf " %s:%s", $i, $(i+1); print ""}'
done < $TMP | grep : | rrdtool - >/dev/null 2>/dev/null

# 4. Clean up.

rm $TMP
