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