home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / KmdOps / wstats.c < prev   
Encoding:
C/C++ Source or Header  |  1990-08-30  |  8.1 KB  |  321 lines

  1. /*  C O P Y R I G H T   N O T I C E :
  2.  * Copyright 1986 Eric Jul.  May not be used for any
  3.  * purpose without written permission from the author
  4.  */
  5.  
  6. /* File:        wstats.c     Eric Jul, 1986-06-03     */
  7.  
  8. /*
  9.  * $Header$
  10.  * INTERFACE:    See usage printout.
  11.  *
  12.  * FUNCTION:    Print Kernel statistics repeatedly using windows,
  13.  *              see curses(3X).
  14.  *
  15.  * IMPORTS:    loads, see #include
  16.  *
  17.  * EXPORTS:    Nothing.
  18.  *
  19.  * DESIGN:    Send start snapshot message to Emerald process and then
  20.  *              repeatedly receive and print data until stop snapshot
  21.  *              message arrives.
  22.  *              Then repeat after waiting t seconds.
  23.  *
  24.  * $Log$
  25.  * 1986-06-03   Modified to work with 4.2bsd sockets.
  26.  * 1985-01-06   Initial implementation, Eric Jul.
  27.  */
  28.  
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <stdio.h>
  33. #include <curses.h>
  34. #include <signal.h>
  35.  
  36.  
  37. #define mSUCCESS(X) (((X) & 7) == 1)
  38.  
  39.  
  40. #undef FALSE
  41. #undef TRUE
  42. #undef integer
  43.  
  44. typedef int KKStatus;
  45.  
  46. #define MAXMESSAGESIZE        1000
  47. #include "Kernel/h/emPorts.h"
  48. #include "Kernel/h/kmdDefs.h"
  49.  
  50. #include <netdb.h>
  51. #include <signal.h>
  52.  
  53. #define endcase break
  54.  
  55. char *snapshotname, *snapshotstring;
  56. int   talkSock, pid = 0, prodTime = 1, MType, MInt;
  57. char  MString[500];
  58.  
  59. struct sockaddr_in destSockDescr = {AF_INET};
  60.  
  61. /* Define signal handlers */
  62.  
  63. void PipeHandler(sig)
  64. int sig;
  65. {
  66.   printf("SIGPIPE>>> The connection to the process broke.\nTerminating.\n");
  67.   exit(1);
  68. }
  69.  
  70. void TerminationHandler(sig)
  71. int sig;
  72.   fprintf(stderr, "Terminating wstats.\n");
  73.   /* Important to tell kernel that we are bye, bye ... */
  74.   KMDSend(talkSock, KMDCLASTMSG, 0, "Done");
  75.   if (shutdown(talkSock, 1) < 0) perror("shutdown1");
  76.   while(mSUCCESS(KMDReceive(talkSock, &MType, &MInt, MString)));
  77.   close(talkSock);
  78.   printw("Terminating.\n");
  79.   endwin();
  80.   exit(sig);
  81. }
  82.  
  83.  
  84.  
  85. main(argc, argv)
  86. int   argc;
  87. char *argv[];
  88. {
  89.   int  OKtoGoOn;
  90.   int  MType, MInt;
  91.   char MString[500];
  92.   char snapbuf[500];
  93.  
  94.   static int        i, EPLNN, SValue;
  95.   static int        pidGiven = 0, machineNameGiven = 0;
  96.   KKStatus          kstat;
  97.   char              myHostName[100];
  98.   struct hostent   *myHost;
  99.   struct servent   *myService;
  100.   int               WaitTime;
  101.   long              theTime;
  102.  
  103.     snapshotname = "MMStats";        /* default */
  104.     snapshotstring = (char *) 0;
  105.  
  106.     SValue = 0;                      /* default */
  107.     EPLNN = 8;                       /* default */
  108.     WaitTime = 5;                    /* default */
  109.  
  110.         if (argc == 2) 
  111.             if (!strcmp(argv[1], "usage")) {
  112.             fprintf(stderr,
  113.             "Usage: wstats [-N <LNN>] [-M <Machine Name>] [-t <WaitInterval>] [-n <Snapshot>\n");
  114.                 TerminationHandler(0);
  115.             };
  116.  
  117.     /*
  118.      * Parse command line options
  119.      */
  120.     while (argc > 1 && argv[1][0] == '-') {
  121.         switch (argv[1][1]) {
  122.         case 'N':        /* Set logical node number */
  123.             if (argv[1][2] == '\0') {
  124.                 EPLNN = atoi(argv[2]);
  125.                 argc--;
  126.                 argv++;
  127.             } else
  128.                 EPLNN = atoi(&argv[1][2]);
  129.                         
  130.             printf("warning: -N ignored.\n");
  131.             break;
  132.         case 'X':        /* Set logical node number (hex) */
  133.                         if (argv[1][2] == '\0') {
  134.                                 sscanf(argv[2], "%x", &EPLNN);
  135.                                 argc--;
  136.                                 argv++;
  137.                         } else
  138.                                 sscanf(&argv[1][2], "%x", &EPLNN);
  139.             printf("warning: -X ignored.\n");
  140.             break;
  141.         case 'd':
  142.             if (argv[1][2] == '\0') {
  143.                 SValue = atoi(argv[2]);
  144.                 argc--;
  145.                 argv++;
  146.             } else
  147.                 SValue = atoi(&argv[1][2]);
  148.             break;
  149.         case 'P':       /* Set pid, cancel LNN usage. */
  150.             if (argv[1][2] == '\0') {
  151.                 pid = atoi(argv[2]);
  152.                 argc--;
  153.                 argv++;
  154.             } else
  155.                 pid = atoi(&argv[1][2]);
  156.                         pidGiven = 1;
  157.             printf("warning: -P ignored.\n");
  158.             break;
  159.                 case 'h':
  160.                         if (argv[1][2] == '\0') {
  161.                                 sscanf(argv[2], "%x", &SValue);
  162.                                 argc--;
  163.                                 argv++;
  164.                         } else
  165.                                 sscanf(&argv[1][2], "%x", &SValue);
  166.                         break;
  167.         case 't':
  168.             if (argv[1][2] == '\0') {
  169.                 WaitTime = atoi(argv[2]);
  170.                 argc--;
  171.                 argv++;
  172.             } else
  173.                 WaitTime = atoi(&argv[1][2]);
  174.             break;
  175.                 case 'n':
  176.                         snapshotname = argv[2];
  177.                         argc--;
  178.                         argv++;
  179.                         break;
  180.                 case 'S':
  181.                         snapshotstring = argv[2];
  182.                         argc--;
  183.                         argv++;
  184.                         break;
  185.                 case 'M':
  186.                 case 'm':
  187.                         strcpy(myHostName, argv[2]);
  188.                         argc--;
  189.                         argv++;
  190.             machineNameGiven ++;
  191.                         break;
  192.                 default:
  193.                         fprintf(stderr,
  194.                 "wstats: (warning) Unknown option: %c",
  195.                 argv[1][2]);
  196.                         break;
  197.  
  198.         }
  199.         argc--;
  200.         argv++;
  201.     }
  202.  
  203.     
  204.     /* Set termination handler. */
  205.     signal(SIGHUP, TerminationHandler);
  206.     signal(SIGQUIT, TerminationHandler);
  207.     signal(SIGINT, TerminationHandler);
  208.     signal(SIGBUS, TerminationHandler);
  209.     signal(SIGSEGV, TerminationHandler);
  210.     signal(SIGTERM, TerminationHandler);
  211.     signal(SIGPIPE, PipeHandler);
  212.     
  213.     talkSock = socket(AF_INET, SOCK_STREAM, 0, 0);
  214.     if (talkSock < 0) {
  215.                 perror("snapshot: socket");
  216.     }
  217.  
  218.     if (! machineNameGiven) {
  219.     if (gethostname(myHostName, 100) < 0) {
  220.         perror("snapshot: gethostname");
  221.                 TerminationHandler(0);
  222.     }
  223.     }
  224.     myHost = gethostbyname(myHostName);
  225.     if (myHost == NULL) {
  226.             perror("snapshot: gethostbyname");
  227.                 TerminationHandler(0);
  228.     }
  229.  
  230.     bcopy(myHost->h_addr, &destSockDescr.sin_addr, myHost->h_length);
  231.  
  232.     {
  233.       char *emplanename;
  234.       int emPlane = 0;
  235.       int emPort = EMKERNELDEFAULTPORTNUMBER;
  236.       struct servent   *myService;
  237.       emplanename = (char *) getenv("EMPLANE");
  238.       if (emplanename != NULL) emPlane = atoi(emplanename);
  239. #ifdef DIKU
  240.       myService = getservbyname(EMKERNELSERVICEPORT, "tcp");
  241.       if(myService)
  242.     emPort = ntohs(myService->s_port);
  243. #endif
  244.       destSockDescr.sin_port = htons(emPort + emPlane*4 + 1);
  245.     }
  246.  
  247.     if (connect(talkSock, &destSockDescr, sizeof(destSockDescr)) == -1) {
  248.                 perror("snapshot: connect");
  249.                 TerminationHandler(0);
  250.     }
  251.     if ( KMDTest )
  252.         printf("Connected to %d\n", ntohs(destSockDescr.sin_port));
  253.  
  254.     KMDReceive(talkSock, &MType, &MInt, MString);
  255.     printf("%s", MString);
  256.  
  257.     /* Initialize window. */
  258.     initscr();
  259.  
  260. Restartit:
  261.     /* Reset window. */
  262.     move(0, 0);
  263.     erase();
  264.     theTime = time(0);
  265.     standout();
  266.     printw("%s at %s\n", snapshotname, ctime(&theTime));
  267.     standend();
  268.     /* Start snapshot.*/
  269.     sprintf(snapbuf, "%s %s", snapshotname, snapshotstring);
  270.     kstat = KMDSend(talkSock, KMDCSNAPSHOT, SValue, snapbuf);
  271.  
  272.     if (! mSUCCESS( kstat ) ) {
  273.     endwin();
  274.         fprintf(stderr,
  275.         "wstats: Send snapshot msg failed, kstat = 0x%08x\n", kstat);
  276.                 TerminationHandler(0);
  277.     };
  278.  
  279.     OKtoGoOn = 1;
  280.     while (OKtoGoOn==1) {
  281.         if(!mSUCCESS(KMDReceive(talkSock, &MType, &MInt, MString))){
  282.         fprintf(stderr, "Premature snapshot termination\n");
  283.         perror("recv");
  284.         TerminationHandler(0);
  285.     };
  286.     
  287.         if ( KMDTest ) {
  288.             printf("Msg from: %d, (%d, %d, %s)\n", talkSock,
  289.                 MType, MInt, MString),
  290.             fflush(stdout);
  291.     }
  292.         
  293.         switch ( MType ) {
  294.         
  295.         case KMDCDATA:
  296.             printw("%s", MString);
  297.             fflush(stdout);
  298.         endcase;
  299.         
  300.         case KMDCERRMSG:
  301.         endwin();
  302.             fprintf(stderr, "wstats: %s\n", MString);
  303.             OKtoGoOn = 0;         /* quit */
  304.             TerminationHandler(0);
  305.         endcase;
  306.  
  307.         case KMDCLASTMSG:
  308.         refresh();
  309.             sleep(WaitTime);
  310.         goto Restartit;
  311.         endcase;
  312.  
  313.         default:
  314.             fprintf(stderr,
  315.             "**** KMD error: Warning, bad msgtype: %d \n", MType);
  316.         }
  317.     }
  318.     TerminationHandler(0);        
  319. }
  320.