#!/usr/local/bin/wermit +

; m p s e r v e r s  --  Census of modem-pool servers.

; Reports number of ports defined, in use, and free on a terminal-server
; based modem-pool farm.  Summarizes by server and by phone number.
; Assumes one phone number per server, but allows many servers per phone
; number.  Assumes servers respond to "finger" request with a list of
; active sessions.  Results are written to stdout.
;
; Illustrates:
;  . Another tool for modem-pool management.
;  . Associative arrays.
;  . Reading output from external commands.
;
; Requires C-Kermit 7.1 on a UNIX host.
;
; Author: F. da Cruz, Columbia University, Apr 2001.

; Parallel arrays of server hostnames, port counts, and phone numbers.
; For example, the server called abcts1 has 92 ports and is on the
; 5551234 hunt group.
;
; CHANGE THESE DEFINITIONS FOR YOUR OWN INSTALLATION.
;
dcl \&s[] = abcts1  abcts2  xyzts1  xyzts2  xyzts3  xyzts4  xyzts5  xyzts6
dcl \&n[] = 92      92      92      92      92      184     46      92
dcl \&t[] = 5551234 5551234 5554321 5554321 5554321 5554321 5554321 5559999

define badversion echo C-Kermit 7.1 required, exit 1
if LLT \v(version) 701199 badversion

.n := \fdim(&n)                     ; Number of servers
.myname := \&_[0]                   ; Pathname of this script

def RECORD {                        ; Write a record
    local s
    .s := \v(ndate) \v(time)  \%1
    echo {\m(s)}
}
.ports := 0                         ; Get total port count
for \i 1 n 1 {
    incr ports \&n[i]
    _incr xn<\&t[i]> \&n[i]         ; And number of ports per phone number
}

echo {-------}                      ; Write heading
show mac ports
echo date.... time....  server  ports busy free
echo {-------}

.\%s = 0                            ; Clear counts
for i 1 n 1 {
    _def pn<\&t[i]> 0
}

; Send "finger" command to each server.
; Pipe results through "grep -c tty" to count number of active tty ports.
; Accumulate per-server and per-phone-number sums.

for i 1 n 1 {                       ; Loop for each server
    .\%x := \fcommand(finger @\&s[i] | grep -c tty)
    .\%y ::= \&n[i] - \%x
    record {\frpad(\&s[i],7) \flpad(\&n[i],4) \flpad(\%x,4) \flpad(\%y,4)}
    incr \%s \%x
    _incr pn<\&t[i]> \%x
}

.z := \faaconvert(pn,&a,&b)         ; Convert associative array
for i 1 z 1 {                       ; Loop thru phone numbers
    .\%x := \m(xn<\&a[i]>)          ; Number of ports for this number
    .\%y ::=  \%x - \&b[i]          ; Number free
    record {\&a[i] \flpad(\%x,4) \flpad(\&b[i],4) \flpad(\%y,4)}
}
.\%y ::= \m(ports) - \%s            ; Grand totals

record {TOTAL   \flpad(\m(ports),4) \flpad(\%s,4) \flpad(\%y,4)}
exit 0
