/* NSP pr v1 ===== NetStat Parser - for NetStat.Net Copyright (C) 1998 Roddy Richards This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ===== See the included LICENSE file for licensing information, and COPYING for copyright information. */ #define RESULTS_FILE "results" #define URL_FOR_NETSTAT_IMAGE "images/ns-pars.gif" /* shouldn't need to modify below here */ #include #include #include #include #include #include #include #include #define GREEN_COLOR "#00EE00" #define YELLOW_COLOR "#FFFF00" #define RED_COLOR "#FF3300" #define FIRST_CLR "#B4CFF7" #define SECND_CLR "#D2D2D2" #define DARK_CLR "#848786" #define TOP_COLOR "#B4CFF7" typedef struct { int net_count; /* number of nets that exist in the file */ char *backbone; char *name; /* name of net info is from */ char *dname; char *address; /* address for lynx */ char *host; int recv; int status; } nettype; typedef struct { char *from; /* what backbone returned these results */ char *name; /* backbone name */ char *dname; int status; /* 0 if everything's ok, 1 if it's not responding right */ float min; /* Minimum time :from ping */ float avg; /* Average time :from ping */ float max; /* Maximum time :from ping */ int loss; /* Packet Loss :from ping */ } infotype; FILE *fcheck(char name[], char *mode) { FILE *fp; if ((fp = fopen(name,mode)) == NULL) { fprintf(stderr, "Unable to access: %s. Exiting.\n", name); exit(1); } return (fp); } void main (void) { infotype results[128]; /* net data from host */ int max, j, k, loss_gif, rcount, color, overall; char line[255], last[64], fullpath[255], table_color[16], bgcolor[16]; /* time */ struct stat stbuf; struct tm *tmptr; FILE *fp; printf("Content-type: text/html\n\n"); j = 0; max = 128; rcount = 0; overall = 0; loss_gif = 0; strcpy(fullpath, RESULTS_FILE); fp = fcheck(fullpath, "r"); while(fgets(line, max, fp) != NULL) { /* divide up the file */ if(line[0] == '|') { results[rcount].name = strdup(strtok(line, "|")); results[rcount].dname = strdup(strtok(NULL, "|")); results[rcount].status = atof(strtok(NULL, "|")); if (results[rcount].status == 0) { results[rcount].min = atof(strtok(NULL, "|")); results[rcount].avg = atof(strtok(NULL, "|")); results[rcount].max = atof(strtok(NULL, "|")); results[rcount].loss = atof(strtok(NULL, "\n")); j = j +1; overall = overall + results[rcount].loss; } rcount++; } /* if */ } overall = overall/j; color =0; /* time stamp */ if (stat(fullpath, &stbuf) == 0) { tmptr = localtime(&stbuf.st_mtime); strftime(last, 40, "%m/%d/%y at %I:%M %p %Z", tmptr); } /* just format the rest of the output here */ printf("

\n",DARK_CLR); printf("\n", last); printf("\n", DARK_CLR); printf("\n", DARK_CLR); printf("\n", DARK_CLR); for(k=0;k<=rcount-1; k++) { /* alternate colors */ if (color == 0) { strcpy(bgcolor, FIRST_CLR); color = 1; } else { strcpy(bgcolor, SECND_CLR); color = 0; } if (results[k].status == 0) { strcpy(table_color, bgcolor); if (results[k].loss >= 0) { strcpy(table_color,GREEN_COLOR); } if (results[k].loss > 5) { strcpy(table_color,YELLOW_COLOR); } if (results[k].loss > 10) { strcpy(table_color,RED_COLOR); } printf("",bgcolor, results[k].dname); printf("",bgcolor, results[k].min, bgcolor); printf("",bgcolor, results[k].avg, bgcolor); printf("\n",bgcolor, results[k].max); printf("", results[k].loss); } if (results[k].status == 1) { strcpy(table_color, bgcolor); printf("",bgcolor, results[k].dname); printf("",bgcolor,bgcolor); printf("",bgcolor,bgcolor); printf("",bgcolor); printf("\n"); } } printf("
Last Updated : %s
Backbone

Min/Avg/Max Latency

Loss

%s %.1f ms / %.1f ms / %.1f ms ",bgcolor, table_color); printf("
%4d%%
%sn/a/n/a/n/a",bgcolor, bgcolor); printf("
100%%
\n\n"); printf("

\n", URL_FOR_NETSTAT_IMAGE); }