X-Git-Url: https://git.harvie.cz/?a=blobdiff_plain;f=prometheus.c;h=b6eeb27beb663cf9a18eed5131cd5b72bd9d766b;hb=6fb3c58ad91b3214a985824e1eb51ecaae442c60;hp=494ce5085b60acedff8d96dbf67f5370ab3b88e1;hpb=52e8df5d8b101f15c4287c9fe7e9e5154cef76a4;p=svn%2FPrometheus-QoS%2F.git diff --git a/prometheus.c b/prometheus.c index 494ce50..b6eeb27 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, 20120612 ludva, 20080415 Prometheus QoS is free software; you can redistribute it and/or @@ -33,14 +33,14 @@ #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. */ /* C source code development versions ("beta"): 0.7.9-a, 0.8.1-b, etc. */ /* C source code release versions: 0.8.0, 0.8.2, 0.8.4, etc. */ -const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2012 Michael Polak, Arachne Labs
\n"; +const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2012 Michael Polak, Arachne Labs
\n"; /* ======= All path names are defined here (for RPM patch) ======= */ @@ -57,8 +57,9 @@ char *iptablesfile = "/var/spool/prometheus.iptables"; /* temporary file for 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 *preview = "/var/www/preview.html"; /* hall of fame preview - html version */ +char *json = "/var/www/logs/traffic.json"; /* hall of fame - json version */ +char *json_preview = "/var/www/logs/preview.json"; /* hall of fame preview - 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 +90,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 ==== */ @@ -440,6 +440,7 @@ void get_config(char *config_filename) option("hall-of-fame-filename",html); option("json-filename",json); option("hall-of-fame-preview",preview); + option("json-preview",json_preview); option("log-filename",cmdlog); option("credit-filename",credit); option("classmap-filename",classmap); @@ -476,7 +477,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 +611,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 +700,7 @@ void run_restore(void) { printf("%s\n",_); } - done; + done; /* ugly macro end */ } sprintf(restor,"%s <%s",iptablesrestore, iptablesfile); @@ -880,7 +881,7 @@ void parse_ip_log(int argc, char **argv) accept_month = 1; } } - done; + done; /* ugly macro end */ if(accept_month) { @@ -917,13 +918,13 @@ void parse_ip_log(int argc, char **argv) if(iplog->traffic) { fprintf(f, "%s%d\n\ -%s\n\ +%s\n\ ", 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); + fprintf(f, "%04d\n", lms_url, iplog->lmsid, iplog->lmsid); } else if(iplog->lmsid == 0) { @@ -939,9 +940,9 @@ void parse_ip_log(int argc, char **argv) } } fprintf(f,"\ - Total:\ - %ld GB\ - %Ld kb/s\n", total, line); + Total:\ + %ld GB\ + %Ld kb/s\n", total, line); fputs("\n", f); row_odd_even = 0; @@ -958,7 +959,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 +971,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 +981,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 +991,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 +1001,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,20 +1021,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,"\n\ -All users, all traffic\n\ -%d\n\ + 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); } @@ -1069,16 +1083,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; @@ -1087,12 +1102,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); } @@ -1101,6 +1115,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; } @@ -1251,7 +1266,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... */ @@ -1294,7 +1309,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } } } - done; + done; /* ugly macro end */ } if(!just_preview) @@ -1621,13 +1636,53 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if(just_preview) { - f=fopen(preview,"w"); - ptr=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 */ + } + html=preview; + json=json_preview; } - else if(!dry_run && !just_flush) + + if(!dry_run && !just_flush) { /*-----------------------------------------------------------------*/ - printf("Writing daily statistics %s ... ", json); + printf("Writing json overview %s ... ", json); /*-----------------------------------------------------------------*/ f=fopen(json, "w"); if(f > 0) @@ -1636,16 +1691,17 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); fprintf(f, "{\n"); for_each(ip, ips) { - if(jsoncount) + if( ip->lmsid > 0 + && (ip->traffic || ip->direct || ip->proxy || ip->upload)) { - fprintf(f, ",\n"); - } - if(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, \"min\":%d, \"max\":%d, \"limit\":%d }", + ip->name, ip->lmsid, ip->addr, ip->traffic, ip->direct, ip->proxy, ip->upload, ip->min, ip->desired, ip->max); + jsoncount++; } - jsoncount++; } fprintf(f, "}\n"); fclose(f); @@ -1655,17 +1711,16 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); { perror(json); } - f=fopen(html,"w"); - ptr=html; } - if(f) + f=fopen(html,"w"); + if(f > 0) { int count=1; i=0; /*-----------------------------------------------------------------*/ - printf("Sorting data and generating statistics page %s ...\n", ptr); + printf("Sorting data and generating statistics page %s ...\n", html); /*-----------------------------------------------------------------*/ if(use_jquery_popups) @@ -1726,7 +1781,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } i=0; - if(f) + if(f > 0) { unsigned long long total_traffic=0, total_direct=0, total_proxy=0, total_upload=0, tmp_sum=0; int active_classes=0; @@ -1776,25 +1831,25 @@ 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++; - } + + 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); #endif /* hostnames -------------------------------------- */ - fprintf(f,"%s%d%s\n", + fprintf(f,"%s%d%s\n", tr_odd_even(), ip->name, i, log_url, ip->name, ip->name); if(use_jquery_popups) @@ -1803,7 +1858,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); popup_button=0; for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing)) { - fprintf(f,"
%s\n", log_url, sharedip->name, sharedip->name); + fprintf(f,"
%s\n", log_url, sharedip->name, sharedip->name); popup_button++; } fputs("\n",f); @@ -1821,7 +1876,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); fputs("",f); if(ip->lmsid > 0) { - fprintf(f,"%04d\n", lms_url, ip->lmsid, ip->lmsid); + fprintf(f,"%04d\n", lms_url, ip->lmsid, ip->lmsid); } else if(ip->lmsid == 0) { @@ -1925,7 +1980,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) @@ -1935,7 +1994,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) @@ -2002,10 +2061,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic); } - fprintf(f,"All users, all traffic\n", log_url); + 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); @@ -2035,7 +2094,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); } @@ -2164,9 +2228,9 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); safe_run(str); } - if(f) + if(f > 0) { - fprintf(f, "%s %d", ip->addr, ip->mark); + fprintf(f, "%s %d\n", ip->addr, ip->mark); } } else @@ -2177,7 +2241,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } i++; } - if(f) + if(f > 0) { puts("done."); fclose(f);