Am trying to execute a loop but having some troubles...
Files that will be query'd use the Julian date (eg: cpu032, cpu365) in their naming convention.
I'm a little lost how to maintain the three character format of the numeric portion of the file name while cycling backwards(or forwards for that matter) in a loop.
Bash does not support that option to typeset.
Also, if you're using a Linux-based OS, you most likely have PDKSH installed, instead of ksh. Most versions of pdksh do not support that either - only the "real" ksh can be trusted...
Good news though! You can get David Korn's KSH 93 from here:
Using AT&T's package is a bit of a pain, but it's worth it.
First of all, we seem to be talking about "Day of Year" here rather than "Julian Date". I know it's pro forma for programmers to mix the two up, but to get this right we have to be precise.
Converting a Julian Day Number to a date is rough but it can be done. Converting a Day of Year" to a date is impossible. We must also have the year. We need the year both so that we can display it and to determine if we are dealing with a leap year. Here is a script that takes to parameters, the year and the day of year and converts them to a date:
#! /usr/bin/ksh
# ja fe ma ap ma ju ju ag se oc no de
set -A mlength 31 28 31 30 31 30 31 31 30 31 30 31
typeset -Z2 dmonth dday
year=$1
doy=$2
#
# is this a leap year?
leap=0
if ((!(year%100))); then
((!(year%400))) && leap=1
else
((!(year%4))) && leap=1
fi
echo leap = $leap
#
# set number of days in february
mlength[1]=28
((leap)) && mlength[1]=29
#
# loop on month
month=0
mdoy=0
while ((month<12 && mdoy<doy)); do
((oldmdoy=mdoy))
((mdoy=mdoy+${mlength[month]}))
((month=month+1))
done
((day=doy-oldmdoy))
dday=$day
dmonth=$month
echo "$dmonth/$dday/$year"
exit 0
This is my solution & works great.
Unfortunately I hadn't started gathering stats on the systems this macro is running on since the 16th January.
The scary part is that it took me less than a shift to finish.
Note: This script is intended to run only on the 1st of any month.
#!/bin/ksh
typeset -Z3 xcjdate xpjdate xj31date xidl xpeak
typeset -Z2 xrptmth
#######################################################
# >> NOTE: Ensure that you comment ANY amendments. <<
#######################################################
#
# SCRIPT NAME : sarcpurpt
# Creation of the Monthly CPU Report
# based on data collected from the
# previous month.
# ----------------------------------------------------
# PURPOSE:
# This macro is intended to be run via CRONTAB on the
# 1st of every month at 00:05.
#
# Files to be read.
# /amadeus_stats/sar/cpu/cpuJJJ.?
#
# Output File (Report).
# /amadeus_stats/sar/cpu/Reports/MMMYYYY.JJJHHMM
# n.b. - JJJ = Jilian date.
# MMM = Short Month Name (Jan, Feb, etc.).
# YYYY = Long Year.
# HHMM = 24hr Time.
# ? = CPU Indicator [ 1 , 2 , 3 , 4 ]
#
# ----------------------------------------------------
# AMENDMENTS:
# <DATE> <VER> <WHO> < COMMENTS >
# 260202 001 CJY Initial script.
# 270202 002 CJY Ready for production.
#
# ----------------------------------------------------
# >> NOTE: Ensure that you comment ANY amendments. <<
#######################################################
# - Environment setup ----------------------------------#
## File location/assignments.
xcpudir=/amadeus_stats/sar/cpu/
xrptdir=$xcpudir\Reports/
## Email Recipients.
xemails="users@somedomain.com.au"
## Long Name Month Listing.
lngmth="January February March April May June \
July August September October November December"
## Short Name Month Listing.
shtmth="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
# - Functions ------------------------------------------#
#
function SendEmail {
xsub="EVEREST-AU - CPU Monthly Detailed Report - "$2", "$3"."
mail -s "$xsub" $xemails < $1
} ## End-of-function SendEmail
#
# - START-OF-SCRIPT ------------------------------------#
##
xcjdate=`date '+%j'`
xpjdate=`expr $xcjdate - 1`
xcurmth=`date '+%m'` ; xcuryr=`date '+%Y'`
xrptmth=`expr $xcurmth - 1` ; xrptyr=`expr $xcuryr - 1`
if [ $xrptmth -eq 0 ] ; then
xshtmth=`echo $shtmth | cut -f12 -d" "`$xrptyr
xlngmth=`echo $lngmth | cut -f12 -d" "`
else
xshtmth=`echo $shtmth | cut -f$xrptmth -d" "`$xcuryr
xlngmth=`echo $lngmth | cut -f$xrptmth -d" "`
fi
if [ $xcjdate -eq 001 ] ; then
xpjdate=366
xj31date=`expr $xpjdate - 32`
else
xpjdate=`expr $xcjdate - 1`
xj31date=`expr $xpjdate - 32`
fi
## Set Report File Name.
xcpurpt=$xrptdir/$xshtmth.`date '+%j%H%M'`
##
## Create Header.
echo > $xcpurpt
echo "SYSTEM: "`hostname`" (EVEREST-AU)" >> $xcpurpt
echo "CPU Detailed Usage Report - "$xlngmth", "`echo $xshtmth | cut -c4-7` \
"." >> $xcpurpt
echo "--------------------------------------------------------\c" >> $xcpurpt
echo "----------------------------------------------" >> $xcpurpt
echo " <----- CPU #1 -----> <----- CPU #2 ----->\c" >> $xcpurpt
echo " <----- CPU #3 -----> <----- CPU #4 ----->" >> $xcpurpt
echo "Date - %Avg %Peak Peak-Time - %Avg %Peak Peak-Time\c" >> $xcpurpt
echo " - %Avg %Peak Peak-Time - %Avg %Peak Peak-Time" >> $xcpurpt
while [ $xj31date -lt $xpjdate ] ; do
for xfile in 1 2 3 4 # Number of CPU files for each day.
do
#-- Get Date of CPU report -----------------------------#
xcpudd=`awk '/SCO_SV/' $xcpudir\cpu$xj31date.$xfile | cut -c36-37`
xcpumm=`awk '/SCO_SV/' $xcpudir\cpu$xj31date.$xfile | cut -c33-34`
xcpuyy=`awk '/SCO_SV/' $xcpudir\cpu$xj31date.$xfile | cut -c39-42`
xcpudate="$xcpudd/$xcpumm/$xcpuyy"
if [ $xcpumm -eq $xrptmth ] ; then
xidl=`awk '/SCO_SV/,/Average/' $xcpudir\cpu$xj31date.$xfile |\
tail -1 | cut -c38-40`
xidl=`expr 100 - $xidl`
xpeak=`awk '/SCO_SV/,/Average/' $xcpudir\cpu$xj31date.$xfile | \
grep ':' | grep -v '%' | grep -v 'mpx restarts' | sort -k 5 | \
head -1 | cut -c38-40`
xpeak=`expr 100 - $xpeak`
xtime=`awk '/SCO_SV/,/Average/' $xcpudir\cpu$xj31date.$xfile |\
grep ':' | grep -v '%' | grep -v 'mpx restarts' |\
sort -k 5 | head -1 | cut -c1-8`
case $xfile in
1) xcpu1=" "$xidl" "$xpeak" "$xtime" -";;
2) xcpu2=" "$xidl" "$xpeak" "$xtime" -";;
3) xcpu3=" "$xidl" "$xpeak" "$xtime" -";;
4) xcpu4=" "$xidl" "$xpeak" "$xtime;;
esac
fi
done
if [ $xcpumm -eq $xrptmth ] ; then
IFS=""
echo $xcpudate" - "$xcpu1" "$xcpu2" "$xcpu3" "$xcpu4 >> $xcpurpt
IFS=" "
fi
xj31date=`expr $xj31date + 1`
done
echo "--------------------------------------------------------\c" >> $xcpurpt
echo "----------------------------------------------" >> $xcpurpt
echo " End-Of-Report." >> $xcpurpt
echo >> $xcpurpt
echo " Nb: Print in Landscape mode suggested." >> $xcpurpt
chown operator:group $xcpurpt
chmod 644 $xcpurpt
SendEmail $xcpurpt $xlngmth `echo $xshtmth | cut -c4-7`
##
# - END-OF-SCRIPT --------------------------------------#