/***************************** ** woho's MRTG Logfile cgi ** *****************************/ /* modifications by Roman Vasiyarov aka Winex, rvasiyarov@gmail.com 2007-09-01 */ #define VERSION "1.3" #include "./mrtglog.h" #include #include typedef struct { time_t t; float in; float out; } mrtg; int str2time(char *s); signed long round(float f); /************************************/ int main(int argc, char *argv[]) { mrtg ar[3000]; int i, y, a1, a2, a3, a4 , a5, diff, newmon, oldmon=13, year; char s[128], *str, *ctmp, *start, *stop, *log, ch, ch2, ch3, hex[8]; time_t t1, t2; struct tm *zeit; float IN, OUT, ISUM=0, OSUM=0; FILE *fp; if (argc == 4) { start = argv[1]; stop = argv[2]; log = argv[3]; } else { ctmp = (char *) getenv("QUERY_STRING"); //printf("%sQUERY_STRING: '%s'\n", HTMLHEAD, ctmp); if (ctmp) { y = strlen(ctmp) + 1; str = (char *)malloc (y * sizeof(char)); *str = '\0'; for (i = 0; i < strlen(ctmp); i++) { ch = ctmp[i]; // fflush (stdout); if (ctmp[i] == '+') ch = ' '; if (ctmp[i] == '%') { ch2 = ctmp[i+1]; ch3 = ctmp[i+2]; i += 2; snprintf (hex, sizeof(hex), "%c%c", ch2, ch3); ch = strtol (hex, NULL, 16); if ((ch == 10) || (ch == 13)) ch = ' '; } snprintf (&str[strlen(str)], y-strlen(str),"%c",ch); } while (str) { ctmp = str; while (*ctmp != '=' && *ctmp != '&') { *ctmp = tolower (*ctmp); ctmp++; } if ((char *)strstr(str,"start") == str) { start = ctmp+1; ctmp = (char *) strchr(start, '&'); } else if ((char *)strstr(str,"stop") == str) { stop = ctmp+1; ctmp = (char *) strchr(stop, '&'); } else if ((char *)strstr(str,"log") == str) { log = ctmp+1; ctmp = (char *) strchr(log, '&'); } else { printf("%sParameter '%s' is wrong\n",HTMLHEAD,str); exit(1); } if (ctmp) { str = ctmp+1; *ctmp = '\0'; } else str = NULL; } } } if (!start || !stop || !log) { printf("%sUsage: mrtglog 2003-05.01 2003-05-31 mrtg.log # Count all traffic in May 2003\n", HTMLHEAD); exit(1); } i = y = IN = OUT = 0; t1 = str2time(start); t2 = str2time(stop); sprintf(s, "%s%s", PFAD, log); fp = fopen(s, "r"); if (fp) { while (fgets(s, 100, fp)) { if (sscanf(s, "%d %d %d %d %d\n", &a1, &a2, &a3, &a4, &a5) == 5) { ar[y].t = a1; ar[y].in = a2; ar[y].out = a3; y++; } } fclose(fp); } else { printf("%scan't open '%s'!\n", HTMLHEAD, s); exit(1); } str = (char *) strrchr(log,'/'); if (str) *str++; else str = log; if (ctmp = (char *) strstr(str,".log")) *ctmp=0; printf("%s\n\n%s %s %s %s %s %s\n" "\n\n\ \n\ \n\ \n\ \n", HTMLHEAD,str,STATS,FROM,start,TO,stop,str,STATS,FROM,start,TO,stop); for (i = 0; i < y; i++) { if ((t1 <= ar[i].t) && (ar[i].t <= t2)) { zeit = gmtime((const time_t *)&ar[i].t); newmon = zeit->tm_mon; if ((oldmon != 13 && newmon != oldmon) || i+1 == y) { if (i+1 == y) { diff = ar[i].t - ar[i+1].t; IN = IN + ar[i].in * diff; OUT = OUT + ar[i].out * diff; } if (IN == 0 && OUT == 0) break; ISUM += IN; OSUM += OUT; printf("\n", year+1900,oldmon+1,round(IN/1024/1024),round(OUT/1024/1024),round((IN+OUT)/1024/1024)); IN = OUT = 0; } if (i+1 < y) { oldmon = newmon; year = zeit->tm_year; diff = ar[i].t - ar[i+1].t; IN = IN + ar[i].in * diff; OUT = OUT + ar[i].out * diff; } } } /* // what is this??? if (IN != 0 || OUT != 0) { ISUM += IN; OSUM += OUT; printf("\n", year+1900,oldmon+1,round(IN/1024/1024),round(OUT/1024/1024),round((IN+OUT)/1024/1024)); } */ printf("\n\ \n

%s

\n\

%s %s %s %s %s

INOUTTOTAL
%04i-%02i%d MB%d MB%d MB
%04i-%02i%d MB%d MB%d MB
TOTAL%d MB%d MB%d MB
\n
\n\ from \n\ to \n\ \n\ \n\
\n

%s

\n\ mrtglog %s, copyleft 2000 by http://www.netpark.at
\n\ modified by Roman Vasiyarov aka Winex\n\ \n\n", round(ISUM/1024/1024),round(OSUM/1024/1024),round((ISUM+OSUM)/1024/1024), argv[0],start,stop,log,INFOTEXT,VERSION); return 0; } /************************************/ // desc: converts ISO date YYYY-MM-DD to int (time_t?) int str2time(char *s) { struct tm tim; int yyyy, mm, dd; if (sscanf(s, "%d.%d.%d", &yyyy, &mm, &dd) != 3) { printf("%s: can't convert '%s' to date\n", HTMLHEAD, s); exit(10); } if (yyyy < 100) yyyy += 2000; tim.tm_sec = tim.tm_min = tim.tm_hour = 0; tim.tm_mday = dd; tim.tm_mon = mm - 1; tim.tm_year = yyyy - 1900; return mktime(&tim); } /************************************/ signed long round(float fval) { signed long basis, nachkomma; if (!fval) return 0; basis = fval; if ((nachkomma = labs((fval-basis)*10)) == 0) return basis; if (nachkomma > 5) { if (basis > 0) return ++basis; else return --basis; } else if (nachkomma < 5) return basis; else { nachkomma = labs((fval-basis)*100); if (nachkomma > 50) { if (basis > 0) return ++basis; else return --basis; } else if (nachkomma < 50) return basis; else { nachkomma = labs((fval-basis)*1000); if (nachkomma > 500) { if (basis > 0) return ++basis; else return --basis; } else if (nachkomma < 500) return basis; else { nachkomma = labs((fval-basis)*10000); if (nachkomma > 5000) { if (basis > 0) return ++basis; else return --basis; } else if (nachkomma < 5000) return basis; else return basis; } } } } // and in the end, the love you take, is equal to the love you make. (John Lennon)