Problem
I want to check the number of OpenServer user licenses currently expected to have been consumed.
Solution
The following awk script performs the check:
===============================================================================
#!/usr/bin/awk -f
# licensesinuse: Rule-of-thumb count of number of system user licenses expected
# to have been consumed. Count assumes maximum of one console scologin;
# other scologins are assumed to be to remote displays, and thus consume
# additional licenses.
BEGIN {
split("",PIDs)
split("",Procs)
split("",Children)
getPS(PIDs,Procs,"CMDT,LINE,ARGS,S_TTY,PPID",Children)
count = 0
for (pid in PIDs)
if ((procname = Procs[pid,"CMDT"]) == "login" && (pid in Children)) {
if (Procs[pid,"S_TTY"] ~ /^[0-9][0-9]$/)
if (gotconsole)
continue
else
gotconsole = 1
count++
}
else if (procname == "scologin" && (pid in Children) && \
Procs[pid,"PPID"] != 1) {
if (gotconsole) {
if (gotone)
count++
else
gotone = 1
continue
}
gotone = 1
gotconsole = 1
count++
}
print count
}# The following is excerpted from the ps lib as it appeared in
# ftp://ftp.armory.com/pub/scripts/aj
# Please see aj for complete version of ps library.### Begin ps lib
# getPS 1.2
# 96-10-09 john h. dubois iii (john@armory.com)
# 96-02-11 Added Debug flag.
# 96-05-09 Added COMM field.
# 96-05-23 Added selection args, and saving of "ps" PID.
# 96-05-25 Added makePSline()
# 96-10-09 Added RUSER field.
# 96-12-14 Added CMDT field.
# 98-04-30 1.2 Added makePSformat()
#
# Note: makePSline() needs assign() from array lib.
# todo: generalize based on -o args to 5.0 ps
#
# Do a ps and save the output into an array, indexed by pid and field name.
#
# Input vars:
# Fields: Comma-separated list of fields to put in Procs.
# If Debug is true, debugging info is output.
# selectionArgs may be set to ps options that will report on selected processes
# (e.g. -usomeone -ttty01)
# The default for selectionArgs is -e, which causes information on all
# processes to be gathered.
#
# Output vars:
# PIDs[]: the set of all PIDs seen.
# Also, the element with index "ps" is set to the PID for the ps process.
# Procs[pid,fieldname]: output by field.
#
# The possible fields are:
# USER: Effective user ID; name if available, else number. (WAS: UID)
# PPID: Parent process ID.
# C: CPU scheduling.
# STIME: Start time. If the start time in the ps output contains a space,
# it is replaced with a "-". "-" is returned for a defunct process.
# TTY: tty name; may or may not have leading "tty" part. "-" for defunct proc;
# "?" for proc with no controlling tty.
# S_TTY: tty name with any leading 'tty' removed.
# TIME: CPU time used.
# CMD: First element of arg vector (argv[0]).
# CMDT: Like CMD, but just the tail (leading path components removed), unless
# the path ends with /, in which case it is identical to CMD.
# CMDTA: Like CMDT, but with arguments included.
# ARGS: Entire (truncated) arg vector (command + args).
# LINE: Entire ps output line.
# Under 3.2v5, any of the field names given in the section of the
# ps man page describing the -o option may be used.#
# The header line read is also put in Procs with the index "Header".
# The PIDs of the children of each process are put in a comma-separated list
# in Children[pid].
# Return value: the number of processes found, or -2 if an invalid field name
# is passed, or -1 if an error occurs reading from ps.
# If -2 is returned, the bad field name is stored in the global _getPS_badname.
# Globals: FS is set to " "
#
# ps -f produces output in these forms, under various conditions & releases:
# UID PID PPID C STIME TTY TIME CMD
# root 10118 10107 2 Jan-03 ttyp0 00:00:05 -ksh
# root 10118 10107 2 Jan 03 ttyp0 00:00:05 -ksh
# root 18197 1 0 08:02:56 ttyp0 00:00:03 /usr/bin/X11/scoterm -geo
function getPS(PIDs,Procs,Fields,Children,Debug,selectionArgs,
stimeI,pidI,ttyI,ppidI,WantLine,psArgs,psSet,newPS,
FieldNames,Wanted,Cmd,getI,Field2Ind,i,Name,Lines,WantArgs,Header,CmdIndex,fn,
wantCmdt,wantStty,wantCMDTA) {
FS = " " # magic pattern to reset FS to its default special behaviour
split("UID,PID,PPID,C,STIME,TTY,TIME,CMD",FieldNames,",")
# psSet[] maps field number to field name
split("user,pid,ppid,c,stime,tty,time,args",psSet,",")
# Alt[] maps new ps field names to field numbers
Alt["RUSER"] = 1
Alt["COMM"] = 8
FieldNames[0] = "LINE"
for (i in FieldNames) # Field2Ind[] maps field names to field numbers
Field2Ind[FieldNames[i]] = i
split(Fields,Wanted,",")
pidI = Field2Ind["PID"]
ppidI = Field2Ind["PPID"]
stimeI = Field2Ind["STIME"]
ttyI = Field2Ind["TTY"]
timeI = Field2Ind["TIME"]
cmdI = Field2Ind["CMD"]
psArgs = "-f"
for (i in Wanted) {
Name = Wanted[i]
if (Debug)
printf "Asked for %s\n",Name > "/dev/stderr"
# getI[] is made to contain the indices of fields to record
if (Name == "ARGS")
WantArgs = 1
else if (Name == "LINE")
WantLine = 1
else if (Name == "CMDT")
wantCmdt = 1
else if (Name == "S_TTY")
wantStty = 1
else if (Name == "CMDTA")
wantCMDTA = 1
else if (Name in Alt) { # New ps fields
newPS = 1
# Change the name of this field to that of the alternate requested
psSet[Alt[Name]] = tolower(Name)
fn = Field2Ind[Name] = Alt[Name] # Map this name to its field number
getI[fn]
FieldNames[fn] = Name
}
else if (Name in Field2Ind)
getI[Field2Ind[Name]]
else {
_getPS_badname = Name
return -2
}
}
if (newPS) {
psArgs = ""
for (i = 1; i in psSet; i++)
psArgs = psArgs " -o" psSet[i]
}
Lines = 0
if (selectionArgs == "")
selectionArgs = "-e"
Cmd = "echo $$; exec /bin/ps " selectionArgs " " psArgs " < /dev/null"
if ((Cmd | getline PIDs["ps"]) != 1)
return -1
if ((Cmd | getline Header) != 1)
return -1
Procs["Header"] = Header
if (!(CmdIndex = index(Header,"CMD")) &&
!(CmdIndex = index(Header,"COMMAND")))
return -1
while ((Cmd | getline) == 1) {
PIDs[pid = $pidI]
if (Debug)
printf "Process %d (%d fields): %s\n",pid,NF,$0 > "/dev/stderr"
ppid = $ppidI
if (ppid in Children)
Children[ppid] = Children[ppid] "," pid
else
Children[ppid] = pid
if (WantArgs)
Procs[pid,"ARGS"] = substr($0,CmdIndex)
if (wantCMDTA) {
Procs[pid,"CMDTA"] = substr($0,CmdIndex)
if (Procs[pid,"CMDTA"] !~ "^[^ ]*/$")
sub("^[^ ]*/","",Procs[pid,"CMDTA"])
}
# Handle this as a special case so that it can be set before the
# line is (possibly) modified
if (WantLine)
Procs[pid,"LINE"] = $0
# Time field with either contain a : (time), a - (new date format),
# or neither, in which case it occupies 2 fields (old date format).
if (NF == 6) { # old ps defunct proc
# Assign new values to fields, from right to left to avoid
# overwriting fields before value is moved
$cmdI = $ttyI
$timeI = $stimeI
$ttyI = "-"
$stimeI = "-"
}
if ($stimeI !~ "[-:]") {
if (!timePos)
timePos = index($0,$stimeI)
# Replace space in stime field with "-"
$0 = substr($0,1,timePos+2) "-" substr($0,timePos+5)
}
if (wantCmdt) {
Procs[pid,"CMDT"] = $cmdI
if ($cmdI !~ "/$")
sub(".*/","",Procs[pid,"CMDT"])
}
if (wantStty) {
Procs[pid,"S_TTY"] = $ttyI
sub("^tty","",Procs[pid,"S_TTY"])
}
for (i in getI) {
Procs[pid,FieldNames[i]] = $i
if (Debug)
printf "%s=%s ",FieldNames[i],$i > "/dev/stderr"
}
if (Debug)
print "" > "/dev/stderr"
Lines++
}
close(Cmd)
return Lines
}
===============================================================================