Linux Training

Linux training for private, public & voluntary sector.

0793 572 8612

City LinUX sample scripts - webv


#!/bin/bash
# $Id: webv,v 1.94 2019/04/04 18:11:28 fulford Exp fulford $
# $Source: /src/admin/usr/local/etc/RCS/webv,v $
# $Revision: 1.94 $
# Author C W Fulford.
# Copyright 2014 (c) C W Fulford.
# Licensed for public use under the LGPL, .
# For assistance contact fulford@fulford.net 0709 229 5385
########################################################################
cmd=`basename $0`
syntax="$cmd: [[-d] [-v] <id> [-C # ]] | -V"
ver=`echo "$Id: webv,v 1.94 2019/04/04 18:11:28 fulford Exp fulford $"|awk '{print $3,$4,$5}'`
while [ $# -gt 0 ] ;do
	case $1 in 
		-C) ttd=$2;shift 2;;
		-c) config=$2;shift 2;;
		-d) debug=:;set -x;shift;;
		-s) sort=0;shift;;
		-v) verbose=:;shift;;
		-V) echo "$cmd $ver";exit;;
		 *) sites="$sites $1";shift;;
	esac
done
[ -z "$sites" ]&&{ echo "syntax: $syntax" >&2;exit 1;}
[ `id -u` -eq 0 ] &&{ echo "$cmd: Cannot run as root user.">&2;exit 1 ;}
config=${config:-"/usr/local/etc/webv.cf"}
[ -f "$config" ]||{ echo "$cmd: $config not found">&2;exit 1 ;}

tmp=/tmp/webv$$

_exit () {
	[ -f $tmp ] && rm $tmp
	exit $1 
}
# total days in full month
Days=`cal \`date +"%m %Y"\` | awk 'NF {DAYS=$NF} END {print (DAYS)}'`

# total mins in full month
Mins=`bc<<- .
	scale=2
	$Days * 24 * 60
	.
`	
[ $verbose ] &&{
	printf "%30s %10s %9s %8s\n" "Website" "Visitors" "Average" "Prediction"
}

for site in $sites ;do
	eval `sed -ne "/^$site:/,/^$/{
			/^[ \t][^#]*/p
		}" $config
	      `
	[ -z "$LOC" ] &&{
       		echo "$cmd: No LOC for $site in $config" >&2
		_exit 1
	}
	[ -z "$STATSRVR" ] &&{
       		echo "$cmd: No STATSRVR for $site in $config" >&2
		_exit 1
	}
	[ -z "$URI" ] &&{
       		echo "$cmd: No URI for $site in $config" >&2
		_exit 1
	}

	read -r eDays eHrs eMins <<- .
		$(date +"%d %H %M")
	.
	toteMins=`bc <<- .
		($eDays - 1) * 24 * 60
		.
	`
	toteMins=`bc <<- .
		$eHrs *  60 + $toteMins
		.
	`
	toteMins=`bc <<- .
		scale=2
		$eMins + $toteMins
		.
	`
	[ ! $ttd ] &&{
	  if wget --spider -q $STATSRVR$LOC ;then
		ttd=`wget -qO - $STATSRVR$LOC |
			sed -ne '/Viewed traffic/s/[^0-9]*\([0-9,]*\).*/\1/p'|
			sed -e 's/,//'
		`
	  else
		error=$?
		echo "$cmd: $STATSRVR$LOC not available.">&2
		_exit $error
	  fi
	}
	[ ! $ttd ] &&{
		echo "$cmd: stats at $URI not found" >&2
		continue
	}
	
	ave=`bc <<- .
		scale=6
		$ttd / $toteMins
		.
	`
	perDay=`bc <<- .
		scale=6
		$ave * 60 * 24 
		.
	`

	predn=`bc <<- .
		scale=6
		$ave * $Mins
		.
	`
	#home=`echo $URI|sed -ne 's/\([^\/]*\)\/.*$/\1/p'`
	home=$URI
	(( $(#home) > 30 )) && home="${home:0:27}..."
	if [ "$verbose" ];then
		printf "%30s %8i %10.2f %8.0f\n" $home $ttd $perDay $predn
	else
		printf "%30s %3i %4.2f %4.0f\n" $home $ttd $perDay $predn
	fi
	unset ttd URI LOC STATSRVR 
done >$tmp 
if [ $sort ];then
	sort  -n -r -k3 <$tmp
else
	cat $tmp
fi
_exit 0	
######################################################################
# This program is free software: you can redistribute it and or      #
# modify it under the terms of the Lesser GNU General Public License #
# as published by the Free Software Foundation, either version 3 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.                       #
#                                                                    # 
# A copy of the Lesser GNU General Public License and the GNU        #
# General Public License is available at                             #
# <http://www.gnu.org/licenses/>.                                    #
######################################################################

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.