File: //opt/OV/OpC/examples/copcagtapi/opcapitest.c
/* -*-C-*-
********************************************************************************
*
* File: opcapitest.c
* RCS: $Header: opcapitest.c,v 1.1 94/09/12 20:05:06 hmgr Exp $
* Description: This file contains a sample program using the VPO Agent API
* to send messages and monitor values.
* Author: Thomas Gentsch, NSMD R&D
* Created: Fri Aug 06 14:07:56 1993
* Modified: 13-Jul-1998 Peter Stoldt ([email protected])
* Language: K&R C
* Package: HP OpenView VantagePoint Operations
* Status: Example
*
* (c) Copyright 1993-98, Hewlett-Packard Company, all rights reserved.
*
********************************************************************************
*/
#include <stdio.h>
#include <opcapi.h>
/*
*----------------------------------------------------------------------------
* Function: report_error
* ----------------------
*
* Description:
* Maps an VPO error code to an error text and prints this text to
* stdout.
*
* Parameters:
* err_code [in] VPO error code.
* add_msg [in] Additional information
*
* Return values:
* none.
*
*----------------------------------------------------------------------------
*/
static void report_error (add_msg, err_code)
char * add_msg;
int err_code;
{
/* print the error message to stdout */
printf ("%s returned: %s (%d)\n\n", add_msg,
opcdata_report_error (err_code), err_code);
return;
}
/*
*----------------------------------------------------------------------------
* End of function report_error
*----------------------------------------------------------------------------
*/
/*
*----------------------------------------------------------------------------
* Function: main
* --------------
*
* Description:
* Main routine of the apitest program. A menu is printed and the user
* can enter whether to send an VPO message or a monitor value. The
* user is prompted to enter needed parameters.
*
* Input Parameters:
* argc, argv Default main() parameters. Unused.
*
* Output Parameters:
* none.
*
* Errors:
* none.
*
* Return values:
* 0 always.
*
*----------------------------------------------------------------------------
*/
main (argc, argv)
int argc;
char **argv;
{
int choice;
char c_buf[10];
char msggrp[256];
char appl[256];
char object[256];
char obj[256];
char text[256];
char severity[256];
char node[256];
char floatval[256];
char datainfo[256];
char optvar[256];
char mid[256];
char svname[256];
char *msggrppt;
char *applpt;
char *objectpt;
char *objpt;
char *textpt;
char *nodept;
char *optvarpt;
char *id;
char *svnamept;
int sev;
long data;
//int data;
int rc;
double val;
opcdata msg;
opcdata mon;
opcdata msg_id;
/* Do an infinite loop and request the user to enter some values. */
/*----------------------------------------------------------------*/
for (;;)
{
/* Print a menu ... */
/*------------------*/
printf ("\n");
printf (" Agent Message API\n");
printf (" -----------------\n");
printf (" 1 ..... Send message opcagtmsg_send()\n");
printf (" 2 ..... Acknowledge message opcagtmsg_ack()\n");
printf (" 3 ..... Send monitor value opcagtmon_send()\n\n");
printf (" Old Message API\n");
printf (" ---------------\n");
printf (" 4 ..... Send message opcmsg()\n");
printf (" 5 ..... Send monitor value opcmon()\n\n");
printf (" 0 ..... Exit\n\n");
printf (" > ");
/* ... and get the response. */
/*---------------------------*/
fflush (stdin);
if (fgets (c_buf, 3, stdin) == NULL)
exit (0);
/* Try to translate the input into numeric value. */
/*------------------------------------------------*/
if (sscanf (c_buf, "%d", &choice) < 1)
{
fprintf (stderr, "Invalid choice. Ignored\n");
continue;
}
/* Let the user enter needed values. */
/*-----------------------------------*/
switch (choice)
{
/* Exit. */
/*-------*/
case 0:
printf ("Bye.\n");
exit (0);
/* Send and VPO message with opcagtmsg_send() */
/*--------------------------------------------*/
case 1:
printf ("Please enter message attributes.\n");
printf ("Application: "); applpt = gets (appl);
clearerr (stdin);
printf ("Object: "); objectpt = gets (object);
clearerr (stdin);
printf ("Text: "); textpt = gets (text);
clearerr (stdin);
printf ("Msg group: "); msggrppt = gets (msggrp);
clearerr (stdin);
printf ("Severity: "); gets (severity);
clearerr (stdin);
printf ("Node: "); nodept = gets (node);
clearerr (stdin);
printf ("Optional Variables: "); optvarpt = gets (optvar);
clearerr (stdin);
printf ("Data Info (REMARK_FOR_ACK = 256): "); gets (datainfo);
clearerr (stdin);
printf ("Service Name: "); svnamept = gets (svname);
clearerr (stdin);
/* Translate data info into number */
/*---------------------------------*/
sscanf (datainfo, "%ld", &data);
/* Translate severity into numeric value. */
/*----------------------------------------*/
if (!strcmp (severity, "normal"))
sev = OPC_SEV_NORMAL;
else if (!strcmp (severity, "warning"))
sev = OPC_SEV_WARNING;
else if (!strcmp (severity, "minor"))
sev = OPC_SEV_MINOR;
else if (!strcmp (severity, "major"))
sev = OPC_SEV_MAJOR;
else if (!strcmp (severity, "critical"))
sev = OPC_SEV_CRITICAL;
else
sev = OPC_SEV_UNKNOWN;
/* Create the message information */
/*--------------------------------*/
if ( (rc = opcdata_create (OPCDTYPE_MESSAGE, &msg)) != OPC_ERR_OK)
{
fprintf (stderr, "Creating the opcdata structure failed\n");
report_error ("opcdata_create()", rc);
}
/* Filling in the message data */
/*-----------------------------*/
if ( (rc = opcdata_set_str (msg, OPCDATA_APPLICATION, applpt)) !=
OPC_ERR_OK)
{
fprintf (stderr, "Setting the application failed\n");
report_error ("opcdata_set_str()", rc);
}
if ( (rc = opcdata_set_str (msg, OPCDATA_OBJECT, objectpt)) != OPC_ERR_OK)
{
fprintf (stderr, "Setting the object failed\n");
report_error ("opcdata_set_str()", rc);
}
if ( (rc = opcdata_set_str (msg, OPCDATA_MSGTEXT, textpt)) != OPC_ERR_OK)
{
fprintf (stderr, "Setting the message text failed\n");
report_error ("opcdata_set_str()", rc);
}
if ( (rc = opcdata_set_str (msg, OPCDATA_GROUP, msggrppt)) != OPC_ERR_OK)
{
fprintf (stderr, "Setting the message group failed\n");
report_error ("opcdata_set_str", rc);
}
if ( (rc = opcdata_set_str (msg, OPCDATA_NODENAME, nodept)) != OPC_ERR_OK)
{
fprintf (stderr, "Setting the node name failed\n");
report_error ("opcdata_set_str()", rc);
}
if ( (rc = opcdata_set_str (msg, OPCDATA_OPTION_VAR, optvarpt)) !=
OPC_ERR_OK)
{
fprintf (stderr, "Setting the option var failed\n");
report_error ("opcdata_set_str()", rc);
}
if ( (rc = opcdata_set_str (msg, OPCDATA_SERVICE_NAME, svnamept)) !=
OPC_ERR_OK)
{
fprintf (stderr, "Setting the service name failed\n");
report_error ("opcdata_set_str()", rc);
}
if ( (rc = opcdata_set_long (msg, OPCDATA_SEVERITY, sev)) != OPC_ERR_OK)
{
fprintf (stderr, "Setting the severity failed\n");
report_error ("opcdata_set_long()", rc);
}
if ( (rc = opcdata_set_long (msg, OPCDATA_DATA_INFO, data)) !=
OPC_ERR_OK)
{
fprintf (stderr, "Setting the data info failed\n");
report_error ("opcdata_set_long()", rc);
}
/* Send the message. */
/*-------------------*/
rc = opcagtmsg_send (msg);
/* Print an error if necessary. */
/*------------------------------*/
if (rc != OPC_ERR_OK)
{
fprintf (stderr, "Sending VPO message failed");
}
report_error ("\nopcagtmsg_send()", rc);
if ( rc == OPC_ERR_OK )
{
/* retrieve UUID for message */
/*---------------------------*/
id = opcdata_get_str (msg, OPCDATA_MSGID);
if (id != NULL)
{
printf ("Sent VPO messsage with ID: %s\n", id);
}
else
{
fprintf (stderr, "Could not retrieve message ID!\n");
}
}
/* free the memory */
/*-----------------*/
opcdata_free (&msg);
break;
/* Acknowledge a message with opcagtmsg_ack() */
/*--------------------------------------------*/
case 2:
printf ("Please enter message ID.\n");
printf ("ID: ");
gets (mid);
clearerr (stdin);
/* Create the message-ID information */
/*-----------------------------------*/
if ( (rc = opcdata_create (OPCDTYPE_MESSAGE_ID, &msg_id)) != OPC_ERR_OK )
{
fprintf (stderr, "Creating the opcdata structure failed\n");
report_error ("opcdata_create()", rc);
}
/* Filling in the message data */
/*-----------------------------*/
if ( (rc = opcdata_set_str (msg_id, OPCDATA_MSGID, mid)) != OPC_ERR_OK )
{
fprintf (stderr, "Setting the Message-ID failed\n");
report_error ("opcdata_set_str()", rc);
}
/* Acknowledge the message. */
/*--------------------------*/
rc = opcagtmsg_ack (msg_id);
/* Print an error if necessary. */
/*------------------------------*/
if ( rc != OPC_ERR_OK )
{
fprintf (stderr, "Acknowledging VPO message failed");
}
report_error ("\nopcagtmsg_ack()", rc);
/* free the memory */
/*-----------------*/
opcdata_free (&msg_id);
break;
/* Send and VPO monitor value with opcagtmon_send() */
/*--------------------------------------------------*/
case 3:
printf ("Object: "); objectpt = gets (object);
clearerr (stdin);
printf ("Value: "); gets (floatval);
clearerr (stdin);
printf ("Message object: "); objpt = gets (obj);
clearerr (stdin);
printf ("Optional variables: "); optvarpt = gets (optvar);
clearerr (stdin);
/* Convert float value */
/*---------------------*/
sscanf (floatval, "%lf", &val);
/* Create the message information */
/*--------------------------------*/
if ( (rc = opcdata_create (OPCDTYPE_MONITOR_MESSAGE, &mon)) != OPC_ERR_OK)
{
fprintf (stderr, "Creating the opcdata structure failed\n");
report_error ("opcdata_create()", rc);
}
if ( (rc = opcdata_set_str (mon, OPCDATA_MON_VAR, objectpt)) !=
OPC_ERR_OK)
{
fprintf (stderr, "Setting the monitored object name failed\n");
report_error ("opcdata_set_str()", rc);
}
if ( (rc = opcdata_set_str (mon, OPCDATA_OBJECT, objpt)) !=
OPC_ERR_OK)
{
fprintf (stderr, "Setting the message object failed\n");
report_error ("opcdata_set_str()", rc);
}
if ( (rc = opcdata_set_str (mon, OPCDATA_OPTION_VAR, optvarpt)) !=
OPC_ERR_OK)
{
fprintf (stderr, "Setting the option variables failed\n");
report_error ("opcdata_set_str()", rc);
}
if ( (rc = opcdata_set_double (mon, OPCDATA_MON_VALUE, val)) !=
OPC_ERR_OK)
{
fprintf (stderr, "Setting the monitor value failed\n");
report_error ("opcdata_set_double()", rc);
}
/* Send the message. */
/*-------------------*/
rc = opcagtmon_send (mon);
if (rc != OPC_ERR_OK)
{
fprintf (stderr, "\nSending VPO monitor value failed");
}
report_error ("\nopcagtmon_send()", rc);
/* free up the memory */
/*--------------------*/
opcdata_free (&mon);
break;
/* Send an VPO message with opcmsg() (Old) */
/*-----------------------------------------*/
case 4:
printf ("Please enter message attributes.\n");
printf ("Application: "); applpt = gets (appl);
clearerr (stdin);
printf ("Object: "); objectpt = gets (object);
clearerr (stdin);
printf ("Text: "); textpt = gets (text);
clearerr (stdin);
printf ("Msg group: "); msggrppt = gets (msggrp);
clearerr (stdin);
printf ("Severity: "); gets (severity);
clearerr (stdin);
printf ("Node: "); nodept = gets (node);
/* Translate severity into numeric value. */
/*----------------------------------------*/
if (!strcmp (severity, "normal"))
sev = OPC_SEV_NORMAL;
else if (!strcmp (severity, "warning"))
sev = OPC_SEV_WARNING;
else if (!strcmp (severity, "minor"))
sev = OPC_SEV_MINOR;
else if (!strcmp (severity, "major"))
sev = OPC_SEV_MAJOR;
else if (!strcmp (severity, "critical"))
sev = OPC_SEV_CRITICAL;
else
sev = OPC_SEV_UNKNOWN;
/* Send the message. */
/*-------------------*/
rc = opcmsg (sev, applpt, objectpt, textpt, msggrppt, nodept);
/* Print an error if necessary. */
/*------------------------------*/
if (rc != OPC_ERR_OK)
{
fprintf (stderr, "\nSending VPO message failed");
}
report_error ("\nopcmsg()", rc);
break;
/* Send an VPO monitor value with opcmon() (Old). */
/*------------------------------------------------*/
case 5:
printf ("Object: "); objectpt = gets (object);
clearerr (stdin);
printf ("Value: "); gets (floatval);
clearerr (stdin);
sscanf (floatval, "%lf", &val);
rc = opcmon (objectpt, val);
if (rc != OPC_ERR_OK)
{
fprintf (stderr, "\nSending VPO monitor value failed");
}
report_error ("\nopcmon()", rc);
break;
/* Any other. */
/*------------*/
default:
fprintf (stderr, "Invalid choice. Ignored\n");
continue;
} /* switch */
} /* for */
return 0;
}
/*
*----------------------------------------------------------------------------
* End of function main
*----------------------------------------------------------------------------
*/
/*
********************************************************************************
* end of opcapitest.c
********************************************************************************
*/