| 1 | |
| 2 | #include "cll1-0.6.h" |
| 3 | |
| 4 | #define STRLEN 128 |
| 5 | |
| 6 | const char *logdir="/var/www/logs"; |
| 7 | const char *htmldir="/var/www/logs/html"; |
| 8 | |
| 9 | /* Modified: Michal Polak (xChaos) 20070202 */ |
| 10 | |
| 11 | struct Ip |
| 12 | { |
| 13 | char *name; |
| 14 | long traffic; |
| 15 | list(Ip); |
| 16 | } *ip,*ips; |
| 17 | |
| 18 | int main (int argc, char **argv) |
| 19 | { |
| 20 | char *month,*year,*str,*name,*ptr,*ptr2; |
| 21 | long traffic,traffic_month,total=0; |
| 22 | int col,col2,y_ok,m_ok,accept_month,i=1,any_month=0; |
| 23 | FILE *f; |
| 24 | |
| 25 | string(str,STRLEN); |
| 26 | |
| 27 | if(argc<3) |
| 28 | { |
| 29 | puts("Usage: monthly-stats Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)"); |
| 30 | exit(-1); |
| 31 | } |
| 32 | else |
| 33 | { |
| 34 | month=argv[1]; |
| 35 | if(eq(month,"Year")) any_month=1; |
| 36 | year=argv[2]; |
| 37 | } |
| 38 | |
| 39 | sprintf(str,"/bin/ls %s/*.log",logdir); |
| 40 | shell(str); |
| 41 | input(str,STRLEN) |
| 42 | { |
| 43 | ptr=strrchr(str,'\n'); |
| 44 | if(ptr) *ptr='\0'; |
| 45 | printf("Parsing %s ...",str); |
| 46 | accept_month=0; |
| 47 | traffic_month=0; |
| 48 | parse(str) |
| 49 | { |
| 50 | y_ok=m_ok=0; |
| 51 | valid_columns(ptr,_,'\t',col) switch(col) |
| 52 | { |
| 53 | case 2: name=ptr;break; |
| 54 | case 3: traffic=atol(ptr);break; |
| 55 | case 7: valid_columns(ptr2,ptr,' ',col2) switch(col2) |
| 56 | { |
| 57 | case 2: if(any_month || eq(ptr2,month)) m_ok=1; break; |
| 58 | case 5: if(eq(ptr2,year)) y_ok=1; break; |
| 59 | } |
| 60 | } |
| 61 | if(y_ok && m_ok) |
| 62 | { |
| 63 | traffic_month+=traffic; |
| 64 | accept_month=1; |
| 65 | } |
| 66 | } |
| 67 | done; |
| 68 | if(accept_month) |
| 69 | { |
| 70 | create(ip,Ip); |
| 71 | ip->name=name; |
| 72 | ip->traffic=traffic_month; |
| 73 | insert(ip,ips,desc_order_by,traffic); |
| 74 | printf(" %ld MB\n",ip->traffic); |
| 75 | } |
| 76 | else |
| 77 | puts(" no records."); |
| 78 | } |
| 79 | sprintf(str,"%s/%s-%s.html",htmldir,year,month); |
| 80 | printf("Writing %s ...",str); |
| 81 | f=fopen(str,"w"); |
| 82 | if(f) |
| 83 | { |
| 84 | fprintf(f,"<table border><tr><th colspan=\"4\">Data transfers - %s %s</th></tr>\n ",month,year); |
| 85 | every(ip,ips) |
| 86 | if(ip->traffic) |
| 87 | { |
| 88 | fprintf(f,"<tr><td align=\"right\">%d</td><th>%s</td><td align=\"right\">%ld MB</td><th align=\"right\">%ld GB</th></tr>\n",i++,ip->name,ip->traffic,ip->traffic>>10); |
| 89 | total+=ip->traffic>>10; |
| 90 | } |
| 91 | fprintf(f,"<tr><th colspan=\"3\" align=\"left\">Total:</th><th align=\"right\">%ld GB</th></tr>\n",total); |
| 92 | fputs("</table>\n",f); |
| 93 | fclose(f); |
| 94 | puts(" done."); |
| 95 | } |
| 96 | } |