⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rateup.c

📁 主要用于监控网络流量并动态产生可视化结果
💻 C
📖 第 1 页 / 共 3 页
字号:
	  break;	case 4:	  if (tm->tm_mday == 1) 	    lhist[x].time |= 1;	  if (tm->tm_yday == 0)	    lhist[x].time |= 2;	  	  if (tm->tm_mday ==15)  {	    if ((graph_label[x] = calloc(1,sizeof(char) * 5)) == NULL) {	      fprintf(stderr,"Rateup Error: Out of memory in graph labeling\n");	      exit(0);	    } else {	      strftime(graph_label[x],4,"%b",tm);	    }	  }	  break;	}	NEXT(step);	/*scale with muli */	inr *=muli; outr *=muli; inmax *=muli; outmax *=muli;			y = ((double)inr/nmax) * maxy;	if (y >= maxy) y = maxy;	lhist[x].in = y;	y = ((double)outr/nmax) * maxy;	if (y >= maxy) y = maxy;	lhist[x].out = y;	y = ((double)inmax/nmax) * maxy;	if (y >= maxy) y = maxy;	if (x && y < (lhist[x-1].inmax)) {	  y += (lhist[x-1].inmax); y /= 2;	}        lhist[x].inmax = y;	y = ((double)outmax/nmax) * maxy;	if (y >= maxy) y = maxy;	if (x && y < (lhist[x-1].outmax)) {	  y += (lhist[x-1].outmax); y /= 2;	}        lhist[x].outmax = y;	if (dorelpercent ) {	   if (outr != (unsigned long)0) {              percent = (double)inr/(double)outr;/*	   if (lhist[x].out != (unsigned long)0) {              percent = (double)lhist[x].in/(double)lhist[x].out;	   if (lhist[x].in != (double)0.) {              percent = (double)lhist[x].out/lhist[x].in;*/	   }	   else {              percent = (double)0.;	   }	   if (percent > (double)1) percent = (double)1.;	   percent *= maxy;           lhist[x].percent = (unsigned long)percent;	}	else {           lhist[x].percent = (unsigned long)0;	}		now -= step;    }    /* the graph is made ten pixels higher to acomodate the x labels */    graph = gdImageCreate(XSIZE, YSIZE);    brush_out = gdImageCreate(1,2);    brush_outm = gdImageCreate(1,2);    brush_outp = gdImageCreate(1,2);        /* the first color allocated will be the background color. */    i_blank = gdImageColorAllocate(graph,c_blank);    i_light = gdImageColorAllocate(graph,c_light);    i_dark = gdImageColorAllocate(graph,c_dark);    if (transparent) {       gdImageColorTransparent(graph, i_blank);    }    gdImageInterlace(graph, 1);         i_major = gdImageColorAllocate(graph,c_major);    i_in = gdImageColorAllocate(graph,col_in[0], col_in[1], col_in[2]);    i_out = gdImageColorAllocate(brush_out,col_out[0], col_out[1], col_out[2]);    i_grid = gdImageColorAllocate(graph,c_grid );    i_inm = gdImageColorAllocate(graph,col_inm[0], col_inm[1], col_inm[2]);    i_outm = gdImageColorAllocate(brush_outm,col_outm[0], col_outm[1], col_outm[2]);    i_outp = gdImageColorAllocate(brush_outp,col_outp[0], col_outp[1], col_outp[2]);    i_outpg = gdImageColorAllocate(graph,col_outp[0], col_outp[1], col_outp[2]);        /* draw the image border */    gdImageLine(graph,0,0,XSIZE-1,0,i_light);    gdImageLine(graph,1,1,XSIZE-2,1,i_light);    gdImageLine(graph,0,0,0,YSIZE-1,i_light);    gdImageLine(graph,1,1,1,YSIZE-2,i_light);    gdImageLine(graph,XSIZE-1,0,XSIZE-1,YSIZE-1,i_dark);    gdImageLine(graph,0,YSIZE-1,XSIZE-1,YSIZE-1,i_dark);    gdImageLine(graph,XSIZE-2,1,XSIZE-2,YSIZE-2,i_dark);    gdImageLine(graph,1,YSIZE-2,XSIZE-2,YSIZE-2,i_dark);    /* draw the incoming traffic */    for (x=0; x<maxx; x++) {      /* peak is always above average, we therefore only draw the upper part */      if (peak) gdImageLine(graph,xtr(x),ytr(lhist[x].in),                                  xtr(x),ytr(lhist[x].inmax),i_inm);#ifdef INCOMING_UNFILLED      gdImageLine(graph,xtr(x),ytr(lhist[x].in),xtr(x),ytr(lhist[x+1].in),i_in);#else      gdImageLine(graph,xtr(x),ytr(0),xtr(x),ytr(lhist[x].in),i_in);      /* draw the line a second time offset slightly. makes the graph       * look better if xscale is fractional */      gdImageLine(graph,xtr(x+0.5),ytr(0),xtr(x+0.5),ytr(lhist[x].in),i_in);#endif    }    /* draw the percentage */    if (dorelpercent) {      gdImageSetBrush(graph, brush_outp);      for (x=0; x<maxx-1; x++)          gdImageLine(graph,xtr(x), ytr(lhist[x].percent),		   xtr(x+1), ytr(lhist[x+1].percent),		   gdBrushed);    }     /* draw the outgoing traffic */    gdImageSetBrush(graph, brush_outm);    if (peak) for (x=0; x<maxx-1; x++)        gdImageLine(graph,xtr(x), ytr(lhist[x].outmax),		   xtr(x+1), ytr(lhist[x+1].outmax),		   gdBrushed);    gdImageSetBrush(graph, brush_out);       for (x=0; x<maxx-1; x++)       gdImageLine(graph,xtr(x),ytr(lhist[x].out),		  xtr(x+1),ytr(lhist[x+1].out),		  gdBrushed);     /* draw the graph border */        gdImageRectangle(graph,		     xtr(0),ytr(0),xtr(maxx),ytr(maxy),i_grid);        /*create a dotted style for the grid lines*/    styleDotted[0] = i_grid;    styleDotted[1] = gdTransparent;    styleDotted[2] = gdTransparent;    gdImageSetStyle(graph, styleDotted, 3);        /* draw the horizontal grid */    if ((longup == NULL) || (shortup == NULL)) {	if (!bits) { longup = "Bytes per Second"; shortup = "Bytes/s"; }	else { longup = "Bits per Second"; shortup = "Bits/s"; }    }    if (maxy < gdFontSmall->w*16) {		gdImageStringUp(graph, gdFontSmall,8, ytr((maxy-gdFontSmall->w*strlen(shortup))/2),		      (unsigned char*)shortup, i_grid);    } else {	gdImageStringUp(graph, gdFontSmall,8, ytr((maxy-gdFontSmall->w*strlen(longup))/2),		      (unsigned char*)longup, i_grid);    }    for(i=0;i<=ytics;i++){      	gdImageLine(graph,xtr(-2),ytr(i*maxy/ytics),		    xtr(1),ytr(i*maxy/ytics),i_grid);	gdImageLine(graph,xtr(maxx+2),ytr(i*maxy/ytics),		    xtr(maxx-1),ytr(i*maxy/ytics),i_grid);	gdImageLine(graph,xtr(0),ytr(i*maxy/ytics),		    xtr(maxx),ytr(i*maxy/ytics),gdStyled);	/*    	sprintf(ylab,"%6.1f %s",sca_max_q*i,short_si[digits/3]);*/    	sprintf(ylab,"%6.1f %s",sca_max_q*i*yticsf,short_si_out);        gdImageString(graph, gdFontSmall,23,         	      ytr(i*maxy/ytics+gdFontSmall->h/2),		      (unsigned char*)ylab, i_grid);	if (dorelpercent) {	    	   sprintf(ylab,"% 6.1f%%",(float)25.*i); 	   gdImageString(graph, gdFontSmall,77+((maxx)*xscale)+1,         	      ytr(i*maxy/ytics+gdFontSmall->h/2),		      (unsigned char*)ylab, i_outpg);	}    }        /* draw vertical grid and horizontal labels */    for (x=0; x<maxx; x++) {      if (lhist[x].time) {	gdImageLine(graph,xtr(x),ytr(-2),xtr(x),ytr(1),i_grid);	gdImageLine(graph,xtr(x),ytr(0),xtr(x),ytr(maxy),gdStyled);       }      if (graph_label[x] != NULL) {        gdImageString(graph, gdFontSmall,		      (xtr(x) - (strlen(graph_label[x]) * 			    gdFontSmall->w / 2)), 		      ytr(-4),		      (unsigned char*)graph_label[x], i_grid);	free(graph_label[x]);      }      if (lhist[x].time & 2)	gdImageLine(graph,xtr(x),ytr(0),xtr(x),ytr(maxy),i_major);    }    /* draw line at peak In value in i_major color */    /* only draw the line if it's within the graph ... */    if ( ytr( maxy ) < ytr(((double)origmaxvi/nmax)*maxy)) {      styleDotted[0] = i_major;       gdImageSetStyle(graph, styleDotted, 3);      gdImageLine(graph, xtr(0),ytr(((double)origmaxvi/nmax)*maxy),		  xtr(maxx),ytr(((double)origmaxvi/nmax)*maxy),		  gdStyled);    }    /* draw line at peak Out value in i_major color */    /* only draw the line if it's within the graph ... */    if ( ytr( maxy ) < ytr(((double)origmaxvo/nmax)*maxy)) {      styleDotted[0] = i_major;      gdImageSetStyle(graph, styleDotted, 3);      gdImageLine(graph, xtr(0),ytr(((double)origmaxvo/nmax)*maxy),		  xtr(maxx),ytr(((double)origmaxvo/nmax)*maxy),		  gdStyled);    }    /* draw a red arrow a 0,0 */    gdImageLine(graph, xtr(2),ytr(3),xtr(2),ytr(-3), i_major);    gdImageLine(graph, xtr(1),ytr(3),xtr(1),ytr(-3), i_major);    gdImageLine(graph, xtr(0),ytr(2),xtr(0),ytr(-2), i_major);    gdImageLine(graph, xtr(-1),ytr(1),xtr(-1),ytr(-1), i_major);    gdImageLine(graph, xtr(-2),ytr(1),xtr(-2),ytr(-1), i_major);    gdImageLine(graph, xtr(-3),ytr(0),xtr(-3),ytr(0), i_major);    if ((fo = fopen(file,"wb")) == NULL) {      perror(program);/*      fprintf(stderr,"Rateup Error: Can't open %s for write\n",file);*/      exit(1);    }    GFORM_GD (graph, fo);        fclose(fo);    gdImageDestroy(graph);    gdImageDestroy(brush_out);    gdImageDestroy(brush_outm);    gdImageDestroy(brush_outp);    free(lhist);}	    double diff(a,b)char *a,*b;{    char res[MAXL], *a1, *b1, *r1;    int c,x,m;    while (!isdigit(*a))	a++;    while (!isdigit(*b))	b++;    a1 = &a[strlen(a)-1];    m = max(strlen(a),strlen(b));    r1 = &res[m+1];    for (b1 = res;b1 <= r1; b1++) *b1 = ' ';    b1 = &b[strlen(b)-1];    r1[1] = 0;	/* Null terminate result */    c = 0;    for (x=0; x<m; x++) {	if (a1 >= a && b1 >= b) {	    *r1 = ((*a1 - c) - *b1) + '0';	} else if (a1 >= a) {	    *r1 = (*a1 - c);	} else {	    *r1 = ('0' - *b1 - c) + '0';	}	if (*r1 < '0') {	    *r1 += 10; 	    c=1;	} else {	    c=0;	}	a1--;b1--;r1--;    }    if (c) {	r1 = &res[m+1];	for (x=0; isdigit(*r1) && x<m; x++,r1--)  {	    *r1 = ('9' - *r1 + c) + '0';	    if (*r1 > '9') {		*r1 -= 10; 		c=1;	    } else {		c=0;	    }	}	return(-atof(res));    } else	return(atof(res));}int readhist(file)char *file;{    FILE *fi;    int i,x, retcode = 0;    char buf[256];    struct HISTORY *hist;    unsigned long rd[5];    time_t cur;    if ((fi = fopen(file,"r")) != NULL) {	if (fscanf(fi,"%ld %s %s\n",&last.time,&last.in[0],&last.out[0]) != 3){           fprintf(stderr,"Read Error: File %s lin 1\n",file);	   retcode = 1;	}        cur = last.time;	x = histvalid=0;	hist = history;	while(!feof(fi)) {	    fgets(buf,256,fi);	    if (sscanf(buf,"%lu %lu %lu %lu %lu",                 &rd[0],&rd[1],&rd[2],&rd[3],&rd[4]) <5 ) {	      rd[3]=rd[1];rd[4]=rd[2]; }	    for (i=0;i<=4;i++){	      rd[i]=rd[i] < 0 ? 0 : rd[i];	    }						    hist->time=rd[0];	    hist->in=rd[1];	    hist->out=rd[2];	    hist->inmax=rd[3];	    hist->outmax=rd[4];	    if (hist->inmax < hist->in)		hist->inmax = hist->in;	    if (hist->outmax < hist->out)		hist->outmax = hist->out;	    if (hist->time > cur) {                fprintf(stderr,"Rateup ERROR: %s found %s's log file was corrupt\n          or not in sorted order:\ntime: %lu.",program,router,(unsigned long)hist->time);	        retcode = 2;		break;	    }	    cur = hist->time;	    if (++x >= Mh) {		struct HISTORY *lh;		Mh += MAX_HISTORY;		lh = realloc(history, (Mh + 1) * sizeof(struct HISTORY));		if (lh == NULL)		{	/*	    fprintf(stderr,"Rateup WARNING: (pay attention to this)\nWARNING: %s found %s's log file had too many entries, data discarded\n",program,router); */		    break;		}		hist = lh + (hist - history);		history = lh;	    }	    hist++;	}	histvalid = x;	fclose(fi);    } else {	retcode = 1;    }    return(retcode);}void readfile() {    char buf[128];    int err, x;    time_t now;    struct HISTORY *hist;    sprintf(buf,"%s.log",router);    if ((err = readhist(buf)) != 0) {	/* Read of log file failed.  Try backup */      /*  fprintf(stderr,"Rateup WARNING: %s could not read the primary log file for %s\n",program,router);*/	sprintf(buf,"%s.old",router);	if ((err = readhist(buf)) != 0) {	/* Backup failed too. New file? */	   /* fprintf(stderr,"Rateup WARNING: %s The backup log file for %s was invalid as well\n",program,router); */	    if (err == 2) exit(1);	    /* File does not exist - it must be created */ 	    now = NOW - DAY_SAMPLE;	    hist = history;	    histvalid = DAY_COUNT+WEEK_COUNT+MONTH_COUNT+YEAR_COUNT-1;	    last.time = now;	    /* calculating a diff does not make sense */	    last.in[0] = 'x';	    now /= DAY_SAMPLE;	    now *= DAY_SAMPLE;        	    for (x=0; x <DAY_COUNT; x++, hist++) {		hist->time = now;	        hist->in = hist->inmax = hist->out = hist->outmax = 0;			now -= DAY_SAMPLE;	    }	    now /= WEEK_SAMPLE;	    now *= WEEK_SAMPLE;	    for (x=0; x<WEEK_COUNT; x++,hist++) {		hist->time = now;	        hist->in = hist->inmax = hist->out = hist->outmax = 0;			now -= WEEK_SAMPLE;	    }	    now /= MONTH_SAMPLE;	    now *= MONTH_SAMPLE;	    for (x=0; x<MONTH_COUNT; x++,hist++) {		hist->time = now;	        hist->in = hist->inmax = hist->out = hist->outmax = 0;			now -= MONTH_SAMPLE;	    }	    now /= YEAR_SAMPLE;	    now *= YEAR_SAMPLE;	    for (x=0; x<YEAR_COUNT; x++,hist++) {		hist->time = now;	        hist->in = hist->inmax = hist->out = hist->outmax = 0;			now -= YEAR_SAMPLE;	    }	    	}    }}	     void update(in, out, abs_max, absupdate)char *in, *out;unsigned long abs_max;int absupdate;{    FILE *fo;    char buf[128],buf1[128],buf2[128];    time_t now,nextnow,plannow;    long inrate, outrate;    unsigned long avc, inmax, outmax;    double period, interval;    int x,n,nout;    struct HISTORY *lhist,*hist;    double inr, outr;    now = NOW;     if (now < last.time) {       fprintf(stderr,"Rateup ERROR: %s found that %s's log file time of %lu was greater than now (%lu)\nERROR: Let's not do the time warp, again. Logfile unchanged.\n",program,router,last.time,now);       return;    }    sprintf(buf,"%s.tmp",router);    sprintf(buf1,"%s.log",router);    sprintf(buf2,"%s.old",router);    if ((lhist = calloc(1,sizeof(struct HISTORY) * (MAX_HISTORY + 1))) == NULL) {	fprintf(stderr,"Rateup ERROR: Out of memory in update\n");	exit(1);    }    hist = lhist;    period = ( now - last.time);    if (period <= 0 || period > (60*60) || last.in[0] == 'x') {	/* if last update is strange */      if (unknaszero == 1) {	inrate = 0;                             /* sync unknown to zero */	outrate = 0;      } else {	inrate = history[0].in;			/* Sync by using last value */	outrate = history[0].out;      }    } else {      /* gauge and absolute */	if ((absupdate != 0) && (absupdate !=3) &&(absupdate!=4)) {	    strcpy(last.in,"0");	    strcpy(last.out,"0");	}	/* gauge option */	if (absupdate == 2) {	    inrate = diff(in,last.in);	    outrate = diff(out,last.out);        } else if (absupdate == 3) {            inrate=diff(in,last.in)*(3600.0/(period*1.0));            outrate=diff(out,last.out)*(3600.0/(period*1.0));        } else if (absupdate == 4)  {            inrate=diff(in,last.in)*(60.0/(period*1.0));            outrate=diff(out,last.out)*(60.0/(period*1.0));	} else {	    inrate = diff(in,last.in) / period;	    outrate = diff(out,last.out) / period;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -