Sample C program


The sample C language program and associated header files are distributed in hlq.CZAGENT.C. It is recommended that you copy them to a zFS folder with names as shown in the following sections:

Source code

/* Sample.c : Sample program for the CorreLog zDefender for z/OS API1 from C/C++ */
#include <stdio.h>
#include <memory.h>
#include <stddef.h>
#include <string.h>
#include "CZAPI1.h"
#include "CZAPI1DG.h"

/* some things that would be parameters
in a real program */
static const int Repetitions = 10;
static const int Instance = 1;
static const int Severity = CZ1SevNotice;

int main(int argc, char* argv[])
{
    /* The API1 control block */
    struct CZAPI1CB Control_Block;
    int rc;
    int i;

    /* The record that we are going to send */
    struct {
        int sample_int;
        int rep;
        short sample_short;
        char c_string[100];
    } record = {
      
1234,           /* sample_int */      
0,              /* rep set in code below */      
-57,            /* sample_short */
"Hello, world"
};

    /* Diagnostic message parms */
struct CZ1DiagRec diagRec;
    /* Our diagnostic message */
    char diagMsg[] = "%3$u records sent to zDefender Instance %4$u by %7$.8s";

   /* Say Hello */
printf("CorreLog
API1 Sample C program\n");

    /* initialize the control block */
Control_Block.CZ1Ver = CZ1Ver1;
Control_Block.CZ1Flag1 = 0;
Control_Block.CZ1Inst = Instance;           
Control_Block.CZ1Sev = Severity;
/* Initialize control block for data records */
Control_Block.CZ1Major = CZ1MajTest;
Control_Block.CZ1Minor = 2;             /* Minor type 2 */
Control_Block.CZ1Lenth = sizeof(record);


/* Loop sending the record the specified number of times */
for ( i = 1; i <= Repetitions; i++ )
{
        /* set the repetition number in the record */
        record.rep = i;
        rc = CZAPI1(&record, &Control_Block);
        if ( rc != CZ1Reas_Good )
        {
            printf("Error %x from API1 on record %d\n", rc, i);
            return 8;
        }
}
    /* Initialize control block for diagnostic records */

Control_Block.CZ1Major = CZ1MajDiag;
Control_Block.CZ1Minor = CZ1DiagMinSubstA;         /* with substitution */    

/* Variables */
    diagRec.CZ1DiagWords[2] = record.rep;
    diagRec.CZ1DiagWords[3] = Instance;
    memcpy(diagRec.CZ1DiagChar[0], "API1CSMP", 8);
    strcpy(diagRec.CZ1DiagText, diagMsg);
    diagRec.CZ1DiagTextL = strlen(diagMsg);
    Control_Block.CZ1Lenth = offsetof(struct CZ1DiagRec, CZ1DiagText) + diagRec.CZ1DiagTextL;
    rc = CZAPI1(&diagRec, &Control_Block);
    if ( rc != CZ1Reas_Good )
    {
        printf("Error %x from API1 on diagnostic record\n", rc, i);
        return 8;
  }
    printf("Completed -- %d records sent\n", record.rep);
    return 0;
}

Sample compile command

c89 -Wl,AC=1,EXTATTR=APF -o Sample Sample.c

Field definitions

; API1 Major=Test, Minor=2 for C Sample Program
TYPE API_Test2() API1GENERAL( +
 CEF( NAME(CEF_Test_2_Name) ) +
  MAJOR(4) MINOR(2) PROCESS('Test2') FACILITY(Syslogd) )

DEF CEF_Test_2_Name '' API_Test1() FChar() GConst('TEST2')
DEF A1Sint     Int    API_Test2(CEF(cn1)) FInt(ULong) GBase(0 4)
DEF A1Srep     Rep    API_Test2(CEF(cn1)) FInt(ULong) GBase(4 4)
DEF A1Sshort   Short  API_Test2()         FInt(Short) GBase(8 2)
DEF A1Sstring  String API_Test2(CEF(cs1))
FCString() GBase(10 100)

Parameter file statement

EVENT TEST2 FIELDS(          +
                   A1Sint    +
                   A1Srep    +
                   A1Sshort  +
                   A1Sstring +
                   IEF_EXIT  +
                   CURRENT_TIME +
                             )   

Syslog output

The following message would be repeated 10 times, with Rep varying from 1 to 10:

Sep 07 10:29:12 hostname
Test2: Int: 1234 - Rep: 1 - Short: -57 - String: Hello, world - Exit: API1 -
Time: 2014-09-07T14:29:11.615


 

Tip: For faster searching, add an asterisk to the end of your partial query. Example: cert*