ještě ERP
[svn/Prometheus-QoS/.git] / prometheus.c
index 9853a5a..5420588 100644 (file)
@@ -7,7 +7,7 @@
 /*  Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf  */\r
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */\r
 \r
-/* Modified by: xChaos, 20110427\r
+/* Modified by: xChaos, 20110428\r
                  ludva, 20080415\r
  \r
    Prometheus QoS is free software; you can redistribute it and/or\r
@@ -85,6 +85,7 @@ void help(void)
 -s            start shaping! (keep data transfer statistics - but apply shaping)\n\\r
 */\r
 }\r
+\r
 /* === Configuraration file values defaults - stored in global variables ==== */\r
 \r
 int        filter_type = 1; /*1 mark, 2 classify*/\r
@@ -135,7 +136,7 @@ const int idxtable_treshold2 = 12;      /* this is no longer configurable */
 const int idxtable_bitmask1  = 3;        /* this is no longer configurable */\r
 const int idxtable_bitmask2  = 3;        /* this is no longer configurable */\r
 \r
-/* ==== This is C<<1 stuff - learn C<<1 first! http://cll1.arachne.cz ==== */\r
+/* ==== This is C<<1 stuff - learn C<<1 first! https://dev.arachne.cz/svn/cll1h ==== */\r
 \r
 struct IP\r
 {\r
@@ -211,7 +212,7 @@ void TheIP(void)
  ip->addr        = "";\r
  ip->sharing     = NULL;\r
  ip->prio        = highest_priority+1;\r
- ip->lmsid       = \\r
+ ip->lmsid       = -1;\r
  ip->fixedprio   = \\r
  ip->mark        = \\r
  ip->min         = \\r
@@ -303,10 +304,14 @@ char *very_ugly_ipv4_code(char *inip,int bitmask,int format_as_chainname)
 }\r
 \r
 char *hash_id(char *ip,int bitmask)\r
-{ return very_ugly_ipv4_code(ip,bitmask,1); }\r
+{ \r
+ return very_ugly_ipv4_code(ip,bitmask,1);\r
+}\r
 \r
 char *subnet_id(char *ip,int bitmask)\r
-{ return very_ugly_ipv4_code(ip,bitmask,0); }\r
+{\r
+ return very_ugly_ipv4_code(ip,bitmask,0);\r
+}\r
 \r
 /* ================= Let's parse configuration file here =================== */\r
 \r
@@ -532,8 +537,14 @@ void get_traffic_statistics(void)
   \r
     if(accept && traffic>0 && ipaddr)\r
     {\r
-     if(proxyflag)printf("(proxy) ");\r
-     else if(!downloadflag) printf("(upload) ");\r
+     if(proxyflag)\r
+     {\r
+      printf("(proxy) ");\r
+     }\r
+     else if(!downloadflag)\r
+     {\r
+      printf("(upload) ");\r
+     }\r
      printf("IP %s: %Lu M (%ld pkts)\n", ipaddr, traffic, pkts);\r
 \r
      if_exists(ip,ips,eq(ip->addr,ipaddr)); \r
@@ -552,9 +563,13 @@ void get_traffic_statistics(void)
      if(downloadflag)\r
      {\r
       if(proxyflag)\r
+      {\r
        ip->proxy=traffic;\r
+      }\r
       else\r
+      {\r
        ip->traffic+=traffic;\r
+      }\r
       ip->direct=ip->traffic-ip->upload-ip->proxy;\r
       ip->pktsdown=pkts;\r
      }\r
@@ -584,8 +599,18 @@ void get_traffic_statistics(void)
 \r
 void safe_run(char *cmd)\r
 {\r
- if(dry_run) printf("\n=>%s\n",cmd); else system(cmd);\r
- if(log_file) fprintf(log_file,"%s\n",cmd);\r
+ if(dry_run)\r
+ {\r
+  printf("\n=>%s\n",cmd);\r
+ }\r
+ else\r
+ {\r
+  system(cmd);\r
+ }\r
+ if(log_file)\r
+ {\r
+  fprintf(log_file,"%s\n",cmd);\r
+ }\r
 }\r
 \r
 void save_line(char *line)\r
@@ -595,7 +620,7 @@ void save_line(char *line)
 \r
 void run_restore(void)\r
 {\r
- char *restor, *str;\r
+ char *restor;\r
  string(restor,STRLEN);\r
 \r
  /*-----------------------------------------------------------------*/\r
@@ -606,11 +631,11 @@ void run_restore(void)
  fclose(iptables_file);\r
  if(dry_run) \r
  {\r
-    parse(iptablesfile)\r
-    {\r
-        str=_;\r
-        printf("%s\n", str);\r
-    }done;\r
+  parse(iptablesfile)\r
+  {\r
+   printf("%s\n",_);\r
+  }\r
+  done;\r
  }\r
 \r
  sprintf(restor,"%s <%s",iptablesrestore, iptablesfile);\r
@@ -630,22 +655,30 @@ void parse_ip(char *str)
  {\r
   lmsid=++ptr;\r
   while(*ptr && *ptr!='}')\r
+  {\r
    ptr++;\r
+  }\r
   *ptr=0;\r
  }\r
  \r
  ptr=str;\r
  while(*ptr && *ptr!=' ' && *ptr!=9)\r
+ {\r
   ptr++;\r
+ }\r
  \r
  *ptr=0;\r
  ipaddr=str;\r
  ptr++;\r
  while(*ptr && (*ptr==' ' || *ptr==9))\r
+ {\r
   ptr++;\r
+ }\r
  ipname=ptr; \r
  while(*ptr && *ptr!=' ' && *ptr!=9)\r
+ {\r
   ptr++;\r
+ }\r
  *ptr=0;\r
 \r
  if_exists(ip,ips,eq(ip->addr,ipaddr));\r
@@ -684,6 +717,7 @@ struct IpLog
  long traffic;\r
  long guaranted;\r
  int i;\r
+ int lmsid;\r
  long l;\r
  list(IpLog);\r
 } *iplog,*iplogs;\r
@@ -748,7 +782,7 @@ void parse_ip_log(int argc, char **argv)
     accept_month=0;\r
     traffic_month=0;\r
     guaranted=0;\r
-    lmsid=0;\r
+    lmsid=-1;\r
     parse(filename)\r
     {\r
      y_ok=m_ok=0;  \r
@@ -790,6 +824,7 @@ void parse_ip_log(int argc, char **argv)
      iplog->name = name;\r
      iplog->guaranted = guaranted;\r
      iplog->traffic = traffic_month;\r
+     iplog->lmsid = lmsid;\r
      insert(iplog,iplogs,desc_order_by,traffic);\r
      printf(" %ld MB\n",iplog->traffic);\r
     }\r
@@ -804,25 +839,35 @@ void parse_ip_log(int argc, char **argv)
  f=fopen(str,"w");\r
  if(f)\r
  {\r
-  fprintf(f,"<table border><tr><th colspan=\"2\">%s %s</th><th colspan=\"2\">Data transfers</th><th align=\"right\">Min.speed</th></tr>\n ",month,year);\r
+  fprintf(f,"<table border><tr><th colspan=\"2\">%s %s</th><th align=\"right\">lms</th><th colspan=\"2\">Data transfers</th><th align=\"right\">Min.speed</th></tr>\n ",month,year);\r
 \r
   for_each(iplog,iplogs)\r
   {\r
    if(iplog->traffic)\r
    {\r
-    fprintf(f,"<tr><td align=\"right\">%d</td><th align=\"left\">%s</td><td align=\"right\">%ld M</td><th align=\"right\">%ld G</th><td align=\"right\">%ld kbps</th></tr>\n",\r
-              i++, iplog->name, iplog->traffic, iplog->traffic>>10, iplog->guaranted);\r
+    fprintf(f,"<tr><td align=\"right\">%d</td><th align=\"left\">%s</td><td align=\"right\">", i++, iplog->name); \r
+    if(iplog->lmsid > 0)\r
+    {\r
+     /*base URL will be configurable soon ... */\r
+     fprintf(f,"<a href=\"https://hermes.spoje.net/?m=customerinfo&amp;id=%d\">%04d</a>\n", iplog->lmsid, iplog->lmsid);\r
+    }\r
+    else if(iplog->lmsid == 0)\r
+    {\r
+     fputs("-------",f);\r
+    }    \r
+    fprintf(f,"<td align=\"right\">%ld M</td><th align=\"right\">%ld G</th><td align=\"right\">%ld kbps</th></tr>\n",\r
+              iplog->traffic, iplog->traffic>>10, iplog->guaranted);\r
     total+=iplog->traffic>>10;\r
     iplog->i=i;\r
     iplog->l=total;\r
    }\r
   }\r
-  fprintf(f,"<tr><th colspan=\"3\" align=\"left\">Total:</th><th align=\"right\">%ld GB</th><th align=\"right\">%Ld kbps</th></tr>\n", total, line);\r
+  fprintf(f,"<tr><th colspan=\"4\" align=\"left\">Total:</th><th align=\"right\">%ld GB</th><th align=\"right\">%Ld kbps</th></tr>\n", total, line);\r
   fputs("</table>\n", f);\r
 \r
   if(i>10)\r
   {\r
-   fputs("<a name=\"erp\"></a><p><table border><tr><th colspan=\"5\">Enterprise Research and Planning (ERP)</th></tr>\n",f);\r
+   fputs("<a name=\"erp\"></a><p><table border><tr><th colspan=\"5\">Enterprise Resource Planning (ERP)</th></tr>\n",f);\r
    fputs("<tr><td>Analytic category</td>\n",f);\r
    fputs("<td colspan=\"2\" align=\"center\">Active Classes</td><td colspan=\"2\" align=\"center\">Data transfers</td></tr>\n",f);\r
 \r
@@ -902,13 +947,14 @@ program
  int just_preview=FALSE;                /* preview - generate just stats */\r
  int just_logs=FALSE;                   /* just parse logs */\r
  int run=FALSE;\r
+ int total=0;\r
  \r
  char *chain_forward, *chain_postrouting;\r
  char *althosts=NULL;\r
   \r
  printf("\n\\r
 Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\\r
-Version %s - Copyright (C)2005-2008 Michael Polak (xChaos)\n\\r
+Version %s - Copyright (C)2005-2011 Michael Polak (xChaos)\n\\r
 iptables-restore & burst tunning & classify modification by Ludva\n\\r
 Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);\r
 \r
@@ -1088,6 +1134,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
    sharedip->traffic+=ip->traffic;\r
    ip->traffic=0;\r
    ip->mark=sharedip->mark; \r
+   ip->lmsid=sharedip->lmsid;\r
    break;\r
   }\r
   if(!sharedip)\r
@@ -1456,7 +1503,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
 \r
  if(f)\r
  {\r
-  int total=0;\r
+\r
   int count=1;\r
   i=0;\r
 \r
@@ -1505,7 +1552,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  i=1;\r
  if(f)\r
  {\r
-  unsigned long long total=0, total_direct=0, total_proxy=0, total_upload=0, tmp_sum=0;\r
+  unsigned long long total_traffic=0, total_direct=0, total_proxy=0, total_upload=0, tmp_sum=0;\r
   int active_classes=0;\r
   int colspan;\r
   FILE *iplog;\r
@@ -1563,11 +1610,15 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
    if(found_lmsid)\r
    {\r
     fputs("<td align=\"right\">",f);\r
-    if(ip->lmsid)\r
+    if(ip->lmsid > 0)\r
     {\r
      /*base URL will be configurable soon ... */\r
      fprintf(f,"<a href=\"https://hermes.spoje.net/?m=customerinfo&amp;id=%d\">%04d</a>\n", ip->lmsid, ip->lmsid);\r
     }\r
+    else if(ip->lmsid == 0)\r
+    {\r
+     fputs("-------",f);\r
+    }\r
     fputs("</td>\n",f);    \r
    }\r
    fprintf(f,"<td align=\"right\">%Lu M</td>\n", ip->credit);\r
@@ -1581,7 +1632,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
    fprintf(f,"<td align=\"right\">%Lu M</td>\n", ip->upload);\r
    fprintf(f,"<td align=\"right\">%d k</td><td align=\"right\">%d k</td><td align=\"right\">%s%d k%s</td><td>%s%d%s</td></tr>\n",\r
              ip->min,ip->desired,f1,ip->max,f2,f1,ip->prio,f2);\r
-   total+=ip->traffic;\r
+   total_traffic+=ip->traffic;\r
    total_direct+=ip->direct;\r
    total_proxy+=ip->proxy;\r
    total_upload+=ip->upload;\r
@@ -1608,11 +1659,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
      fclose(iplog);\r
     }\r
    }\r
-\r
   }\r
   fprintf(f,"<tr><th colspan=\"%d\" align=\"left\">SUMMARY:</td>",colspan-7);\r
   fprintf(f,"<th align=\"right\">%Lu M</th>\\r
-  <th align=\"right\">%Lu M</th>\n", total, total_direct);\r
+  <th align=\"right\">%Lu M</th>\n", total_traffic, total_direct);\r
   if(qos_proxy)\r
   {\r
    fprintf(f,"<th align=\"right\">%Lu M</th>\n", total_proxy);\r
@@ -1622,62 +1672,84 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
 \r
   if(active_classes>10)\r
   {\r
-   fputs("<a name=\"erp\"></a><p><table border><tr><th colspan=\"5\">Enterprise Research and Planning (ERP)</th></tr>\n",f);\r
+   int top20_count=0,top20_perc1=0;\r
+   long long top20_perc2=0;\r
+   unsigned long long top20_sum=0l;\r
+  \r
+   fputs("<a name=\"erp\"></a><p><table border><tr><th colspan=\"5\">Enterprise Resource Planning (ERP)</th></tr>\n",f);\r
    fputs("<tr><td>Analytic category</td>\n",f);\r
    fputs("<td colspan=\"2\" align=\"center\">Active Classes</td><td colspan=\"2\" align=\"center\">Data transfers</td></tr>\n",f);\r
 \r
-   if_exists(sum,sums,sum->l>=total/4)\r
+   if_exists(sum,sums,sum->l>=total_traffic/4)\r
    {\r
     fprintf(f,"<tr><td>Top 25%% of traffic</td>\n");\r
-    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);\r
+    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
    }\r
    \r
    if_exists(sum,sums,sum->i==10)\r
    {\r
     fprintf(f,"<tr><td>Top 10 downloaders</td>\n");\r
-    fprintf(f,"<th align=\"right\">10</th><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);\r
+    fprintf(f,"<th align=\"right\">10</th><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
    }\r
 \r
-   if_exists(sum,sums,sum->l>=total/2)\r
+   if_exists(sum,sums,sum->l>=total_traffic/2)\r
    {\r
     fprintf(f,"<tr><td>Top 50%% of traffic</td>\n");\r
-    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><th align=\"right\">%Ld %%</th></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);\r
+    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><th align=\"right\">%Ld %%</th></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
    }\r
 \r
-   if_exists(sum,sums,sum->l>=4*total/5)\r
+   if_exists(sum,sums,sum->l>=4*total_traffic/5)\r
    {\r
     fprintf(f,"<tr><td>Top 80%% of traffic</td>\n");\r
-    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><th align=\"right\">%Ld %%</th></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);\r
+    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><th align=\"right\">%Ld %%</th></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
    }\r
 \r
-   if_exists (sum,sums,sum->i>=(active_classes+1)/5)\r
+   if_exists(sum,sums,sum->i>=(active_classes+1)/5)\r
    {\r
     fprintf(f,"<tr><td>Top 20%% downloaders</td>\n");\r
-    fprintf(f,"<td align=\"right\">%d</td><th align=\"right\">%d %%</th><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);\r
+    top20_count=sum->i;\r
+    top20_perc1=(100*sum->i+50)/active_classes;\r
+    top20_sum=sum->l;\r
+    top20_perc2=(100*sum->l+50)/total_traffic;\r
+    fprintf(f,"<td align=\"right\">%d</td><th align=\"right\">%d %%</th><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",top20_count,top20_perc1,top20_sum,top20_perc2);\r
    }\r
 \r
    if_exists(sum,sums,sum->i>=(active_classes+1)/4)\r
    {\r
     fprintf(f,"<tr><td>Top 25%% downloaders</td>\n");\r
-    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);\r
+    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
    }\r
 \r
    if_exists(sum,sums,sum->i>=(active_classes+1)/2)\r
    {\r
     fprintf(f,"<tr><td>Top 50%% downloaders</td>\n");\r
-    fprintf(f,"<td align=\"right\">%d</td><th align=\"right\">%d %%</th><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);\r
+    fprintf(f,"<td align=\"right\">%d</td><th align=\"right\">%d %%</th><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
    }\r
 \r
    if_exists(sum,sums,sum->i>=4*(active_classes+1)/5)\r
    {\r
     fprintf(f,"<tr><td>Top 80%% downloaders</td>\n");\r
-    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);\r
+    fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
    }\r
 \r
-   fprintf(f,"<tr><td>All users, all traffic</td>\n");\r
-   fprintf(f,"<th align=\"right\">%d</th><th align=\"right\">100 %%</th><th align=\"right\">%Lu M</th><th align=\"right\">100 %%</th></tr>\n",active_classes,total);\r
+   fprintf(f,"<tr><td><a href=\"%sERP.log\">All users, all traffic</a></td>\n", log_url);\r
+   fprintf(f,"<th align=\"right\">%d</th><th align=\"right\">100 %%</th><th align=\"right\">%Lu M</th><th align=\"right\">100 %%</th></tr>\n",active_classes,total_traffic);\r
    fputs("</table>\n", f);\r
+\r
+   /* write basic ERP data to log directory */\r
+   if(!just_preview)\r
+   {\r
+    sprintf(str,"%s/ERP.log",log_dir);\r
+    iplog=fopen(str,"a");\r
+    if(iplog)\r
+    {\r
+     fprintf(iplog,"%ld\t%d\t%d %%\t%Lu M\t%Ld %%\t%d\t%Lu M\t%d\t%s",\r
+                    time(NULL), top20_count, top20_perc1, top20_sum, top20_perc2, active_classes, total_traffic, total, d); /* d = date*/\r
+     fclose(iplog);\r
+    }\r
+   }\r
   }\r
+\r
   fprintf(f, stats_html_signature, version);\r
   fclose(f);\r
  }\r
This page took 0.230289 seconds and 4 git commands to generate.