X-Git-Url: http://git.harvie.cz/?p=svn%2FPrometheus-QoS%2F.git;a=blobdiff_plain;f=parselogs.c;fp=parselogs.c;h=6d92680c66e2377bb07cb38906033bce0f178c7a;hp=0000000000000000000000000000000000000000;hb=d4eabe362aad5c58ac1ddfedd4085e1c6a7b884b;hpb=5692c7757847ed4d412173d820392dbf2cfb5a70 diff --git a/parselogs.c b/parselogs.c new file mode 100644 index 0000000..6d92680 --- /dev/null +++ b/parselogs.c @@ -0,0 +1,293 @@ +#include "cll1-0.6.2.h" +#define STRLEN 512 + +extern char *log_dir; +extern const char *version; +extern const char *stats_html_signature; +extern const char *ls; +extern char *html_log_dir; +extern int row_odd_even; +extern char *log_url; +extern char *lms_url; +extern long long int line; + +const char *tr_odd_even(void); +/* implemented in prometheus.c */ + +struct IpLog +{ + char *name; + long traffic; + long guaranted; + int i; + int lmsid; + long l; + list(IpLog); +} *iplog,*iplogs; + +void parse_ip_log(int argc, char **argv) +{ + char *month, *year, *str, *name="(undefined)", *ptr, *ptr2, *filename; + long traffic=0l, traffic_month, total=0, guaranted; + int col, col2, y_ok, m_ok, accept_month, i=1, any_month=0, lmsid; + char mstr[4], ystr[5]; + FILE *f; + string(str,STRLEN); + string(filename,STRLEN); + + if(argv[1][1]=='l') /* -l */ + { + if(argc<4) + { + puts("Missing parameter(s)!\nUsage: prometheus -l Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)"); + exit(-1); + } + else + { + month=argv[2]; + if(eq(month,"Year")) any_month=1; + year=argv[3]; + } + } + else + { + time_t t = time(NULL) - 3600*24 ; /* yesterday's timestamp*/ + struct tm *timep = localtime(&t); + + if(argv[1][1]=='m') /* -m yestarday - month */ + { + strftime(mstr, 4, "%b", timep); + month=mstr; + strftime(ystr, 5, "%Y", timep); + year=ystr; + } + else /* -y yesterday - year */ + { + month="Year"; + any_month=1; + strftime(ystr, 5, "%Y", timep); + year=ystr; + } + } + printf("Analysing traffic for %s %s ...\n",month,year); + + /* sorry... next release of C<<1 header file will include for_path_files(name,path) { } macro */ + sprintf(str,"%s %s/",ls,log_dir); + shell(str); + input(str,STRLEN) + { + if(strstr(str,".log")) + { + ptr=strrchr(str,'\n'); + if(ptr) *ptr='\0'; + sprintf(filename,"%s/%s",log_dir,str); + printf("Parsing %s ...",filename); + accept_month=0; + traffic_month=0; + guaranted=0; + lmsid=-1; + parse(filename) + { + y_ok=m_ok=0; + valid_columns(ptr,_,'\t',col) switch(col) + { + case 2: name = ptr;break; + case 3: traffic = atol(ptr);break; + /* column number - was 7, now 11...*/ + case 7: + case 8: + case 9: + case 10: + case 11: if(isalpha(*ptr)) /* character, not numeric string = date, just one*/ + { + valid_columns(ptr2,ptr,' ',col2) switch(col2) + { + case 2: if(any_month || eq(ptr2,month)) m_ok = 1; break; + case 5: if(eq(ptr2,year)) y_ok = 1; break; + } + } + else + { + if(col == 7) guaranted = atol(ptr); + if(col == 10) lmsid = atoi(ptr); + } + } + + if(y_ok && m_ok) + { + traffic_month += traffic; + accept_month = 1; + } + } + done; /* ugly macro end */ + + if(accept_month) + { + create(iplog,IpLog); + iplog->name = name; + iplog->guaranted = guaranted; + iplog->traffic = traffic_month; + iplog->lmsid = lmsid; + insert(iplog,iplogs,desc_order_by,traffic); + printf(" %ld MB\n",iplog->traffic); + } + else + { + puts(" no records."); + } + } + } + sprintf(str,"%s/%s-%s.html",html_log_dir,year,month); + printf("Writing %s ... ",str); + f=fopen(str,"w"); + if(f > 0) + { + fprintf(f, "\n\ +\n\ +\n\ +\n\ +\n\ +\n ", + month, year); + + row_odd_even = 0; + for_each(iplog, iplogs) + { + if(iplog->traffic) + { + fprintf(f, "%s\n\ +\n\ +\n\ + \n\ + \n", + iplog->traffic, iplog->traffic>>10, iplog->guaranted); + total+=iplog->traffic>>10; + iplog->i=i; + iplog->l=total; + } + } + fprintf(f,"\ + \ + \ + \n", total, line); + fputs("
%s %slmsData transfersMin.speed
%d%s", + tr_odd_even(), i++, log_url, iplog->name, iplog->name); + if(iplog->lmsid > 0) + { + /*base URL will be configurable soon ... */ + fprintf(f, "%04d\n", lms_url, iplog->lmsid, iplog->lmsid); + } + else if(iplog->lmsid == 0) + { + fputs("-------",f); + } + fprintf(f, "%ld MB%ld GB%ld kb/s
Total:%ld GB%Ld kb/s
\n", f); + + row_odd_even = 0; + if(i>10) + { + fputs("

\n\ +\n\ +\n\ +\n\ +\n\ +\n\ +\n",f); + + if_exists(iplog,iplogs,iplog->l>=total/4) + { + fprintf(f,"%s\n", tr_odd_even()); + fprintf(f,"\n\ +\n\ +\n\ +\n", + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); + } + + if_exists(iplog,iplogs,iplog->i==10) + { + fprintf(f,"%s\n", tr_odd_even()); + fprintf(f,"\n\ +\n\ +\n\ +\n", + (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); + } + + if_exists(iplog,iplogs,iplog->l>=total/2) + { + fprintf(f,"%s\n", tr_odd_even()); + fprintf(f,"\n\ +\n\ +\n\ +\n", + iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total)); + } + + if_exists(iplog,iplogs,iplog->l>=4*total/5) + { + fprintf(f,"%s\n",tr_odd_even()); + fprintf(f,"\n\ +\n\ +\n\ +\n", + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); + } + + if_exists (iplog,iplogs,iplog->i>=i/5) + { + fprintf(f,"%s\n",tr_odd_even()); + fprintf(f,"\n\ +\n\ +\n\ +\n", + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); + } + + if_exists(iplog,iplogs,iplog->i>=i/4) + { + fprintf(f,"%s\n", tr_odd_even()); + fprintf(f,"\n\ +\n\ +\n\ +\n", + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); + } + + if_exists(iplog,iplogs,iplog->i>=i/2) + { + fprintf(f,"%s\n",tr_odd_even()); + fprintf(f,"\n\ +\n\ +\n\ +\n", + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); + } + + if_exists(iplog,iplogs,iplog->i>=4*i/5) + { + fprintf(f,"%s\n",tr_odd_even()); + fprintf(f,"\n\ +\n\ +\n\ +\n", + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); + } + + fprintf(f,"\n", log_url); + fprintf(f,"\n\ +\n\ +\n\ +\n",i-1,total); + fputs("
Enterprise Resource Planning (ERP)
Analytic categoryActive ClassesData transfers
Top 25%% of traffic%d%d %%%ld GB%d %%
Top 10 downloaders10%d %%%ld GB%d %%
Top 50%% of traffic%d%d %%%ld GB%d %%
Top 80%% of traffic%d%d %%%ld GB%d %%
Top 20%% downloaders%d%d %%%ld GB%d %%
Top 25%% downloaders%d%d %%%ld GB%d %%
Top 50%% downloaders%d%d %%%ld GB%d %%
Top 80%% downloaders%d%d %%%ld GB%d %%
All users, all traffic%d100 %%%ld GB100 %%
\n", f); + } + + fprintf(f, stats_html_signature, version); + fclose(f); + puts("done."); + } + else + { + perror(str); + } +}