/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* perf720 src/perf/perfagent/usr/samples/perfagent/server/iphosts.c 1.1  */
/*                                                                        */
/*                                                                        */
/*                                                                        */
/* OBJECT CODE ONLY SOURCE MATERIALS                                      */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 1996                   */
/* All Rights Reserved                                                    */
/*                                                                        */
/* The source code for this program is not published or otherwise         */
/* divested of its trade secrets, irrespective of what has been           */
/* deposited with the U.S. Copyright Office.                              */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
static char *Sccs_id = "@(#)70  1.1  src/perf/perfagent/usr/samples/perfagent/server/iphosts.c, perfagent, perf720 12/3/96 06:49:45";
/*
 *   COMPONENT_NAME: PERFAGENT
 *
 *   FUNCTIONS: findstats
 *		lststats
 *		main
 *
 *   ORIGINS: 30
 *
 *
 *   (C) COPYRIGHT International Business Machines Corp. 1992,1996
 *   All Rights Reserved
 *   Licensed Materials - Property of IBM
 *   US Government Users Restricted Rights - Use, duplication or
 *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 *       NOTICE TO USERS OF THE SOURCE CODE EXAMPLES
 *
 * THE SOURCE CODE EXAMPLES PROVIDED BY IBM ARE ONLY INTENDED TO ASSIST IN THE
 * DEVELOPMENT OF A WORKING SOFTWARE PROGRAM.  THE SOURCE CODE EXAMPLES DO NOT
 * FUNCTION AS WRITTEN:  ADDITIONAL CODE IS REQUIRED.  IN ADDITION, THE SOURCE
 * CODE EXAMPLES MAY NOT COMPILE AND/OR BIND SUCCESSFULLY AS WRITTEN.
 * 
 * INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THE SOURCE CODE
 * EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS, "AS IS" WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
 * OF THE SOURCE CODE EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS,
 * IS WITH YOU.  SHOULD ANY PART OF THE SOURCE CODE EXAMPLES PROVE
 * DEFECTIVE, YOU (AND NOT IBM OR AN AUTHORIZED RISC System/6000* WORKSTATION
 * DEALER) ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING, REPAIR OR
 * CORRECTION.
 * 
 * IBM does not warrant that the contents of the source code examples, whether
 * individually or as one or more groups, will meet your requirements or that
 * the source code examples are error-free.
 * 
 * IBM may make improvements and/or changes in the source code examples at
 * any time.
 * 
 * Changes may be made periodically to the information in the source code
 * examples; these changes may be reported, for the sample device drivers
 * included herein, in new editions of the examples.
 * 
 * References in the source code examples to IBM products, programs, or
 * services do not imply that IBM intends to make these available in all
 * countries in which IBM operates.  Any reference to an IBM licensed
 * program in the source code examples is not intended to state or imply
 * that only IBM's licensed program may be used.  Any functionally equivalent
 * program may be used.
 * 
 * RISC System/6000 is a trademark of International Business Machines
 * Corporation.
 *
 */

/* EXECUTION NOTES:  
 * iphosts is a sample program.  The program has the purpose of adding IP
 * response time contexts to the Spmi data pool, based upon three sources:
 *
 * 1. A list of hostnames in a file (command line argument -f).  Each host
 *    must be specified on a separate line, beginning in column one.
 * 2. A list of hostnames given on tha command line (-h argument).  If more
 *    than one host is given, the list must be enclosed in double quotes.
 * 3. A list of hostnames as obtained by issuing RSiInvite (-i argument).
 *
 * No matter how the list of hosts is obtained, the iphosts program will
 * issue an SpmiPathGetCx call for each host to cause an IP response time
 * context to be generated for that host.
 *
 * The iphosts command line syntax is:
 *
 *     iphosts [-i] [-f filename] [-h "list of hostnames"]
 *
 */

#include <sys/types.h>
#include <sys/errno.h>
#include <stdio.h>
#include <sys/Spmidef.h>

extern char *optarg;
extern char SpmiErrmsg[];     /* Error Msg array */
extern int  SpmiErrno;        /* Error Number */
extern char RSiEMsg[];        /* Error Msg array */
extern int  RSiInvTabActive;

#ifdef _NO_PROTO
void addit(navn)
char *navn;
#else
void addit(char *navn)
#endif
{
   char path[256] = "RTime/LAN/";

   strcat(path, navn);
   printf("Now creating IP response time context %s\n", path);
   SpmiPathGetCx(path, NULL);
}

/*================================  main()  ============================*/

#ifdef _NO_PROTO
main(argc, argv)
int argc;
char **argv;
#else
main(int argc, char **argv)
#endif
{
   int spmierr = 0;
   int errflag = 0;
   int c;
   int i;
   char *s;
   char *filename = NULL;
   boolean invite = FALSE;
   char *hostlist = NULL;
   char **hostnames = NULL;
   char l[256];
   FILE *fp;

   while ((c = getopt(argc, argv, "?if:h:")) != EOF)
   {
      switch (c)
      {
         case 'i':
                  invite = TRUE;
                  break;
         case 'f':
                  filename = (char *)strdup(optarg);
						if (!strlen(filename))
							errflag++;
                  break;
         case 'h':
                  hostlist = (char *)strdup(optarg);
						if (!strlen(hostlist))
							errflag++;
                  break;
         case '?':
         default:
                  errflag++;
                  break;
      }
   }

   if ((errflag) || ((!invite) && (!hostlist) && (!filename)))
   {
      printf("Usage:\tiphosts [-i] [-f filename] [-h \"list of hostnames\"]\n");
      printf("\tFlags:\t-i get hostnames by inviting data suppliers.\n");
      printf("\tFlags:\t-f get hostnames from specified file.\n");
      printf("\tFlags:\t-h get hostnames from command line.\n");
	   exit(0);
   }

   /* Initialize SPMI interface */
   if ((spmierr = SpmiInit(15)) != 0)
   {
      printf("ERROR:  SpmiInit Failed\n");
      if (strlen(SpmiErrmsg))
         printf("%s", SpmiErrmsg);
      exit(-98);
   }

   /*
       If we were asked to invite through the RSi, do so here.
   */
	if (invite)
   {
      if (!(hostnames = (char **)RSiInvite(NULL, NULL)))
      {
         if (strlen(RSiEMsg))
            fprintf(stderr, "%s", RSiEMsg);
         fprintf(stderr, "No hosts responded to invitation\n");
         exit(-97);
      }
      for (i = 0; i < RSiInvTabActive; i++)
			addit(hostnames[i]);
   }

   /*
      Add any host from command line.
   */
   if ((s = (char *)strtok(hostlist, " \t\n")))
   {
		addit(s);
      while ((s = (char *)strtok(NULL, " \t\n")))
		   addit(s);
   }

   /*
      Add any host from a file, if requested
   */
   if (filename)
   {
      if (!(fp = fopen(filename, "r")))
      {
         perror("open");
         exit(-96);
      }
      while (fgets(l, 80, fp))
      {
         if ((l[0] == '#') || (l[0] <= ' '))
            continue;
         if ((s = (char *)strtok(l, " \t\n")))
		      addit(s);
      }
      fclose(fp);
   }
		
	/* Exit SPMI Interface */
	SpmiExit();
	exit(0);
}
