X-Git-Url: https://git.harvie.cz/?a=blobdiff_plain;f=prometheus.c;h=529315d91e32ed97c5cfea649299689f7768c34c;hb=8e29188ae8234c9bab4ede7d1e8f3abd26eb621a;hp=f12de1530e477e15cf1fbf45689f2f31aa56f352;hpb=4f4d182088d6b40224a5475f284a9c756ae604a1;p=svn%2FPrometheus-QoS%2F.git diff --git a/prometheus.c b/prometheus.c index f12de15..529315d 100644 --- a/prometheus.c +++ b/prometheus.c @@ -2,12 +2,12 @@ /* Prometheus QoS - you can "steal fire" from your ISP */ /* "fair-per-IP" quality of service (QoS) utility */ /* requires Linux 2.4.x or 2.6.x with HTB support */ -/* Copyright(C) 2005-2008 Michael Polak (xChaos) */ +/* Copyright(C) 2005-2012 Michael Polak, Arachne Labs */ /* iptables-restore support Copyright(C) 2007-2008 ludva */ /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -/* Modified by: xChaos, 20120516 +/* Modified by: xChaos, 20120610 ludva, 20080415 Prometheus QoS is free software; you can redistribute it and/or @@ -33,7 +33,7 @@ #include "cll1-0.6.2.h" -const char *version = "0.8.3-d"; +const char *version = "0.8.3-f"; /* Version numbers: 0.8.3 is development releases ("beta"), 0.8.4 will be "stable" */ /* Debian(RPM) package versions/patchlevels: 0.7.9-2, 0.8.0-1, 0.8.0-2, etc. */ @@ -58,7 +58,7 @@ char *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */ char *classmap = "/var/lib/misc/prometheus.classes"; /* credit log file */ char *html = "/var/www/traffic.html"; /* hall of fame - html version */ char *preview = "/var/www/preview.html"; /* hall of fame preview */ -char *json = "/var/www/traffic.json"; /* hall of fame - json version */ +char *json = "/var/www/logs/traffic.json"; /* hall of fame - json version */ char *cmdlog = "/var/log/prometheuslog"; /* command log filename */ char *log_dir = "/var/www/logs/"; /* log directory pathname, ended with slash */ char *log_url = "/logs/"; /* log directory relative URI prefix (partial URL) */ @@ -89,22 +89,21 @@ void help(void) { puts("Command line switches:\n\ \n\ --?, --help this help screen\n\ --v, --version show Version number of this utility and exit\n\ --c filename force alternative /etc/prometheus.Conf filename\n\ --h filename force alternative /etc/Hosts filename (overrides hosts keyword)\n\ --f just Flush iptables and tc classes and exit (stop shaping)\n\ --9 emergency iptables flush (do not read data transfer statistics)\n\ --p just generate Preview of data transfer statistics and exit\n\ --d Dry run (preview tc and iptables commands on stdout)\n\ --r Run (reset all statistics and start shaping)\n\ --n run Now (start shaping without delay - overrides qos-free-delay keyword)\n\ --l Mmm YYYY generate HTML summary of traffic Logs (Mmm=Jan-Dec or Year, YYYY=year)\n\ --m generate HTML summary of traffic logs for yesterday's Month\n\ --y generate HTML summary of traffic logs for yesterday's Year\n"); -/* not yet implemented: --s start shaping! (keep data transfer statistics - but apply shaping)\n\ -*/ +-d Dry run (preview tc and iptables commands on stdout)\n\ +-r Run (reset all statistics and start shaping - daily usage)\n\ +-p just generate Preview of data transfer statistics and exit (after -r)\n\ +-s start Shaping FUP limits (keeps data transfer stat like -p) (after -r)\n\ +-n run Now (like -r delay - overrides qos-free-delay keyword, after boot)\n\ +-f just Flush iptables and tc classes and exit (stop shaping, no QiS)\n\ +-9 emergency iptables flush (like -f, but dumps data transfer statistics)\n\ +\n\ +-c filename force alternative /etc/prometheus/prometheus.conf filename\n\ +-h filename force alternative /etc/hosts filename (overrides hosts keyword)\n\ +-l Mmm YYYY generate HTML summary of Logged traffic (Mmm=Jan-Dec) (and exit)\n\ +-m generate HTML summary of traffic for yesterday's Month (and exit)\n\ +-y generate HTML summary of traffic for yesterday's Year (and exit)\n\ +-? --help show this help scree (and exit)\n\ +-v --version show Version number of this utility (and exit)\n"); } /* === Configuraration file values defaults - stored in global variables ==== */ @@ -476,7 +475,7 @@ void get_config(char *config_filename) perror(config_filename); puts("Warning - using built-in defaults instead ..."); } - done; + done; /* ugly macro end */ printf("\n"); /* leaf discipline for keywords */ @@ -610,7 +609,7 @@ void get_traffic_statistics(void) { printf("(upload) "); } - printf("IP %s: %Lu M (%ld pkts)\n", ipaddr, traffic, pkts); + printf("IP %s: %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts); if_exists(ip,ips,eq(ip->addr,ipaddr)); else @@ -699,7 +698,7 @@ void run_restore(void) { printf("%s\n",_); } - done; + done; /* ugly macro end */ } sprintf(restor,"%s <%s",iptablesrestore, iptablesfile); @@ -880,7 +879,7 @@ void parse_ip_log(int argc, char **argv) accept_month = 1; } } - done; + done; /* ugly macro end */ if(accept_month) { @@ -958,7 +957,11 @@ void parse_ip_log(int argc, char **argv) if_exists(iplog,iplogs,iplog->l>=total/4) { fprintf(f,"%sTop 25%% of traffic\n", tr_odd_even()); - fprintf(f,"%d%d %%%ld G%d %%\n",iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total)); + fprintf(f,"%d\n\ +%d %%\n\ +%ld GB\n\ +%d %%\n", + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); } if_exists(iplog,iplogs,iplog->i==10) @@ -966,9 +969,9 @@ void parse_ip_log(int argc, char **argv) fprintf(f,"%sTop 10 downloaders\n", tr_odd_even()); fprintf(f,"10\n\ %d %%\n\ -%ld G\n\ +%ld GB\n\ %d %%\n", - (100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total)); + (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); } if_exists(iplog,iplogs,iplog->l>=total/2) @@ -976,7 +979,7 @@ void parse_ip_log(int argc, char **argv) fprintf(f,"%sTop 50%% of traffic\n", tr_odd_even()); fprintf(f,"%d\n\ %d %%\n\ -%ld G\n\ +%ld GB\n\ %d %%\n", iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total)); } @@ -986,9 +989,9 @@ void parse_ip_log(int argc, char **argv) fprintf(f,"%sTop 80%% of traffic\n",tr_odd_even()); fprintf(f,"%d\n\ %d %%\n\ -%ld G\n\ +%ld GB\n\ %d %%\n", - iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total)); + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); } if_exists (iplog,iplogs,iplog->i>=i/5) @@ -996,15 +999,19 @@ void parse_ip_log(int argc, char **argv) fprintf(f,"%sTop 20%% downloaders\n",tr_odd_even()); fprintf(f,"%d\n\ %d %%\n\ -%ld G\n\ +%ld GB\n\ %d %%\n", - iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total)); + 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,"%sTop 25%% downloaders\n", tr_odd_even()); - fprintf(f,"%d%d %%%ld G%d %%\n",iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total)); + fprintf(f,"%d\n\ +%d %%\n\ +%ld GB\n\ +%d %%\n", + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); } if_exists(iplog,iplogs,iplog->i>=i/2) @@ -1012,19 +1019,25 @@ void parse_ip_log(int argc, char **argv) fprintf(f,"%sTop 50%% downloaders\n",tr_odd_even()); fprintf(f,"%d\n\ %d %%\n\ -%ld G%d %%\n",iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total)); +%ld GB\n\ +%d %%\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,"%sTop 80%% downloaders\n",tr_odd_even()); - fprintf(f,"%d%d %%%ld G%d %%\n",iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total)); + fprintf(f,"%d\n\ +%d %%\n\ +%ld GB\n\ +%d %%\n", + iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total)); } fprintf(f,"All users, all traffic\n", log_url); fprintf(f,"%d\n\ 100 %%\n\ -%ld G\n\ +%ld GB\n\ 100 %%\n",i-1,total); fputs("\n", f); } @@ -1068,16 +1081,17 @@ void append_log(struct IP *self) /*using global variables*/ program { - int i=0; - FILE *f=NULL; - char *str, *ptr, *d; + int i=0; /* just plain old Fortran style integer :-) */ + FILE *f=NULL; /* everything is just stream of bytes... */ + char *str, *ptr, *d; /* LET A$=B$ :-) */ char *substring; int class_count=0,ip_count=0; int parent=1; - int just_flush=FALSE; + int just_flush=FALSE; /* deactivates all previous actions */ int nodelay=FALSE; - int just_preview=FALSE; /* preview - generate just stats */ - int just_logs=FALSE; /* just parse logs */ + int just_preview=FALSE; /* preview - generate just stats */ + int start_shaping=FALSE; /* apply FUP - requires classmap file */ + int just_logs=FALSE; /* just parse logs */ int run=FALSE; int total=0; @@ -1086,12 +1100,11 @@ program printf("\n\ Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\ -Version %s - Copyright (C)2005-2012 Michael Polak (xChaos)\n\ +Version %s - Copyright (C)2005-2012 Michael Polak, Arachne Labs\n\ iptables-restore & burst tunning & classify modification by Ludva\n\ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); - /*----- Boring... we have to check command line options first: ----*/ - + /*----- Boring... we have to check command line options first: ----*/ arguments { argument("-c") { nextargument(config); } @@ -1100,6 +1113,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); argument("-f") { run=TRUE; just_flush=TRUE; } argument("-9") { run=TRUE; just_flush=9; } argument("-p") { run=TRUE; just_preview=TRUE; } + argument("-s") { run=TRUE; just_preview=TRUE; start_shaping=TRUE; } argument("-r") { run=TRUE; } argument("-n") { run=TRUE; nodelay=TRUE; } argument("-l") { just_logs=TRUE; } @@ -1250,7 +1264,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); perror(hosts); exit(-1); } - done; + done; /* ugly macro end */ /*-----------------------------------------------------------------*/ /* cll1.h - let's allocate brand new character buffer... */ @@ -1293,7 +1307,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } } } - done; + done; /* ugly macro end */ } if(!just_preview) @@ -1620,6 +1634,45 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if(just_preview) { + if(start_shaping) + { + printf("Reading %s and applying Fair Use Policy rules ... \n", classmap); + parse(classmap) + { + ptr=strchr(_,' '); + if(ptr) + { + *ptr=0; + ptr++; + if_exists(ip,ips,eq(ip->addr,_)) + { + ip->mark=atoi(ptr); + if(ip->max < ip->desired) /* apply FUP limit immediately.... */ + { + printf("Applying limit for %-22s %-16s %04d ", ip->name, ip->addr, ip->mark); + printf("(down: %dk-%dk ", ip->min, ip->max); + sprintf(str, "%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", + tc, lan, ip->group, ip->mark,ip->min,ip->max, burst, ip->prio); + safe_run(str); + printf("up: %dk-%dk)\n", (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), + (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed)); + sprintf(str,"%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", + tc, wan, ip->group, ip->mark, + (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), + (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), burst, ip->prio); + safe_run(str); + } + } + } + } + fail + { + perror(classmap); + puts("Warning - classmap file not fund, just generating preview ..."); + start_shaping=FALSE; + } + done; /* ugly macro end */ + } f=fopen(preview,"w"); ptr=preview; } @@ -1635,16 +1688,17 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); fprintf(f, "{\n"); for_each(ip, ips) { - if(jsoncount) - { - fprintf(f, ",\n"); - } - if(ip->traffic || ip->direct || ip->proxy || ip->upload) + if( ip->lmsid > 0 + && (ip->traffic || ip->direct || ip->proxy || ip->upload)) { - fprintf(f, " %d:{ \"ip\":\"%s\", \"total\":%Lu, \"down\":%Lu, \"proxy\":%Lu, \"up\":%Lu }", - ip->lmsid, ip->addr, ip->traffic, ip->direct, ip->proxy, ip->upload); + if(jsoncount) + { + fprintf(f, ",\n"); + } + fprintf(f, " \"%s\":{ \"lms\": %d, \"ip\":\"%s\", \"total\":%Lu, \"down\":%Lu, \"proxy\":%Lu, \"up\":%Lu }", + ip->name, ip->lmsid, ip->addr, ip->traffic, ip->direct, ip->proxy, ip->upload); + jsoncount++; } - jsoncount++; } fprintf(f, "}\n"); fclose(f); @@ -1776,18 +1830,6 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); char *f1="", *f2=""; i++; - if(ip->max < ip->desired) - { - f1=""; - f2=""; - limit_count++; - } - else if(ip->prio > highest_priority+1) - { - f1=""; - f2=""; - prio_count++; - } #ifdef DEBUG printf("%03d. %-22s %10Lu (%d/%d)\n",i ,ip->name, ip->traffic, ip->min, ip->max); @@ -1924,7 +1966,11 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if_exists(sum,sums,sum->l>=total_traffic/4) { fprintf(f,"%sTop 25%% of traffic\n", tr_odd_even()); - fprintf(f,"%d%d %%%Lu M%Ld %%\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); + fprintf(f,"%d\n\ +%d %%\n\ +%Lu MB\n\ +%Ld %%\n", + sum->i, (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic); } if_exists(sum,sums,sum->i==10) @@ -1934,7 +1980,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); %d %%\n\ %Lu MB\n\ %Ld %%\n", - (100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); + (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic); } if_exists(sum,sums,sum->l>=total_traffic/2) @@ -2004,7 +2050,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); fprintf(f,"All users, all traffic\n", log_url); fprintf(f,"%d\n\ 100 %%\n\ -%Lu M\n\ +%Lu MB\n\ 100 %%\n",active_classes,total_traffic); fputs("\n", f); @@ -2034,7 +2080,12 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if(just_preview) { - puts("Statistics preview generated (-p switch) - now exiting ..."); + char swchar='p'; + if(start_shaping) + { + swchar='s'; + } + printf("Statistics preview generated (-%c switch) - now exiting ...\n", swchar); exit(0); } @@ -2165,7 +2216,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if(f) { - fprintf(f, "%s %d", ip->addr, ip->mark); + fprintf(f, "%s %d\n", ip->addr, ip->mark); } } else