From 5692c7757847ed4d412173d820392dbf2cfb5a70 Mon Sep 17 00:00:00 2001 From: xchaos Date: Sun, 7 Oct 2012 18:15:00 +0000 Subject: [PATCH] begining of code cleanup: more modular source code git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@193 251d49ef-1d17-4917-a970-b30cf55b089b --- Makefile | 2 +- parselog.c | 293 +++++++++++++++++++++++++++++++++++++++++++++++++++ prometheus.c | 280 +----------------------------------------------- 3 files changed, 298 insertions(+), 277 deletions(-) create mode 100644 parselog.c diff --git a/Makefile b/Makefile index 35a1f60..2145cfb 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ sbindir=$(prefix)/sbin sysconfdir=/etc main: prometheus - $(CC) -o prometheus prometheus.c + $(CC) -o prometheus parselog.c prometheus.c deb: main debian/prometheus.debian diff --git a/parselog.c b/parselog.c new file mode 100644 index 0000000..6d92680 --- /dev/null +++ b/parselog.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); + } +} diff --git a/prometheus.c b/prometheus.c index b6eeb27..6401255 100644 --- a/prometheus.c +++ b/prometheus.c @@ -26,7 +26,7 @@ GNU General Public License is located in file COPYING */ -#define STRLEN 256 +#define STRLEN 512 #define FIRSTGROUPID 1024 #define FIRSTIPCLASS 2048 #undef DEBUG @@ -71,6 +71,9 @@ int use_jquery_popups = 1; int row_odd_even = 0; /* */ +void parse_ip_log(int argc, char **argv); +/* implementid in parselog.c */ + const char *tr_odd_even(void) { row_odd_even = 1 - row_odd_even; @@ -776,283 +779,8 @@ char *parse_datafile_line(char *str) } } -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); - } -} void append_log(struct IP *self) /*using global variables*/ { -- 2.30.2