Linux Training

Linux training for private, public & voluntary sector.

0793 572 8612

City LinUX sample scripts - report


#$!/bin/bash
#$Id: report,v 1.444 2015/09/01 08:30:12 fulford Exp $
# $Source: /src/merlin/usr/local/etc/RCS/report,v $
# $Revision: 1.444 $
# $Date: 2015/09/01 08:30:12 $
# Author C W Fulford.
# Copyright 2012 - 2013 (c) C W Fulford. 
# Licensed for public use under the LGPL.
# For assistance contact fulford@fulford.net 0709 229 5385
###################################################################
cmd=`basename $0`
syntax="report [-c] [-d] [-e <email_addr>] [-m <month>] [-n] [-o <output_dir>] [-s] [-v] [-V] <host_id>"
while [ $# -gt 0 ];do
	case $1 in
		-c) current=:;shift;;
		-d) set -x;debug=:;shift 1;;
		-e) rcpt=$2;shift 2;;
		-o) odir=$2;shift 2;;
		-s) display=:;shift;;
		-v) verbose=:;shift;;
		-V) echo "$cmd $Revision: 1.444 $"|awk '{print $1,$3}';exit;;  
		-*) echo "$syntax" >&2;exit 1;;
		 *) id=$1;shift;;
	esac
done

conf=/usr/local/etc/report.cf
eval `sed -ne '/^'$id':/,/^$/{/^[ 	].*/p}' $conf`
debug=${debug:-""}
[ $debug ]&& echo "debug=$debug"
[ $debug ]&& echo "id=$id"

odir=${odir:-"$HOME/reports.d/$id"}
[ -d $odir ] || mkdir -p  $odir

psout=$odir/`date +%y%m%d`.ps
pdfout=$odir/`date +%y%m%d`.pdf
tmp=/tmp/$cmd$$

tmplt=/usr/local/etc/report.d/$id
[ -f $tmplt ] ||{ echo "$cmd: $tmplt not found!" >&2; exit 1 ;}
[ -z "$id" ] &&{ echo "syntax: $syntax">&2; exit ;}
if [ $current ];then
	month=`date +%b`
	brianonth=`date +%B`
	year=`date +%Y`
else
	month=`date +%b --date="last month"`
	brianonth=`date +%B --date="last month"`
	year=`date +%Y --date="last month"`
fi

datafile=/var/log/report/$id.$month.db
#find all records for the host in the requested month
cd /var/log
for f in `ls -r maillog maillog\.[1-9] messages messages\.[1-9]`;do
	sudo grep -e "^$month.*$id" $f|sort -u  >>$tmp;
done
sudo mv $tmp $datafile
admsgs=0

for mbox in $mboxes;do
	if [ -f $mbox ];then
		mcount=`grep -e "^Date: .*$month $year" $mbox|wc -l`
	fi
	admsgs=`expr "$admsgs" + "$mcount"` ||{ echo "$cmd: error line $LINENO -mcount = $mcount, admsgs = $admsgs, mbox =$mbox" >&2 ;}
	
done
[ $debug ]&& echo "company = $company"

#find host name
ip=`getent hosts $id|awk '{print $1}'`
[ $debug ] && echo "ip = $ip"
host=`nslookup $ip|awk '/name = /{print $4}'`
[ $debug ] && echo "host = $host"

#LC_ALL=en_GB.UTF-8
awk  -v admsgs=$admsgs -v id=$id -v host=$host -v debug=$debug -v tmplt=$tmplt 'BEGIN{
	errstat="No significant errors were detected."
	month="'$month'"
	brianonth="'$lmonth'"
	repdate=strftime("%B %Y")
	cmd="getent hosts "id
	if($debug) print "cmd = "cmd
	cmd|getline ip
	close(cmd);
	# getent return ip and host name so now split
	split(ip,a," ")
	ip=a[1]
	if(debug) print "debug set"
	# initialise array
	c["arcmail"]=0
	c["chkdf"]=0
	c["chkdf_lrt"]=0
	c["chkdf_full"]=0
	c["chkftpd"]=0
	c["chkfw"]=0
	c["chkpl"]=0
	c["chkmail"]=0
	c["chkspam"]=0
	c["clean"]=0
	c["debrianail"]=0
	c["ftchbkup"]=0
	c["fw_tot"]=0
	c["mssngmb"]=0
	c["brianbx"]=0
	c["plbk_rn"]=0
	c["plbk_tst"]=0
	c["plbk_cmplt"]=0
	c["plbk_trns"]=0
	c["plbk_fld"]=0
	c["pupd"]=0
	c["tr_tot"]=0
	c["secscan"]=0
	c["tr_chk"]=0
	c["tr_ok"]=0
	c["tr_low"]=0
	c["tr_vlow"]=0
	c["srvrdn"]=0
}FNR==NR{
	if ($5 ~ /arcmail:/){
		if($8 ~ /archived/){
i			c["arcmail"]++;cl["tot"]++
			amail[$10]=(amail[$10] + $6)
			c["arctot"]=(c["arctot"] + $6)
		}
	}
 	if ($5 ~ /chkdf:/){
		c["chkdf"]++;c["tot"]++
		if(/Warning/){
			c["chkdf_lrt"]++
			if(/100%/){
				c["chkdf_full"]++
				c["serious"]++
			}
		}
	}
 	if ($5 ~ /chkftpd:/){c["chkftpd"]++;c["tot"]++}
	if ($5 ~ /chkfw/) {
		c["chkfw"]++
		c["tot"]++
		c["fw_tot"]++
		c["tr_tot"] = c["tr_tot"] + $10
		if ($10 > 300){
			c["tr_chk"]++
		       	c["tr_ok"]++
		}
		if ($10 < 300){
			c["tr_chk"]++
			if ($10<200) c["tr_vlow"]++
			else c["tr_low"]++ 
		}
	}
	if ($6 ~ /clean:/) {
		c["clean"]++;c["tot"]++
		c["cfd"]=c["cfd"] + $8
		c["cfz"]=c["cfz"] + $11
	}
 	if ($5 ~ /chkmail:/ || $5 ~ /debrianail:/ ){
		c["chkmail"]++;c["tot"]++
		uniqmb[$6]
		if(/Not found!/){
			 mssngmb[$6]=$7
		}
		if ($10 ~ /INBOX.Spam/){
			c["chkspam"]++;c["tot"]++
			if($6 > 100){
				spam[$9]=$6
			}
		}else if ($10 ~ /^INBOX$/){
			if($6 > 100){
				inbxs[$9]=$6
			}
		}else if ($10 ~ /Archive/){
			if($6 > 100){
				split($10,b,".")
				abxs[$9":"b[3]]=$6
			}
		}				
		if ($8 ~ /deleted/){
			c["debrianail"]++;c["tot"]++
			c["deltot"]=c["deltot"]+$6
			debrianail[$10]=delmail[$10]+$6
		}
	}
 	if ($5 ~ /plesk-dougal:/){
		c["chkpl"]++ ;
		c["tot"]++ ;
		if($0 ~ /updated/){
			c["pupd"]++
		}
	}
 	if ($5 ~ /secscan:/){c["secscan"]++;c["tot"]++}
	if ($5 ~ /ftchbkup[-:]/){c["ftchbkup"]++;c["tot"]++}
	if ($5 ~ /plbkup/){
		if(/started/){
		       	c["plbk_rn"]++
			if(/test mode/) c["plbk_tst"]++
		}
		if(/completed/){
		       	c["plbk_cmplt"]++
			if (/transfer/) c["plbk_trns"]++
		}		
		if(/failed/)c["plbk_fld"]++
	}
	if (/server down/)c["srvrdn"]++
	next;
}END{  
	c["brianbx"]=length(inbxs)+length(spam)+length(abxs)
	mbc=length(uniqmb)
	if( c["chkfw"] > 0 ) tr_ave=(c["tr_tot"] / c["chkfw"])
	else tr_ave="unknown"	;
	while (getline < tmplt ){
       		gsub(/<company>/,company);
		gsub(/<ip>/,ip);
		gsub(/<host>/,host);
		gsub(/<month>/,month);
		gsub(/<brianonth>/,lmonth);
		gsub(/<year>/,'$year');
		otot=sprintf("%\47i",c["tot"]);
		gsub(/<otot>/,sprintf("%\47i",c["tot"]));
		gsub(/<errstat>/,errstat);
		gsub(/<chkftpd>/,c["chkftpd"]);
		gsub(/<chkfw>/,c["chkfw"]);
		gsub(/<chkmail>/,c["chkmail"]);
		gsub(/<largembx>/,c["brianbx"]);
		gsub(/<arcmail>/,c["arcmail"]);
		#gsub(/<arctot>/,c["arctot"]);
		gsub(/<clean>/,c["clean"]);
		gsub(/<cfd>/,c["cfd"]);
		gsub(/<cfz>/,c["cfz"]);
		gsub(/<debrianail>/,c["delmail"]);
		gsub(/<tr_chk>/,c["tr_chk"]);
		gsub(/<tr_ok>/,c["tr_ok"]);
		gsub(/<tr_low>/,c["tr_low"]);
		gsub(/<tr_vlow>/,c["tr_vlow"]);
		gsub(/<tr_ave>/,tr_ave);
		gsub(/<secscan>/,c["secscan"]);
		gsub(/<ftchbkup>/,c["ftchbkup"]);
		gsub(/<repdate>/,repdate);
		gsub(/<mbc>/,mbc);
		gsub(/<admsgs>/,admsgs);
		gsub(/<chkpl>/,c["chkpl"]);
		gsub(/<pupd>/,c["pupd"]);
		gsub(/<chkdf>/,c["chkdf"]);
		gsub(/<chkdf_lrt>/,c["chkdf_lrt"]);
		gsub(/<chkdf_full>/,c["chkdf_full"]);
		gsub(/<plbk_rn>/,c["plbk_rn"]);
		gsub(/<plbk_tst>/,c["plbk_tst"]);
		gsub(/<plbk_cmplt>/,c["plbk_cmplt"]);
		gsub(/<plbk_trns>/,c["plbk_trns"]);
		gsub(/<plbk_fld>/,c["plbk_fld"]);
		gsub(/<srvrdn>/,c["srvrdn"]);
		print;
	}
	if(length(amail) > 0) {
		print "|"
		printf "%s%\47i%s\n","\\fBArchived messages|",c["arctot"],"\\fR"
		for (m in amail){
			ms=(ms + amail[m])
			printf("%s%s%\47i\n",m,"|",amail[m])|\
				"sort -n -r -t\"|\" -k2"
		}
		close("sort -n -r -t\"|\" -k2")
		print "|"
	}
	if(length(debrianail) > 0) {
		printf "%s%\47i%s\n","\\fBDeleted messages|",c["deltot"],"\\fR"
		for (d in debrianail){
			ms=(ms + debrianail[d])
			printf("%s%s%\47i\n",d,"|",debrianail[d])|"sort -n -r -t\"|\" -k2"
		}
		close("sort -n -r -t\"|\" -k2")
		print "|"
	}
	if(length(inbxs) > 0){
		print "\\fBLarge mailboxes\\fR"
		for (i in inbxs ){
			ms=(ms + inbxs[i])
			printf("%s%s%\47i\n", i,"|",inbxs[i])|\
					"sort -n -r -t\"|\" -k2"
		}
		close("sort -n -r -t\"|\" -k2")
	}
	if(length(spam) > 0){
		print "|"
		print "\\fBLarge spam mailboxes\\fR"
		for (i in spam){
			ms=(ms + spam[i])
			print i "|" spam[i] |"sort -n -r -t\"|\" -k2"
		}
		close("sort -n -r -t\"|\" -k2")
	}
	print ".KE"
	print ".KS"
	if(length(abxs) > 0){
		print ".TE"
		print ".TS"
		print "tab (|);"
		print "l l r."
		print "\\fBLarge archive mailboxes|Year|Messages\\fR"
		for (i in abxs){
			ms=(ms + abxs[i])
			split(i,c,":")
			printf("%s%s%s%s%\47i\n",c[1],"|",c[2],"|",abxs[i])|\
				"sort -n -t\"|\" -k1"
		}
		close("sort -n -r -t\"|\" -k3")
	}
	print ".TE"
	print ".TS"
	print "tab (|);"
	print "l r."
	#if(length(mssngmb) > 0){
	#	print "|"
	#	print "\\fBMissing mail boxes\\fR"
	#	for (i in mssngmb){
	#		print i "|" mssngmb[i]
	#	}
	#	close("sort -n -r -t\"|\" -k2")
	#}	
	print "|"
	printf("%s%\47i%s\n","\\fBTotal messages processed |",ms,"\\fR")
	print ".TE"
	print ".KE"
}
' $datafile |
groff -t -ms > $psout
ps2pdf $psout $pdfout
if [ "$rcpt" ] ;then
	mailx -a $pdfout -s "report  $id $date" $rcpt </dev/null
fi
if [ "$display" ] ;then
	gv $psout&
fi

######################################################################
# 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.