home *** CD-ROM | disk | FTP | other *** search
- /* C O P Y R I G H T N O T I C E :
- * Copyright 1986 Eric Jul. May not be used for any
- * purpose without written permission from the author
- */
-
- /* File: wstats.c Eric Jul, 1986-06-03 */
-
- /*
- * $Header$
- * INTERFACE: See usage printout.
- *
- * FUNCTION: Print Kernel statistics repeatedly using windows,
- * see curses(3X).
- *
- * IMPORTS: loads, see #include
- *
- * EXPORTS: Nothing.
- *
- * DESIGN: Send start snapshot message to Emerald process and then
- * repeatedly receive and print data until stop snapshot
- * message arrives.
- * Then repeat after waiting t seconds.
- *
- * $Log$
- * 1986-06-03 Modified to work with 4.2bsd sockets.
- * 1985-01-06 Initial implementation, Eric Jul.
- */
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <curses.h>
- #include <signal.h>
-
-
- #define mSUCCESS(X) (((X) & 7) == 1)
-
-
- #undef FALSE
- #undef TRUE
- #undef integer
-
- typedef int KKStatus;
-
- #define MAXMESSAGESIZE 1000
- #include "Kernel/h/emPorts.h"
- #include "Kernel/h/kmdDefs.h"
-
- #include <netdb.h>
- #include <signal.h>
-
- #define endcase break
-
- char *snapshotname, *snapshotstring;
- int talkSock, pid = 0, prodTime = 1, MType, MInt;
- char MString[500];
-
- struct sockaddr_in destSockDescr = {AF_INET};
-
- /* Define signal handlers */
-
- void PipeHandler(sig)
- int sig;
- {
- printf("SIGPIPE>>> The connection to the process broke.\nTerminating.\n");
- exit(1);
- }
-
- void TerminationHandler(sig)
- int sig;
- {
- fprintf(stderr, "Terminating wstats.\n");
- /* Important to tell kernel that we are bye, bye ... */
- KMDSend(talkSock, KMDCLASTMSG, 0, "Done");
- if (shutdown(talkSock, 1) < 0) perror("shutdown1");
- while(mSUCCESS(KMDReceive(talkSock, &MType, &MInt, MString)));
- close(talkSock);
- printw("Terminating.\n");
- endwin();
- exit(sig);
- }
-
-
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int OKtoGoOn;
- int MType, MInt;
- char MString[500];
- char snapbuf[500];
-
- static int i, EPLNN, SValue;
- static int pidGiven = 0, machineNameGiven = 0;
- KKStatus kstat;
- char myHostName[100];
- struct hostent *myHost;
- struct servent *myService;
- int WaitTime;
- long theTime;
-
- snapshotname = "MMStats"; /* default */
- snapshotstring = (char *) 0;
-
- SValue = 0; /* default */
- EPLNN = 8; /* default */
- WaitTime = 5; /* default */
-
- if (argc == 2)
- if (!strcmp(argv[1], "usage")) {
- fprintf(stderr,
- "Usage: wstats [-N <LNN>] [-M <Machine Name>] [-t <WaitInterval>] [-n <Snapshot>\n");
- TerminationHandler(0);
- };
-
- /*
- * Parse command line options
- */
- while (argc > 1 && argv[1][0] == '-') {
- switch (argv[1][1]) {
- case 'N': /* Set logical node number */
- if (argv[1][2] == '\0') {
- EPLNN = atoi(argv[2]);
- argc--;
- argv++;
- } else
- EPLNN = atoi(&argv[1][2]);
-
- printf("warning: -N ignored.\n");
- break;
- case 'X': /* Set logical node number (hex) */
- if (argv[1][2] == '\0') {
- sscanf(argv[2], "%x", &EPLNN);
- argc--;
- argv++;
- } else
- sscanf(&argv[1][2], "%x", &EPLNN);
- printf("warning: -X ignored.\n");
- break;
- case 'd':
- if (argv[1][2] == '\0') {
- SValue = atoi(argv[2]);
- argc--;
- argv++;
- } else
- SValue = atoi(&argv[1][2]);
- break;
- case 'P': /* Set pid, cancel LNN usage. */
- if (argv[1][2] == '\0') {
- pid = atoi(argv[2]);
- argc--;
- argv++;
- } else
- pid = atoi(&argv[1][2]);
- pidGiven = 1;
- printf("warning: -P ignored.\n");
- break;
- case 'h':
- if (argv[1][2] == '\0') {
- sscanf(argv[2], "%x", &SValue);
- argc--;
- argv++;
- } else
- sscanf(&argv[1][2], "%x", &SValue);
- break;
- case 't':
- if (argv[1][2] == '\0') {
- WaitTime = atoi(argv[2]);
- argc--;
- argv++;
- } else
- WaitTime = atoi(&argv[1][2]);
- break;
- case 'n':
- snapshotname = argv[2];
- argc--;
- argv++;
- break;
- case 'S':
- snapshotstring = argv[2];
- argc--;
- argv++;
- break;
- case 'M':
- case 'm':
- strcpy(myHostName, argv[2]);
- argc--;
- argv++;
- machineNameGiven ++;
- break;
- default:
- fprintf(stderr,
- "wstats: (warning) Unknown option: %c",
- argv[1][2]);
- break;
-
- }
- argc--;
- argv++;
- }
-
-
- /* Set termination handler. */
- signal(SIGHUP, TerminationHandler);
- signal(SIGQUIT, TerminationHandler);
- signal(SIGINT, TerminationHandler);
- signal(SIGBUS, TerminationHandler);
- signal(SIGSEGV, TerminationHandler);
- signal(SIGTERM, TerminationHandler);
- signal(SIGPIPE, PipeHandler);
-
- talkSock = socket(AF_INET, SOCK_STREAM, 0, 0);
- if (talkSock < 0) {
- perror("snapshot: socket");
- }
-
- if (! machineNameGiven) {
- if (gethostname(myHostName, 100) < 0) {
- perror("snapshot: gethostname");
- TerminationHandler(0);
- }
- }
- myHost = gethostbyname(myHostName);
- if (myHost == NULL) {
- perror("snapshot: gethostbyname");
- TerminationHandler(0);
- }
-
- bcopy(myHost->h_addr, &destSockDescr.sin_addr, myHost->h_length);
-
- {
- char *emplanename;
- int emPlane = 0;
- int emPort = EMKERNELDEFAULTPORTNUMBER;
- struct servent *myService;
- emplanename = (char *) getenv("EMPLANE");
- if (emplanename != NULL) emPlane = atoi(emplanename);
- #ifdef DIKU
- myService = getservbyname(EMKERNELSERVICEPORT, "tcp");
- if(myService)
- emPort = ntohs(myService->s_port);
- #endif
- destSockDescr.sin_port = htons(emPort + emPlane*4 + 1);
- }
-
- if (connect(talkSock, &destSockDescr, sizeof(destSockDescr)) == -1) {
- perror("snapshot: connect");
- TerminationHandler(0);
- }
- if ( KMDTest )
- printf("Connected to %d\n", ntohs(destSockDescr.sin_port));
-
- KMDReceive(talkSock, &MType, &MInt, MString);
- printf("%s", MString);
-
- /* Initialize window. */
- initscr();
-
- Restartit:
- /* Reset window. */
- move(0, 0);
- erase();
- theTime = time(0);
- standout();
- printw("%s at %s\n", snapshotname, ctime(&theTime));
- standend();
- /* Start snapshot.*/
- sprintf(snapbuf, "%s %s", snapshotname, snapshotstring);
- kstat = KMDSend(talkSock, KMDCSNAPSHOT, SValue, snapbuf);
-
- if (! mSUCCESS( kstat ) ) {
- endwin();
- fprintf(stderr,
- "wstats: Send snapshot msg failed, kstat = 0x%08x\n", kstat);
- TerminationHandler(0);
- };
-
- OKtoGoOn = 1;
- while (OKtoGoOn==1) {
- if(!mSUCCESS(KMDReceive(talkSock, &MType, &MInt, MString))){
- fprintf(stderr, "Premature snapshot termination\n");
- perror("recv");
- TerminationHandler(0);
- };
-
- if ( KMDTest ) {
- printf("Msg from: %d, (%d, %d, %s)\n", talkSock,
- MType, MInt, MString),
- fflush(stdout);
- }
-
- switch ( MType ) {
-
- case KMDCDATA:
- printw("%s", MString);
- fflush(stdout);
- endcase;
-
- case KMDCERRMSG:
- endwin();
- fprintf(stderr, "wstats: %s\n", MString);
- OKtoGoOn = 0; /* quit */
- TerminationHandler(0);
- endcase;
-
- case KMDCLASTMSG:
- refresh();
- sleep(WaitTime);
- goto Restartit;
- endcase;
-
- default:
- fprintf(stderr,
- "**** KMD error: Warning, bad msgtype: %d \n", MType);
- }
- }
- TerminationHandler(0);
- }
-