d241618fcc351c084294e9e9e293cc637bd6617e
1 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2 /* Prometheus QoS - you can "steal fire" from your ISP */
3 /* "fair-per-IP" quality of service (QoS) utility */
4 /* requires Linux 2.4.x or 2.6.x with HTB support */
5 /* Copyright(C) 2005-2008 Michael Polak (xChaos) */
6 /* iptables-restore support Copyright(C) 2007-2008 ludva */
7 /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */
8 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
10 /* Modified by: xChaos, 20120516
13 Prometheus QoS is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2.1 of
16 the License, or (at your option) any later version.
18 Prometheus QoS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with Prometheus Qos; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 GNU General Public License is located in file COPYING */
30 #define FIRSTGROUPID 1024
31 #define FIRSTIPCLASS 2048
34 #include "cll1-0.6.2.h"
36 const char *version
= "0.8.3-d";
38 /* Version numbers: 0.8.3 is development releases ("beta"), 0.8.4 will be "stable" */
39 /* Debian(RPM) package versions/patchlevels: 0.7.9-2, 0.8.0-1, 0.8.0-2, etc. */
40 /* C source code development versions ("beta"): 0.7.9-a, 0.8.1-b, etc. */
41 /* C source code release versions: 0.8.0, 0.8.2, 0.8.4, etc. */
43 const char *stats_html_signature
= "<span class=\"small\">Statistics generated by Prometheus QoS version %s<br />GPL+Copyright(C)2005-2012 Michael Polak, <a href=\"http://www.arachne.cz/\">Arachne Labs</a></span>\n";
45 /* ======= All path names are defined here (for RPM patch) ======= */
47 const char *tc
= "/sbin/tc"; /* requires tc with HTB support */
48 const char *iptables
= "/sbin/iptables"; /* requires iptables utility */
49 const char *iptablessave
= "/sbin/iptables-save"; /* not yet required */
50 const char *iptablesrestore
= "/sbin/iptables-restore"; /* requires iptables-restore */
51 const char *ls
= "/bin/ls"; /* this is not user configurable :-) */
53 char *config
= "/etc/prometheus/prometheus.conf"; /* main configuration file */
54 char *hosts
= "/etc/prometheus/hosts"; /* per-IP bandwidth definition file */
56 char *iptablesfile
= "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/
57 char *credit
= "/var/lib/misc/prometheus.credit"; /* credit log file */
58 char *classmap
= "/var/lib/misc/prometheus.classes"; /* credit log file */
59 char *html
= "/var/www/traffic.html"; /* hall of fame - html version */
60 char *preview
= "/var/www/preview.html"; /* hall of fame preview */
61 char *json
= "/var/www/traffic.json"; /* hall of fame - json version */
62 char *cmdlog
= "/var/log/prometheuslog"; /* command log filename */
63 char *log_dir
= "/var/www/logs/"; /* log directory pathname, ended with slash */
64 char *log_url
= "/logs/"; /* log directory relative URI prefix (partial URL) */
65 char *html_log_dir
= "/var/www/logs/html/";
67 char *jquery_url
= "http://code.jquery.com/jquery-latest.js";
68 char *lms_url
= "/lms/?m=customerinfo&id=";
69 int use_jquery_popups
= 1;
70 int row_odd_even
= 0; /*<tr class="odd/even"> */
73 const char *tr_odd_even(void)
75 row_odd_even
= 1 - row_odd_even
;
78 return "<tr class=\"even\">\n";
82 return "<tr class=\"odd\">\n";
86 /* ======= Help screen is hopefuly self-documenting part of code :-) ======= */
90 puts("Command line switches:\n\
92 -?, --help this help screen\n\
93 -v, --version show Version number of this utility and exit\n\
94 -c filename force alternative /etc/prometheus.Conf filename\n\
95 -h filename force alternative /etc/Hosts filename (overrides hosts keyword)\n\
96 -f just Flush iptables and tc classes and exit (stop shaping)\n\
97 -9 emergency iptables flush (do not read data transfer statistics)\n\
98 -p just generate Preview of data transfer statistics and exit\n\
99 -d Dry run (preview tc and iptables commands on stdout)\n\
100 -r Run (reset all statistics and start shaping)\n\
101 -n run Now (start shaping without delay - overrides qos-free-delay keyword)\n\
102 -l Mmm YYYY generate HTML summary of traffic Logs (Mmm=Jan-Dec or Year, YYYY=year)\n\
103 -m generate HTML summary of traffic logs for yesterday's Month\n\
104 -y generate HTML summary of traffic logs for yesterday's Year\n");
105 /* not yet implemented:
106 -s start shaping! (keep data transfer statistics - but apply shaping)\n\
110 /* === Configuraration file values defaults - stored in global variables ==== */
112 int filter_type
= 1; /*1 mark, 2 classify*/
114 char *mark_iptables
= "MARK --set-mark ";
115 int dry_run
= 0; /* preview - use puts() instead of system() */
116 char *iptablespreamble
= "*mangle\n:PREROUTING ACCEPT [0:0]\n:POSTROUTING ACCEPT [0:0]\n:INPUT ACCEPT [0:0]\n:OUTPUT ACCEPT [0:0]\n:FORWARD ACCEPT [0:0]";
117 FILE *iptables_file
= NULL
;
118 int enable_credit
= 1; /* enable credit file */
119 int use_credit
= 0; /* use credit file (if enabled)*/
120 char *title
= "Hall of Fame - Greatest Suckers"; /* hall of fame title */
121 int hall_of_fame
= 1; /* enable hall of fame */
122 char *lan
= "eth0"; /* LAN interface */
123 char *lan_medium
= "100Mbit"; /* 10Mbit/100Mbit ethernet */
124 char *wan
= "eth1"; /* WAN/ISP interface */
125 char *wan_medium
= "100Mbit"; /* 10Mbit/100Mbit ethernet */
126 char *qos_leaf
= "sfq perturb 5"; /* leaf discipline */
127 char *qos_free_zone
= NULL
; /* QoS free zone */
128 int qos_proxy
= 1; /* include proxy port to QoS */
129 int found_lmsid
= 0; /* show links to users in LMS information system */
130 int include_upload
= 1; /* upload+download=total traffic */
131 char *proxy_ip
= "192.168.1.1/32"; /* our IP with proxy port */
132 int proxy_port
= 3128; /* proxy port number */
133 long long int line
= 1024; /* WAN/ISP download in kbps */
134 long long int up
= 1024; /* WAN/ISP upload in kbps */
135 int free_min
= 32; /* minimum guaranted bandwidth for all undefined hosts */
136 int free_max
= 64; /* maximum allowed bandwidth for all undefined hosts */
137 int qos_free_delay
= 0; /* seconds to sleep before applying new QoS rules */
138 int digital_divide
= 2; /* controls digital divide weirdness ratio, 1...3 */
139 int max_nesting
= 3; /* maximum nesting of HTB clases, built-in maximum seems to be 4 */
140 int htb_r2q
= 256; /* should work for leaf values 512 kbps to 8 Mbps */
141 int burst
= 8; /* HTB burst (in kbits) */
143 int burst_group
= 32;
144 int magic_treshold
= 8; /* reduce ceil by X*magic_treshhold kbps (hard shaping) */
145 int keywordcount
= 0;
146 /* not yet implemented:
147 int fixed_packets = 0; maximum number of pps per IP address (not class!)
148 int packet_limit = 5; maximum number of pps to htn CEIL, not rate !!!
150 FILE *log_file
= NULL
;
151 char *kwd
= "via-prometheus"; /* /etc/hosts comment, eg. #qos-64-128 */
153 const int highest_priority
= 0; /* highest HTB priority (HTB built-in value is 0) */
154 const int lowest_priority
= 7; /* lowest HTB priority (HTB built-in value is 7) */
155 const int idxtable_treshold1
= 24; /* this is no longer configurable */
156 const int idxtable_treshold2
= 12; /* this is no longer configurable */
157 const int idxtable_bitmask1
= 3; /* this is no longer configurable */
158 const int idxtable_bitmask2
= 3; /* this is no longer configurable */
160 /* ==== This is C<<1 stuff - learn C<<1 first! https://dev.arachne.cz/svn/cll1h ==== */
175 unsigned long long direct
;
176 unsigned long long proxy
;
177 unsigned long long upload
;
178 unsigned long long traffic
;
179 unsigned long long credit
;
180 unsigned long pktsup
;
181 unsigned long pktsdown
;
182 struct Keyword
*keyword
;
184 } *ips
=NULL
, *ip
, *sharedip
;
193 } *groups
=NULL
, *group
;
199 struct Index
*parent
;
203 } *idxs
=NULL
, *idx
, *metaindex
;
209 int asymetry_ratio
; /* ratio for ADSL-like upload */
210 int asymetry_fixed
; /* fixed treshold for ADSL-like upload */
211 int data_limit
; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */
212 int data_prio
; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */
213 long fixed_limit
; /* fixed data limit for setting lower HTB ceil */
214 long fixed_prio
; /* fixed data lmit for setting lower HTB prio */
215 int reserve_min
; /* bonus for nominal HTB rate bandwidth (in kbps) */
216 int reserve_max
; /* malus for nominal HTB ceil (in kbps) */
217 // int divide_max; /* relative malus: new_ceil=rate+(old_ceil-rate)/divide_max */
218 // int htb_ceil_bonus_divide; /* relative bonus: new_ceil=old_ceil+old_ceil/htb_ceil_bonus_divide */
219 int default_prio
; /* default HTB priority for this keyword */
222 char *leaf_discipline
;
225 } *keyword
,*defaultkeyword
=NULL
,*keywords
=NULL
;
227 /* Damned, this must be object oriented! This looks almost like constructor ;-) */
235 ip
->prio
= highest_priority
+1;
249 ip
->keyword
= keywords
;
253 /* ====== iptables indexes are used to reduce complexity to log8(N) ===== */
255 char *very_ugly_ipv4_code(char *inip
,int bitmask
,int format_as_chainname
)
257 /* warning: this function was debugged only for bitmask values 20,24,28 !!!*/
259 char *ip
,*outip
,*outptr
,*fmt
;
262 /* debug printf("(%s,%d) -> ",ip,bitmask); */
264 if(ip
&& *ip
&& bitmask
>=0 && bitmask
<=32)
266 string(outip
,strlen(ip
)+10); /*fuck unicode? assertion: 10>strlen("_%d_%d") */
270 /* should never exit here */
278 if(dot
<(bitmask
/8-1))
280 if(format_as_chainname
)
293 char *cutdot
=strchr(ip
+1,'.'); /*for bitmask<24*/
299 if(format_as_chainname
)
310 n
= atoi(ip
+1)-atoi(ip
+1)%(1<<(8-bitmask
%8));
317 /*debug printf("%d/%d => [_%d_%d]\n",atoi(ip+1),bitmask,n,bitmask); */
318 sprintf(outptr
,fmt
,n
,bitmask
);
319 if(!format_as_chainname
)
327 /* debug printf("[%s]\n",outip); */
338 /*should never exit here*/
343 char *hash_id(char *ip
,int bitmask
)
345 return very_ugly_ipv4_code(ip
,bitmask
,1);
348 char *subnet_id(char *ip
,int bitmask
)
350 return very_ugly_ipv4_code(ip
,bitmask
,0);
353 /* ================= Let's parse configuration file here =================== */
355 void reject_config_and_exit(char *filename
)
357 printf("Configuration file %s rejected - abnormal exit.",filename
);
361 void get_config(char *config_filename
)
365 printf("Configured keywords: ");
366 parse(config_filename
)
368 option("keyword",kwd
);
373 create(keyword
,Keyword
);
375 keyword
->asymetry_ratio
=1; /* ratio for ADSL-like upload */
376 keyword
->asymetry_fixed
=0; /* fixed treshold for ADSL-like upload */
377 keyword
->data_limit
=8; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */
378 keyword
->data_prio
=4; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */
379 keyword
->fixed_limit
=0; /* fixed data limit for setting lower HTB ceil */
380 keyword
->fixed_prio
=0; /* fixed data limit for setting lower HTB prio */
381 keyword
->reserve_min
=8; /* bonus for nominal HTB rate bandwidth (in kbps) */
382 keyword
->reserve_max
=0; /* malus for nominal HTB ceil (in kbps) */
383 keyword
->default_prio
=highest_priority
+1;
384 keyword
->html_color
="000000";
386 keyword
->leaf_discipline
="";
388 push(keyword
,keywords
);
389 if(!defaultkeyword
) defaultkeyword
=keyword
;
396 for_each(keyword
,keywords
)
398 int l
=strlen(keyword
->key
);
400 if(!strncmp(keyword
->key
,_
,l
) && strlen(_
)>l
+2)
402 char *tmptr
=_
; /* <---- l+1 ----> */
403 _
+=l
+1; /* via-prometheus-asymetry-ratio, etc. */
404 ioption("asymetry-ratio",keyword
->asymetry_ratio
);
405 ioption("asymetry-treshold",keyword
->asymetry_fixed
);
406 ioption("magic-relative-limit",keyword
->data_limit
);
407 ioption("magic-relative-prio",keyword
->data_prio
);
408 loption("magic-fixed-limit",keyword
->fixed_limit
);
409 loption("magic-fixed-prio",keyword
->fixed_prio
);
410 ioption("htb-default-prio",keyword
->default_prio
);
411 ioption("htb-rate-bonus",keyword
->reserve_min
);
412 ioption("htb-ceil-malus",keyword
->reserve_max
);
413 option("leaf-discipline",keyword
->leaf_discipline
);
414 option("html-color",keyword
->html_color
);
417 if(keyword
->data_limit
|| keyword
->fixed_limit
||
418 keyword
->data_prio
|| keyword
->fixed_prio
)
427 option("iptables",iptables
);
428 option("iptables-save",iptablessave
); /* new */
429 option("iptables-restore",iptablesrestore
); /* new */
430 option("iptables-in-filename",iptablesfile
); /* new */
431 option("hosts",hosts
);
432 option("lan-interface",lan
);
433 option("wan-interface",wan
);
434 option("lan-medium",lan_medium
);
435 option("wan-medium",wan_medium
);
436 lloption("wan-download",line
);
437 lloption("wan-upload",up
);
438 ioption("hall-of-fame-enable",hall_of_fame
);
439 option("hall-of-fame-title",title
);
440 option("hall-of-fame-filename",html
);
441 option("json-filename",json
);
442 option("hall-of-fame-preview",preview
);
443 option("log-filename",cmdlog
);
444 option("credit-filename",credit
);
445 option("classmap-filename",classmap
);
446 ioption("credit-enable",enable_credit
);
447 option("log-traffic-directory",log_dir
);
448 option("log-traffic-html-directory",html_log_dir
);
449 option("log-traffic-url-path",log_url
);
450 option("jquery-url",jquery_url
);
451 option("lms-url",lms_url
);
452 ioption("use-jquery-popups",use_jquery_popups
);
453 option("qos-free-zone",qos_free_zone
);
454 ioption("qos-free-delay",qos_free_delay
);
455 ioption("qos-proxy-enable",qos_proxy
);
456 option("qos-proxy-ip",proxy_ip
);
457 option("htb-leaf-discipline",qos_leaf
);
458 ioption("qos-proxy-port",proxy_port
);
459 ioption("free-rate",free_min
);
460 ioption("free-ceil",free_max
);
461 ioption("htb-burst",burst
);
462 ioption("htb-burst-main",burst_main
);
463 ioption("htb-burst-group",burst_group
);
464 ioption("htb-nesting-limit",max_nesting
);
465 ioption("htb-r2q",htb_r2q
);
466 ioption("magic-include-upload",include_upload
);
467 ioption("magic-treshold",magic_treshold
);
468 option("filter-type", cnf
);
469 /* not yet implemented:
470 ioption("magic-fixed-packets",fixed_packets);
471 ioption("magic-relative-packets",packet_limit);
476 perror(config_filename
);
477 puts("Warning - using built-in defaults instead ...");
482 /* leaf discipline for keywords */
483 for_each(keyword
,keywords
)
485 if(!strcmpi(keyword
->leaf_discipline
, ""))
487 keyword
->leaf_discipline
= qos_leaf
;
491 if(strcmpi(cnf
, "mark"))
495 mark_iptables
= "CLASSIFY --set-class 1:";
501 mark_iptables
= "MARK --set-mark ";
504 /* are supplied values meaningful ?*/
507 puts("Illegal value of LAN or WAN bandwidth: 0 kbps.");
508 reject_config_and_exit(config_filename
);
512 /* ===================== traffic analyser - uses iptables ================ */
514 void get_traffic_statistics(void)
519 textfile(Pipe
,str
) *line
,*lines
=NULL
;
523 sprintf(cmd
,"%s -L -v -x -n -t mangle",iptables
);
535 int col
, accept
=0,proxyflag
=0,valid
=1,setchainname
=0,commonflag
=0;
536 unsigned long long traffic
=0;
537 unsigned long pkts
=0;
538 char *ipaddr
=NULL
,*ptr
;
540 /* debug puts(line->str); */
541 valid_columns(ptr
,line
->str
,' ',col
)
542 if(valid
) switch(col
)
544 case 1: if(eq(ptr
,"Chain"))
548 else if(eq(ptr
,"pkts"))
554 sscanf(ptr
,"%lu",&pkts
);
557 case 2: if(setchainname
)
559 if(!strncmp(ptr
,"post_",5) || eq(ptr
,"POSTROUTING"))
565 if(!strncmp(ptr
,"forw_",5) || eq(ptr
,"FORWARD"))
570 if(eq(ptr
,"post_common") || eq(ptr
,"forw_common"))
577 sscanf(ptr
,"%Lu",&traffic
);
582 case 3: if((strncmp(ptr
,"post_",5) && strncmp(ptr
,"forw_",5)) || commonflag
)
586 /*if(filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/
588 case 8: if(downloadflag
)
590 if(strstr(proxy_ip
,ptr
))
600 case 9: if(downloadflag
)ipaddr
=ptr
;break;
603 if(accept
&& traffic
>0 && ipaddr
)
609 else if(!downloadflag
)
613 printf("IP %s: %Lu M (%ld pkts)\n", ipaddr
, traffic
, pkts
);
615 if_exists(ip
,ips
,eq(ip
->addr
,ipaddr
));
620 if(eq(ip
->addr
,"0.0.0.0/0"))
622 ip
->name
="(unregistered)";
624 ip
->max
=ip
->desired
=free_max
;
636 ip
->traffic
+=traffic
;
638 ip
->direct
=ip
->traffic
-ip
->upload
-ip
->proxy
;
647 ip
->traffic
+=traffic
;
651 if(traffic
>ip
->traffic
)
662 /* ========== This function executes, logs OR ALSO prints command ========== */
664 void safe_run(char *cmd
)
668 printf("\n=>%s\n",cmd
);
676 fprintf(log_file
,"%s\n",cmd
);
680 void save_line(char *line
)
682 fprintf(iptables_file
,"%s\n",line
);
685 void run_restore(void)
688 string(restor
,STRLEN
);
690 /*-----------------------------------------------------------------*/
691 printf("Running %s <%s ...\n", iptablesrestore
, iptablesfile
);
692 /*-----------------------------------------------------------------*/
695 fclose(iptables_file
);
705 sprintf(restor
,"%s <%s",iptablesrestore
, iptablesfile
);
711 /* == This function strips extra characters after IP address and stores it = */
713 void parse_ip(char *str
)
715 char *ptr
,*ipaddr
=NULL
,*ipname
=NULL
,*lmsid
=NULL
;
721 while(*ptr
&& *ptr
!='}')
729 while(*ptr
&& *ptr
!=' ' && *ptr
!=9)
737 while(*ptr
&& (*ptr
==' ' || *ptr
==9))
742 while(*ptr
&& *ptr
!=' ' && *ptr
!=9)
748 if_exists(ip
,ips
,eq(ip
->addr
,ipaddr
));
757 ip
->lmsid
=atoi(lmsid
);
762 char *parse_datafile_line(char *str
)
764 char *ptr
=strchr(str
,' ');
789 void parse_ip_log(int argc
, char **argv
)
791 char *month
, *year
, *str
, *name
="(undefined)", *ptr
, *ptr2
, *filename
;
792 long traffic
=0l, traffic_month
, total
=0, guaranted
;
793 int col
, col2
, y_ok
, m_ok
, accept_month
, i
=1, any_month
=0, lmsid
;
794 char mstr
[4], ystr
[5];
797 string(filename
,STRLEN
);
799 if(argv
[1][1]=='l') /* -l */
803 puts("Missing parameter(s)!\nUsage: prometheus -l Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)");
809 if(eq(month
,"Year")) any_month
=1;
815 time_t t
= time(NULL
) - 3600*24 ; /* yesterday's timestamp*/
816 struct tm
*timep
= localtime(&t
);
818 if(argv
[1][1]=='m') /* -m yestarday - month */
820 strftime(mstr
, 4, "%b", timep
);
822 strftime(ystr
, 5, "%Y", timep
);
825 else /* -y yesterday - year */
829 strftime(ystr
, 5, "%Y", timep
);
833 printf("Analysing traffic for %s %s ...\n",month
,year
);
835 /* sorry... next release of C<<1 header file will include for_path_files(name,path) { } macro */
836 sprintf(str
,"%s %s/",ls
,log_dir
);
840 if(strstr(str
,".log"))
842 ptr
=strrchr(str
,'\n');
844 sprintf(filename
,"%s/%s",log_dir
,str
);
845 printf("Parsing %s ...",filename
);
853 valid_columns(ptr
,_
,'\t',col
) switch(col
)
855 case 2: name
= ptr
;break;
856 case 3: traffic
= atol(ptr
);break;
857 /* column number - was 7, now 11...*/
862 case 11: if(isalpha(*ptr
)) /* character, not numeric string = date, just one*/
864 valid_columns(ptr2
,ptr
,' ',col2
) switch(col2
)
866 case 2: if(any_month
|| eq(ptr2
,month
)) m_ok
= 1; break;
867 case 5: if(eq(ptr2
,year
)) y_ok
= 1; break;
872 if(col
== 7) guaranted
= atol(ptr
);
873 if(col
== 10) lmsid
= atoi(ptr
);
879 traffic_month
+= traffic
;
889 iplog
->guaranted
= guaranted
;
890 iplog
->traffic
= traffic_month
;
891 iplog
->lmsid
= lmsid
;
892 insert(iplog
,iplogs
,desc_order_by
,traffic
);
893 printf(" %ld MB\n",iplog
->traffic
);
897 puts(" no records.");
901 sprintf(str
,"%s/%s-%s.html",html_log_dir
,year
,month
);
902 printf("Writing %s ... ",str
);
906 fprintf(f
, "<table class=\"decorated last\"><thead>\n\
907 <tr><th colspan=\"2\">%s %s</th>\n\
908 <th style=\"text-align: right\">lms</th>\n\
909 <th colspan=\"2\">Data transfers</th>\n\
910 <th style=\"text-align: right\">Min.speed</th>\n\
911 </tr></thead><tbody>\n ",
915 for_each(iplog
, iplogs
)
919 fprintf(f
, "%s<td style=\"text-align: right\">%d</td>\n\
920 <td style=\"text-align: left\"><a class=\"blue\" href=\"%s%s.log\">%s</td>\n\
921 <td style=\"text-align: right\">",
922 tr_odd_even(), i
++, log_url
, iplog
->name
, iplog
->name
);
925 /*base URL will be configurable soon ... */
926 fprintf(f
, "<a class=\"blue\" href=\"%s%d\">%04d</a>\n", lms_url
, iplog
->lmsid
, iplog
->lmsid
);
928 else if(iplog
->lmsid
== 0)
932 fprintf(f
, "<td style=\"text-align: right\">%ld MB</td>\n\
933 <td style=\"text-align: right\"><strong>%ld GB</strong></td>\n\
934 <td style=\"text-align: right\">%ld kb/s</th></tr>\n",
935 iplog
->traffic
, iplog
->traffic
>>10, iplog
->guaranted
);
936 total
+=iplog
->traffic
>>10;
941 fprintf(f
,"</tbody><thead><tr>\
942 <td colspan=\"4\" style=\"text-align: left\">Total:</td>\
943 <td style=\"text-align: right\"><strong>%ld GB</strong></td>\
944 <td style=\"text-align: right\"><strong>%Ld kb/s</strong></td></tr>\n", total
, line
);
945 fputs("</thead></table>\n", f
);
950 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\">\n\
951 <caption>Enterprise Resource Planning (ERP)</caption>\n\
953 <th>Analytic category</th>\n\
954 <th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\
955 <th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\
956 </tr></thead><tbody>\n",f
);
958 if_exists(iplog
,iplogs
,iplog
->l
>=total
/4)
960 fprintf(f
,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());
961 fprintf(f
,"<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d %%</td><td style=\"text-align: right\">%ld G</td><td style=\"text-align: right\">%d %%</td></tr>\n",iplog
->i
,(100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
964 if_exists(iplog
,iplogs
,iplog
->i
==10)
966 fprintf(f
,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());
967 fprintf(f
,"<td style=\"text-align: right\"><strong>10</strong></td>\n\
968 <td style=\"text-align: right\">%d %%</td>\n\
969 <td style=\"text-align: right\">%ld G</td>\n\
970 <td style=\"text-align: right\">%d %%</td></tr>\n",
971 (100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
974 if_exists(iplog
,iplogs
,iplog
->l
>=total
/2)
976 fprintf(f
,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());
977 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
978 <td style=\"text-align: right\">%d %%</td>\n\
979 <td style=\"text-align: right\">%ld G</td>\n\
980 <td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",
981 iplog
->i
,(100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
984 if_exists(iplog
,iplogs
,iplog
->l
>=4*total
/5)
986 fprintf(f
,"%s<td>Top 80%% of traffic</td>\n",tr_odd_even());
987 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
988 <td style=\"text-align: right\">%d %%</td>\n\
989 <td style=\"text-align: right\">%ld G</td>\n\
990 <td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",
991 iplog
->i
,(100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
994 if_exists (iplog
,iplogs
,iplog
->i
>=i
/5)
996 fprintf(f
,"%s<td>Top 20%% downloaders</td>\n",tr_odd_even());
997 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
998 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
999 <td style=\"text-align: right\">%ld G</td>\n\
1000 <td style=\"text-align: right\">%d %%</td></tr>\n",
1001 iplog
->i
,(100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
1004 if_exists(iplog
,iplogs
,iplog
->i
>=i
/4)
1006 fprintf(f
,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());
1007 fprintf(f
,"<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d %%</td><td style=\"text-align: right\">%ld G</td><td style=\"text-align: right\">%d %%</td></tr>\n",iplog
->i
,(100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
1010 if_exists(iplog
,iplogs
,iplog
->i
>=i
/2)
1012 fprintf(f
,"%s<td>Top 50%% downloaders</td>\n",tr_odd_even());
1013 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
1014 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
1015 <td style=\"text-align: right\">%ld G</td><td style=\"text-align: right\">%d %%</td></tr>\n",iplog
->i
,(100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
1018 if_exists(iplog
,iplogs
,iplog
->i
>=4*i
/5)
1020 fprintf(f
,"%s<td>Top 80%% downloaders</td>\n",tr_odd_even());
1021 fprintf(f
,"<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d %%</td><td style=\"text-align: right\">%ld G</td><td style=\"text-align: right\">%d %%</td></tr>\n",iplog
->i
,(100*iplog
->i
+50)/i
,iplog
->l
,(int)((100*iplog
->l
+50)/total
));
1024 fprintf(f
,"</tbody><thead><tr>\n\
1025 <th>All users, all traffic</th>\n\
1026 <th style=\"text-align: right\">%d</th>\n\
1027 <th style=\"text-align: right\">100 %%</th>\n\
1028 <th style=\"text-align: right\">%ld G</th>\n\
1029 <th style=\"text-align: right\">100 %%</th></tr>\n",i
-1,total
);
1030 fputs("</thead></table>\n", f
);
1033 fprintf(f
, stats_html_signature
, version
);
1043 void append_log(struct IP
*self
) /*using global variables*/
1048 date(d
); /* this is typical cll1.h macro - prints current date */
1050 sprintf(str
,"%s/%s.log", log_dir
, self
->name
);
1054 fprintf(f
,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%d\t%s",
1055 time(NULL
), self
->name
, self
->traffic
, self
->direct
, self
->proxy
,
1056 self
->upload
, self
->min
, self
->max
, self
->desired
, self
->lmsid
, d
); /* d = date*/
1066 /*-----------------------------------------------------------------*/
1067 /* Are you looking for int main(int argc, char **argv) ? :-)) */
1068 /*-----------------------------------------------------------------*/
1074 char *str
, *ptr
, *d
;
1076 int class_count
=0,ip_count
=0;
1078 int just_flush
=FALSE
;
1080 int just_preview
=FALSE
; /* preview - generate just stats */
1081 int just_logs
=FALSE
; /* just parse logs */
1085 char *chain_forward
, *chain_postrouting
;
1086 char *althosts
=NULL
;
1089 Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\
1090 Version %s - Copyright (C)2005-2012 Michael Polak (xChaos)\n\
1091 iptables-restore & burst tunning & classify modification by Ludva\n\
1092 Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version
);
1094 /*----- Boring... we have to check command line options first: ----*/
1098 argument("-c") { nextargument(config
); }
1099 argument("-h") { nextargument(althosts
);}
1100 argument("-d") { run
=TRUE
; dry_run
=TRUE
; }
1101 argument("-f") { run
=TRUE
; just_flush
=TRUE
; }
1102 argument("-9") { run
=TRUE
; just_flush
=9; }
1103 argument("-p") { run
=TRUE
; just_preview
=TRUE
; }
1104 argument("-r") { run
=TRUE
; }
1105 argument("-n") { run
=TRUE
; nodelay
=TRUE
; }
1106 argument("-l") { just_logs
=TRUE
; }
1107 argument("-m") { just_logs
=TRUE
; }
1108 argument("-y") { just_logs
=TRUE
; }
1109 argument("-?") { help(); exit(0); }
1110 argument("--help") { help(); exit(0); }
1111 argument("-v") { exit(0); }
1112 argument("--version") { exit(0); }
1117 puts("*** THIS IS JUST DRY RUN ! ***\n");
1120 date(d
); /* this is typical cll1.h macro - prints current date */
1122 /*-----------------------------------------------------------------*/
1123 printf("Parsing configuration file %s ...\n", config
);
1124 /*-----------------------------------------------------------------*/
1129 parse_ip_log(argc
,argv
);
1145 /*-----------------------------------------------------------------*/
1146 puts("Parsing iptables verbose output ...");
1147 /*-----------------------------------------------------------------*/
1148 get_traffic_statistics();
1151 /*-----------------------------------------------------------------*/
1152 printf("Parsing class defintion file %s ...\n", hosts
);
1153 /*-----------------------------------------------------------------*/
1154 int groupidx
= FIRSTGROUPID
;
1159 if(*str
<'0' || *str
>'9')
1161 /* any line starting with non-number is comment ...*/
1165 //Does this IP share QoS class with some other ?
1166 substring
=strstr(str
,"sharing-");
1169 substring
+=8; //"sharing-"
1172 ip
->sharing
=substring
;
1173 ip
->keyword
=defaultkeyword
; /* settings for default keyword */
1174 while(*substring
&& *substring
!='\n')
1182 //Do we have to create new QoS class for this IP ?
1184 if_exists(keyword
,keywords
,(substring
=strstr(str
,keyword
->key
)))
1188 ip
->keyword
=keyword
;
1189 keyword
->ip_count
++;
1190 ip
->prio
=keyword
->default_prio
;
1191 substring
+=strlen(keyword
->key
)+1;
1193 while(*ptr
&& *ptr
!='-')
1200 ip
->max
= ip
->desired
=atoi(ptr
+1);
1202 ip
->min
= atoi(substring
);
1205 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",
1209 if(ip
->max
<= ip
->min
)
1212 ip
->max
= ip
->min
+ip
->keyword
->reserve_min
;
1216 ip
->max
-= ip
->keyword
->reserve_max
;
1222 ip
->mark
=FIRSTIPCLASS
+1+class_count
++;
1224 if_exists(group
,groups
,group
->min
==ip
->min
)
1227 group
->desired
+= ip
->min
;
1228 ip
->group
= group
->id
;
1232 create(group
,Group
);
1233 group
->min
= ip
->min
;
1234 group
->id
= groupidx
++;
1235 ip
->group
= group
->id
;
1237 if(group
->min
<8) group
->min
=8;
1238 /* Warning - this is maybe because of primitive tc namespace, can be fixed */
1239 /* it is because class IDs are derived from min. bandwidth. - xCh */
1240 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;
1243 group
->desired
=ip
->min
;
1244 insert(group
,groups
,desc_order_by
,min
);
1256 /*-----------------------------------------------------------------*/
1257 /* cll1.h - let's allocate brand new character buffer... */
1258 /*-----------------------------------------------------------------*/
1261 /*-----------------------------------------------------------------*/
1262 puts("Resolving shared connections ...");
1263 /*-----------------------------------------------------------------*/
1264 for_each(ip
,ips
) if(ip
->sharing
)
1266 for_each(sharedip
,ips
) if(eq(sharedip
->name
,ip
->sharing
))
1268 sharedip
->traffic
+=ip
->traffic
;
1270 ip
->mark
=sharedip
->mark
;
1271 ip
->lmsid
=sharedip
->lmsid
;
1276 printf("Unresolved shared connection: %s %s sharing-%s\n",
1277 ip
->addr
, ip
->name
, ip
->sharing
);
1281 if(enable_credit
&& just_flush
<9)
1283 /*-----------------------------------------------------------------*/
1284 printf("Parsing credit file %s ...\n", credit
);
1285 /*-----------------------------------------------------------------*/
1288 ptr
=parse_datafile_line(_
);
1291 if_exists(ip
,ips
,eq(ip
->addr
,_
))
1293 sscanf(ptr
,"%Lu",&(ip
->credit
));
1302 /*-----------------------------------------------------------------*/
1303 puts("Initializing iptables and tc classes ...");
1304 /*-----------------------------------------------------------------*/
1306 iptables_file
=fopen(iptablesfile
,"w");
1307 if(iptables_file
== NULL
)
1309 puts("Cannot open iptablesfile!");
1313 log_file
=fopen(cmdlog
,"w");
1314 if(log_file
== NULL
)
1316 puts("Cannot open logfile!");
1320 save_line(iptablespreamble
);
1323 sprintf(str
,"%s qdisc del dev %s root 2>/dev/null",tc
,lan
);
1326 sprintf(str
,"%s qdisc del dev %s root 2>/dev/null",tc
,wan
);
1329 iptables_file
=fopen(iptablesfile
,"w");
1330 save_line(iptablespreamble
);
1332 if(qos_free_zone
&& *qos_free_zone
!='0')
1336 sprintf(str
,"-A FORWARD -d %s -o %s -j ACCEPT", qos_free_zone
, wan
);
1341 save_line(":post_noproxy - [0:0]");
1342 sprintf(str
,"-A POSTROUTING -p ! tcp -o %s -j post_noproxy", lan
);
1344 sprintf(str
,"-A POSTROUTING -s ! %s -o %s -j post_noproxy", proxy_ip
, lan
);
1346 sprintf(str
,"-A POSTROUTING -s %s -p tcp --sport ! %d -o %s -j post_noproxy", proxy_ip
, proxy_port
, lan
);
1349 chain
="post_noproxy";
1353 chain
="POSTROUTING";
1356 sprintf(str
,"-A %s -s %s -o %s -j ACCEPT", chain
, qos_free_zone
, lan
);
1360 if(ip_count
>idxtable_treshold1
&& !just_flush
)
1362 int idxcount
=0, bitmask
=32-idxtable_bitmask1
; /* default net mask: 255.255.255.240 */
1364 /*-----------------------------------------------------------------*/
1365 printf("Detected %d addresses - indexing iptables rules to improve performance...\n",ip_count
);
1366 /*-----------------------------------------------------------------*/
1368 save_line(":post_common - [0:0]");
1369 save_line(":forw_common - [0:0]");
1371 for_each(ip
,ips
) if(ip
->addr
&& *(ip
->addr
) && !eq(ip
->addr
,"0.0.0.0/0"))
1373 buf
=hash_id(ip
->addr
,bitmask
);
1374 if_exists(idx
,idxs
,eq(idx
->id
,buf
))
1383 idx
->bitmask
=bitmask
;
1391 /* brutal perfomance optimalization */
1392 while(idxcount
>idxtable_treshold2
&& bitmask
>2*idxtable_bitmask2
)
1394 bitmask
-=idxtable_bitmask2
;
1397 for_each(idx
,idxs
) if(idx
->parent
== NULL
)
1399 buf
=hash_id(idx
->addr
,bitmask
);
1400 if_exists(metaindex
,idxs
,eq(metaindex
->id
,buf
))
1402 metaindex
->children
++;
1406 create(metaindex
,Index
);
1407 metaindex
->addr
=idx
->addr
;
1409 metaindex
->bitmask
=bitmask
;
1410 metaindex
->parent
=NULL
;
1411 metaindex
->children
=0;
1413 push(metaindex
,idxs
);
1415 idx
->parent
=metaindex
;
1419 /* this should slightly optimize throughout ... */
1420 sort(idx
,idxs
,desc_order_by
,children
);
1421 sort(idx
,idxs
,order_by
,bitmask
);
1426 subnet
=subnet_id(idx
->addr
,idx
->bitmask
);
1427 printf("%d: %s/%d\n",
1428 ++i
, subnet
, idx
->bitmask
);
1430 sprintf(str
,":post_%s - [0:0]", idx
->id
);
1433 sprintf(str
,":forw_%s - [0:0]", idx
->id
);
1438 string(buf
,strlen(idx
->parent
->id
)+6);
1439 sprintf(buf
,"post_%s",idx
->parent
->id
);
1446 sprintf(str
,"-A %s -d %s/%d -o %s -j post_%s", buf
, subnet
, idx
->bitmask
, lan
, idx
->id
);
1449 sprintf(str
,"-A %s -d %s/%d -o %s -j post_common", buf
, subnet
, idx
->bitmask
, lan
);
1454 string(buf
,strlen(idx
->parent
->id
)+6);
1455 sprintf(buf
,"forw_%s",idx
->parent
->id
);
1462 sprintf(str
,"-A %s -s %s/%d -o %s -j forw_%s", buf
, subnet
, idx
->bitmask
, wan
, idx
->id
);
1465 sprintf(str
,"-A %s -s %s/%d -o %s -j forw_common", buf
, subnet
, idx
->bitmask
, wan
);
1468 printf("Total indexed iptables chains created: %d\n", i
);
1470 sprintf(str
,"-A FORWARD -o %s -j forw_common", wan
);
1473 sprintf(str
,"-A POSTROUTING -o %s -j post_common", lan
);
1481 fclose(iptables_file
);
1486 puts("Just flushed iptables and tc classes - now exiting ...");
1492 if(!dry_run
&& !nodelay
&& qos_free_delay
)
1494 printf("Flushed iptables and tc classes - now sleeping for %d seconds...\n",qos_free_delay
);
1495 sleep(qos_free_delay
);
1498 sprintf(str
,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",
1502 sprintf(str
, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d",
1503 tc
,lan
,lan_medium
,lan_medium
,burst_main
,highest_priority
);
1506 sprintf(str
, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d",
1507 tc
,lan
,line
,line
,burst_main
,highest_priority
);
1510 sprintf(str
,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",tc
,wan
,htb_r2q
);
1513 sprintf(str
, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d",
1514 tc
,wan
,wan_medium
,wan_medium
,burst_main
,highest_priority
);
1517 sprintf(str
, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d",
1518 tc
,wan
,up
,up
,burst_main
,highest_priority
);
1522 /*-----------------------------------------------------------------*/
1523 puts("Locating heavy downloaders and generating root classes ...");
1524 /*-----------------------------------------------------------------*/
1525 sort(ip
,ips
,desc_order_by
,traffic
);
1527 /*-----------------------------------------------------------------*/
1528 /* sub-scope - local variables */
1530 long long int rate
= line
;
1531 long long int max
= line
;
1532 int group_count
= 0;
1533 FILE *credit_file
= NULL
;
1535 if(!just_preview
&& !dry_run
&& enable_credit
)
1537 credit_file
= fopen(credit
,"w");
1540 for_each(group
,groups
)
1545 sprintf(str
,"%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d #down desired %d",
1546 tc
, lan
, parent
, group
->id
, rate
, max
, burst_group
, highest_priority
+1, group
->desired
);
1550 sprintf(str
,"%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d #up desired %d",
1551 tc
, wan
, parent
, group
->id
, rate
*up
/line
, max
*up
/line
, burst_group
, highest_priority
+1, group
->desired
);
1555 if(group_count
++ < max_nesting
)
1560 rate
-= digital_divide
*group
->min
;
1561 if(rate
< group
->min
)
1566 /*shaping of aggresive downloaders, with credit file support */
1569 int group_rate
= group
->min
, priority_sequence
= lowest_priority
;
1571 for_each(ip
, ips
) if(ip
->min
== group
->min
&& ip
->max
> ip
->min
)
1573 if( ip
->keyword
->data_limit
&& !ip
->fixedprio
1574 && ( ip
->traffic
>ip
->credit
1575 + (ip
->min
*ip
->keyword
->data_limit
+(ip
->keyword
->fixed_limit
<<20))) )
1577 if(group_rate
<ip
->max
)
1581 group_rate
+=magic_treshold
;
1582 ip
->prio
=lowest_priority
;
1583 if(ip
->prio
<highest_priority
+2)
1585 ip
->prio
=highest_priority
+2;
1590 if( ip
->keyword
->data_prio
1592 && ( ip
->traffic
>ip
->credit
1593 + (ip
->min
*ip
->keyword
->data_prio
+(ip
->keyword
->fixed_prio
<<20))) )
1595 ip
->prio
=priority_sequence
--;
1596 if(ip
->prio
<highest_priority
+1)
1598 ip
->prio
=highest_priority
+1;
1604 unsigned long long lcredit
=0;
1606 if((ip
->min
*ip
->keyword
->data_limit
+(ip
->keyword
->fixed_limit
<<20))>ip
->traffic
)
1608 lcredit
=(ip
->min
*ip
->keyword
->data_limit
+(ip
->keyword
->fixed_limit
<<20))-ip
->traffic
;
1610 fprintf(credit_file
,"%s %Lu\n",ip
->addr
,lcredit
);
1618 fclose(credit_file
);
1624 f
=fopen(preview
,"w");
1627 else if(!dry_run
&& !just_flush
)
1629 /*-----------------------------------------------------------------*/
1630 printf("Writing daily statistics %s ... ", json
);
1631 /*-----------------------------------------------------------------*/
1643 if(ip
->traffic
|| ip
->direct
|| ip
->proxy
|| ip
->upload
)
1645 fprintf(f
, " %d:{ \"ip\":\"%s\", \"total\":%Lu, \"down\":%Lu, \"proxy\":%Lu, \"up\":%Lu }",
1646 ip
->lmsid
, ip
->addr
, ip
->traffic
, ip
->direct
, ip
->proxy
, ip
->upload
);
1667 /*-----------------------------------------------------------------*/
1668 printf("Sorting data and generating statistics page %s ...\n", ptr
);
1669 /*-----------------------------------------------------------------*/
1671 if(use_jquery_popups
)
1673 fprintf(f
,"<script type=\"text/javascript\" src=\"%s\"></script>\n", jquery_url
);
1675 fputs("<table class=\"decorated last\">\n\
1676 <caption>Bandwidth classes</caption>\n\
1678 <th style=\"text-align: right\">#</th>\n\
1679 <th style=\"text-align: right\">group</th>\n\
1680 <th style=\"text-align: right\">IPs</th>\n\
1681 <th style=\"text-align: right\">requested</th>\n",f
);
1682 fprintf(f
,"<th colspan=\"%d\">data limits</th>\n", keywordcount
);
1683 fputs("</tr></thead><tbody>\n",f
);
1686 for_each(group
, groups
)
1689 printf("%d kb/s group: %d bandwidth requested: %d kb/s\n",group
->min
,group
->count
,group
->desired
);
1691 fprintf(f
, "%s<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d kb/s</td>",
1692 tr_odd_even(), count
, group
->min
);
1693 fprintf(f
, "<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d kb/s</td>",
1694 group
->count
, group
->desired
);
1696 for_each(keyword
, keywords
) if(keyword
->ip_count
)
1698 fprintf(f
,"<td style=\"text-align: right\"><span style=\"color:#%s\">%d MB</span></td>",
1699 keyword
->html_color
, group
->min
*keyword
->data_limit
);
1701 i
+= group
->desired
;
1702 total
+= group
->count
;
1706 printf("Total groups: %d Total bandwidth requested: %d kb/s\nAGGREGATION: 1/%d\n",
1709 fprintf(f
,"</tr></tbody>\n\
1711 <th colspan=\"2\" style=\"text-align: left\">Line %Ld kb/s</td>",line
);
1712 fprintf(f
,"<th style=\"text-align: right\">%d</td><th style=\"text-align: right\">%d kb/s</td>",total
,i
);
1714 for_each(keyword
, keywords
) if(keyword
->ip_count
)
1716 fprintf(f
,"<th style=\"text-align: right\">%d IPs</th>",keyword
->ip_count
);
1718 fprintf(f
,"</tr><tr><th colspan=\"4\">Aggregation 1/%d</th>\n", (int)(0.5+i
/line
));
1719 fprintf(f
,"<th colspan=\"%d\">%d traffic classes</th></tr>\n", keywordcount
, total
);
1721 fputs("</thead></table>\n",f
);
1723 else if(!dry_run
&& !just_flush
)
1731 unsigned long long total_traffic
=0, total_direct
=0, total_proxy
=0, total_upload
=0, tmp_sum
=0;
1732 int active_classes
=0;
1734 struct Sum
{unsigned long long l
; int i
; list(Sum
);} *sum
,*sums
=NULL
;
1735 int limit_count
=0, prio_count
=0;
1743 fprintf(f
,"<p><table class=\"decorated last\">\n<caption>%s",title
);
1744 fprintf(f
," (%s)</caption>\n", d
);
1745 fputs("<thead><tr>\n<th colspan=\"3\"> </th>\n",f
);
1746 fputs("<th style=\"text-align: right\">credit</th>\n\
1747 <th style=\"text-align: right\">FUP</th>\n\
1748 <th style=\"text-align: right\">total</th>\n\
1749 <th style=\"text-align: right\">down</th>\n",f
);
1752 fputs("<th style=\"text-align: right\">proxy</th>\n",f
);
1754 fputs("<th style=\"text-align: right\">up</th>\n\
1755 <th style=\"text-align: right\">min</th>\n\
1756 <th style=\"text-align: right\">max</th>\n\
1757 <th style=\"text-align: right\">limit</th>\n\
1760 <th style=\"text-align: right\">#</th>\n\
1761 <th>hostname [+sharing]</th>\n\
1762 <th style=\"text-align: right\">LMS</th>\n\
1763 <th style=\"text-align: right\">MB</th>\n\
1764 <th style=\"text-align: right\">MB</th>\n\
1765 <th style=\"text-align: right\">MB</th>\n\
1766 <th style=\"text-align: right\">MB</th>\n\
1767 <th style=\"text-align: right\">MB</th>\n\
1768 <th style=\"text-align: right\">kb/s</th>\n\
1769 <th style=\"text-align: right\">kb/s</th>\n\
1770 <th style=\"text-align: right\">kb/s</th>\n\
1772 </tr></thead><tbody>\n",f
);
1775 for_each(ip
,ips
) if(!use_jquery_popups
|| !ip
->sharing
)
1777 char *f1
="", *f2
="";
1780 if(ip
->max
< ip
->desired
)
1782 f1
="<span style=\"color:red\">";
1786 else if(ip
->prio
> highest_priority
+1)
1788 f1
="<span style=\"color:brown\">";
1794 printf("%03d. %-22s %10Lu (%d/%d)\n",i
,ip
->name
, ip
->traffic
, ip
->min
, ip
->max
);
1796 /* hostnames -------------------------------------- */
1797 fprintf(f
,"%s<td style=\"text-align: right\"><a name=\"%s\"></a>%d</td><td><a class=\"blue\" href=\"%s%s.log\">%s</a>\n",
1798 tr_odd_even(), ip
->name
, i
, log_url
, ip
->name
, ip
->name
);
1800 if(use_jquery_popups
)
1802 fprintf(f
,"<span id=\"sharing_%d\" style=\"display:none\">",i
);
1804 for_each(sharedip
, ips
) if(eq(ip
->name
, sharedip
->sharing
))
1806 fprintf(f
,"<br /><a class=\"blue\" href=\"%s%s.log\">%s</a>\n", log_url
, sharedip
->name
, sharedip
->name
);
1809 fputs("</span>\n",f
);
1812 fprintf(f
,"<span>[<a class=\"blue\" href=\"#\" onClick=\"$(this).parent().hide();$(\'#sharing_%d\').show();$(\'#download_%d\').show();$(\'#upload_%d\').show();return(false);\" style=\"cursor: pointer;\">+%d</a>]</span>",
1813 i
, i
, i
, popup_button
);
1817 /* ----------------------------------------------- */
1821 fputs("<td style=\"text-align: right\">",f
);
1824 fprintf(f
,"<a class=\"blue\" href=\"%s%d\">%04d</a>\n", lms_url
, ip
->lmsid
, ip
->lmsid
);
1826 else if(ip
->lmsid
== 0)
1832 fprintf(f
,"<td style=\"text-align: right\">%Lu</td>\n", ip
->credit
);
1833 fprintf(f
,"<td style=\"text-align: right\"><span style=\"color:#%s\">%Lu</span></td>",
1834 ip
->keyword
->html_color
,
1835 ip
->credit
+(ip
->min
*ip
->keyword
->data_limit
+(ip
->keyword
->fixed_limit
<<20)));
1836 fprintf(f
,"<td style=\"text-align: right\">%s%Lu%s", f1
, ip
->traffic
, f2
);
1838 /* download --------------------------------------- */
1839 fprintf(f
,"</td><td style=\"text-align: right\">%Lu", ip
->direct
);
1840 if(use_jquery_popups
)
1842 fprintf(f
,"<span id=\"download_%d\" style=\"display:none\">",i
);
1843 for_each(sharedip
, ips
) if(eq(ip
->name
, sharedip
->sharing
))
1845 fprintf(f
,"<br />%Lu", sharedip
->direct
);
1847 fputs("</span>\n",f
);
1850 /* ----------------------------------------------- */
1854 fprintf(f
,"<td style=\"text-align: right\">%Lu</td>\n", ip
->proxy
);
1856 /* upload ---------------------------------------- */
1857 fprintf(f
,"<td style=\"text-align: right\">%Lu", ip
->upload
);
1858 if(use_jquery_popups
)
1860 fprintf(f
,"<span id=\"upload_%d\" style=\"display:none\">",i
);
1861 for_each(sharedip
,ips
) if(eq(ip
->name
, sharedip
->sharing
))
1863 fprintf(f
,"<br />%Lu", sharedip
->upload
);
1866 append_log(sharedip
);
1869 fputs("</span>\n",f
);
1872 /* ----------------------------------------------- */
1874 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
1875 <td style=\"text-align: right\">%d</td>\n\
1876 <td style=\"text-align: right\">%s%d%s</td>\n\
1877 <td>%s%d%s</td></tr>\n",
1878 ip
->min
, ip
->desired
,
1882 total_traffic
+=ip
->traffic
;
1883 total_direct
+=ip
->direct
;
1884 total_proxy
+=ip
->proxy
;
1885 total_upload
+=ip
->upload
;
1889 tmp_sum
+=ip
->traffic
;
1892 sum
->i
=active_classes
;
1893 insert(sum
,sums
,order_by
,i
);
1901 fprintf(f
,"</tbody><thead><tr>\n\
1902 <th colspan=\"%d\" style=\"text-align: left\">%d CLASSES</th>", colspan
-7, i
);
1903 fprintf(f
,"<th style=\"text-align: right\">%Lu</th><th style=\"text-align: right\">%Lu</th>\n", total_traffic
, total_direct
);
1906 fprintf(f
,"<th style=\"text-align: right\">%Lu</th>\n", total_proxy
);
1908 fprintf(f
,"<th style=\"text-align: right\">%Lu</th>", total_upload
);
1909 fprintf(f
,"<th colspan=\"4\"><span style=\"color:red\">LIMIT %dx</span> <span style=\"color:brown\">LOW-PRIO %dx</span></th></tr>\n</thead></table>\n",limit_count
,prio_count
);
1912 if(active_classes
>10)
1914 int top20_count
=0,top20_perc1
=0;
1915 long long top20_perc2
=0;
1916 unsigned long long top20_sum
=0l;
1918 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\"><caption>Enterprise Resource Planning (ERP)</caption>\n",f
);
1919 fputs("<thead><tr>\n\
1920 <th>Analytic category</th>\n\
1921 <th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\
1922 <th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\
1923 </tr></thead><tbody>\n",f
);
1925 if_exists(sum
,sums
,sum
->l
>=total_traffic
/4)
1927 fprintf(f
,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());
1928 fprintf(f
,"<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d %%</td><td style=\"text-align: right\">%Lu M</td><td style=\"text-align: right\">%Ld %%</td></tr>\n",sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total_traffic
);
1931 if_exists(sum
,sums
,sum
->i
==10)
1933 fprintf(f
,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());
1934 fprintf(f
,"<td style=\"text-align: right\"><strong>10</strong></td>\n\
1935 <td style=\"text-align: right\">%d %%</td>\n\
1936 <td style=\"text-align: right\">%Lu MB</td>\n\
1937 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
1938 (100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total_traffic
);
1941 if_exists(sum
,sums
,sum
->l
>=total_traffic
/2)
1943 fprintf(f
,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());
1944 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
1945 <td style=\"text-align: right\">%d %%</td>\n\
1946 <td style=\"text-align: right\">%Lu MB</td>\n\
1947 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
1948 sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total_traffic
);
1951 if_exists(sum
,sums
,sum
->l
>=4*total_traffic
/5)
1953 fprintf(f
,"%s<td>Top 80%% of traffic</td>\n", tr_odd_even());
1954 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
1955 <td style=\"text-align: right\">%d %%</td>\n\
1956 <td style=\"text-align: right\">%Lu MB</td>\n\
1957 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
1958 sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total_traffic
);
1961 if_exists(sum
,sums
,sum
->i
>=(active_classes
+1)/5)
1963 fprintf(f
,"%s<td>Top 20%% downloaders</td>\n", tr_odd_even());
1965 top20_perc1
=(100*sum
->i
+50)/active_classes
;
1967 top20_perc2
=(100*sum
->l
+50)/total_traffic
;
1968 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
1969 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
1970 <td style=\"text-align: right\">%Lu MB</td>\n\
1971 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
1972 top20_count
,top20_perc1
,top20_sum
,top20_perc2
);
1975 if_exists(sum
,sums
,sum
->i
>=(active_classes
+1)/4)
1977 fprintf(f
,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());
1978 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
1979 <td style=\"text-align: right\">%d %%</td>\n\
1980 <td style=\"text-align: right\">%Lu MB</td>\n\
1981 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
1982 sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total_traffic
);
1985 if_exists(sum
,sums
,sum
->i
>=(active_classes
+1)/2)
1987 fprintf(f
,"%s<td>Top 50%% downloaders</td>\n", tr_odd_even());
1988 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
1989 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
1990 <td style=\"text-align: right\">%Lu MB</td>\n\
1991 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
1992 sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total_traffic
);
1995 if_exists(sum
,sums
,sum
->i
>=4*(active_classes
+1)/5)
1997 fprintf(f
,"%s<td>Top 80%% downloaders</td>\n", tr_odd_even());
1998 fprintf(f
,"<td style=\"text-align: right\">%d</td>\n\
1999 <td style=\"text-align: right\">%d %%</td>\n\
2000 <td style=\"text-align: right\">%Lu MB</td>\n\
2001 <td style=\"text-align: right\">%Ld %%</td></tr></tbody>\n",
2002 sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total_traffic
);
2005 fprintf(f
,"<tr><thead><th><a class=\"blue\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url
);
2006 fprintf(f
,"<th style=\"text-align: right\">%d</th>\n\
2007 <th style=\"text-align: right\">100 %%</th>\n\
2008 <th style=\"text-align: right\">%Lu M</th>\n\
2009 <th style=\"text-align: right\">100 %%</th></tr>\n",active_classes
,total_traffic
);
2010 fputs("</thead></table>\n", f
);
2012 /* write basic ERP data to log directory */
2016 sprintf(str
,"%s/ERP.log",log_dir
);
2017 iplog
=fopen(str
,"a");
2020 fprintf(iplog
,"%ld\t%d\t%d %%\t%Lu M\t%Ld %%\tACTIVE %d\tTRAFFIC %Lu M\tCLASSES %d\tFUP-LIMIT %d\tLOW-PRIO %d\t%s",
2021 time(NULL
), top20_count
, top20_perc1
, top20_sum
, top20_perc2
,
2022 active_classes
, total_traffic
, i
, limit_count
, prio_count
, d
); /* d = date*/
2032 fprintf(f
, stats_html_signature
, version
);
2038 puts("Statistics preview generated (-p switch) - now exiting ...");
2044 printf("%-22s %-15s mark\n","name","ip");
2047 printf("Writing %s ... ", classmap
);
2048 f
= fopen(classmap
, "w");
2054 /*-----------------------------------------------------------------*/
2055 puts("Generating iptables and tc classes ... ");
2056 /*-----------------------------------------------------------------*/
2058 for_each(ip
, ips
) if(ip
->mark
> 0)
2063 duplicate(ip
->addr
,buf
);
2064 buf
=hash_id(ip
->addr
,32-idxtable_bitmask1
);
2066 string(chain_forward
,6+strlen(buf
));
2067 strcpy(chain_forward
,"forw_");
2068 strcat(chain_forward
,buf
);
2070 string(chain_postrouting
,6+strlen(buf
));
2071 strcpy(chain_postrouting
,"post_");
2072 strcat(chain_postrouting
,buf
);
2078 chain_forward
="FORWARD";
2079 chain_postrouting
="POSTROUTING";
2083 printf("%-22s %-16s %04d ", ip
->name
, ip
->addr
, ip
->mark
);
2086 /* -------------------------------------------------------- mark download */
2088 sprintf(str
, "-A %s -d %s/32 -o %s -j %s%d",
2089 chain_postrouting
, ip
->addr
, lan
, mark_iptables
, ip
->mark
);
2090 /*sprintf(str,"-A %s -d %s/32 -o %s -j MARK --set-mark %d",chain_postrouting,ip->addr,lan,ip->mark);*/
2091 /* -m limit --limit 1/s */
2096 sprintf(str
, "-A %s -s %s -p tcp --sport %d -d %s/32 -o %s -j %s%d",
2097 chain_postrouting
, proxy_ip
, proxy_port
, ip
->addr
, lan
, mark_iptables
, ip
->mark
);
2098 /*sprintf(str,"-A %s -s %s -p tcp --sport %d -d %s/32 -o %s -j MARK --set-mark %d",chain_postrouting,proxy_ip,proxy_port,ip->addr,lan,ip->mark);*/
2102 sprintf(str
, "-A %s -d %s/32 -o %s -j ACCEPT",
2103 chain_postrouting
, ip
->addr
, lan
);
2106 /* -------------------------------------------------------- mark upload */
2107 sprintf(str
, "-A %s -s %s/32 -o %s -j %s%d",
2108 chain_forward
, ip
->addr
, wan
, mark_iptables
, ip
->mark
);
2109 /* sprintf(str,"-A %s -s %s/32 -o %s -j MARK --set-mark %d",chain_forward,ip->addr,wan,ip->mark);*/
2112 sprintf(str
, "-A %s -s %s/32 -o %s -j ACCEPT",
2113 chain_forward
, ip
->addr
, wan
);
2118 /* -------------------------------------------------------- download class */
2120 printf("(down: %dk-%dk ", ip
->min
, ip
->max
);
2123 sprintf(str
, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
2124 tc
, lan
, ip
->group
, ip
->mark
,ip
->min
,ip
->max
, burst
, ip
->prio
);
2127 if(strcmpi(ip
->keyword
->leaf_discipline
, "none"))
2129 sprintf(str
, "%s qdisc add dev %s parent 1:%d handle %d %s",
2130 tc
, lan
, ip
->mark
, ip
->mark
, ip
->keyword
->leaf_discipline
); /*qos_leaf*/
2134 if(filter_type
== 1)
2136 sprintf(str
, "%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",
2137 tc
, lan
, ip
->mark
, ip
->mark
);
2141 /* -------------------------------------------------------- upload class */
2143 printf("up: %dk-%dk)\n", (int)((ip
->min
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
),
2144 (int)((ip
->max
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
));
2147 sprintf(str
,"%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
2148 tc
, wan
, ip
->group
, ip
->mark
,
2149 (int)((ip
->min
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
),
2150 (int)((ip
->max
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
), burst
, ip
->prio
);
2153 if(strcmpi(ip
->keyword
->leaf_discipline
, "none"))
2155 sprintf(str
, "%s qdisc add dev %s parent 1:%d handle %d %s",
2156 tc
, wan
, ip
->mark
, ip
->mark
, ip
->keyword
->leaf_discipline
); /*qos_leaf*/
2160 if(filter_type
== 1)
2162 sprintf(str
, "%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",
2163 tc
, wan
, ip
->mark
, ip
->mark
);
2169 fprintf(f
, "%s %d", ip
->addr
, ip
->mark
);
2175 printf("(sharing %s)\n", ip
->sharing
);
2188 chain_forward
= "forw_common";
2189 chain_postrouting
= "post_common";
2193 chain_forward
= "FORWARD";
2194 chain_postrouting
= "POSTROUTING";
2196 /* -------------------------------- classify or reject free download */
2198 char *final_chain
= "DROP"; /* REJECT would be better, but it is impossible in mangle */
2201 final_chain
= "ACCEPT";
2207 sprintf(str
,"-A %s -s %s -p tcp --sport %d -o %s -j %s%d",
2208 chain_postrouting
,proxy_ip
,proxy_port
,lan
,mark_iptables
,3);
2211 sprintf(str
,"-A %s -s %s -p tcp --sport %d -o %s -j %s",
2212 chain_postrouting
,proxy_ip
,proxy_port
,lan
,final_chain
);
2217 sprintf(str
,"-A %s -o %s -j %s%d", chain_postrouting
, lan
, mark_iptables
, 3);
2220 sprintf(str
,"-A %s -o %s -j %s", chain_postrouting
, lan
, final_chain
);
2222 /* ------------------------------- classify or reject free upload */
2225 sprintf(str
,"-A %s -o %s -j %s%d", chain_forward
, wan
, mark_iptables
, 3);
2228 sprintf(str
,"-A %s -o %s -j %s", chain_forward
, wan
, final_chain
);
2232 if(free_min
) /* allocate free bandwith if it is not zero... */
2234 /*-----------------------------------------------------------------*/
2235 puts("Generating free bandwith classes ...");
2236 /*-----------------------------------------------------------------*/
2237 sprintf(str
, "%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio %d",
2238 tc
, lan
, parent
, free_min
, free_max
,burst
, lowest_priority
);
2240 sprintf(str
, "%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio %d",
2241 tc
, wan
, parent
, free_min
, free_max
, burst
, lowest_priority
);
2244 if(strcmpi(qos_leaf
, "none"))
2246 sprintf(str
,"%s qdisc add dev %s parent 1:3 handle 3 %s", tc
, lan
, qos_leaf
);
2249 sprintf(str
,"%s qdisc add dev %s parent 1:3 handle 3 %s", tc
, wan
, qos_leaf
);
2252 /* tc handle 1 fw flowid */
2253 sprintf(str
,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3", tc
, lan
);
2256 sprintf(str
,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3", tc
, wan
);
2259 printf("Total IP count: %d\n", i
);
2266 /* that's all folks, thank you for reading it all the way up to this point ;-) */
2267 /* bad luck C<<1 is not yet finished, I promise no sprintf() next time... */
This page took 1.892496 seconds and 3 git commands to generate.