X-Git-Url: http://git.harvie.cz/?p=svn%2FPrometheus-QoS%2F.git;a=blobdiff_plain;f=prometheus.c;h=5420588d352e8d6550e0c430305926ea2e33bfc2;hp=7ab712f737f15fa340c797d35e4656581e6c0273;hb=b014f18189d582cd408e0c488ef734a070500e5b;hpb=bb9e11ee53e58e69ace298e537acd814fef53271 diff --git a/prometheus.c b/prometheus.c index 7ab712f..5420588 100644 --- a/prometheus.c +++ b/prometheus.c @@ -7,7 +7,7 @@ /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -/* Modified by: xChaos, 20110221 +/* Modified by: xChaos, 20110428 ludva, 20080415 Prometheus QoS is free software; you can redistribute it and/or @@ -85,6 +85,7 @@ void help(void) -s start shaping! (keep data transfer statistics - but apply shaping)\n\ */ } + /* === Configuraration file values defaults - stored in global variables ==== */ int filter_type = 1; /*1 mark, 2 classify*/ @@ -104,6 +105,7 @@ char *wan_medium = "100Mbit"; /* 10Mbit/100Mbit ethernet */ char *qos_leaf = "sfq perturb 5"; /* leaf discipline */ char *qos_free_zone = NULL; /* QoS free zone */ int qos_proxy = 1; /* include proxy port to QoS */ +int found_lmsid = 0; /* show links to users in LMS information system */ int include_upload = 1; /* upload+download=total traffic */ char *proxy_ip = "192.168.1.1/32"; /* our IP with proxy port */ int proxy_port = 3128; /* proxy port number */ @@ -134,7 +136,7 @@ const int idxtable_treshold2 = 12; /* this is no longer configurable */ const int idxtable_bitmask1 = 3; /* this is no longer configurable */ const int idxtable_bitmask2 = 3; /* this is no longer configurable */ -/* ==== This is C<<1 stuff - learn C<<1 first! http://cll1.arachne.cz ==== */ +/* ==== This is C<<1 stuff - learn C<<1 first! https://dev.arachne.cz/svn/cll1h ==== */ struct IP { @@ -210,7 +212,7 @@ void TheIP(void) ip->addr = ""; ip->sharing = NULL; ip->prio = highest_priority+1; - ip->lmsid = \ + ip->lmsid = -1; ip->fixedprio = \ ip->mark = \ ip->min = \ @@ -302,10 +304,14 @@ char *very_ugly_ipv4_code(char *inip,int bitmask,int format_as_chainname) } char *hash_id(char *ip,int bitmask) -{ return very_ugly_ipv4_code(ip,bitmask,1); } +{ + return very_ugly_ipv4_code(ip,bitmask,1); +} char *subnet_id(char *ip,int bitmask) -{ return very_ugly_ipv4_code(ip,bitmask,0); } +{ + return very_ugly_ipv4_code(ip,bitmask,0); +} /* ================= Let's parse configuration file here =================== */ @@ -531,8 +537,14 @@ void get_traffic_statistics(void) if(accept && traffic>0 && ipaddr) { - if(proxyflag)printf("(proxy) "); - else if(!downloadflag) printf("(upload) "); + if(proxyflag) + { + printf("(proxy) "); + } + else if(!downloadflag) + { + printf("(upload) "); + } printf("IP %s: %Lu M (%ld pkts)\n", ipaddr, traffic, pkts); if_exists(ip,ips,eq(ip->addr,ipaddr)); @@ -551,9 +563,13 @@ void get_traffic_statistics(void) if(downloadflag) { if(proxyflag) + { ip->proxy=traffic; + } else + { ip->traffic+=traffic; + } ip->direct=ip->traffic-ip->upload-ip->proxy; ip->pktsdown=pkts; } @@ -583,8 +599,18 @@ void get_traffic_statistics(void) void safe_run(char *cmd) { - if(dry_run) printf("\n=>%s\n",cmd); else system(cmd); - if(log_file) fprintf(log_file,"%s\n",cmd); + if(dry_run) + { + printf("\n=>%s\n",cmd); + } + else + { + system(cmd); + } + if(log_file) + { + fprintf(log_file,"%s\n",cmd); + } } void save_line(char *line) @@ -594,7 +620,7 @@ void save_line(char *line) void run_restore(void) { - char *restor, *str; + char *restor; string(restor,STRLEN); /*-----------------------------------------------------------------*/ @@ -605,11 +631,11 @@ void run_restore(void) fclose(iptables_file); if(dry_run) { - parse(iptablesfile) - { - str=_; - printf("%s\n", str); - }done; + parse(iptablesfile) + { + printf("%s\n",_); + } + done; } sprintf(restor,"%s <%s",iptablesrestore, iptablesfile); @@ -622,19 +648,37 @@ void run_restore(void) void parse_ip(char *str) { - char *ptr=str,*ipaddr=NULL,*ipname=NULL;; + char *ptr,*ipaddr=NULL,*ipname=NULL,*lmsid=NULL; + + ptr=strchr(str,'{'); + if(ptr) + { + lmsid=++ptr; + while(*ptr && *ptr!='}') + { + ptr++; + } + *ptr=0; + } + ptr=str; while(*ptr && *ptr!=' ' && *ptr!=9) + { ptr++; + } *ptr=0; ipaddr=str; ptr++; while(*ptr && (*ptr==' ' || *ptr==9)) + { ptr++; + } ipname=ptr; while(*ptr && *ptr!=' ' && *ptr!=9) + { ptr++; + } *ptr=0; if_exists(ip,ips,eq(ip->addr,ipaddr)); @@ -644,6 +688,11 @@ void parse_ip(char *str) } ip->addr=ipaddr; ip->name=ipname; + if(lmsid) + { + ip->lmsid=atoi(lmsid); + found_lmsid=1; + } } char *parse_datafile_line(char *str) @@ -657,7 +706,9 @@ char *parse_datafile_line(char *str) return ptr; } else + { return NULL; + } } struct IpLog @@ -666,6 +717,7 @@ struct IpLog long traffic; long guaranted; int i; + int lmsid; long l; list(IpLog); } *iplog,*iplogs; @@ -674,7 +726,7 @@ 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; + 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); @@ -729,7 +781,8 @@ void parse_ip_log(int argc, char **argv) printf("Parsing %s ...",filename); accept_month=0; traffic_month=0; - guaranted = 0; + guaranted=0; + lmsid=-1; parse(filename) { y_ok=m_ok=0; @@ -737,11 +790,12 @@ void parse_ip_log(int argc, char **argv) { case 2: name = ptr;break; case 3: traffic = atol(ptr);break; - /* column number - was 7, now 10...*/ + /* column number - was 7, now 11...*/ case 7: case 8: case 9: - case 10: if (isalpha(*ptr)) /* character, not numeric string = date, just one*/ + case 10: + case 11: if (isalpha(*ptr)) /* character, not numeric string = date, just one*/ { valid_columns(ptr2,ptr,' ',col2) switch(col2) { @@ -752,6 +806,7 @@ void parse_ip_log(int argc, char **argv) else { if(col == 7) guaranted = atol(ptr); + if(col == 10) lmsid = atoi(ptr); } } @@ -769,6 +824,7 @@ void parse_ip_log(int argc, char **argv) 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); } @@ -783,25 +839,35 @@ void parse_ip_log(int argc, char **argv) f=fopen(str,"w"); if(f) { - fprintf(f,"\n ",month,year); + fprintf(f,"
%s %sData transfersMin.speed
\n ",month,year); for_each(iplog,iplogs) { if(iplog->traffic) { - fprintf(f,"\n", - i++, iplog->name, iplog->traffic, iplog->traffic>>10, iplog->guaranted); + fprintf(f,"\n", + iplog->traffic, iplog->traffic>>10, iplog->guaranted); total+=iplog->traffic>>10; iplog->i=i; iplog->l=total; } } - fprintf(f,"\n", total, line); + fprintf(f,"\n", total, line); fputs("
%s %slmsData transfersMin.speed
%d%s%ld M%ld G%ld kbps
%d%s", i++, iplog->name); + if(iplog->lmsid > 0) + { + /*base URL will be configurable soon ... */ + fprintf(f,"%04d\n", iplog->lmsid, iplog->lmsid); + } + else if(iplog->lmsid == 0) + { + fputs("-------",f); + } + fprintf(f,"%ld M%ld G%ld kbps
Total:%ld GB%Ld kbps
Total:%ld GB%Ld kbps
\n", f); if(i>10) { - fputs("

\n",f); + fputs("

Enterprise Research and Planning (ERP)
\n",f); fputs("\n",f); fputs("\n",f); @@ -881,13 +947,14 @@ program int just_preview=FALSE; /* preview - generate just stats */ int just_logs=FALSE; /* just parse logs */ int run=FALSE; + int total=0; char *chain_forward, *chain_postrouting; char *althosts=NULL; printf("\n\ Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\ -Version %s - Copyright (C)2005-2008 Michael Polak (xChaos)\n\ +Version %s - Copyright (C)2005-2011 Michael Polak (xChaos)\n\ iptables-restore & burst tunning & classify modification by Ludva\n\ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); @@ -957,7 +1024,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); str=_; if(*str<'0' || *str>'9') + { + /* any line starting with non-number is comment ...*/ continue; + } //Does this IP share QoS class with some other ? substring=strstr(str,"sharing-"); @@ -969,7 +1039,9 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); ip->sharing=substring; ip->keyword=defaultkeyword; /* settings for default keyword */ while(*substring && *substring!='\n') + { substring++; + } *substring=0; } else @@ -1062,6 +1134,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); sharedip->traffic+=ip->traffic; ip->traffic=0; ip->mark=sharedip->mark; + ip->lmsid=sharedip->lmsid; break; } if(!sharedip) @@ -1319,7 +1392,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); int group_count=0; FILE *credit_file=NULL; - if(!just_preview && !dry_run && enable_credit) credit_file=fopen(credit,"w"); + if(!just_preview && !dry_run && enable_credit) + { + credit_file=fopen(credit,"w"); + } for_each(group,groups) { @@ -1342,7 +1418,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } rate-=digital_divide*group->min; - if(ratemin)rate=group->min; + if(ratemin) + { + rate=group->min; + } /*shaping of aggresive downloaders, with credit file support */ if(use_credit) @@ -1355,10 +1434,16 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); ip->traffic>ip->credit+ (ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20)) ) { - if(group_ratemax) ip->max=group_rate; + if(group_ratemax) + { + ip->max=group_rate; + } group_rate+=magic_treshold; ip->prio=lowest_priority; - if(ip->prioprio=highest_priority+2; + if(ip->prioprio=highest_priority+2; + } } else { @@ -1367,15 +1452,20 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); (ip->min*ip->keyword->data_prio+(ip->keyword->fixed_prio<<20)) ) { ip->prio=priority_sequence--; - if(ip->prioprio=highest_priority+1; + if(ip->prioprio=highest_priority+1; + } } if(credit_file) { unsigned long long lcredit=0; - if((ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))>ip->traffic) + if((ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))>ip->traffic) + { lcredit=(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))-ip->traffic; + } fprintf(credit_file,"%s %Lu\n",ip->addr,lcredit); } } @@ -1383,7 +1473,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } } - if(credit_file)fclose(credit_file); + if(credit_file) + { + fclose(credit_file); + } } if(just_preview) @@ -1410,7 +1503,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if(f) { - int total=0; + int count=1; i=0; @@ -1459,31 +1552,42 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); i=1; if(f) { - unsigned long long total=0, total_direct=0, total_proxy=0, total_upload=0, tmp_sum=0; + unsigned long long total_traffic=0, total_direct=0, total_proxy=0, total_upload=0, tmp_sum=0; int active_classes=0; int colspan; FILE *iplog; struct Sum {unsigned long long l; int i; list(Sum);} *sum,*sums=NULL; + colspan=11; if(qos_proxy) - colspan=12; - else - colspan=11; + { + colspan++; + } + if(found_lmsid) + { + colspan++; + } fprintf(f,"

Enterprise Resource Planning (ERP)
Analytic categoryActive ClassesData transfers
\n\n", d); - fputs("\ - \ - \ - \ - \n",f); + fputs("",f); + if(found_lmsid) + { + fputs("\n",f); + } + fputs("\ +\ +\ +\n",f); if(qos_proxy) + { fputs("\n",f); + } fputs("\ - \ - \ - \ - \n",f); +\ +\ +\ +\n",f); for_each(ip,ips) { @@ -1502,15 +1606,33 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); #ifdef DEBUG printf("%03d. %-22s %10Lu (%d/%d)\n",i ,ip->name, ip->traffic, ip->min, ip->max); #endif - fprintf(f,"\n", - ip->name, i, log_url, ip->name, ip->name, ip->credit); - fprintf(f,"",ip->keyword->html_color,ip->credit+(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))); + fprintf(f,"\n", ip->name, i, log_url, ip->name, ip->name); + if(found_lmsid) + { + fputs("\n",f); + } + fprintf(f,"\n", ip->credit); + fprintf(f,"", + ip->keyword->html_color, ip->credit+(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))); fprintf(f,"\n", f1, ip->traffic, f2, ip->direct); if(qos_proxy) + { fprintf(f,"\n", ip->proxy); + } fprintf(f,"\n", ip->upload); - fprintf(f,"\n",ip->min,ip->desired,f1,ip->max,f2,f1,ip->prio,f2); - total+=ip->traffic; + fprintf(f,"\n", + ip->min,ip->desired,f1,ip->max,f2,f1,ip->prio,f2); + total_traffic+=ip->traffic; total_direct+=ip->direct; total_proxy+=ip->proxy; total_upload+=ip->upload; @@ -1532,79 +1654,102 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); iplog=fopen(str,"a"); if(iplog) { - fprintf(iplog,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%s", - time(NULL),ip->name,ip->traffic,ip->direct,ip->proxy,ip->upload,ip->min,ip->max,ip->desired,d); /* d = date*/ + fprintf(iplog,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%d\t%s", + time(NULL),ip->name,ip->traffic,ip->direct,ip->proxy,ip->upload,ip->min,ip->max,ip->desired,ip->lmsid,d); /* d = date*/ fclose(iplog); } } - } - fprintf(f,"\ - \n", total, total_direct); + \n", total_traffic, total_direct); if(qos_proxy) + { fprintf(f,"\n", total_proxy); + } fprintf(f,"", total_upload); fputs("\n
%s",colspan,title); fprintf(f," (%s)
#hostnamecreditlimittotaldirect
#hostnamelmscreditlimittotaldirectproxyuploadminimumdesiredmaximumprio
minimumdesiredmaximumprio
%d%s%Lu M%Lu M
%d%s",f); + if(ip->lmsid > 0) + { + /*base URL will be configurable soon ... */ + fprintf(f,"%04d\n", ip->lmsid, ip->lmsid); + } + else if(ip->lmsid == 0) + { + fputs("-------",f); + } + fputs("%Lu M%Lu M%s%Lu M%s%Lu M%Lu M%Lu M%d k%d k%s%d k%s%s%d%s
%d k%d k%s%d k%s%s%d%s
SUMMARY:"); + fprintf(f,"
SUMMARY:",colspan-7); fprintf(f,"%Lu M%Lu M%Lu M%Lu M%Lu M
\n",f); if(active_classes>10) { - fputs("

\n",f); + int top20_count=0,top20_perc1=0; + long long top20_perc2=0; + unsigned long long top20_sum=0l; + + fputs("

Enterprise Research and Planning (ERP)
\n",f); fputs("\n",f); fputs("\n",f); - if_exists(sum,sums,sum->l>=total/4) + if_exists(sum,sums,sum->l>=total_traffic/4) { fprintf(f,"\n"); - fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total); + fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); } if_exists(sum,sums,sum->i==10) { fprintf(f,"\n"); - fprintf(f,"\n",(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total); + fprintf(f,"\n",(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); } - if_exists(sum,sums,sum->l>=total/2) + if_exists(sum,sums,sum->l>=total_traffic/2) { fprintf(f,"\n"); - fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total); + fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); } - if_exists(sum,sums,sum->l>=4*total/5) + if_exists(sum,sums,sum->l>=4*total_traffic/5) { fprintf(f,"\n"); - fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total); + fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); } - if_exists (sum,sums,sum->i>=(active_classes+1)/5) + if_exists(sum,sums,sum->i>=(active_classes+1)/5) { fprintf(f,"\n"); - fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total); + top20_count=sum->i; + top20_perc1=(100*sum->i+50)/active_classes; + top20_sum=sum->l; + top20_perc2=(100*sum->l+50)/total_traffic; + fprintf(f,"\n",top20_count,top20_perc1,top20_sum,top20_perc2); } if_exists(sum,sums,sum->i>=(active_classes+1)/4) { fprintf(f,"\n"); - fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total); + fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); } if_exists(sum,sums,sum->i>=(active_classes+1)/2) { fprintf(f,"\n"); - fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total); + fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); } if_exists(sum,sums,sum->i>=4*(active_classes+1)/5) { fprintf(f,"\n"); - fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total); + fprintf(f,"\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); } - fprintf(f,"\n"); - fprintf(f,"\n",active_classes,total); + fprintf(f,"\n", log_url); + fprintf(f,"\n",active_classes,total_traffic); fputs("
Enterprise Resource Planning (ERP)
Analytic categoryActive ClassesData transfers
Top 25%% of traffic%d%d %%%Lu M%Ld %%
%d%d %%%Lu M%Ld %%
Top 10 downloaders10%d %%%Lu M%Ld %%
10%d %%%Lu M%Ld %%
Top 50%% of traffic%d%d %%%Lu M%Ld %%
%d%d %%%Lu M%Ld %%
Top 80%% of traffic%d%d %%%Lu M%Ld %%
%d%d %%%Lu M%Ld %%
Top 20%% downloaders%d%d %%%Lu M%Ld %%
%d%d %%%Lu M%Ld %%
Top 25%% downloaders%d%d %%%Lu M%Ld %%
%d%d %%%Lu M%Ld %%
Top 50%% downloaders%d%d %%%Lu M%Ld %%
%d%d %%%Lu M%Ld %%
Top 80%% downloaders%d%d %%%Lu M%Ld %%
%d%d %%%Lu M%Ld %%
All users, all traffic%d100 %%%Lu M100 %%
All users, all traffic%d100 %%%Lu M100 %%
\n", f); + + /* write basic ERP data to log directory */ + if(!just_preview) + { + sprintf(str,"%s/ERP.log",log_dir); + iplog=fopen(str,"a"); + if(iplog) + { + fprintf(iplog,"%ld\t%d\t%d %%\t%Lu M\t%Ld %%\t%d\t%Lu M\t%d\t%s", + time(NULL), top20_count, top20_perc1, top20_sum, top20_perc2, active_classes, total_traffic, total, d); /* d = date*/ + fclose(iplog); + } + } } + fprintf(f, stats_html_signature, version); fclose(f); }