C exit program to send message to QSYSOPR
This Query Supervisor exit program sends an informational message to the QSYSOPR message queue.
To compile this C program, use the following commands. By using USRPRF(*OWNER), *PUBLIC does not need to be given access to the objects used by this program. The owning profile of the created program must have authority to the commands and objects used by the exit program.
CRTCMOD MODULE(SUPERVISOR/SND_QS_MSG) SRCFILE(SUPERVISOR/QCSRC)
CRTPGM PGM(SUPERVISOR/SND_QS_MSG) USRPRF(*OWNER) ACTGRP(*CALLER)
The program can be registered as an exit program using this
command:
ADDEXITPGM EXITPNT(QIBM_QQQ_QRY_SUPER) FORMAT(QRYS0100) PGMNBR(*LOW) PGM(SUPERVISOR/SND_QS_MSG)
THDSAFE(*YES) TEXT('Query Supervisor send QSYSOPR message')
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <iconv.h>
#include <stdio.h>
#include <eqqqrysv.h>
static void convertThresholdNameToJobCCSID(const char* input, char* output)
{
iconv_t converter;
char from_code[32], to_code[32];
size_t input_bytes, output_bytes;
int iconv_rc;
memset(from_code, 0, sizeof(from_code));
memset(to_code, 0, sizeof(to_code));
memcpy(from_code, "IBMCCSID012000000000", 20);
memcpy(to_code, "IBMCCSID00000", 13);
converter = iconv_open(to_code, from_code);
if (converter.return_value == 0) {
input_bytes = 60;
output_bytes = 30;
iconv_rc = iconv(converter,
&input, &input_bytes,
&output, &output_bytes);
iconv_close(converter);
if (iconv_rc >= 0)
return; /* Conversion was successful. */
}
sprintf(output, "iconv_open() failed with: %d", converter.return_value);
}
int trimmed_length(const char* str, int len)
{
const char* first_blank = memchr(str, ' ', len);
if (first_blank)
return first_blank - str;
return len;
}
/************************************************************************/
int main(int argc, char* argv[])
{
char length_string[10];
char cmd[600];
char thresholdNameInJobCCSID[30];
char msg[512];
const QQQ_QRYSV_QRYS0100_t* input = (QQQ_QRYSV_QRYS0100_t*)argv[1];
int* rc = (int*)argv[2];
*rc = 0; /* don't terminate the query */
memset(thresholdNameInJobCCSID, 0, sizeof(thresholdNameInJobCCSID));
convertThresholdNameToJobCCSID(input->Threshold_Name,
thresholdNameInJobCCSID);
memset(msg, 0, sizeof(msg));
strcat(msg,"QUERY SUPERVISOR THRESHOLD ");
strncat(msg, thresholdNameInJobCCSID, 30);
strcat(msg," REACHED IN JOB ");
strncat(msg, input->Job_Number, trimmed_length(input->Job_Number,6));
strcat(msg, "/");
strncat(msg, input->Job_User, trimmed_length(input->Job_User,10));
strcat(msg, "/");
strncat(msg, input->Job_Name, trimmed_length(input->Job_Name,10));
memset(length_string, 0, sizeof(length_string));
sprintf(length_string,"%d",strlen(msg));
/************************************************************************/
/* Use the SQL service send_message to send the message to QSYSOPR. */
/* SQL cannot be run within the exit program, so submit to batch. */
/************************************************************************/
memset(cmd, 0, sizeof(cmd));
strcat(cmd, "SBMJOB CMD(RUNSQL SQL('call qsys2.send_message(''SQL7064'',");
strcat(cmd,length_string);
strcat(cmd,",''");
strcat(cmd, msg);
strcat(cmd, "'')'))");
system(cmd);
}