home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/time.h>
-
- #include "hostup.h"
-
- #define FIELDWIDTH 11
-
- FILE *f; /* input file */
- struct timeval first, last; /* GMT of polls */
- struct timezone timezone; /* my offset */
- char * timeof();
- int currentpos = 0, width = 80; /* for formatting screen */
- char tbuf[1024];
-
- u_long lastnetwork = 0;
-
- struct perhost thishost;
-
- main()
- {
- long seconds;
- int cols;
- u_long network;
- if (tgetent(tbuf, getenv("TERM")) == 1) {
- if ((cols = tgetnum("co")) > 0) {
- width = cols;
- }
- }
- if ((f = fopen(FILENAME, "r")) == NULL) {
- perror(FILENAME);
- exit(1);
- }
- gettimeofday(&first, &timezone);
- last.tv_sec = 0;
- while (fread(&thishost, sizeof(thishost), 1, f) == 1) {
- network = ntohl(thishost.ph_to.sin_addr.s_addr);
- if (IN_CLASSA(network)) {
- network &= IN_CLASSA_NET;
- } else if (IN_CLASSB(network)) {
- network &= IN_CLASSB_NET;
- } else {
- network &= IN_CLASSC_NET;
- }
- if (network != lastnetwork) {
- lastnetwork = network;
- donl();
- }
- printstat(thishost.ph_name, thishost.ph_result);
- seconds = thishost.ph_time.tv_sec;
- if (first.tv_sec > seconds) {
- first.tv_sec = seconds;
- }
- if (last.tv_sec < seconds) {
- last.tv_sec = seconds;
- }
- }
- donl();
- printf("Polled from %s", timeof(&first));
- printf(" to %s; KEY: ", timeof(&last));
- printstat("Up", ECONNREFUSED);
- printstat("Down", ETIMEDOUT);
- printstat("UnReachable", ENETUNREACH);
- donl();
- exit(0);
- }
-
- char *
- timeof(tp)
- struct timeval *tp;
- {
- char *rp;
- struct tm *tm;
- tm = localtime(&tp->tv_sec);
- rp = asctime(tm);
- rp[19] = '\0'; /* truncate before timezone */
- return(rp + 11); /* return just the time */
- }
-
- printstat(name, errno)
- char *name;
- {
- int up = 0;
- char extra = ' ';
- switch(errno) {
- case ECONNREFUSED:
- up = 1;
- break;
- case 0:
- up = 1;
- extra = '!';
- break;
- case ETIMEDOUT:
- break;
- case ENETUNREACH:
- extra = '-';
- break;
- default:
- extra = '?';
- break;
- }
- if (up) {
- printf(" %s%c ", name, extra);
- } else {
- printf("(%s)%c", name, extra);
- }
- if (currentpos > (width - (2 * FIELDWIDTH))) {
- donl();
- } else {
- int i;
- currentpos += FIELDWIDTH;
- for (i = strlen(name)+3; i < FIELDWIDTH; i++) {
- putchar(' ');
- }
- }
- }
-
- donl()
- {
- if (currentpos) {
- putchar('\n');
- }
- currentpos = 0;
- }
-