almost new release (some aggregation related stuff and more)
authorxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Mon, 30 Mar 2015 20:06:12 +0000 (20:06 +0000)
committerxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Mon, 30 Mar 2015 20:06:12 +0000 (20:06 +0000)
git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@249 251d49ef-1d17-4917-a970-b30cf55b089b

Makefile
ipstruct.h
prometheus.c
utils.c [new file with mode: 0644]

index 5f3a608531644f255fbc4d231dcaba0fb9defd01..a926c5721468db134e120b7472712627f05ee91a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ prefix=/usr
 mandir=$(prefix)/share/man
 sbindir=$(prefix)/sbin
 sysconfdir=/etc
-OBJECTS=parsehosts.o networks.o parseiptables.o parselogs.o ipv4subnets.o ipv6subnets.o json.o htmlandlogs.o help.o prometheus.o
+OBJECTS=parsehosts.o networks.o parseiptables.o parselogs.o ipv4subnets.o ipv6subnets.o json.o htmlandlogs.o help.o utils.o prometheus.o
 HEADERS=cll1-0.6.2.h ipstruct.h
 
 main: prometheus
index 6f0e5694dc2aab8b15174b4c7ad8b59992964a45..d4383b32fc4192e98ce6d18f01dd2976e966b680 100644 (file)
@@ -53,6 +53,8 @@ struct Keyword
 // int divide_max;         /* relative malus: new_ceil=rate+(old_ceil-rate)/divide_max */\r
 // int htb_ceil_bonus_divide; /* relative bonus: new_ceil=old_ceil+old_ceil/htb_ceil_bonus_divide */\r
  int default_prio;         /* default HTB priority for this keyword */\r
+ int download_aggregation;  /* apply agregation with -s start_shaping switch */\r
+ int upload_aggregation;    /* apply agregation with -s start_shaping switch */\r
  char *html_color;\r
  int ip_count;\r
  char *leaf_discipline;\r
index 75162f3b21be08fcacfa1bd132579db3f50b600b..313941946142eb2cba45a134ae20ae66c507bff6 100644 (file)
@@ -160,6 +160,11 @@ void write_htmlandlogs(char *html, char *d, int total, int just_preview);
 void analyse_topology(char *traceroute);\r
 /* implemented in networks.c */\r
 \r
+char *parse_datafile_line(char *str);\r
+/* implemented in utils.c */\r
+\r
+time_t get_mtime(const char *path);\r
+/* implemented in utils.c */\r
 \r
 const char *tr_odd_even(void)\r
 {\r
@@ -234,10 +239,12 @@ void get_config(char *config_filename)
    keyword->reserve_min = 8;            /* bonus for nominal HTB rate bandwidth (in kbps) */\r
    keyword->reserve_max = 0;            /* malus for nominal HTB ceil (in kbps) */\r
    keyword->default_prio = highest_priority+1;\r
+   keyword->download_aggregation = keyword->upload_aggregation = 0; /* disable by default */\r
    keyword->html_color = "000000";\r
    keyword->ip_count = 0;\r
    keyword->leaf_discipline = "";\r
    keyword->allowed_avgmtu = 0;\r
+   keyword->download_aggregation = keyword->upload_aggregation = 1;\r
 \r
    push(keyword, keywords);\r
    if(!defaultkeyword)\r
@@ -267,6 +274,8 @@ void get_config(char *config_filename)
       ioption("htb-default-prio", keyword->default_prio);\r
       ioption("htb-rate-bonus", keyword->reserve_min);\r
       ioption("htb-ceil-malus", keyword->reserve_max);\r
+      ioption("download-aggregation", keyword->download_aggregation);\r
+      ioption("upload-aggregation", keyword->upload_aggregation);\r
       option("leaf-discipline", keyword->leaf_discipline);\r
       option("html-color", keyword->html_color);\r
       ioption("allowed-avgmtu" ,keyword->allowed_avgmtu);\r
@@ -450,30 +459,10 @@ void run_iptables_restore(void)
  free(restor);\r
 }\r
 \r
-char *parse_datafile_line(char *str)\r
-{\r
- char *ptr = strchr(str,' ');\r
- if(!ptr)\r
- {\r
-  ptr = strchr(str,'\t');\r
- }\r
-\r
- if(ptr)\r
- {\r
-  *ptr = 0;\r
-  ptr++;\r
-  while(*ptr == ' ' || *ptr == '\t')\r
-  {\r
-   ptr++;\r
-  }\r
-  return ptr;\r
- } \r
- else \r
- {\r
-  return NULL;\r
- }\r
-}\r
+/**/\r
 \r
+char *parse_datafile_line(char *str);\r
+time_t get_mtime(const char *path);\r
 \r
 /*-----------------------------------------------------------------*/\r
 /* Are you looking for int main(int argc, char **argv) ? :-))      */\r
@@ -1049,7 +1038,9 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  {\r
   if(start_shaping || stop_shaping || reduce_ceil)\r
   {\r
-   printf("Reading %s and applying Fair Use Policy rules ... \n", classmap);\r
+   time_t how_much_seconds = time(NULL) - get_mtime(classmap); /* sice start of daily aggregation session */\r
+   printf("Reading %s (%ld seconds old) and applying Fair Use Policy and Aggregation rules... \n", classmap, how_much_seconds);\r
+     \r
    parse(classmap)\r
    {\r
     ptr=strchr(_,' ');\r
@@ -1059,28 +1050,85 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
      ptr++;\r
      if_exists(ip,ips,eq(ip->addr,_))\r
      {\r
+      int unshape_this_ip = stop_shaping;\r
+      long avg_mbps_down = ip->direct * 8 / how_much_seconds;\r
+      long avg_mbps_up = ip->upload * 8 / how_much_seconds;\r
+      int min_mbps = ip->min>>10;\r
+      int agreg = 1, print_stats = 1;\r
+      \r
+      if(min_mbps < 1)\r
+      {\r
+       min_mbps = 1;\r
+      }\r
+      \r
+      if(ip->keyword->download_aggregation)\r
+      {\r
+       if(min_mbps <= avg_mbps_down)\r
+       {\r
+        unshape_this_ip = 0;\r
+        agreg = (avg_mbps_down+1)/min_mbps;\r
+        ip->max /= agreg;\r
+        printf("Download aggregation 1:%d for %s (min: %lu Mbps avg: %ld Mbps)\n", agreg, ip->name, min_mbps, avg_mbps_down);\r
+       }\r
+       else\r
+       {\r
+        unshape_this_ip = 1;\r
+       }\r
+      }\r
+      else if(ip->keyword->upload_aggregation)\r
+      {\r
+       if(min_mbps <= avg_mbps_up)\r
+       {\r
+        unshape_this_ip = 0;\r
+        agreg = (avg_mbps_up+1)/min_mbps;\r
+        ip->max /= agreg;\r
+        printf("Upload aggregation 1:%d for %s: (min: %lu Mbps avg: %ld Mbps)\n", agreg, ip->name, min_mbps, avg_mbps_up);\r
+       }\r
+       else\r
+       {\r
+        unshape_this_ip = 1;\r
+       }\r
+      }\r
       ip->mark=atoi(ptr);\r
-      if(ip->max < ip->desired || stop_shaping || reduce_ceil) /* apply or disable FUP limit immediately.... */\r
+      if(ip->max < ip->desired || unshape_this_ip || reduce_ceil) /* apply or disable FUP limit immediately.... */\r
       {\r
-       if(stop_shaping)\r
+       if(unshape_this_ip)\r
        {\r
         ip->max = ip->desired;\r
-        printf("Removing limit for %-22s %-16s %04d ", ip->name, ip->addr, ip->mark);               \r
+        if(stop_shaping) /* all limits removed, but not printed with -s (start_shaping) switch */\r
+        {\r
+         printf("Removing limit for %s (%s) ", ip->name, ip->addr);\r
+        }\r
+        else\r
+        {\r
+         print_stats = 0;\r
+        }\r
        }\r
        else\r
        {\r
-        printf("Applying limit for %-22s %-16s %04d ", ip->name, ip->addr, ip->mark);\r
+        printf("Applying limit for %s (%s) ", ip->name, ip->addr);\r
         if(reduce_ceil)\r
         {\r
          ip->max = ip->min + (ip->desired-ip->min)/reduce_ceil;\r
         }\r
+        else if(ip->max < ip->min)\r
+        {\r
+         ip->max = ip->min;\r
+        }        \r
+       }\r
+       if(print_stats)\r
+       {\r
+        printf("(down: %dk-%dk wants %dk, ", ip->min, ip->max, ip->desired);\r
        }\r
-       printf("(down: %dk-%dk ", ip->min, ip->max); \r
        sprintf(str, "%s class change 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
-       printf("up: %dk-%dk)\n", (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), \r
-                                (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed));\r
+       if(print_stats)\r
+       {\r
+        printf("up: %dk-%dk wants %dk)\n", (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), \r
+                                           (int)((ip->desired/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),\r
+                                           (int)((ip->desired/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed));\r
+       }\r
        sprintf(str,"%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",\r
                     tc, wan, ip->group, ip->mark,\r
                     (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),\r
diff --git a/utils.c b/utils.c
new file mode 100644 (file)
index 0000000..09ae9f9
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,41 @@
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+/* http://stackoverflow.com/questions/4021479/getting-file-modification-time-on-unix-using-utime-in-c */
+time_t get_mtime(const char *path)
+{
+ struct stat statbuf;
+ if (stat(path, &statbuf) == -1) 
+ {
+  perror(path);
+  exit(1);
+ }
+ return statbuf.st_mtime;
+}
+
+/* just text line parsing */
+char *parse_datafile_line(char *str)
+{
+ char *ptr = strchr(str,' ');
+ if(!ptr)
+ {
+  ptr = strchr(str,'\t');
+ }
+
+ if(ptr)
+ {
+  *ptr = 0;
+  ptr++;
+  while(*ptr == ' ' || *ptr == '\t')
+  {
+   ptr++;
+  }
+  return ptr;
+ }
+ else
+ {
+  return NULL;
+ }
+}
This page took 0.210591 seconds and 4 git commands to generate.