/* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */\r
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */\r
\r
-/* Modified by: xChaos, 20120511\r
+/* Modified by: xChaos, 20120516\r
ludva, 20080415\r
\r
Prometheus QoS is free software; you can redistribute it and/or\r
\r
#include "cll1-0.6.2.h"\r
\r
-const char *version = "0.8.3-c";\r
+const char *version = "0.8.3-d";\r
\r
/* Version numbers: 0.8.3 is development releases ("beta"), 0.8.4 will be "stable" */\r
/* Debian(RPM) package versions/patchlevels: 0.7.9-2, 0.8.0-1, 0.8.0-2, etc. */\r
\r
/* ======= All path names are defined here (for RPM patch) ======= */\r
\r
-char *tc = "/sbin/tc"; /* requires tc with HTB support */\r
-char *iptables = "/sbin/iptables"; /* requires iptables utility */\r
-char *iptablessave = "/sbin/iptables-save"; /* not yet required */\r
-char *iptablesrestore = "/sbin/iptables-restore"; /* requires iptables-restore */\r
+const char *tc = "/sbin/tc"; /* requires tc with HTB support */\r
+const char *iptables = "/sbin/iptables"; /* requires iptables utility */\r
+const char *iptablessave = "/sbin/iptables-save"; /* not yet required */\r
+const char *iptablesrestore = "/sbin/iptables-restore"; /* requires iptables-restore */\r
const char *ls = "/bin/ls"; /* this is not user configurable :-) */\r
\r
char *config = "/etc/prometheus/prometheus.conf"; /* main configuration file */\r
\r
char *iptablesfile = "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/\r
char *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */\r
-char *html = "/var/www/traffic.html"; /* hall of fame filename */\r
+char *classmap = "/var/lib/misc/prometheus.classes"; /* credit log file */\r
+char *html = "/var/www/traffic.html"; /* hall of fame - html version */\r
char *preview = "/var/www/preview.html"; /* hall of fame preview */\r
+char *json = "/var/www/traffic.json"; /* hall of fame - json version */\r
char *cmdlog = "/var/log/prometheuslog"; /* command log filename */\r
char *log_dir = "/var/www/logs/"; /* log directory pathname, ended with slash */\r
char *log_url = "/logs/"; /* log directory relative URI prefix (partial URL) */\r
int use_jquery_popups = 1;\r
int row_odd_even = 0; /*<tr class="odd/even"> */\r
\r
+\r
const char *tr_odd_even(void)\r
{\r
row_odd_even = 1 - row_odd_even;\r
option("iptables",iptables);\r
option("iptables-save",iptablessave); /* new */\r
option("iptables-restore",iptablesrestore); /* new */\r
- option("iptables-file",iptablesfile); /* new */\r
+ option("iptables-in-filename",iptablesfile); /* new */\r
option("hosts",hosts);\r
option("lan-interface",lan);\r
option("wan-interface",wan);\r
ioption("hall-of-fame-enable",hall_of_fame);\r
option("hall-of-fame-title",title);\r
option("hall-of-fame-filename",html);\r
+ option("json-filename",json);\r
option("hall-of-fame-preview",preview);\r
option("log-filename",cmdlog);\r
option("credit-filename",credit);\r
+ option("classmap-filename",classmap);\r
ioption("credit-enable",enable_credit);\r
option("log-traffic-directory",log_dir);\r
option("log-traffic-html-directory",html_log_dir);\r
done;\r
printf("\n");\r
\r
- /*leaf discipline for keywords*/\r
+ /* leaf discipline for keywords */\r
for_each(keyword,keywords)\r
{\r
if(!strcmpi(keyword->leaf_discipline, ""))\r
sprintf(str,"%s/%s-%s.html",html_log_dir,year,month);\r
printf("Writing %s ... ",str);\r
f=fopen(str,"w");\r
- if(f)\r
+ if(f > 0)\r
{\r
fprintf(f, "<table class=\"decorated last\"><thead>\n\\r
<tr><th colspan=\"2\">%s %s</th>\n\\r
iplog->l=total;\r
}\r
}\r
- fprintf(f,"<tr>\\r
+ fprintf(f,"</tbody><thead><tr>\\r
<td colspan=\"4\" style=\"text-align: left\">Total:</td>\\r
<td style=\"text-align: right\"><strong>%ld GB</strong></td>\\r
<td style=\"text-align: right\"><strong>%Ld kb/s</strong></td></tr>\n", total, line);\r
- fputs("</tbody></table>\n", f);\r
+ fputs("</thead></table>\n", f);\r
\r
row_odd_even = 0;\r
if(i>10)\r
\r
fprintf(f, stats_html_signature, version);\r
fclose(f);\r
- puts(" done.");\r
+ puts("done.");\r
}\r
else\r
{\r
string(str,STRLEN); \r
sprintf(str,"%s/%s.log", log_dir, self->name);\r
f=fopen(str,"a");\r
- if(f)\r
+ if(f > 0)\r
{\r
fprintf(f,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%d\t%s",\r
time(NULL), self->name, self->traffic, self->direct, self->proxy,\r
}\r
else if(!dry_run && !just_flush)\r
{\r
- const char *previous="/var/run/prometheus.previous";\r
/*-----------------------------------------------------------------*/\r
- printf("Writing data transfer database %s ... ", previous);\r
+ printf("Writing daily statistics %s ... ", json);\r
/*-----------------------------------------------------------------*/\r
- f=fopen(previous,"w");\r
- if(f)\r
+ f=fopen(json, "w");\r
+ if(f > 0)\r
{\r
- for_each(ip,ips)\r
+ int jsoncount=0;\r
+ fprintf(f, "{\n");\r
+ for_each(ip, ips)\r
{\r
+ if(jsoncount)\r
+ {\r
+ fprintf(f, ",\n");\r
+ }\r
if(ip->traffic || ip->direct || ip->proxy || ip->upload)\r
{\r
- fprintf(f,"%s %Lu %Lu %Lu %Lu\n",\r
- ip->addr, ip->traffic, ip->direct, ip->proxy, ip->upload);\r
+ fprintf(f, "%d:{\"ip\":%s, \"total\":%Lu, \"down\":%Lu, \"proxy\":%Lu, \"up\":%Lu}\n",\r
+ ip->lmsid, ip->addr, ip->traffic, ip->direct, ip->proxy, ip->upload);\r
}\r
+ jsoncount++;\r
}\r
+ fprintf(f, "}\n");\r
fclose(f);\r
- puts(" done.");\r
+ puts("done.");\r
}\r
else\r
{\r
- perror(previous);\r
+ perror(json);\r
}\r
f=fopen(html,"w");\r
ptr=html;\r
{\r
puts("Statistics preview generated (-p switch) - now exiting ...");\r
exit(0);\r
- }\r
- \r
- /*-----------------------------------------------------------------*/\r
- puts("Generating iptables and tc classes ...");\r
- /*-----------------------------------------------------------------*/\r
+ } \r
\r
i=0;\r
#ifdef DEBUG\r
printf("%-22s %-15s mark\n","name","ip");\r
#endif\r
\r
- for_each(ip,ips) if(ip->mark>0)\r
- { \r
+ printf("Writing %s ... ", classmap); \r
+ f = fopen(classmap, "w"); \r
+ if(f < 0)\r
+ {\r
+ perror(classmap);\r
+ }\r
+\r
+ /*-----------------------------------------------------------------*/\r
+ puts("Generating iptables and tc classes ... ");\r
+ /*-----------------------------------------------------------------*/\r
+\r
+ for_each(ip, ips) if(ip->mark > 0)\r
+ {\r
if(idxs)\r
{\r
char *buf;\r
\r
/* -------------------------------------------------------- mark download */\r
\r
- sprintf(str,"-A %s -d %s/32 -o %s -j %s%d",chain_postrouting,ip->addr,lan,mark_iptables,ip->mark);\r
+ sprintf(str, "-A %s -d %s/32 -o %s -j %s%d",\r
+ chain_postrouting, ip->addr, lan, mark_iptables, ip->mark);\r
/*sprintf(str,"-A %s -d %s/32 -o %s -j MARK --set-mark %d",chain_postrouting,ip->addr,lan,ip->mark);*/\r
/* -m limit --limit 1/s */ \r
save_line(str);\r
\r
if(qos_proxy)\r
{\r
- sprintf(str,"-A %s -s %s -p tcp --sport %d -d %s/32 -o %s -j %s%d",chain_postrouting,proxy_ip,proxy_port,ip->addr,lan,mark_iptables,ip->mark);\r
+ sprintf(str, "-A %s -s %s -p tcp --sport %d -d %s/32 -o %s -j %s%d",\r
+ chain_postrouting, proxy_ip, proxy_port, ip->addr, lan, mark_iptables, ip->mark);\r
/*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);*/\r
save_line(str);\r
}\r
\r
- sprintf(str,"-A %s -d %s/32 -o %s -j ACCEPT",chain_postrouting,ip->addr,lan);\r
+ sprintf(str, "-A %s -d %s/32 -o %s -j ACCEPT",\r
+ chain_postrouting, ip->addr, lan);\r
save_line(str);\r
\r
/* -------------------------------------------------------- mark upload */\r
- sprintf(str,"-A %s -s %s/32 -o %s -j %s%d",chain_forward,ip->addr,wan,mark_iptables,ip->mark);\r
+ sprintf(str, "-A %s -s %s/32 -o %s -j %s%d", \r
+ chain_forward, ip->addr, wan, mark_iptables, ip->mark);\r
/* sprintf(str,"-A %s -s %s/32 -o %s -j MARK --set-mark %d",chain_forward,ip->addr,wan,ip->mark);*/\r
save_line(str);\r
\r
- sprintf(str,"-A %s -s %s/32 -o %s -j ACCEPT",chain_forward,ip->addr,wan);\r
+ sprintf(str, "-A %s -s %s/32 -o %s -j ACCEPT",\r
+ chain_forward, ip->addr, wan);\r
save_line(str);\r
\r
if(ip->min)\r
printf("(down: %dk-%dk ", ip->min, ip->max); \r
#endif\r
\r
- sprintf(str,"%s class add 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);\r
+ sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", \r
+ tc, lan, ip->group, ip->mark,ip->min,ip->max, burst, ip->prio);\r
safe_run(str);\r
\r
if(strcmpi(ip->keyword->leaf_discipline, "none"))\r
{\r
- sprintf(str,"%s qdisc add dev %s parent 1:%d handle %d %s", tc, lan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/\r
+ sprintf(str, "%s qdisc add dev %s parent 1:%d handle %d %s", \r
+ tc, lan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/\r
safe_run(str);\r
- } \r
+ }\r
+\r
if(filter_type == 1)\r
{\r
- sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d", tc, lan, ip->mark, ip->mark);\r
+ sprintf(str, "%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",\r
+ tc, lan, ip->mark, ip->mark);\r
safe_run(str);\r
}\r
\r
\r
if(strcmpi(ip->keyword->leaf_discipline, "none"))\r
{\r
- sprintf(str,"%s qdisc add dev %s parent 1:%d handle %d %s",tc, wan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/\r
+ sprintf(str, "%s qdisc add dev %s parent 1:%d handle %d %s",\r
+ tc, wan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/\r
safe_run(str);\r
} \r
+\r
if(filter_type == 1)\r
{\r
- sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",tc, wan, ip->mark, ip->mark);\r
+ sprintf(str, "%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",\r
+ tc, wan, ip->mark, ip->mark);\r
safe_run(str);\r
}\r
+ \r
+ if(f)\r
+ {\r
+ fprintf(f, "%s %d", ip->addr, ip->mark);\r
+ }\r
}\r
else\r
{\r
}\r
i++;\r
}\r
-\r
+ if(f)\r
+ {\r
+ puts("done.");\r
+ fclose(f);\r
+ }\r
+ \r
if(idxs)\r
{\r
chain_forward = "forw_common";\r
/* -------------------------------- classify or reject free download */\r
{\r
char *final_chain = "DROP"; /* REJECT would be better, but it is impossible in mangle */\r
- if(free_min) final_chain = "ACCEPT";\r
+ if(free_min)\r
+ {\r
+ final_chain = "ACCEPT";\r
+ }\r
if(qos_proxy)\r
{\r
if(free_min)\r
{\r
- sprintf(str,"-A %s -s %s -p tcp --sport %d -o %s -j %s%d",chain_postrouting,proxy_ip,proxy_port,lan,mark_iptables,3);\r
+ sprintf(str,"-A %s -s %s -p tcp --sport %d -o %s -j %s%d",\r
+ chain_postrouting,proxy_ip,proxy_port,lan,mark_iptables,3);\r
save_line(str);\r
}\r
- sprintf(str,"-A %s -s %s -p tcp --sport %d -o %s -j %s",chain_postrouting,proxy_ip,proxy_port,lan,final_chain);\r
+ sprintf(str,"-A %s -s %s -p tcp --sport %d -o %s -j %s",\r
+ chain_postrouting,proxy_ip,proxy_port,lan,final_chain);\r
save_line(str);\r
}\r
if(free_min)\r
{\r
- sprintf(str,"-A %s -o %s -j %s%d",chain_postrouting,lan,mark_iptables,3);\r
+ sprintf(str,"-A %s -o %s -j %s%d", chain_postrouting, lan, mark_iptables, 3);\r
save_line(str);\r
}\r
- sprintf(str,"-A %s -o %s -j %s",chain_postrouting,lan,final_chain);\r
+ sprintf(str,"-A %s -o %s -j %s", chain_postrouting, lan, final_chain);\r
save_line(str);\r
/* ------------------------------- classify or reject free upload */\r
if(free_min)\r
{\r
- sprintf(str,"-A %s -o %s -j %s%d",chain_forward,wan,mark_iptables,3);\r
+ sprintf(str,"-A %s -o %s -j %s%d", chain_forward, wan, mark_iptables, 3);\r
save_line(str);\r
}\r
- sprintf(str,"-A %s -o %s -j %s",chain_forward,wan,final_chain);\r
+ sprintf(str,"-A %s -o %s -j %s", chain_forward, wan, final_chain);\r
save_line(str);\r
}\r
\r
puts("Generating free bandwith classes ...");\r
/*-----------------------------------------------------------------*/\r
sprintf(str, "%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio %d",\r
- tc,lan,parent,free_min,free_max,burst,lowest_priority);\r
+ tc, lan, parent, free_min, free_max,burst, lowest_priority);\r
safe_run(str);\r
sprintf(str, "%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio %d",\r
- tc,wan,parent,free_min,free_max,burst,lowest_priority);\r
+ tc, wan, parent, free_min, free_max, burst, lowest_priority);\r
safe_run(str);\r
/* tc SFQ */\r
if(strcmpi(qos_leaf, "none"))\r
{\r
- sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s",tc,lan,qos_leaf);\r
+ sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s", tc, lan, qos_leaf);\r
safe_run(str);\r
\r
- sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s",tc,wan,qos_leaf);\r
+ sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s", tc, wan, qos_leaf);\r
safe_run(str);\r
} \r
/* tc handle 1 fw flowid */\r
- sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3",tc,lan);\r
+ sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3", tc, lan);\r
safe_run(str);\r
\r
- sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3",tc,wan);\r
+ sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3", tc, wan);\r
safe_run(str);\r
}\r
printf("Total IP count: %d\n", i);\r