#!/bin/bash
# Crossfire metaserver statistics generator script
# Copyright (c) 2005 Heikki Hokkanen <heikki.hokkanen@tut.fi>
# License: GPLv2

# the stats file format is:
# updatehits:requesthits:totalservers:totalplayers

statsfile=/var/tmp/meta_xfire.stats
rrdfile=cfstats.rrd

if [ ! -f ${rrdfile} ]; then
	# daily (5min), weekly (30min), monthly (2h), yearly (1d)
	# (24*3600)/300 = 288
	# (7*24**3600)/(300*6) = 336
	# (5*7*24*3600)/(7200) = 420
	rrdtool create ${rrdfile} \
		DS:updates:COUNTER:600:0:U \
		DS:requests:COUNTER:600:0:U \
		DS:servers:GAUGE:600:0:U \
		DS:players:GAUGE:600:0:U \
		RRA:AVERAGE:0.5:1:288 \
		RRA:AVERAGE:0.5:6:336 \
		RRA:AVERAGE:0.5:24:420 \
		RRA:AVERAGE:0.5:288:3650 \
		RRA:MAX:0.5:1:288 \
		RRA:MAX:0.5:6:336 \
		RRA:MAX:0.5:24:420 \
		RRA:MAX:0.5:288:3650
fi

# update
def="
	DEF:updates=${rrdfile}:updates:AVERAGE
	DEF:requests=${rrdfile}:requests:AVERAGE
	DEF:servers=${rrdfile}:servers:AVERAGE
	DEF:players=${rrdfile}:players:AVERAGE
	"

gfx="
	LINE1:updates#00CC00:updates
	LINE1:requests#0000CC:hits
	LINE1:servers#00FFFF:servers
	LINE1:players#FF0000:players
	"

if [ "$1" == "graph" ]; then
	rrdtool graph stats-day.png $def $gfx
	rrdtool graph stats-week.png $def $gfx -s '-8days'
	rrdtool graph stats-month.png $def $gfx -s '-1month'
	rrdtool graph stats-year.png $def $gfx -s '-1year'
else
	rrdtool update ${rrdfile} N:$(cat ${statsfile})
fi
