Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

O.K. Im new and confused ksh

Status
Not open for further replies.

StephenCrane

IS-IT--Management
Mar 3, 2004
4
0
0
US
I have been reading several tutorials on ksh programming and am kind of (really stumped). I need to learn this but hardly ahve time. I need to read the output from a command on AIX, "datapath query adapter" which gives you:

Adpt# Adapter Name State Mode Select Errors Paths Active
0 fscsi0 NORMAL ACTIVE 13138734 0 22 22
1 fscsi1 FAILED ACTIVE 1424115 12 22 0

As you can see the second card is failing. I need to read this output, find out the state of card NORMAL, ACTIVE, FAILED, etc. Then match these up with system states for reporting to TEC CONSOLE with states of MINOR, WARNING, CRITICAL, FATAL... My attempt is below, can anyone give me some guidance on a good tutorial or book to get started on this???? or should I go back to school.

This is what they requested:

Write a script for the Dynix hosts that takes command line arguments and compares the oldest file in a given directory against the arguments and echoes out results. Command line should look like "./shellscript -w WARNHOURS -m MINORHOURS -c CRITICALHOURS -f FATALHOURS -d /path/to/directory/to/check". Script would find oldest file in directory specified in -d argument, and compare it against the -w, -m, -c and -f arguments to see if it's more than that number of hours old. The script should handle not having all of the HOURS items specified but the directory and at least one HOURS comparison must be present, or it should dump to screen a proper usage message.

Here is my attempt to start:

#!/bin/ksh
########################################################
#Created: 2/16/2004
#Name: /usr/local/bin/q_adapter.ksh
#By: Stephen Crane x5097
#Purpose: To query fiber adapter to parse output against values
# specified.
#Version 0.1
########################################################
#
#Process:
#
# Query the adapter
# Capture "datapath query adapter" output and parse against values
# Generate events as needed
##########################################
#command looks like:
#
#root@rgtsscsr04:/ # datapath query adapter
#
#Active Adapters :2
#
#Adpt# Adapter Name State Mode Select Errors Paths Active
# 0 fscsi0 NORMAL ACTIVE 13138734 0 22 22
# 1 fscsi1 FAILED ACTIVE 1424115 12 22 0
#########################################

# Set constants
EVENT[0]="WARN"
EVENT[1]="MINOR"
EVENT[2]="CRIT"
EVENT[3]="FATAL"
HOST=`hostname`
OS=`uname -s`
TIVOLISPEC="hlyw_base SENTRY"
WPOSTEMSG="/opt/tivoli/lcf/bin/*/bin/wpostemsg"

# Inherit the Tivoli environment
. /opt/tivoli/lcf/dat/1/lcf_env.sh

# Set variables
awk="awk"

function setplatform {
# Determine platform we're running on, and do specific things
# based on platform (like Dynix, awk=nawk).

case $OS in
"AIX") dbgprt "Platform is AIX\n"
;;
"DYNIX/ptx") dbgprt "Platform is Dynix\n"
awk="nawk"
;;
"Solaris") dbgprt "Platform is Solaris\n"
;;
"HP-UX") dbgprt "Platform is HP-UX\n"
;;
"Linux") dbgprt "Platform is Linux\n"
;;
*) echo "ERR: Platform of $os is UNKNOWN. Cannot proceed."
exit 12
;;
esac
}



rm /tmp/q_adapter.file

touch /tmp/q_adapter.file

datapath query adapter > /tmp/q_adapter.file

awk '/fscsi/ {print $1, $3, $4; }' q_adapter.file

if [$3 -eq NORMAL] ; then
print adapter is "$3"
else
print adapter failure "$3"
if [$4 -eq NORMAL] ; then
print adapter is "$4"
else
print adapter failure "$4"
fi


#If any condition suffice:
#datapath query adapter | awk '
#$3!="NORMAL" || $4!="ACTIVE" || $6>0'

#If all 3 conditions are mandatory:
#datapath query adapter | awk '
#$3!="NORMAL" && $4!="ACTIVE" && $6>0'

=================================

And here is an example I have of an example for disk filesystem alerts:

#!/bin/ksh
#
# diskevent
#
# Version 0.3
#
# Purpose:
# To parse the output of a df against thresholds specified in
# the configuration file to determine whether Tivoli events
# should be generated, and if so, generate them.
#
# Process:
# Read configuration file
# Capture df output
# Parse df output against configuration file
# Generate events as needed
#
# Changes:
# v0.3: Modifications for Dynix (CG)

DEBUG=0

# Set constants
CFGFILE="/opt/admin/bin/diskevent.cfg"
EVENT[0]="WARN"
EVENT[1]="MINOR"
EVENT[2]="CRIT"
EVENT[3]="FATAL"
HOST=`hostname`
OS=`uname -s`
TIVOLISPEC="hlyw_base SENTRY"
WPOSTEMSG="/opt/tivoli/lcf/bin/*/bin/wpostemsg"

# Default threshold, if file doesn't specify one, is nothing for warning
# or minor, critical at 95%, fatal at 100%.
DEFAULT="-:-:95:100"

# Inherit the Tivoli environment
. /opt/tivoli/lcf/dat/1/lcf_env.sh


# Set variables
awk="awk"
df="df -k"

# Functions

function dbgprt {
# If DEBUG is non-zero, print messages for debugging

if [[ $DEBUG != 0 ]]
then
echo $1
fi
}

function setplatform {
# Determine platform we're running on, and do specific things
# based on platform (like Dynix, awk=nawk).

case $OS in
"AIX") dbgprt "Platform is AIX\n"
;;
"DYNIX/ptx") dbgprt "Platform is Dynix\n"
awk="nawk"
;;
"Solaris") dbgprt "Platform is Solaris\n"
;;
"HP-UX") dbgprt "Platform is HP-UX\n"
;;
"Linux") dbgprt "Platform is Linux\n"
;;
*) echo "ERR: Platform of $os is UNKNOWN. Cannot proceed."
exit 12
;;
esac
}

function setdefault {
# Get DEFAULT category from configuration file, if it exists
# Set default variables

defspec=`grep "^DEFAULT" $CFGFILE`
if [[ $? -gt 0 ]]
then
# No DEFAULT specified in config file!
echo "No DEFAULT specified in config file $CFGFILE. Using built-in defaults."
defspec=$DEFAULT
else
# DEFAULT specified
dbgprt "Found DEFAULT of $defspec"
fi
cfgdefault=`echo $defspec | cut -d":" -f2-5`
dbgprt "cfgdefault=$cfgdefault"
dbgprt ""
}

function getexcludes {
# Get list of excludes from config file, build array

numexcl=`grep -c "^EXCLUDE" $CFGFILE`

dbgprt "$numexcl EXCLUDES to process"

if [[ $numexcl -gt 0 ]]
then
linenum=0

for line in `grep "^EXCLUDE" $CFGFILE | $awk '{ print $2}'`
do
exarray[$linenum]=$line
dbgprt "exarray = ${exarray[$linenum]}"
((linenum+=1))
done

dbgprt ""
fi
}

function getcleandf {
# Snag working copy of df output, remove bogus lines,
# remove excludes, build array

linenum=0
dbgprt "Process df lines"

for line in `$df | $awk '
{
# Eliminate bogus lines
if ($1 ~ "^$") {
# empty line
continue
}
if ($1 ~ "^#") {
# comment line
continue
}
if ($1 ~ "^DEFAULT") {
# DEFAULT definition
continue
}
if ($1 ~ "^Filesystem") {
# header line
continue
}
printf("%s:%s\n", $7, $4)
}'`
do
dbgprt "line=$line"

excluded=0

df1=`echo $line | cut -d":" -f1`
df2=`echo $line | cut -d":" -f2 | cut -d"%" -f1`

for excl in ${exarray[*]}
do
dbgprt "excl=$excl"

if [[ $df1 = "$excl" ]]
then
excluded=1
dbgprt "No array entry for $line -- EXCLUDED"
fi
done

if [[ $excluded = 0 ]]
then
dfarray[$linenum]="$df1:$df2"
dbgprt "dfarray[$linenum]=${dfarray[$linenum]}"
((linenum+=1))
fi
done

dbgprt ""
}

function getcleandynixdf {
# Snag working copy of df output, remove bogus lines,
# remove excludes, build array

linenum=0
dbgprt "Process df lines"

for line in `$df | $awk '
{
# Eliminate bogus lines
if ($1 ~ "^$") {
# empty line
continue
}
if ($1 ~ "^#") {
# comment line
continue
}
if ($1 ~ "^DEFAULT") {
# DEFAULT definition
continue
}
if ($1 ~ "^Filesystem") {
# header line
continue
}
printf("%s:%s\n", $6, $5)
}'`
do
dbgprt "line=$line"

excluded=0

df1=`echo $line | cut -d":" -f1`
df2=`echo $line | cut -d":" -f2 | cut -d"%" -f1`

for excl in ${exarray[*]}
do
dbgprt "excl=$excl"

if [[ $df1 = "$excl" ]]
then
excluded=1
dbgprt "No array entry for $line -- EXCLUDED"
fi
done

if [[ $excluded = 0 ]]
then
dfarray[$linenum]="$df1:$df2"
dbgprt "dfarray[$linenum]=${dfarray[$linenum]}"
((linenum+=1))
fi
done

dbgprt ""
}

function getfs {
# Get list of filesystems from config file, build array

linenum=0
dbgprt "Process filesystems from config"

for line in `cat $CFGFILE | grep "^/"`
do
dbgprt "line=$line"
fsarray[$linenum]=$line
dbgprt "fsarray=${fsarray[$linenum]}"
((linenum+=1))
done

dbgprt "$linenum lines of filesystem specs processed\n"

}

function genevent {
# Determine whether to generate an event, and if yes, do so!

dbgprt "Genevent: fsspec=$fsspec, dfpct=$dfpct"

event="NONE"

for loop in 0 1 2 3
do
fsevent[$loop]=`echo $fsspec | cut -d":" -f$((loop+=2))`

dbgprt "fsevent[$loop]=${fsevent[$loop]}"
done

for loop in 0 1 2 3
do
if [[ "${fsevent[$loop]}" != '-' ]]
then
if [[ "$dfpct" -gt "${fsevent[$loop]}" ]]
then
event=${EVENT[$loop]}
fi
fi
done

dbgprt "EVENT $event"

if [[ $event != "NONE" ]]
then
eventfs=`echo $fsspec | cut -d":" -f1`

case $event in
"WARN") $WPOSTEMSG -m "Size of filesystem $eventfs on $HOST is at ${dfpct}% (over $event threshold of ${fsevent[0]}%)" probe_arg="$eventfs" severity="WARNING" hostname="$HOST" $TIVOLISPEC
echo "Warning event generated for filesystem $eventfs at ${dfpct}% (threshold is ${fsevent[0]}%)"
;;
"MINOR") $WPOSTEMSG -m "Size of filesystem $eventfs on $HOST is at ${dfpct}% (over $event threshold of ${fsevent[1]}%)" probe_arg="$eventfs" severity="MINOR" hostname="$HOST" $TIVOLISPEC
echo "Minor event generated for filesystem $eventfs at ${dfpct}% (threshold is ${fsevent[1]}%)"
;;
"CRIT") $WPOSTEMSG -m "Size of filesystem $eventfs on $HOST is at ${dfpct}% (over $event threshold of ${fsevent[2]}%)" probe_arg="$eventfs" severity="CRITICAL" hostname="$HOST" $TIVOLISPEC
echo "Critical event generated for filesystem $eventfs at ${dfpct}% (threshold is ${fsevent[2]}%)"
;;
"FATAL") $WPOSTEMSG -m "Size of filesystem $eventfs on $HOST is at ${dfpct}% (over $event threshold of ${fsevent[3]}%)" probe_arg="$eventfs" severity="FATAL" hostname="$HOST" $TIVOLISPEC
echo "Fatal event generated for filesystem $eventfs at ${dfpct}% (threshold is ${fsevent[3]}%)"
;;

esac
fi
}

function dfprocess {
# For each filesystem in dfarray, determine whether it has specific
# thresholds defined, or falls under DEFAULT, and build final info
# array

index=0

dbgprt "Processing dfarray for events"
dbgprt "${#dfarray[*]} dfarray elements, ${#fsarray[*]} fsarray elements"

for dfline in ${dfarray[*]}
do
match=0

dbgprt "dfline=$dfline"

dffs=`echo $dfline | cut -d":" -f1`
dfpct=`echo $dfline | cut -d":" -f2`

dbgprt "dffs=$dffs, dfpct=$dfpct"

for fsline in ${fsarray[*]}
do
dbgprt "fsline=$fsline"

fsfs=`echo $fsline | cut -d":" -f1`
fsthresh=`echo $fsline | cut -d":" -f2-5`

if [[ $dffs = $fsfs ]]
then
dbgprt "MATCH: dffs=$dffs, fsfs=$fsfs, fsthresh=$fsthresh"
thresh=$fsthresh
match=1
fi
done

if [[ $match = 0 ]]
then
thresh=$cfgdefault
fi

finarray[index]=`echo "$dffs:$dfpct:$thresh"`

dbgprt "finarray[$index]=${finarray[index]}"

((index+=1))

dbgprt ""
done
}

function eventprocess {
# For each line in finarray, determine whether filesystem is
# over threshold, and if so, generate event.

for line in ${finarray[*]}
do
event="NONE"

dbgprt "line=$line"

fs=`echo $line | cut -d":" -f1`
fspct=`echo $line | cut -d":" -f2`

field=3

for loop in 0 1 2 3
do
fsthresh[$loop]=`echo $line | cut -d":" -f$field`
((field+=1))
done

for loop in 0 1 2 3
do
if [[ ${fsthresh[$loop]} != "-" ]]
then
if [[ $fspct -ge ${fsthresh[$loop]} ]]
then
event=${EVENT[$loop]}
evthresh=${fsthresh[$loop]}
fi
fi
done

if [[ $event != "NONE" ]]
then
case $event in

"WARN") severity="WARNING"
;;
"MINOR") severity="MINOR"
;;
"CRIT") severity="CRITICAL"
;;
"FATAL") severity="FATAL"
;;
esac

echo "$severity event generated for filesystem $fs at ${fspct}% (threshold is $evthresh%)"
$WPOSTEMSG -m "Size of filesystem $fs on $HOST is at ${fspct}% (over $event threshold of $evthresh%)" probe_arg="$fs" severity=$severity hostname="$HOST" $TIVOLISPEC

fi

dbgprt "For $fs at $fspct, event=$event"
done
}

# Main

setplatform

setdefault

getexcludes

getfs

if [[ $OS = "DYNIX/ptx" ]]
then
getcleandynixdf
else
getcleandf
fi

dfprocess

eventprocess
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top