instrumentation event, examples, example" /> Example of using instrumentation events

 

Example of using instrumentation events

 

This example shows how to write a program for instrumentation events. It is written for queue managers in C, for information about which platforms support C see the WebSphere MQ Application Programming Reference manual. It is not part of any WebSphere MQ product and is therefore supplied as source only. The example is incomplete in that it does not enumerate all the possible outcomes of specified actions. Bearing this in mind, we can use this sample as a basis for your own programs that use events, in particular, the PCF formats used in event messages. However, you will need to modify this program to get it to run on your systems.

Figure 10. Event monitoring sample program

 /********************************************************************/
 /*                                                                  */
 /* Program name: EVMON                                              */
 /*                                                                  */
 /* Description: C program that acts as an event monitor             */
 /*                                                                  */
 /*                                                                  */
 /********************************************************************/
 /*                                                                  */
 /* Function:                                                        */
 /*                                                                  */
 /*                                                                  */
 /*   EVMON is a C program that acts as an event monitor - reads an  */
 /*   event queue and tells you if anything appears on it            */
 /*                                                                  */
 /*   Its first parameter is the queue manager name, the second is   */
 /*   the event queue name. If these are not supplied it uses the    */
 /*   defaults.                                                      */
 /*                                                                  */
 /********************************************************************/
 #include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #ifndef min
   #define min(a,b)        (((a) < (b)) ? (a) : (b))
 #endif

 /********************************************************************/
 /* includes for MQI                                                 */
 /********************************************************************/
 #include <cmqc.h>
 #include <cmqcfc.h>
 void printfmqcfst(MQCFST* pmqcfst);
 void printfmqcfin(MQCFIN* pmqcfst);
 void printreas(MQLONG reason);
 
  #define PRINTREAS(param)                                           \
     case param:                                                     \
       printf("Reason = %s\n",#param);                               \
       break;
 

 /********************************************************************/
 /* global variable                                                  */
 /********************************************************************/
 MQCFH    *evtmsg;                    /* evtmsg message buffer       */
 
 int main(int argc, char **argv)
 {
   /******************************************************************/
   /* declare variables                                              */
   /******************************************************************/
   int  i;                             /* auxiliary counter          */
   /******************************************************************/
   /* Declare MQI structures needed                                  */
   /******************************************************************/
   MQOD     od = {MQOD_DEFAULT};      /* Object Descriptor           */
   MQMD     md = {MQMD_DEFAULT};      /* Message Descriptor          */
   MQGMO   gmo = {MQGMO_DEFAULT};     /* get message options         */
   /******************************************************************/
   /* note, uses defaults where it can                               */
   /******************************************************************/

   MQHCONN  Hcon;                     /* connection handle           */
   MQHOBJ   Hobj;                     /* object handle               */
   MQLONG   O_options;                /* MQOPEN options              */
   MQLONG   C_options;                /* MQCLOSE options             */
   MQLONG   CompCode;                 /* completion code             */
   MQLONG   OpenCode;                 /* MQOPEN completion code      */
   MQLONG   Reason;                   /* reason code                 */
   MQLONG   CReason;                  /* reason code for MQCONN      */
   MQLONG   buflen;                   /* buffer length               */
   MQLONG   evtmsglen;                /* message length received     */
   MQCHAR   command[1100];            /* call command string ...     */
   MQCHAR   p1[600];                  /* ApplId insert               */
   MQCHAR   p2[900];                  /* evtmsg insert               */
   MQCHAR   p3[600];                  /* Environment insert          */
   MQLONG   mytype;                   /* saved application type      */
   char     QMName[50];               /* queue manager name          */
   MQCFST  *paras;                    /* the parameters              */
   int      counter;                  /* loop counter                */
   time_t   ltime;
 
   /******************************************************************/
   /* Connect to queue manager                                       */
   /******************************************************************/
   QMName[0] = 0;                      /* default queue manager      */
   if (argc > 1)
     strcpy(QMName, argv[1]);
   MQCONN(QMName,                      /* queue manager              */
          &Hcon,                   /* connection handle          */
          &CompCode,               /* completion code            */
          &CReason);               /* reason code                */
 

   /******************************************************************/
   /* Initialize object descriptor for subject queue                 */
   /******************************************************************/
   strcpy(od.ObjectName, "SYSTEM.ADMIN.QMGR.EVENT");
   if (argc > 2)
     strcpy(od.ObjectName, argv[2]);
 
   /******************************************************************/
   /* Open the event queue for input; exclusive or shared.  Use of   */
   /* the queue is controlled by the queue definition here           */
   /******************************************************************/

   O_options = MQOO_INPUT_AS_Q_DEF     /* open queue for input       */
         + MQOO_FAIL_IF_QUIESCING      /* but not if qmgr stopping   */
         + MQOO_BROWSE;
   MQOPEN(Hcon,                        /* connection handle          */
          &od,                     /* object descriptor for queue*/
          O_options,                   /* open options               */
          &Hobj,                   /* object handle              */
          &CompCode,               /* completion code            */
          &Reason);                /* reason code                */
 
   /******************************************************************/
   /*   Get messages from the message queue                          */
   /******************************************************************/
   while (CompCode != MQCC_FAILED)
   {
     /****************************************************************/
     /* I don't know how big this message is so just get the         */
     /* descriptor first                                             */
     /****************************************************************/
     gmo.Options = MQGMO_WAIT + MQGMO_LOCK
        + MQGMO_BROWSE_FIRST + MQGMO_ACCEPT_TRUNCATED_MSG;
                                       /* wait for new messages      */
     gmo.WaitInterval = MQWI_UNLIMITED;/* no time limit              */
     buflen = 0;                       /* amount of message to get   */
 
     /****************************************************************/
     /* clear selectors to get messages in sequence                  */
     /****************************************************************/
     memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId));
     memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId));
 
     /****************************************************************/
     /* wait for event message                                       */
     /****************************************************************/
     printf("...>\n");
     MQGET(Hcon,                       /* connection handle          */
           Hobj,                       /* object handle              */
           &md,                    /* message descriptor         */
           &gmo,                   /* get message options        */
           buflen,                     /* buffer length              */
           evtmsg,                     /* evtmsg message buffer      */
           &evtmsglen,             /* message length             */
           &CompCode,              /* completion code            */
           &Reason);               /* reason code                */
 

     /****************************************************************/
     /* report reason, if any                                        */
     /****************************************************************/
      if (Reason != MQRC_NONE && Reason != MQRC_TRUNCATED_MSG_ACCEPTED)
     {
       printf("MQGET ==> %ld\n", Reason);
     }
     else
     {
       gmo.Options = MQGMO_NO_WAIT + MQGMO_MSG_UNDER_CURSOR;
       buflen = evtmsglen;             /* amount of message to get   */
       evtmsg = malloc(buflen);
       if (evtmsg != NULL)
       {
         /************************************************************/
         /* clear selectors to get messages in sequence              */
         /************************************************************/
         memcpy(md.MsgId, MQMI_NONE, sizeof(md.MsgId));
         memcpy(md.CorrelId, MQCI_NONE, sizeof(md.CorrelId));
 
         /************************************************************/
         /* get the event message                                    */
         /************************************************************/
         printf("...>\n");
         MQGET(Hcon,                     /* connection handle        */
               Hobj,                     /* object handle            */
               &md,                  /* message descriptor       */
               &gmo,                 /* get message options      */
               buflen,                   /* buffer length            */
               evtmsg,                   /* evtmsg message buffer    */
               &evtmsglen,           /* message length           */
               &CompCode,            /* completion code          */
               &Reason);             /* reason code              */
 
         /************************************************************/
         /* report reason, if any                                    */
         /************************************************************/
         if (Reason != MQRC_NONE)
         {
           printf("MQGET ==> %ld\n", Reason);
         }
       }
       else
       {
         CompCode = MQCC_FAILED;
       }
     }

     /****************************************************************/
     /* . . . process each message received                          */
     /****************************************************************/

     if (CompCode != MQCC_FAILED)
     {
       /**************************************************************/
       /* announce a message                                         */
       /**************************************************************/
       printf("\a\a\a\a\a\a\a");
       time(&ltime);
       printf(ctime(&ltime));
 
       if (evtmsglen != buflen)
         printf("DataLength = %ld?\n", evtmsglen);
       else
       {
         /************************************************************/
         /* right let's look at the data                             */
         /************************************************************/
         if (evtmsg->Type != MQCFT_EVENT)
         {
           printf("Something's wrong this isn't an event message,"
                  " its type is %ld\n",evtmsg->Type);
         }
         else
         {
           if (evtmsg->Command == MQCMD_Q_MGR_EVENT)
           {
             printf("Queue Manager event: ");
           }
           else
             if (evtmsg->Command == MQCMD_CHANNEL_EVENT)
             {
               printf("Channel event: ");
             }
             else
        
  ·
  ·
  ·

             {
               printf("Unknown Event message, %ld.",
                       evtmsg->Command);
             }
 
           if      (evtmsg->CompCode == MQCC_OK)
             printf("CompCode(OK)\n");
           else if (evtmsg->CompCode == MQCC_WARNING)
             printf("CompCode(WARNING)\n");
           else if (evtmsg->CompCode == MQCC_FAILED)
             printf("CompCode(FAILED)\n");
           else
             printf("* CompCode wrong * (%ld)\n",
                       evtmsg->CompCode);
 
           if (evtmsg->StrucLength != MQCFH_STRUC_LENGTH)
           {
             printf("it's the wrong length, %ld\n",evtmsg->StrucLength);
           }
 
           if (evtmsg->V!= MQCFH_VERSION_1)
           {
             printf("it's the wrong version, %ld\n",evtmsg->Version);
           }
 
           if (evtmsg->MsgSeqNumber != 1)
           {
             printf("it's the wrong sequence number, %ld\n",
                     evtmsg->MsgSeqNumber);
           }
 
           if (evtmsg->Control != MQCFC_LAST)
           {
             printf("it's the wrong control option, %ld\n",
                     evtmsg->Control);
           }
 
           printreas(evtmsg->Reason);
           printf("parameter count is %ld\n", evtmsg->ParameterCount);
           /**********************************************************/
           /* get a pointer to the start of the parameters           */
           /**********************************************************/
 

           paras = (MQCFST *)(evtmsg + 1);
           counter = 1;
           while (counter <= evtmsg->ParameterCount)
           {
             switch (paras->Type)
             {
               case MQCFT_STRING:
                 printfmqcfst(paras);
                 paras = (MQCFST *)((char *)paras
                                      + paras->StrucLength);
                 break;
               case MQCFT_INTEGER:
                 printfmqcfin((MQCFIN*)paras);
                 paras = (MQCFST *)((char *)paras
                                      + paras->StrucLength);
                 break;
               default:
                 printf("unknown parameter type, %ld\n",
                        paras->Type);
                 counter = evtmsg->ParameterCount;
                 break;
             }
             counter++;
           }
         }
       }   /* end evtmsg action             */
       free(evtmsg);
       evtmsg = NULL;
     }     /* end process for successful GET */
   }       /* end message processing loop    */
 
   /******************************************************************/
   /* close the event queue - if it was opened                       */
   /******************************************************************/
   if (OpenCode != MQCC_FAILED)
   {
     C_options = 0;                  /* no close options             */
     MQCLOSE(Hcon,                   /* connection handle            */
            &Hobj,               /* object handle                */
            C_options,
            &CompCode,           /* completion code              */
            &Reason);            /* reason code                  */
   /******************************************************************/
   /* Disconnect from queue manager (unless previously connected)    */
   /******************************************************************/
   if (CReason != MQRC_ALREADY_CONNECTED)
   {
     MQDISC(&Hcon,               /* connection handle            */
            &CompCode,           /* completion code              */
            &Reason);            /* reason code                  */
 

 /********************************************************************/
 /*                                                                  */
 /* END OF EVMON                                                     */
 /*                                                                  */
 /********************************************************************/
 }
 
#define PRINTPARAM(param)                                          \
   case param:                                                     \
     {                                                             \
       char *p = #param;                                           \
     strncpy(thestring,pmqcfst->String,min(sizeof(thestring),      \
             pmqcfst->StringLength));                              \
     printf("%s %s\n",p,thestring);                                \
     }                                                             \
     break;
 
#define PRINTAT(param)                                             \
   case param:                                                     \
     printf("MQIA_APPL_TYPE = %s\n",#param);                       \
     break;
 
 
void printfmqcfst(MQCFST* pmqcfst)
{
  char thestring[100];
 
  switch (pmqcfst->Parameter)
  {
    PRINTPARAM(MQCA_BASE_Q_NAME)
    PRINTPARAM(MQCA_PROCESS_NAME)
    PRINTPARAM(MQCA_Q_MGR_NAME)
    PRINTPARAM(MQCA_Q_NAME)
    PRINTPARAM(MQCA_XMIT_Q_NAME)
    PRINTPARAM(MQCACF_APPL_NAME)
    
  ·
  ·
  ·
default: printf("Invalid parameter, %ld\n",pmqcfst->Parameter); break; } }

 
void printfmqcfin(MQCFIN* pmqcfst)
{
  switch (pmqcfst->Parameter)
  {
    case MQIA_APPL_TYPE:
      switch (pmqcfst->Value)
      {
        PRINTAT(MQAT_UNKNOWN)
        PRINTAT(MQAT_OS2)
        PRINTAT(MQAT_DOS)
        PRINTAT(MQAT_UNIX)
        PRINTAT(MQAT_QMGR)
        PRINTAT(MQAT_OS400)
        PRINTAT(MQAT_WINDOWS)
        PRINTAT(MQAT_CICS_VSE)
        PRINTAT(MQAT_VMS)
        PRINTAT(MQAT_GUARDIAN)
        PRINTAT(MQAT_VOS)
      }
      break;
    case MQIA_Q_TYPE:
      if (pmqcfst->Value == MQQT_ALIAS)
      {
        printf("MQIA_Q_TYPE is MQQT_ALIAS\n");
      }
      else
      
  ·
  ·
  ·
{ if (pmqcfst->Value == MQQT_REMOTE) { printf("MQIA_Q_TYPE is MQQT_REMOTE\n"); if (evtmsg->Reason == MQRC_ALIAS_BASE_Q_TYPE_ERROR) { printf("but remote is not valid here\n"); } } else { printf("MQIA_Q_TYPE is wrong, %ld\n",pmqcfst->Value); } } break;

          case MQIACF_REASON_QUALIFIER:
      printf("MQIACF_REASON_QUALIFIER %ld\n",pmqcfst->Value);
      break;
 
    case MQIACF_ERROR_IDENTIFIER:
      printf("MQIACF_ERROR_INDENTIFIER %ld (X'%lX')\n",
              pmqcfst->Value,pmqcfst->Value);
      break;
 
    case MQIACF_AUX_ERROR_DATA_INT_1:
      printf("MQIACF_AUX_ERROR_DATA_INT_1 %ld (X'%lX')\n",
              pmqcfst->Value,pmqcfst->Value);
      break;
 
    case MQIACF_AUX_ERROR_DATA_INT_2:
      printf("MQIACF_AUX_ERROR_DATA_INT_2 %ld (X'%lX')\n",
              pmqcfst->Value,pmqcfst->Value);
      break;

  ·
  ·
  ·
default : printf("Invalid parameter, %ld\n",pmqcfst->Parameter); break; } } void printreas(MQLONG reason) { switch (reason) { PRINTREAS(MQRCCF_CFH_TYPE_ERROR) PRINTREAS(MQRCCF_CFH_LENGTH_ERROR) PRINTREAS(MQRCCF_CFH_VERSION_ERROR) PRINTREAS(MQRCCF_CFH_MSG_SEQ_NUMBER_ERR)
  ·
  ·
  ·
PRINTREAS(MQRC_NO_MSG_LOCKED) PRINTREAS(MQRC_CONNECTION_NOT_AUTHORIZED) PRINTREAS(MQRC_MSG_TOO_BIG_FOR_CHANNEL) PRINTREAS(MQRC_CALL_IN_PROGRESS) default: printf("It's an unknown reason, %ld\n", reason); break; } }