Showing posts with label VBScript. Show all posts
Showing posts with label VBScript. Show all posts

Friday, January 5, 2018

Day 24 The tide is high

Day 24

The tide is high

Let us say that you do not have Sterling Control Center to run the High Water Mark report for maximum concurrent sessions.
You might want this for determining if you are making optimum use of the number of concurrent sessions that you are licensed for.
You may not have enough Connect:Direct nodes that you feel justifies the need for Sterling Control Center.

For Windows

The following script water-mark.vbs is an example of how to do this using VBScript and the Connect:Direct Windows SDK.
In fact this is a fairly simple example of how to use the Connect:Direct Windows SDK.
'
' Script to report on the high water mark for the number of concurrent Connect:Direct sessions 
'

Dim node        ' Represents the Connect Direct Node we are using. 
Dim stats       ' A collection of statistic records. 
Dim stat        ' An individual statistic record. 

' Make the OLE/COM connection to Connect:Direct 
Set node = CreateObject("CD.Node")

' Sign on using defaults from the Client Connection Utility
node.Connect "CD.NICKE","",""

' Get start and end sessions statistics records since yesterday
set stats = node.SelectStats("select statistics startt=(today) recids=(SSTR,SEND) ccode=(eq,0)")

currentNumberOfSessions = 0
highWaterMark  = 0

For Each stat in stats
       Select Case stat.RecId 
          Case "SSTR" currentNumberOfSessions=currentNumberOfSessions+1          Case "SEND" currentNumberOfSessions=currentNumberOfSessions-1       End Select
       If currentNumberOfSessions > highWaterMark Then
          highWaterMark = currentNumberOfSessions
       End If
Next

Wscript.echo "Concurrent sessions high water mark = " & highWaterMark

Set node  = nothing
Set stats = nothing
Set stat  = nothing
If you have the Connect:Direct Windows SDK installed and default sign on credentials registered with the Client Connection Utility you can run the script as follows:
C:\Users\nicke> cscript /nologo water-mark.vbs

Concurrent sessions high water mark = 5
The above script is minimal in that it is only meant to be run interactively as it does not itself check for every error.
You run the script on a Windows machine with the SDK installed, but the node in question can be a remote Windows or UNIX (I have not tested other platforms) Connect:Direct node as long as the node is registered with the Client Connection Utility.

For UNIX

If all you have are UNIX machines then you can use the following shell function for convenience:
function watermark
{
        # Usage: cd work/UNIX.NODE ; cat S201712* | watermark
        egrep "RECI=(SSTR|SEND)" | grep CCOD=0 | awk '
BEGIN{
        currentNumberOfSessions=0
        highWaterMark=0
}
/RECI=SSTR/ { currentNumberOfSessions++ }
/RECI=SEND/ { currentNumberOfSessions-- }
{
        if(currentNumberOfSessions > highWaterMark){
                highWaterMark=currentNumberOfSessions
        }
}
END{
        print "Concurrent sessions high water mark = " highWaterMark
}'
}
Put the above shell function definition either in your .profile or just paste it into a terminal session. You will need to be in the work directory for your UNIX node where the statistics files are. Then you can run it as follows by piping whatever period of statistics files you want through the watermark shell function:
[work/CD.UNIX] $ cat S201712* | watermark
Concurrent sessions high water mark = 8
Now you will know whether the number of sessions you use is appropriate for your licensing of these nodes.



Tuesday, January 22, 2013

No Chit Chat, just Smalltalk


Day 20

This is something that you do not see every day.  Below is an extract from an old Smalltalk workspace listing I found where I was experimenting with Cincom's ObjectStudio Smalltalk programming environment and the OLE/COM interface to Connect:Direct.

It was just as easy if not easier than VBScript to control Connect:Direct using Smalltalk.

D := OLEDispatcher new: 'CD.NODE'.
N :=  D call: 'Connect' params: (Array with: 'MY.NODE' with: '' with: '').


TXT := 'TEST001 PROCESS'                           + CrLf
           + '     MAXDELAY=UNLIMITED'             + CrLf
           + '     REMOTE=CD.REMOTE'               + CrLf
           + '     HOLD = NO'                      + CrLf
           + 'STEP01 COPY  FROM ('                 + CrLf
           + '     FILE=C:\TEMP\INPUT.TXT '        + CrLf
           + '     LOCAL /*$Windows NT$*/)'        + CrLf
           + '     TO ('                           + CRLF
           + '     REMOTE /*$Windows NT$*/'        + CrLf
           + '     FILE=C:\TEMP\OUTPUT.TXT'        + CrLf
           + '     DISP=(RPL))'                    + CrLf
           + 'PEND'.

P := D call: 'Submit' params: (Array with: TXT).

P at: 'ProcessNumber'


S := D call: 'SelectStats' params: (Array with: 'select statistics pnumber=62').
I := S call: 'HasMore'.
I := S call: 'GetNext'.
I at: 'MsgId'

Smalltalk is one of my favourite programming languages, due to its' simple syntax and object orientation.

It might be interesting to see how this compares to using OLE/COM from TCL.  I'll save that for another post.

Monday, June 21, 2010

Driving the Connect:Direct command line

Day 12

So last time I showed you how to use the Connect:Direct SDK OLE/COM interface to Connect:Direct in a VBScript example.

The following example is very similar to the previous one with a few exceptions.

Firstly instead of using the OLE/COM interface we are driving the Connect:Direct command line using the "Direct.exe" program.  We are also running the "Direct.exe" program hidden in the background.

You also won't see any signing on to Connect:Direct as this is taken care of using the "CD Client Connection Utility" to set the default node and user credentials which are picked up when we run the "Direct.exe".

The commands to be sent to the Connect:Direct command line are put in a temporary file and that file is supplied as a parameter when we run the "Direct.exe".

The text of the command that contains the Connect:Direct process is also a little different. In this script you will see it prefixed with the "submit" command, while in the previous script we called the "Submit" function of the "Node" object.

It is important to note that the Connect:Direct process contains the "MAXDELAY" statement if you want the script to wait for the Connect:Direct process to be submitted then run and get the return code.

If you do not have this statement in the process then the process will be submitted and that is all, before control is returned to the VBScript.  As submitting a process hardly ever fails the script could see a zero return code i.e. the submitting the process had no error, but when the Connect:Direct process runs and possibly fails the script will have finished and been unaware of the failure.

So usually for batch scripts you will want to specify the "MAXDELAY" statement so your script gets the result of running the Connect:Direct process and not just submitting it.
You may not want to specify "UNLIMITED", but some reasonable time-out figure such as "01:00:00" for an hour.

For the script below to work it must be placed in the same directory as the "Direct.exe" program.  

This is usually "C:\Program Files\Sterling Commerce\Connect Direct v4.x.00\Common Utilities" .  Where "x" may be different depending on the version you are running.

Both this and the previous script are examples of simple dynamic Connect:Direct process creation.  Later we will look at some more elaborate generation of Connect:Direct processes that apply to a wider range of applications.


' Windows Script Host Sample Connect:Direct Script using the CLI
'
' Includes ability to push and pull files to/from the other Node.

' Declare variables to represent objects

Dim fs, WshShell, tempDir, handle

' Connect:Direct condition codes values 

warningConditionCode = 4
errorConditionCode   = 8

'
' Function to either pull/push a file to/from another node
'                       
Function CDTransfer (pname, fromFile, toFile, otherNode, direc)

        If direct = "PUSH" then
                localOrRemote = "LOCAL"
                remoteOrLocal = "REMOTE"
        else
                localOrRemote = "REMOTE"
                remoteOrLocal = "LOCAL"
        End If

        ' Build the text of the submit command and the process

        txtProcess = "submit"                          + vbCrLf _
                + pname & " PROCESS"                  + vbCrLf _
                + "     MAXDELAY=UNLIMITED"             + vbCrLf _
                + "     REMOTE=" & otherNode            + vbCrLf _
                + "     HOLD = NO"                      + vbCrLf _
                + "STEP01 COPY  FROM ("                 + vbCrLf _
                + "     FILE=" & fromFile               + vbCrLf _
                + "     " & localOrRemote               + vbCrLf _
                + "     TO ("                           + vbCrLf _
                + "     " & remoteOrLocal               + vbCrLf _
                + "     FILE=" & toFile                 + vbCrLf _
                + "     DISP=(RPL))"                    + vbCrLf _
                + "PEND;"                               + vbCrLf _
                + "quit;"                             + vbCrLf

        ' Get temporary file names for input/output with the CLI
        inp=tempDir.path & "\" & fs.GetTempName
        outp=tempDir.path & "\" & fs.GetTempName

        ' Write the input to the CLI in a file
        set handle = fs.OpenTextFile(inp,2,True)
        handle.write txtProcess
        handle.close

        ' Run the CLI hidden, redirecting input and recording the output to a file
        cmd="%comspec% /c Direct.exe -z" & Chr(34) & outp & Chr(34) & " < " & inp
        CDTransfer=WshShell.run(cmd,0,True)

        ' Remove temporary files
        Set handle = fs.GetFile(inp)
        handle.Delete
        Set handle = fs.GetFile(outp)
        handle.Delete
End Function

Function CDSend ( pname, fromfile, toFile, otherNode )
        CDSend = CDTransfer ( pname, fromfile, toFile, otherNode, "PUSH" )
End Function

Function CDRecv ( pname, fromfile, toFile, otherNode )
        CDRecv = CDTransfer ( pname, fromfile, toFile, otherNode, "PULL" )
End Function

' Processing starts here

set fs = CreateObject("Scripting.FileSystemObject")
set tempDir = fs.GetSpecialFolder(2)
Set WshShell = CreateObject("WScript.Shell")

rtc = CDSend ( "SEND","c:\temp\input.txt", "c:\temp\output.txt", "CD.OTHER")

if rtc > warningConditionCode then
        wscript.echo "Send Failed"
        'Return the completon code to the command prompt
        wscript.quit(rtc)
End If

rtc = CDRecv ( "RECEIVE","c:\temp\remote.txt", "c:\temp\recv.txt", "CD.OTHER")

if rtc > warningConditionCode then
        wscript.echo "Recv Failed"
        'Return the completon code to the command prompt
        wscript.quit(rtc)
End If

Monday, May 31, 2010

OLE/COM & Connect:Direct

Day 11

Sometimes we want to be able to automate tasks with regard to Connect:Direct.  On Windows we could do this using batch files (.bat or .cmd), or with an application using the Connect:Direct Windows SDK.  My preferred way is to use some form of VBScript and the OLE/COM interface that the Connect:Direct Windows SDK provides.

That sounds more complicated that it really is.  I'll show an example Windows Script Host (WSH) script that uses the OLE/COM interface to connect to the local node and send/receive files  with a remote node.

Start by looking at the example below from the comment "Processing starts here" where you will see the creation of an object of type "CD.Node".  If my memory serves me well this is not well documented in the Connect:Direct Windows SDK manual.

Once we have an object that represents our local node in my case called "NICKE" we can make a connection with our credentials.  In this example the user name and password are empty strings and it makes the connection based on how the "CD Client Connection Utility" is setup.  This utility allows you to define several user credentials and which one is to be used as the default when none are given as in my example below.

The rest of the script then goes on to perform a transfer using the "CDSend" function to a remote node called "CD.OTHER", check for an error and then perform a receive of a file and then another check for an error.  Both "CDSend" & "CDRecv" make use of a generic function "CDTransfer" to perform a file transfer with the direction of the transfer passed as a parameter which is then used to generate a dynamic Connect:Direct process which is the submitted using the "Node.Submit" function and then the statistics for the process just submitted and executed are analysed and the highest condition code returned to the caller of the "CDTransfer" function.

At the end of the script the connection with the local node is disconnected and the "Node" object de-allocated.

The script below can either be run by double-clicking directly on the ".vbs" script in the file explorer or by giving the name of the script as a parameter to the "CSCRIPT.EXE" program as follows:

C:\> cscript cdvbsdk.vbs

In the next post I will an alternative method of driving Connect:Direct from VBScript.


' Windows Script Host Sample Connect:Direct Script. 
' 
' Includes ability to push and pull files to/from the other Node. 
 
Dim Node        ' Represents the Connect Direct Node we are using. 
Dim procs       ' A collection of process records. 
Dim proc        ' An individual process record. 
Dim stats       ' A collection of statistic records. 
Dim stat        ' An individual statistic record. 
 
' Connect:Direct condition codes values 
 
warningConditionCode = 4 
errorConditionCode   = 8 
 
 
 
 
Function CDTransfer (fromFile, toFile, otherNode, direc) 
 
        If direct = "PUSH" then 
                localOrRemote = "LOCAL" 
                remoteOrLocal = "REMOTE" 
        else 
                localOrRemote = "REMOTE" 
                remoteOrLocal = "LOCAL" 
        End If 
 
        ' The process text to be submitted. 
 
        txtProcess = "TRANSFER PROCESS"                 + vbCrLf _ 
                + "     MAXDELAY=UNLIMITED"             + vbCrLf _ 
                + "     REMOTE=" & otherNode            + vbCrLf _ 
                + "     HOLD = NO"                      + vbCrLf _ 
                + "STEP01 COPY  FROM ("                 + vbCrLf _ 
                + "     FILE=" & fromFile               + vbCrLf _ 
                + "     " & localOrRemote               + vbCrLf _ 
                + "     TO ("                           + vbCrLf _ 
                + "     " & remoteOrLocal               + vbCrLf _ 
                + "     FILE=" & toFile                 + vbCrLf _ 
                + "     DISP=(RPL))"                    + vbCrLf _ 
                + "PEND"                                + vbCrLf
 
 
        set proc = Node.Submit(txtProcess)             ' Submit the process 
 
        ' Get the statistics regarding the process that has now finished. 
 
        set stats = Node.SelectStats("SELECT STATISTICS PNUMBER=" & proc.ProcessNumber) 
 
        ' If any step in the process has a condition code greater than "warning" we have a problem. 
 
        highestConditionCode =0 
 
        For Each stat in stats
 
                if stat.ConditionCode > highestConditionCode then 
                        highestConditionCode = stat.ConditionCode
                End If 
 
        Next 
 
        If highestConditionCode > warningConditionCode then 
 
                CDTRansfer = highestConditionCode
 
        End If 
 
End Function 
 
 
 
 
Function CDSend ( fromfile, toFile, otherNode ) 
        CDSend = CDTransfer ( fromfile, toFile, otherNode, "PUSH" ) 
End Function 
 
Function CDRecv ( fromfile, toFile, otherNode ) 
        CDRecv = CDTransfer ( fromfile, toFile, otherNode, "PULL" ) 
End Function 
 
 
' ------------------------------------------------------------------------------------------ 
' 
' Processing starts here 
' 
Set Node = CreateObject("CD.Node")              ' Make the OLE/COM connection to Connect:Direct 
 
Node.Connect "NICKE","",""      ' Sign on 
 
exitcode = 0 
 
rtc = CDSend ( "c:\temp\input.txt", "c:\temp\output.txt", "CD.OTHER") 
 
if rtc > warningConditionCode then 
        wscript.echo "Send Failed" 
        exitcode = exitcode + rtc
End If 
 
rtc = CDRecv ( "c:\temp\input.txt", "c:\temp\recv.txt", "CD.OTHER") 
 
if rtc > warningConditionCode then 
        wscript.echo "Recv Failed" 
        exitcode = exitcode + rtc
End If 
 
' You need to disconnect from the Node or you will leave cscript or wscript processes hanging around 
Node.Disconnect 
Set Node=Nothing 
Wscript.Quit(exitcode)