/*******************************************************************************
**
** Source File Name = monsz.c
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
** PURPOSE : to demonstrate the use of DATABASE MONITOR APIs.
** The following code requests for the buffer size that would be required
** for issuing a snapshot for locks, tables, and database level information.
**
** OTHER APIs USED :
**          INSTANCE ATTACH               sqleatin()
**
** EXTERNAL FUNCTIONS USED IN CODE SAMPLE:
**          check_error :     Checks for SQLCODE error, and prints out any
**          [in UTIL.C]          related information available.
**
**    EXTERNAL DEPENDENCIES :
**       - Ensure existence of database for precompile purposes.
**       - Compile and link with the IBM Cset++ compiler (AIX and OS/2)
**         or the Microsoft Visual C++ compiler (Windows) 
**         or the compiler supported on your platform.
**
** For more information about these samples see the README file.
**
** For more information on programming in C, see the:
**   -  "Programming in C and C++" section of the Application Development Guide
** For more information on Building C Applications, see the:
**   -  "Building C Applications" section of the Application Building Guide.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/
 /******************************************************************************
    Database Monitor - Estimate Buffer Size for Snapshot

#include "sqlca.h"
#include "sqlmon.h"
#include "utilapi.h"

int SQL_API_FN sqlmonsz(sqluint32         version,
                        char                  *reserved,
                        sqlma                 *sqlma_ptr,
                        sqluint32         *buffer_sz,
                        struct sqlca          *sqlca
                       );

Certain snapshot requests, for instance a snapshot of the locks for a database,
return a variable amount of data.  This routine *estimates* the buffer size
that will be required for receiving snapshot data at a given point in time
and for a specific snapshot request.

Usage notes:

   It is not necessary to call this
   function for snapshot requests that return a fixed amount of data, such
   as an SQLMA_DBASE request, for which sqlmonss() returns a single
   sqlm_dbase structure in the user output buffer.

   The size returned by sqlmonsz() is always a little
   larger than what is actually required.

   The snapshot function, 'sqlmonss()' will truncate
   the data it returns if the buffer provided is too small, for which case it
   returns an sqlcode of SQLM_RC_BUFFER_FULL.

   A possible buffer allocation strategy for taking snapshot
   is to allocate a buffer of nominal size (e.g., 2K), possibly from
   the stack, and keep reusing the same buffer.
   If and when, SQLM_RC_BUFFER_FULL is returned, then one can call
   sqlmonsz() and allocate a new, larger buffer.

INPUT:

version: SQLM_DBMON_VERSION1    - snapshot will be for data that
                                  is available from a DB2 version 1 system
         SQLM_DBMON_VERSION2    - snapshot will be for Version 2 data

sqlma:

   The 'sqlma' structure specifies the data that the user intends to take a
   snapshot of.

   This structure contains an array of sqlm_obj_struct, where each one
   is a request for Monitored data.

   See sqlmonss for a complete description.

OUTPUT:

buffer_sz: The size of the buffer required for receiving data given the
   snapshot requests specified in the sqlma.


*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlca.h"
#include "sqlutil.h"
#include "sqlmon.h"
#include "utilapi.h"


int main (int argc, char* argv[]) {
   char     *dbname = "SAMPLE";    /* database to be monitored */
   char     *buffer_ptr;           /* buffer for the SNAPSHOT */
   sqlm_collected collected;       /* info. structure for DB SYS. MON. APIs */
   char      userid[9];
   char      passwd[19];
   char      nodename[9];
   int       rc;
   struct    sqlca sqlca;
   sqluint32 buffer_sz;

   /* Request SQLMA_DBASE, SQLM_DBASE_TABLES, and SQLMA_DBASE_LOCKS in sqlma */
   struct sqlma* sqlma = (struct sqlma *) malloc(SQLMASIZE(3));
   sqlma->obj_num = 3;
   sqlma->obj_var[0].obj_type = SQLMA_DBASE;
   strcpy(sqlma->obj_var[0].object, dbname);
   sqlma->obj_var[1].obj_type = SQLMA_DBASE_LOCKS;
   strcpy(sqlma->obj_var[1].object, dbname);
   sqlma->obj_var[2].obj_type = SQLMA_DBASE_TABLES;
   strcpy(sqlma->obj_var[2].object, dbname);

   if (argc == 4) {
      strcpy (userid, argv[1]);
      strcpy (passwd, argv[2]);
      strcpy (nodename, argv[3]);

       
      /*************************/
      /* ATTACH API called     */
      /*************************/
      sqleatin (nodename, userid, passwd, &sqlca);
   }
   else if (argc != 1) {
      printf("\nUSAGE: monsz [userid passwd remote_nodename]\n");
      return 1;
   }

   /*********************************************************\
   * ESTIMATE DATABASE SYSTEM MONITOR BUFFER SIZE API called *
   \*********************************************************/
   sqlmonsz(SQLM_DBMON_VERSION2, NULL, sqlma, &buffer_sz, &sqlca);

   API_SQL_CHECK("ESTIMATE BUFFER SIZE");
   printf ("\nBuffer size required for this snapshot is: %d\n",buffer_sz);

   /* ...Take the Snapshot... */
   buffer_ptr = (char *) malloc(buffer_sz);     /* Allocate the buffer */
   /*********************************************\
   * DATABASE SYSTEM MONITOR SNAPSHOT API called *
   \*********************************************/
   rc = sqlmonss(SQLM_DBMON_VERSION2, NULL, sqlma, buffer_sz, buffer_ptr,
            &collected, &sqlca);

   /* ...Process snapshot output in buffer_ptr... */
   free (buffer_ptr);                           /* Free the buffer */

   return 0;
}
/* end of program : monsz.c */