1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
|
/*
ninpaths.c - new inpaths reporting program
Idea, data structures and part of code based on inpaths 2.5
by Brian Reid, Landon Curt Noll
This version written by Olaf Titz, Feb. 1997
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
const char id[]="$Id: ninpaths.c,v 1.5 1998/06/28 13:41:06 olaf Exp $";
#define MAXFNAME 1024 /* max length of file name */
#define MAXLINE 1024 /* max length of Path line */
#define HASH_TBL 65536 /* hash table size (power of two) */
#define MAXHOST 128 /* max length of host name */
#define HOSTF "%127s" /* scanf format for host name */
#define RECLINE 120 /* dump file line length softlimit */
#define VERSION "3.1" /* this program */
/* structure used to tally the traffic between two hosts */
struct trec {
struct trec *rlink; /* next in chain */
struct nrec *linkid; /* pointer to... */
long tally; /* count */
};
/* structure to hold the information about a host */
struct nrec {
struct nrec *link; /* next in chain */
struct trec *rlink; /* start of trec chain */
char *id; /* host name */
long no; /* identificator for dump file */
long sentto; /* tally of articles sent from here */
};
struct nrec *hosthash[HASH_TBL];
time_t starttime; /* Start time */
double atimes=0.0; /* Sum of articles times wrt. starttime */
long total=0, /* Total articles processed */
sites=0; /* Total sites known */
/* malloc and warn if out of mem */
void *
wmalloc(s)
{
void *p=malloc(s);
if (!p)
fprintf(stderr, "warning: out of memory\n");
/* Out of mem is non-fatal here. We will just miss a few counters.
Better than aborting and missing the whole dump. */
return p;
}
/* Hash function due to Glenn Fowler / Landon Curt Noll / Phong Vo */
int
hash(const char *str)
{
unsigned long val;
unsigned long c;
for (val = 0; (c=(unsigned long)(*str)); ++str) {
val *= 16777619; /* magic */
val ^= c; /* more magic */
}
return (int)(val & (unsigned long)(HASH_TBL-1));
}
/* Look up a host in the hash table. Add if necessary. */
struct nrec *
hhost(const char *n)
{
struct nrec *h;
int i=hash(n);
for (h=hosthash[i]; h; h=h->link)
if (!strcmp(n, h->id))
return h;
/* not there - allocate */
h=wmalloc(sizeof(struct nrec));
if (!h)
return NULL;
h->link=hosthash[i];
h->rlink=NULL;
h->id=strdup(n);
if (!h->id) {
free(h); return NULL;
}
h->no=h->sentto=0;
hosthash[i]=h;
sites++;
return h;
}
/* Look up a tally record between hosts. Add if necessary. */
struct trec *
tallyrec(struct nrec *r, struct nrec *h)
{
struct trec *t;
for (t=r->rlink; t; t=t->rlink)
if (t->linkid==h)
return t;
t=wmalloc(sizeof(struct trec));
if (!t)
return NULL;
t->rlink=r->rlink;
t->linkid=h;
t->tally=0;
r->rlink=t;
return t;
}
/* Dump file format:
"!!NINP" <version> <starttime> <endtime> <sites> <total> <avgtime> "\n"
followed by <sites> S-records,
"!!NLREC\n"
[3.0]
followed by max. <sites>^2 L-records
[3.1]
followed by max. <sites> L-records
"!!NEND" <nlrecs> "\n"
starttime, endtime, avgtime as UNIX date
the records are separated by space or \n
an S-record is "site count"
[3.0]
an L-record is "sitea!siteb!count"
[3.1]
an L-record is ":sitea" { "!siteb,count" }...
",count" omitted if count==1
where sitea and siteb are numbers of the S-records starting at 0
*/
int
writedump(FILE *f)
{
int i, j;
long n;
struct nrec *h;
struct trec *t;
if (!total) {
return -1;
}
fprintf(f, "!!NINP " VERSION " %ld %ld %ld %ld %ld\n",
starttime, time(0), sites, total,
(long)(atimes/total)+starttime);
n=j=0;
/* write the S-records (hosts), numbering them in the process */
for (i=0; i<HASH_TBL; ++i)
for (h=hosthash[i]; h; h=h->link) {
h->no=n++;
j+=fprintf(f, "%s %ld", h->id, h->sentto);
if (j>RECLINE) {
j=0;
fprintf(f, "\n");
} else {
fprintf(f, " ");
}
}
if (n!=sites)
fprintf(stderr, "internal error: sites=%ld, dumped=%ld\n", sites, n);
fprintf(f, "\n!!NLREC\n");
n=j=0;
/* write the L-records (links) */
for (i=0; i<HASH_TBL; ++i)
for (h=hosthash[i]; h; h=h->link)
if ((t=h->rlink)) {
j+=fprintf(f, ":%ld", h->no);
for (; t; t=t->rlink) {
j+=fprintf(f, "!%ld", t->linkid->no);
if (t->tally>1)
j+=fprintf(f, ",%ld", t->tally);
n++;
}
if (j>RECLINE) {
j=0;
fprintf(f, "\n");
}
}
fprintf(f, "\n!!NLEND %ld\n", n);
return 0;
}
/* Write dump to a named file. Substitute %d in file name with system time. */
void
writedumpfile(const char *n)
{
char buf[MAXFNAME];
FILE *d;
if (n[0]=='-' && n[1]=='\0') {
(void) writedump(stdout);
return;
}
snprintf(buf, sizeof(buf), n, time(0));
d=fopen(buf, "w");
if (d) {
if (writedump(d)<0)
unlink(buf);
} else {
perror("writedumpfile: fopen");
}
}
/* Read a dump file. */
int
readdump(FILE *f)
{
int a, b;
long i, m, l;
time_t st, et, at;
long sit, tot;
struct nrec **n;
struct trec *t;
char c[MAXHOST];
char v[16];
#define formerr(i) {\
fprintf(stderr, "dump file format error #%d\n", (i)); return -1; }
if (fscanf(f, "!!NINP %15s %ld %ld %ld %ld %ld\n",
v, &st, &et, &sit, &tot, &at)!=6)
formerr(0);
n=calloc(sit, sizeof(struct nrec *));
if (!n) {
fprintf(stderr, "error: out of memory\n");
return -1;
}
for (i=0; i<sit; i++) {
if (fscanf(f, HOSTF " %ld ", c, &l)!=2) {
fprintf(stderr, "read %ld ", i);
formerr(1);
}
n[i]=hhost(c);
if (n[i])
n[i]->sentto+=l;
}
if ((fscanf(f, HOSTF "\n", c)!=1) ||
strcmp(c, "!!NLREC"))
formerr(2);
m=0;
if (!strncmp(v, "3.0", 3)) {
/* Read 3.0-format L-records */
while (fscanf(f, "%d!%d!%ld ", &a, &b, &l)==3) {
t=tallyrec(n[a], n[b]);
if (t)
t->tally+=l;
++m;
}
} else if (!strncmp(v, "3.1", 3)) {
/* Read L-records */
while (fscanf(f, " :%d", &a)==1) {
while ((i=fscanf(f, "!%d,%ld", &b, &l))>0) {
t=tallyrec(n[a], n[b]);
if (i<2)
l=1;
if (t)
t->tally+=l;
++m;
}
}
} else {
fprintf(stderr, "version %s ", v);
formerr(9);
}
if ((fscanf(f, "!!NLEND %ld\n", &i)!=1)
|| (i!=m))
formerr(3);
#ifdef DEBUG
fprintf(stderr, " dumped start %s total=%ld atimes=%ld (%ld)\n",
ctime(&st), tot, at, at-st);
#endif
/* Adjust the time average and total count */
if (starttime>st) {
atimes+=(double)total*(starttime-st);
starttime=st;
}
atimes+=(double)tot*(at-starttime);
total+=tot;
#ifdef DEBUG
fprintf(stderr, " current start %s total=%ld atimes=%.0f (%.0f)\n\n",
ctime(&starttime), total, atimes, atimes/total);
#endif
free(n);
return 0;
}
/* Read dump from a file. */
int
readdumpfile(const char *n)
{
FILE *d;
int i;
if (n[0]=='-' && n[1]=='\0')
return readdump(stdin);
d=fopen(n, "r");
if (d) {
fprintf(stderr, "Reading dump file %s\n", n);
i=readdump(d);
fclose(d);
return i;
} else {
perror("readdumpfile: fopen");
return -1;
}
}
/* Process a Path line. */
void
pathline(char *c)
{
char *c2;
struct nrec *h, *r;
struct trec *t;
r=NULL;
while (*c) {
for (c2=c; *c2 && *c2!='!'; c2++);
if (c2-c>MAXHOST-1)
/* looks broken, dont bother with rest */
return;
while (*c2=='!')
*c2++='\0'; /* skip "!!" too */
h=hhost(c);
if (h) {
++h->sentto;
if (r && r!=h) {
t=tallyrec(r, h);
if (t)
++t->tally;
}
}
c=c2;
r=h;
}
}
/* Take Path lines from file (stdin used here). */
void
procpaths(FILE *f)
{
char buf[MAXLINE];
char *c, *ce;
int v=1; /* current line is valid */
while (fgets(buf, sizeof(buf), f)) {
c=buf;
if (!strncmp(c, "Path: ", 6))
c+=6;
/* find end of line. Some broken newsreaders preload Path with
a name containing spaces. Chop off those entries. */
for (ce=c; *ce && !isspace(*ce); ++ce);
if (!*ce) {
/* bogus line */
v=0;
} else if (v) {
/* valid line */
for (; ce>c && *ce!='!'; --ce); /* ignore last element */
*ce='\0';
pathline(c); /* process it */
/* update average age and grand total */
atimes+=(time(0)-starttime);
++total;
} else {
/* next line is valid */
v=1;
}
}
}
/* Output a report suitable for mailing. From inpaths 2.5 */
void
report(const char *hostname, int verbose)
{
double avgAge;
int i, columns, needHost;
struct nrec *list, *relay;
struct trec *rlist;
char hostString[MAXHOST];
time_t t0=time(0);
if (!total) {
fprintf(stderr, "report: no traffic\n");
return;
}
/* mark own site to not report it */
list=hhost(hostname);
if (list)
list->id[0]='\0';
avgAge=((double)t0 - (atimes/total + (double)starttime)) /86400.0;
printf("ZCZC begin inhosts %s %s %d %ld %3.1f\n",
VERSION,hostname,verbose,total,avgAge);
for (i=0; i<HASH_TBL-1; i++) {
list = hosthash[i];
while (list != NULL) {
if (list->id[0] != 0 && list->rlink != NULL) {
if (verbose > 0 || (100*list->sentto > total))
printf("%ld\t%s\n",list->sentto, list->id);
}
list = list->link;
}
}
printf("ZCZC end inhosts %s\n",hostname);
printf("ZCZC begin inpaths %s %s %d %ld %3.1f\n",
VERSION,hostname,verbose,total,avgAge);
for (i=0; i<HASH_TBL-1; i++) {
list = hosthash[i];
while (list != NULL) {
if (verbose > 1 || (100*list->sentto > total)) {
if (list->id[0] != 0 && list->rlink != NULL) {
columns = 3+strlen(list->id);
sprintf(hostString,"%s H ",list->id);
needHost = 1;
rlist = list->rlink;
while (rlist != NULL) {
if (
(100*rlist->tally > total)
|| ((verbose > 1)&&(5000*rlist->tally>total))
) {
if (needHost) printf("%s",hostString);
needHost = 0;
relay = rlist->linkid;
if (relay->id[0] != 0) {
if (columns > 70) {
printf("\n%s",hostString);
columns = 3+strlen(list->id);
}
printf("%ld Z %s U ", rlist->tally, relay->id);
columns += 9+strlen(relay->id);
}
}
rlist = rlist->rlink;
}
if (!needHost) printf("\n");
}
}
list = list->link;
}
}
printf("ZCZC end inpaths %s\n",hostname);
}
extern char *optarg;
int
main(int argc, char *argv[])
{
int i;
int pf=0, vf=2;
char *df=NULL, *rf=NULL;
for (i=0; i<HASH_TBL; i++)
hosthash[i]=NULL;
starttime=time(0);
while ((i=getopt(argc, argv, "pd:u:r:v:"))!=EOF)
switch (i) {
case 'p':
/* read Path lines from stdin */
pf=1; break;
case 'd':
/* make a dump to the named file */
df=optarg; break;
case 'u':
/* read dump from the named file */
if (readdumpfile(optarg)<0)
exit(1);
break;
case 'r':
/* make a report for the named site */
rf=optarg; break;
case 'v':
/* control report verbosity */
vf=atoi(optarg); break;
default:
fprintf(stderr, "unknown option %c\n", i);
}
if (pf)
procpaths(stdin);
if (df)
writedumpfile(df);
if (rf)
report(rf, vf);
return 0;
}
|