home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / hostup / hostup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.4 KB  |  129 lines

  1. #include <stdio.h>
  2.  
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <sys/time.h>
  8.  
  9. #include "hostup.h"
  10.  
  11. #define FIELDWIDTH 11
  12.  
  13. FILE *f;            /* input file */
  14. struct timeval  first, last;    /* GMT of polls */
  15. struct timezone timezone;    /* my offset */
  16. char * timeof();
  17. int currentpos = 0, width = 80;    /* for formatting screen */
  18. char tbuf[1024];
  19.  
  20. u_long lastnetwork = 0;
  21.  
  22. struct perhost thishost;
  23.  
  24. main()
  25. {
  26.   long seconds;
  27.   int cols;
  28.   u_long network;
  29.   if (tgetent(tbuf, getenv("TERM")) == 1) {
  30.     if ((cols = tgetnum("co")) > 0) {
  31.       width = cols;
  32.     }
  33.   }
  34.   if ((f = fopen(FILENAME, "r")) == NULL) {
  35.     perror(FILENAME);
  36.     exit(1);
  37.   }
  38.   gettimeofday(&first, &timezone);
  39.   last.tv_sec = 0;
  40.   while (fread(&thishost, sizeof(thishost), 1, f) == 1) {
  41.     network = ntohl(thishost.ph_to.sin_addr.s_addr);
  42.     if (IN_CLASSA(network)) {
  43.       network &= IN_CLASSA_NET;
  44.     } else if (IN_CLASSB(network)) {
  45.       network &= IN_CLASSB_NET;
  46.     } else {
  47.       network &= IN_CLASSC_NET;
  48.     }
  49.     if (network != lastnetwork) {
  50.       lastnetwork = network;
  51.       donl();
  52.     }
  53.     printstat(thishost.ph_name, thishost.ph_result);
  54.     seconds = thishost.ph_time.tv_sec;
  55.     if (first.tv_sec > seconds) {
  56.       first.tv_sec = seconds;
  57.     }
  58.     if (last.tv_sec < seconds) {
  59.       last.tv_sec = seconds;
  60.     }
  61.   }
  62.   donl();
  63.   printf("Polled from %s", timeof(&first));
  64.   printf(" to %s; KEY: ", timeof(&last));
  65.   printstat("Up", ECONNREFUSED);
  66.   printstat("Down", ETIMEDOUT);
  67.   printstat("UnReachable", ENETUNREACH);
  68.   donl();
  69.   exit(0);
  70. }
  71.  
  72. char *
  73. timeof(tp)
  74.      struct timeval *tp;
  75. {
  76.   char *rp;
  77.   struct tm *tm;
  78.   tm = localtime(&tp->tv_sec);
  79.   rp = asctime(tm);
  80.   rp[19] = '\0';        /* truncate before timezone */
  81.   return(rp + 11);        /* return just the time */
  82. }
  83.  
  84. printstat(name, errno)
  85.      char *name;
  86. {
  87.   int up = 0;
  88.   char extra = ' ';
  89.   switch(errno) {
  90.   case ECONNREFUSED:
  91.     up = 1;
  92.     break;
  93.   case 0:
  94.     up = 1;
  95.     extra = '!';
  96.     break;
  97.   case ETIMEDOUT:
  98.     break;
  99.   case ENETUNREACH:
  100.     extra = '-';
  101.     break;
  102.   default:
  103.     extra = '?';
  104.     break;
  105.   }
  106.   if (up) {
  107.     printf(" %s%c ", name, extra);
  108.   } else {
  109.     printf("(%s)%c", name, extra);
  110.   }
  111.   if (currentpos > (width - (2 * FIELDWIDTH))) {
  112.     donl();
  113.   } else {
  114.     int i;
  115.     currentpos += FIELDWIDTH;
  116.     for (i = strlen(name)+3; i < FIELDWIDTH; i++) {
  117.       putchar(' ');
  118.     }
  119.   }
  120. }
  121.  
  122. donl()
  123. {
  124.   if (currentpos) {
  125.     putchar('\n');
  126.   }
  127.   currentpos = 0;
  128. }
  129.