#!/bin/bash

if [ -z $SDDIR ]; then
    SDDIR=/etc/statusd
fi

if [ -z $SPOOLDIR ]; then
    SPOOLDIR=/var/spool/statusd
fi

STF=$SPOOLDIR/nginx_status

function parse_ngx_log()
{
	NGX_LOG=""
	for P_LOG in /chroot/proxyd/var/log/nginx/main/error.log /chroot/proxyd/var/log/nginx/error.log /var/log/nginx/main/error.log /var/log/nginx/error.log; do
		if [ -f $P_LOG ]; then
			NGX_LOG=$P_LOG
			break
		fi
	done

	if [ -n $NGX_LOG ]; then
		if (grep 'limiting' $NGX_LOG >/dev/null); then
			echo 'Limiting'
		else
			echo 'OK'
		fi
	else
		echo 'No nginx'
	fi
}


if [ "$1" == "cron" ]; then
	RES=`parse_ngx_log`
	echo "$RES" >$STF
else
	if [ -f $STF ]; then
		RES=`cat $STF`
		echo "$RES"
		if [ "$RES" != "OK" ]; then
			exit 1
		else
			exit 0
		fi
	else
		echo "OK"
		exit 0
	fi
fi
