📄 rateup.c
字号:
} } if (inrate < 0 || inrate > abs_max) { if (unknaszero == 1) { inrate = 0; /* sync unknown to zero */ } else { inrate = history[0].in; /* Sync by using last value */ } } if (outrate < 0 || outrate > abs_max) { if (unknaszero == 1) { outrate = 0; /* sync unknown to zero */ } else { outrate = history[0].out; /* Sync by using last value */ } } if ((fo = fopen(buf,"w")) != NULL) { fprintf(fo,"%lu %s %s\n",now,in,out); last.time = now; /* what is this good for? */ /* gauge und absolute */ if ((absupdate != 0) && (absupdate !=3) &&(absupdate!=4)) { strcpy(last.in,"0"); strcpy(last.out,"0"); } else { strcpy(last.in,in); strcpy(last.out,out); } fprintf(fo,"%lu %lu %lu %lu %lu\n",now, inrate, outrate, inrate, outrate); nout = 1; hist->time = now; hist->in = inrate; hist->out = outrate; hist->inmax = inrate; hist->outmax = outrate; hist++; /* just in case we were dead for a long time, don't try to gather data from non existing log entries */ now = plannow = history[0].time; plannow /= DAY_SAMPLE; plannow *= DAY_SAMPLE; n = 0; /* gobble up every shred of data we can get ... */ if (plannow < now) { NEXT((now-plannow)); fprintf(fo,"%lu %lu %lu %lu %lu\n",now, (unsigned long)inr,(unsigned long)outr,inmax,outmax); hist->time = now; hist->in = inr; hist->out = outr; hist->inmax = inmax; hist->outmax = outmax; nout++;hist++; now = plannow; } for (x=1; x<DAY_COUNT; x++) { NEXT(DAY_SAMPLE); fprintf(fo,"%lu %lu %lu %lu %lu\n",now, (unsigned long)inr,(unsigned long)outr,inmax,outmax); hist->time = now; hist->in = inr; hist->out = outr; hist->inmax = inmax; hist->outmax = outmax; nout++;hist++; now -= DAY_SAMPLE; } plannow=now; plannow /= WEEK_SAMPLE; plannow *= WEEK_SAMPLE; if (plannow < now) { NEXT((now-plannow)); fprintf(fo,"%lu %lu %lu %lu %lu\n",now, (unsigned long)inr,(unsigned long)outr,inmax,outmax); hist->time = now; hist->in = inr; hist->out = outr; hist->inmax = inmax; hist->outmax = outmax; nout++;hist++; now = plannow; } for (x=0; x<WEEK_COUNT; x++) { NEXT(WEEK_SAMPLE); fprintf(fo,"%lu %lu %lu %lu %lu\n",now, (unsigned long)inr,(unsigned long)outr,inmax,outmax); hist->time = now; hist->in = inr; hist->out = outr; hist->inmax = inmax; hist->outmax = outmax; nout++;hist++; now -= WEEK_SAMPLE; } plannow=now; plannow /= MONTH_SAMPLE; plannow *= MONTH_SAMPLE; if (plannow < now) { NEXT((now-plannow)); fprintf(fo,"%lu %lu %lu %lu %lu\n",now, (unsigned long)inr,(unsigned long)outr,inmax,outmax); hist->time = now; hist->in = inr; hist->out = outr; hist->inmax = inmax; hist->outmax = outmax; nout++;hist++; now = plannow; } for (x=0; x<MONTH_COUNT; x++) { NEXT(MONTH_SAMPLE); fprintf(fo,"%lu %lu %lu %lu %lu\n",now, (unsigned long)inr,(unsigned long)outr,inmax,outmax); hist->time = now; hist->in = inr; hist->out = outr; hist->inmax = inmax; hist->outmax = outmax; nout++;hist++; now -= MONTH_SAMPLE; } plannow=now; plannow /= YEAR_SAMPLE; plannow *= YEAR_SAMPLE; if (plannow < now) { NEXT((now-plannow)); fprintf(fo,"%lu %lu %lu %lu %lu\n",now, (unsigned long)inr,(unsigned long)outr,inmax,outmax); hist->time = now; hist->in = inr; hist->out = outr; hist->inmax = inmax; hist->outmax = outmax; nout++;hist++; now = plannow; } for (x=0; x<YEAR_COUNT; x++) { NEXT(YEAR_SAMPLE); fprintf(fo,"%lu %lu %lu %lu %lu\n",now, (unsigned long)inr,(unsigned long)outr,inmax,outmax); hist->time = now; hist->in = inr; hist->out = outr; hist->inmax = inmax; hist->outmax = outmax; nout++;hist++; now -= YEAR_SAMPLE; } if (ferror(fo) || fclose(fo)) { perror(program);/* fprintf(stderr,"Rateup ERROR: Can't write new log file\n"); */ exit(1); } /* another fix to get things working under NT */ if (unlink(buf2)) {/* fprintf(stderr,"Rateup WARNING: %s Can't remove %s updating log file\n", program,buf2); */ } if (rename(buf1,buf2)) {/* fprintf(stderr,"Rateup WARNING: %s Can't rename %s to %s updating log file\n", program,buf1,buf2); */ } if (rename(buf,buf1)) {/* fprintf(stderr,"Rateup WARNING: %s Can't rename %s to %s updating log file\n", program,buf,buf1); */ } for (n=0; n<nout && n < MAX_HISTORY; n++) { history[n] = lhist[n]; } } else { perror(program); fprintf(stderr,"Rateup ERROR: Can't open %s for write\n",buf); exit(1); } free(lhist);}void init_colour(int *colmap, int c0, int c1, int c2) { *colmap++ = c0; *colmap++ = c1; *colmap = c2;} int main(argc,argv)int argc;char **argv;{ int x,argi,used,initarg; program = argv[0]; if (argc < 2) { fprintf(stderr,"%s for mrtg %s\n" "Usage: %s directory basename [sampletime] [t sampletime] " "[-(t)ransparent] " "[u|a|g|h|m in out abs_max] " "[i/p file maxvi maxvo maxx maxy growright step bits]\n", program,VERSION,program); return(1); } routerpath = argv[1]; /* this is for NT compatibility, because it does not seem to like rename across directories */ if (chdir(routerpath)) { fprintf(stderr,"Rateup ERROR: Chdir to %s failed ...\n",routerpath); return(1); } /* Initialiase the colour variables - should be overwritten */ init_colour(&col_in[0], c_in); init_colour(&col_out[0], c_out); init_colour(&col_inm[0], c_inm); init_colour(&col_outm[0], c_outm); init_colour(&col_outp[0], c_outp); if ((history = calloc(1,sizeof(struct HISTORY)*(MAX_HISTORY + 1))) == NULL) { fprintf(stderr,"Rateup ERROR: Out of memory in main\n"); exit(1); }#ifdef __WATCOMC__ memset(history,0,sizeof(struct HISTORY)*(MAX_HISTORY + 1));#endif Mh = MAX_HISTORY; router = argv[2]; NOW = atol(argv[3]); /* from mrtg-2.x with x>5 rateup calling syntax changed to to support time properly ... this is for backward compat we check if now is remotely reasonable ... */ if (NOW > 10*365*24*60*60) { initarg=4; } else { initarg=3; time(&NOW); } readfile(); used = 1; for (argi = initarg; argi < argc; argi += used) { switch(argv[argi][0]) { case '-': /* -options */ switch(argv[argi][1]) { case 't': /* Transparent Image */ transparent = 1; used = 1; break; case 'T': /* non-Transparent Image */ transparent = 0; used = 1; break; case 'z': /* unknown as zero */ used = 1; unknaszero = 1; break; case 'Z': /* repeat last */ used = 1; unknaszero = 0; break; default: fprintf(stderr,"Rateup ERROR: Unknown option: %s, sorry!\n",argv[argi]); return(1); } break; case 'i': /* Create PPM Image record */ image(argv[argi+1], /* Image*/ atol(argv[argi+2]), /* Max Value In */ atol(argv[argi+3]), /* Max Value Out */ atol(argv[argi+4]), /* xsize maxx */ atol(argv[argi+5]), /* ysize maxy */ atof(argv[argi+6]), /* xscale */ atof(argv[argi+7]), /* yscale */ atol(argv[argi+8]), /* growright */ atol(argv[argi+9]), /* step */ atol(argv[argi+10]), /* bits */ atol(argv[argi+11]), /* ytics */ atof(argv[argi+12]), /* yticsfactor */ 0); used = 13; break; case 'p': /* Create PPM Image record with Peak values*/ image(argv[argi+1], atol(argv[argi+2]), /* Max Value In */ atol(argv[argi+3]), /* Max Value Out */ atol(argv[argi+4]), /* xsize maxx */ atol(argv[argi+5]), /* ysize maxy */ atof(argv[argi+6]), /* xscale */ atof(argv[argi+7]), /* yscale */ atol(argv[argi+8]), /* growright */ atol(argv[argi+9]), /* step */ atol(argv[argi+10]), /* bits */ atol(argv[argi+11]), /* ytics */ atof(argv[argi+12]), /* yticsfactor */ 1); used = 13; break; case 'r': /* Create random records, then update */ for (x=0; x<histvalid; x++) { history[x].in = rand() % atoi(argv[argi+1]); history[x].out = rand() % atoi(argv[argi+2]); } case 'u': /* Update file */ if (argv[argi][1] == 'p') { dorelpercent = 1; } update(argv[argi+1],argv[argi+2],atol(argv[argi+3]),0); used = 4; break; case 'a': /* Absolute Update file */ if (argv[argi][1] == 'p') { dorelpercent = 1; } update(argv[argi+1],argv[argi+2],atol(argv[argi+3]),1); used = 4; break; case 'g': /* Gauge Update file */ if (argv[argi][1] == 'p') { dorelpercent = 1; } update(argv[argi+1],argv[argi+2],atol(argv[argi+3]),2); used = 4; break; case 'h': if (argv[argi][1] == 'p') { dorelpercent = 1; } update(argv[argi+1],argv[argi+2],atol(argv[argi+3]),3); used = 4; break; case 'm': if (argv[argi][1] == 'p') { dorelpercent = 1; } update(argv[argi+1],argv[argi+2],atol(argv[argi+3]),4); used = 4; break; case 'W': /* Week format */ weekformat = argv[argi+1][0]; used = 2; break; case 'c': /* Colour Map */ sscanf(argv[argi+1],"#%2x%2x%2x", &col_in[0], &col_in[1], &col_in[2]); sscanf(argv[argi+2],"#%2x%2x%2x", &col_out[0], &col_out[1], &col_out[2]); sscanf(argv[argi+3],"#%2x%2x%2x", &col_inm[0], &col_inm[1], &col_inm[2]); sscanf(argv[argi+4],"#%2x%2x%2x", &col_outm[0], &col_outm[1], &col_outm[2]); used = 5; break; case 'C': /* Extented Colour Map */ sscanf(argv[argi+1],"#%2x%2x%2x", &col_in[0], &col_in[1], &col_in[2]); sscanf(argv[argi+2],"#%2x%2x%2x", &col_out[0], &col_out[1], &col_out[2]); sscanf(argv[argi+3],"#%2x%2x%2x", &col_inm[0], &col_inm[1], &col_inm[2]); sscanf(argv[argi+4],"#%2x%2x%2x", &col_outm[0], &col_outm[1], &col_outm[2]); sscanf(argv[argi+5],"#%2x%2x%2x", &col_outp[0], &col_outp[1], &col_outp[2]); used = 6; break; case 't': NOW = atol(argv[argi+1]); used = 2; break; case 'k': kilo = atol(argv[argi+1]); used = 2; break; case 'K': kMG = calloc(strlen(argv[argi+1])+1, sizeof(char)); strcpy (kMG, argv[argi+1]); kMGcopy = calloc(strlen(argv[argi+1])+1, sizeof(char)); used = 2; break; case 'l': /* YLegend - rewritten by Oliver Haid */ { int i, j, k, loop = 1; char* qstr; longup = (char *)calloc(1,100); *longup = 0; /* this rather twisty argument scanning is necesary because NT command.coms rather dumb argument passing .... or because we don't know better. Under Unix we just would say. if ((sscanf(argv[argi+1],"[%[^]]]", longup); */ for (i=1; (i<argc) && loop; i++) /* check all args delimited by '[' and ']' */ { qstr = argv[argi+i]; /* fprintf(stderr, "qstr = \"%s\"\n", qstr); */ for (j=0; (j<strlen(qstr)) && loop; j++) /* check arg */ { if (qstr[j] == '[') /* beginning of YLegend-string */ { j++; /* skip '[' */ *longup = 0; /* clear */ } if (strlen(qstr+j) >= 2) /* check for escapes... */ { if (qstr[j] == ']') /* end of YLegend-string */ { loop = 0; /* exit */ } else if (qstr[j] == '\\') /* escape character '\' found */ { j++; /* skip '\' */ if (qstr[j+1] == 0) /* end of qstr reached */ { fprintf(stderr, "Rateup ERROR: YLegend: bad escape \"\\\"! Use \"\\\\\" for \"\\\", \"\\[\" for \"[\" or \"\\]\" for \"]\".\n"); return(1); } if ((qstr[j] == '[') || (qstr[j] == '\\') || (qstr[j] == ']')) { /* escape okay -> append qstr[j] */ k = strlen(longup); if ((k + 1) > (size_t)99) { fprintf(stderr, "1: Rateup ERROR: YLegend to long ... !\n"); return(1); } longup[k] = qstr[j]; longup[k + 1] = 0; } else /* bad escape */ { fprintf(stderr, "2: Rateup ERROR: YLegend: bad escape \"\\%c\"! Use \"\\\\\" for \"\\\", \"\\[\" for \"[\" or \"\\]\" for \"]\".\n", qstr[j]); return(1); } } else /* append qstr[j] */ { k = strlen(longup); if ((k + 1) > (size_t)99) { fprintf(stderr, "3: Rateup ERROR: YLegend to long ... !\n"); return(1); } longup[k] = qstr[j]; longup[k + 1] = 0; } } else /* handle last character */ { if (qstr[j] == ']') /* end of YLegend-string */ { loop = 0; /* exit */ } else /* append last character */ { if ((strlen(longup) + strlen(qstr+j)) > (size_t)99) { fprintf(stderr, "4: Rateup ERROR: YLegend to long ... !\n"); return(1); } strcat(longup, qstr+j); } } } /* for (j=0; (j<strlen(qstr)) && loop; j++) */ k = strlen(longup); if ((k + 1) > (size_t)99) { fprintf(stderr, "5: Rateup ERROR: YLegend to long ... !\n"); return(1); } /* strcat (longup," "); */ longup[k] = ' '; /* append space */ longup[k + 1] = 0; } /* for (i=1; (i<argc) && loop; i++) */ used = i; } /* remove the last space in longup: */ longup[max(0, strlen(longup)-1)] = 0; shortup = longup; /* fprintf(stderr, "YLegend = \"%s\"\n", longup); */ break; default: fprintf(stderr,"Rateup ERROR: Can't cope with %s, sorry!\n",argv[argi]); return(1); } } return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -