/* v1.2, 6/4/98 ===== NetStat Client software - 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. */ #include "client.h" /* code begins */ 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 ReadHosts(hosttype hosts[NUM_HOSTS], int *num) { char line[255]; FILE *fp; fp = fcheck(HOST_FILE, "r"); while (fgets(line, MAX, fp) != NULL) { hosts[*num].name = strdup(strtok(line, ":")); hosts[*num].dname = strdup(strtok(NULL, ":")); hosts[*num].ip = strdup(strtok(NULL, "\n")+1); (*num)++; } fclose(fp); } void PingHosts(hosttype hosts[NUM_HOSTS], int start,int end) { int i; char pingfile[255]; for (i=start;i %s%s", PING_LOCATION, PING_TRIES, hosts[i].ip, DATA, hosts[i].name); system(pingfile); } } void PingWrite(hosttype *hosts, FILE *fp) { fprintf(fp, "|%s|%s|%d|%.2f|%.2f|%.2f|%d\n", hosts->name, hosts->dname, hosts->status, hosts->min, hosts->avg, hosts->max, hosts->loss); } void PingParse(hosttype *hosts) { FILE *fz; int die=0; char garbage[MAX], *g2, tempfile[80], line[120], *loss, *recv; sprintf(tempfile,"%s%s", DATA, hosts->name); fz = fcheck(tempfile, "r"); fgets(garbage, MAX, fz); if ((garbage[0] != 'p') && (feof(fz) == 0)) { /* i.e. not an error from ping */ while ((garbage[0] != '-') && (feof(fz) == 0)) { /* skip down */ fgets(garbage, MAX, fz); /* helpful on solaris systems */ } fgets(line, MAX, fz); /* read the line after '-----' */ g2 = strtok(line, ","); recv = strtok(NULL, " "); if (recv!=NULL) { hosts->recv = atof(recv); g2 = strtok(NULL, ","); loss = strtok(NULL, "%")+1; if (loss != NULL) { hosts->loss = atof(loss); if (hosts->recv > 0) { /* if packet loss isn't 100%... */ hosts->status = 0; fgets(line, MAX, fz); /* get next line */ g2 = strtok(line, "="); /* chop it at the =, save the rest */ strcpy(tempfile, strtok(NULL, "\n")+1); /* cut that up */ sscanf(tempfile, "%f/%f/%f", &hosts->min, &hosts->avg, &hosts->max); } /* read only the data i want */ else { /* if packet loss is 100%... */ hosts->status = 1; hosts->min = 0; hosts->avg = 0; hosts->max = 0; } } else { die = 1; } /* loss == NULL */ } else { die = 1; } /* recv == NULL */ } else { die = 1; } /* ping gave an error */ if (die == 1) { hosts->recv = 0; hosts->loss = 100; hosts->status = 1; hosts->min = 0; hosts->avg = 0; hosts->max = 0; } } void main (int argc, char *argv[]) { hosttype info[NUM_HOSTS]; char llama[150]; int i, minutes, seg1, seg2, count; FILE *fp; count = 0; minutes = (60 * 2); /* takes approx 1:30 to ping hosts */ /* problem occurs if this is too late */ /* only if the client runs the 1st time */ /* since the file will exist from a previous */ /* run, otherwise */ ReadHosts(info, &count); seg1 = (count/3); seg2 = (seg1 + seg1); PingHosts(info, 0, seg1); /* it into for the sake of each host's resources */ sleep(minutes); /* this may need to be increased as hosts.nsc */ PingHosts(info, seg1, seg2); sleep(minutes); PingHosts(info, seg2, count); sleep(minutes); sprintf(llama,"%s%s", HTML, RESULT_NAME); fp = fcheck(llama, "w"); for (i=0;i<=count-1;i++) { PingParse(&info[i]); PingWrite(&info[i], fp); } fclose(fp); /* end */ }