#!/bin/bash
# Script to get lighttpd stats

# This script needs the mod_status module enabled in lighttpd.conf
# http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ModStatus

# lighttpd.conf
# server.modules = ( ..., "mod_status", ... )
#
# $HTTP["remoteip"] == "127.0.0.1" {
# status.status-url = "/server-status"
# }

#Legenda:
#
# connectionsp = connect | connectionsC = Close | connectionsE = hard error | connectionsk = keep-alive
# connectionsr = read | connectionsR = read-POST | connectionsW = = write | connectionsh = handle-request
# connectionsq = request-start | connectionsQ = request-end
# connectionss = response-start | connectionsS = response-end

WGET="/usr/bin/wget"

STATUS_URL="http://127.0.0.1/server-status"
LIGHTTPD_TEMPFILE="/tmp/lighttpd_statistics"
ERRORS_TEMPFILE="/tmp/lighttpd_errors"

USER=""
PASSWORD=""

$WGET --user=$USER --password=$PASSWORD -q -O - $STATUS_URL?auto > $LIGHTTPD_TEMPFILE
if [ -s $LIGHTTPD_TEMPFILE ]; then
	totalaccesses=$(grep 'Total Accesses' $LIGHTTPD_TEMPFILE | cut -d' ' -f3)
	totalkbytes=$(grep 'Total kBytes' $LIGHTTPD_TEMPFILE | cut -d' ' -f3)
	uptime=$(grep 'Uptime' $LIGHTTPD_TEMPFILE | cut -d' ' -f2)	
	busyservers=$(grep 'BusyServers' $LIGHTTPD_TEMPFILE | cut -d' ' -f2)
	idleservers=$(grep 'IdleServers' $LIGHTTPD_TEMPFILE | cut -d' ' -f2)
	connectionsp=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o '\.' | wc -w)
	connectionsC=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'C' | wc -w)
	connectionsE=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'E' | wc -w)
	connectionsk=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'k' | wc -w)
	connectionsr=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'r' | wc -w)
	connectionsR=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'R' | wc -w)
	connectionsW=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'W' | wc -w)
	connectionsh=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'h' | wc -w)
	connectionsq=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'q' | wc -w)
	connectionsQ=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'Q' | wc -w)
	connectionss=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 's' | wc -w)
	connectionsS=$(grep 'Scoreboard' $LIGHTTPD_TEMPFILE | cut -d' ' -f2 | grep -o 'S' | wc -w)

	echo "<<<app-lighttpd>>>"
    echo "totalaccesses:$totalaccesses"
	echo "totalkbytes:$totalkbytes"
	echo "uptime:$uptime"
	echo "busyservers:$busyservers"
	echo "idleservers:$idleservers"
	echo "connectionsp:$connectionsp"
	echo "connectionsC:$connectionsC"
	echo "connectionsE:$connectionsE"
	echo "connectionsk:$connectionsk"
	echo "connectionsr:$connectionsr"
	echo "connectionsR:$connectionsR"
	echo "connectionsW:$connectionsW"
	echo "connectionsh:$connectionsh"
	echo "connectionsq:$connectionsq"
	echo "connectionsQ:$connectionsQ"
	echo "connectionss:$connectionss"
	echo "connectionsS:$connectionsS"

else
echo "Can't connect to $STATUS_URL" > $ERRORS_TEMPFILE
fi
rm -f $LIGHTTPD_TEMPFILE
