#!/bin/sh

# ClonePanel - Manages duplicate accounts on two or more webservers,
# including snapshot backups, monitoring and failover dns.
# Copyright (C)2006 Chris Cheers, Internet Lynx.
# Contact chris[at]clonepanel[dot]com.
# Internet Lynx, PO Box 7117, Mannering Park, NSW 2259, Australia
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Version=0.33

set -u
# Be strict about variable declaration

unset PATH
#avoid use of $PATH - limit script to system commands we choose

PROGRAM_DIR=$(/bin/echo $0 |/bin/sed -e "s/\/[a-zA-Z0-9_]*$//")
# Extract the working directory from command line
# NB - if your system doesn't have echo and sed in these locations
# then this line will need to be changed in all shell scripts

# Standard include files:
. $PROGRAM_DIR/includes

BC=/usr/bin/bc
# Probably not necessary to keep this available only here?

handled=''
# Variable to keep track of hosts with errors handled below
month=`$DATE -u +%m`
monthab=`$DATE -u +%b`
year=`$DATE -u +%Y`

total=`$GREP -Po " m_min_\w+" $MONITOR_DIR/${year}-${month}* |$UNIQ |$WC -l`
# Search monitor files this month, select only filename and minutes,
# unique to list just the measurement times, count results.
# Result: total number of measurements taken this month

# Next count fatal errors for each host in this months error logs
while read count hclass
do
	if [ ! "$count" ]; then
		continue
	fi
	host=`$ECHO $hclass |$SED -e "s/m_h_//"`
	handled="$handled $host"
# remember which hosts we've handled for later
	uptime=`$ECHO "scale=3; 100.0 - $count.0 * 100.0 / $total.0" | $BC`
# and calculate uptime percentage for the month so far
	$ECHO "<div class=\"m_h_$host m_uptime_thismonth\">$monthab:$uptime%</div>"
#output is a div containing this information for each host

done <<END
`$GREP -Po "^1000 m_h\w+ m_min_\w+" $ERROR_LOG_DIR/${year}-${month}* |$UNIQ |$AWK '{print $2}' | $SORT | $UNIQ -c`
END
# Search error files this month for 1000 (maximum error), select only filename,
# score, host and minutes, unique to skip multiple errors found at the same
# time, awk to select only host name, sort and count unique hosts.
# Result: list of hosts for which fatal errors have been found, and
# total number of fails for each

# Now handle the hosts that have no errors
mhost=`$FIND $HOSTS_DIR -maxdepth 1 -mindepth 1 -type d -printf "%f "`

for host in ${mhost[*]}
do
	if [ "`$ECHO $handled |$GREP $host`" ]; then
# TODO - what about host whose name is substring of another
		continue
	fi
	config -H $host
	if [ "$MONITOR_IP" != "NONE" ]; then
		$ECHO "<div class=\"m_h_$host m_uptime_thismonth\">$monthab:100.000%</div>"
# Any monitored hosts not already handled have zero errors (potentially a problem...)
	fi
done


