#keys()
DESCRIPTION
returns the number of keys in an associative array;
maps those keys to a string variable when used as
'for <stringvar> in #keys(<array>)';
the keys are sequenced in the order encountered during the script run
or:
returns the number of keys in an associative array;
maps those keys to a string variable when used as
'foreach #keys(<stringvar>, <array>)';
the keys are sequenced in the order encountered during the script run
SYNOPSIS
#keys(s)
or:
#keys(s1,s2)
ARGUMENTS
s - an associative array name
or:
s1 - a string variable name
s2 - an associative array name
EXAMPLES
Grpck
init
status active
level warning
task "Report group problems/anomalies as found by grpck"
input proc "=grpck 2>&1"
filter "=egrep -v 'no changes' | =grep -v '^$'"
begin
set $grplin = ""
rule // some initializations for every new /etc/group line
if $inlin =~ "^[[:alpha:]]" // an actual group file line
// chop trailing "#" off previous $grpcklin[], if any
if $grplin ne ""
&& $grpcklin[$grplin] ne ""
=chop($grpcklin[$grplin])
fi
set $grplin = $inlin
set $grpcklin[$grplin] = ""
next
fi
rule // filter out uninteresting errors, for current /etc/group
// line, build a #-delimited string of "interesting" errors
if $inlin !~ "Duplicate logname entry \\(gid first occurs in passwd entry\\)|
tty - Logname not found in password file"
if $grplin ne ""
set $grpcklin[$grplin] .= $inlin . "#"
fi
fi
end
// chop trailing "#" off previous $grpcklin[], if any
if $grplin ne ""
&& $grpcklin[$grplin] ne ""
=chop($grpcklin[$grplin])
fi
foreach #keys($gl, $grpcklin)
// skip $grpcklin[] with no "interesting" errors
if $gl ne ""
&& $grpcklin[$gl] ne ""
output mail $gl
set #n = #split($grpcklin[$gl], "#")
for #i=1 #i<=#n #i+=1
output mail $[#i]
endfor
fi
endforeach
#if solaris
CountsProcEmergency
init
status active
level emergency
task "Report unusually high counts of per-user procs."
// note: a defunct process might show an empty comm field
// below, so we pipe the ps output through the awk filter, too
input proc "=ps -eo user,comm | =behead(1) | =awk 'NF==2' |
=sort | =uniq -c"
dat #count 1
dat $user 2
dat $proc 3
begin // field separator below is ""
=readvals(=objdir/CountsProc.obj, countsproc, " ", 4)
// it would be more efficient to put everything following in a single
// foreach loop, but by breaking things out into separate foreach
// loops, we can group things into four separate rules--to log, alert,
// page, and kill
rule // log, for gathering diagnostic stats
foreach #keys($pf, $countsproc)
do #split($pf)
if $2 eq "1"
if $proc =~~ "$1" // '=~~', not 'eq',
// so that '\\*' works
// as a default
if #val($countsproc[$pf])
&& #count >= #val($countsproc[$pf])
output log "=counts_proc_log" $inline
fi
break // move on to next rule
fi
fi
endforeach
rule // alert
foreach #keys($pf, $countsproc)
do #split($pf)
if $2 eq "2"
if $proc =~~ "$1"
if #val($countsproc[$pf])
&& #count >= #val($countsproc[$pf])
output mail $inline
fi
break // move on to next rule
fi
fi
endforeach
#ifdef page
rule // page
foreach #keys($pf, $countsproc)
do #split($pf)
if $2 eq "3"
if $proc =~~ "$1"
if #val($countsproc[$pf])
&& #count >= #val($countsproc[$pf])
=page(=pikthostname: $inlin, =pagesysadmins,
! =offhours(#now()))
pause 5
fi
break // move on to next rule
fi
fi
endforeach
#endifdef // page
rule // kill
foreach #keys($pf, $countsproc)
do #split($pf)
if $2 eq "4"
if $proc =~~ "$1"
if #val($countsproc[$pf])
&& #count >= #val($countsproc[$pf])
=kill_user_proc($proc, $user, #true())
fi
break // move on to next rule
fi
fi
endforeach
#endif // solaris
SEE ALSO
#members()
#rkeys(), #reversekeys()
#skeys(), #sortkeys()
Numerical Functions
String Functions