Illegal value of minimum bandwidth 0 kbps, using ....
[svn/Prometheus-QoS/.git] / prometheus.c
index c7887a0e5b099f1326f2229f802a48222b879fb1..37625373453ec61d986f4368b89d02fea7e6e049 100644 (file)
@@ -7,7 +7,7 @@
 /* == Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf == */\r
 /* =============================================================== */\r
 \r
-/* Modified: xChaos, 20080201\r
+/* Modified: xChaos, 20080205\r
              ludva, 20071227\r
 \r
    Prometheus QoS is free software; you can redistribute it and/or\r
 \r
 #include "cll1-0.6.h"\r
 \r
-const char *version="0.7.8"; /*0.7.9 will be last development, 0.8.0 first stable */\r
+const char *version="0.7.8.2"; /*0.7.9 will be last development, 0.8.0 first stable */\r
+\r
+/* ======= All path names are defined hear (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
+char *ls              = "/bin/ls"; /* this is not user configurable :-) */\r
+\r
+char *config          = "/etc/prometheus/prometheus.conf"; /* main configuration file */\r
+char *hosts           = "/etc/prometheus/hosts"; /* per-IP bandwidth definition 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 *preview         = "/var/www/preview.html"; /* hall of fame preview */\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
+char *html_log_dir    = "/var/www/logs/html/";\r
 \r
 /* ======= Help screen is hopefuly self-documenting part of code :-) ======= */\r
 \r
@@ -50,7 +70,13 @@ void help(void)
 -p            just generate preview of data transfer statistics and exit\n\\r
 -n            no delay (overrides qos-free-delay keyword)\n\\r
 -d            dry run (preview tc and iptables commands on stdout)\n\\r
-");\r
+-l Mmm YYYY   generate HTML summary of traffic logs (Mmm=Jan-Dec or Year, YYYY=year)\n\\r
+-m            generate HTML summary of traffic logs for yesterday's month\n\\r
+-y            generate HTML summary of traffic logs for yesterday's year\n");\r
+/* not yet implemented:\r
+-s            start shaping! (keep data transfer statistics - but apply shaping)\n\\r
+-r            just reload configuration (...and keep data transfer statistics)\n\\r
+*/\r
 }\r
 \r
 /* === Configuraration file values defaults - stored in global variables ==== */\r
@@ -59,23 +85,10 @@ int filter_type=1;                      /*1 mark, 2 classify*/
 char *mark="MARK";\r
 char *mark_iptables="MARK --set-mark ";\r
 int dry_run=0;                         /* preview - use puts() instead of system() */\r
-char *config="/etc/prometheus/prometheus.conf";   /* main configuration file */\r
-char *hosts="/etc/prometheus/hosts";              /* line bandwidth definition file */\r
-char *tc="/sbin/tc";                   /* requires tc with HTB support */\r
-char *iptables="/sbin/iptables";       /* requires iptables utility */\r
-char *iptablessave="/sbin/iptables-save"; /* new */\r
-char *iptablesrestore="/sbin/iptables-restore";  /* new */\r
-char *iptablesfile="/var/spool/prometheus.iptables";  /* new; file for iptables-restore*/\r
 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]";\r
 FILE *iptables_file=NULL;\r
-char *html="/var/www/traffic.html";    /* hall of fame filename */\r
-char *preview="/var/www/preview.html"; /* hall of fame preview */\r
-char *cmdlog="/var/log/prometheus";    /* command log filename */\r
-char *credit="/var/run/prometheus.credit";  /* credit log file */\r
 int enable_credit=1;                   /* enable credit file */\r
 int use_credit=0;                      /* use credit file (if enabled)*/\r
-char *log_dir="/var/www/logs/";        /* log directory pathname */\r
-char *log_url="logs/";                 /* log directory URL prefix */\r
 char *title="Hall of Fame - Greatest Suckers"; /* hall of fame title */\r
 int hall_of_fame=1;                           /* enable hall of fame */\r
 char *lan="eth0";                /* LAN interface */\r
@@ -116,12 +129,6 @@ 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
-\r
-/* not yet implemented:\r
--s            start shaping! (keep data transfer statistics - but apply shaping)\n\\r
--r            just reload configuration (...and keep data transfer statistics)\n\\r
-*/\r
-\r
 /* ==== This is C<<1 stuff - learn C<<1 first! http://cll1.arachne.cz ==== */\r
 \r
 struct IP\r
@@ -205,7 +212,7 @@ void TheIP(void)
  push(ip,ips);\r
 }\r
 \r
-/* ====== Iptables indexes are used to reduce complexity to log8(N) ===== */\r
+/* ====== iptables indexes are used to reduce complexity to log8(N) ===== */\r
 \r
 char *very_ugly_ipv4_code(char *inip,int bitmask,int format_as_chainname)\r
 {\r
@@ -350,9 +357,7 @@ void get_config(char *config_filename)
     \r
     if(keyword->data_limit || keyword->fixed_limit || \r
        keyword->data_prio || keyword->fixed_prio)\r
-        use_credit=1;\r
-        \r
-\r
+        use_credit=1;        \r
    }\r
   }\r
 \r
@@ -376,6 +381,7 @@ void get_config(char *config_filename)
   option("credit-filename",credit);\r
   ioption("credit-enable",enable_credit);\r
   option("log-traffic-directory",log_dir);\r
+  option("log-traffic-html-directory",html_log_dir);\r
   option("log-traffic-url-path",log_url);\r
   option("qos-free-zone",qos_free_zone);\r
   ioption("qos-free-delay",qos_free_delay);\r
@@ -392,8 +398,7 @@ void get_config(char *config_filename)
   ioption("htb-r2q",htb_r2q);\r
   ioption("magic-include-upload",include_upload);\r
   ioption("magic-priorities",magic_priorities);\r
-  ioption("magic-treshold",magic_treshold);\r
-  \r
+  ioption("magic-treshold",magic_treshold);  \r
   option("filter-type", cnf);\r
   \r
 /* not yet implemented:\r
@@ -570,17 +575,17 @@ void run_restore(void)
  \r
  save_line("COMMIT");\r
  fclose(iptables_file);\r
- if(dry_run) {\r
+ if(dry_run) \r
+ {\r
     parse(iptablesfile)\r
     {\r
         str=_;\r
         printf("%s\n", str);\r
     }done;\r
- }else{\r
-    //sprintf(restor,"cat %s",iptablesfile); else \r
-    sprintf(restor,"%s <%s",iptablesrestore, iptablesfile);\r
-    system(restor);\r
- };\r
+ }\r
+\r
+ sprintf(restor,"%s <%s",iptablesrestore, iptablesfile);\r
+ safe_run(restor);\r
  \r
  free(restor);\r
 }\r
@@ -623,6 +628,119 @@ char *parse_datafile_line(char *str)
   return NULL;\r
 }\r
 \r
+struct IpLog
+{
+ char *name;
+ long traffic;
+ list(IpLog);
+} *iplog,*iplogs;
+
+void parse_ip_log(int argc, char **argv) 
+{
+ char *month,*year,*str,*name,*ptr,*ptr2;
+ long traffic,traffic_month,total=0;
+ int col,col2,y_ok,m_ok,accept_month,i=1,any_month=0;\r
+ char mstr[4],ystr[5];
+ FILE *f;
+ string(str,STRLEN);
+
+ if(argv[1][1]=='l') /* -l */\r
+ {\r
+   if(argc<4)
+   {
+    puts("Missing parameter(s)!\nUsage: prometheus -l Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)");
+    exit(-1);
+   }
+   else
+   {
+    month=argv[2];
+    if(eq(month,"Year")) any_month=1;
+    year=argv[3];
+   }\r
+ }\r
+ else\r
+ { \r
+   time_t t = time(NULL) - 3600*24 ; /* yesterday's timestamp*/\r
+   struct tm *timep = localtime(&t);                                           \r
\r
+   if(argv[1][1]=='m') /* -m yestarday - month */\r
+   {\r
+    strftime(mstr, 4, "%b", timep);\r
+    month=mstr;\r
+    strftime(ystr, 5, "%Y", timep);\r
+    year=ystr; \r
+   }\r
+   else /* -y yesterday - year */\r
+   {\r
+    month="Year";\r
+    any_month=1;\r
+    strftime(ystr, 5, "%Y", timep);\r
+    year=ystr;\r
+   }\r
+ }\r
+ printf("Analysing traffic for %s %s ...\n",month,year);\r
+
+ sprintf(str,"%s %s/*.log",ls,log_dir);
+ shell(str);
+ input(str,STRLEN)
+ {
+  ptr=strrchr(str,'\n');
+  if(ptr) *ptr='\0';
+  printf("Parsing %s ...",str);
+  accept_month=0;
+  traffic_month=0;
+  parse(str)
+  {
+   y_ok=m_ok=0;  
+   valid_columns(ptr,_,'\t',col) switch(col)
+   {
+    case 2: name=ptr;break;
+    case 3: traffic=atol(ptr);break;
+    case 7: valid_columns(ptr2,ptr,' ',col2) switch(col2)
+            {
+             case 2: if(any_month || eq(ptr2,month)) m_ok=1; break;
+             case 5: if(eq(ptr2,year)) y_ok=1; break;
+            }
+   }
+   if(y_ok && m_ok) 
+   {
+    traffic_month+=traffic;
+    accept_month=1;
+   }
+  }
+  done;
+  if(accept_month)
+  {
+   create(iplog,IpLog);
+   iplog->name=name;
+   iplog->traffic=traffic_month;
+   insert(iplog,iplogs,desc_order_by,traffic);
+   printf(" %ld MB\n",iplog->traffic);
+  }
+  else
+   puts(" no records.");
+ }
+ sprintf(str,"%s/%s-%s.html",html_log_dir,year,month);
+ printf("Writing %s ...",str);
+ f=fopen(str,"w");
+ if(f)
+ {
+  fprintf(f,"<table border><tr><th colspan=\"4\">Data transfers - %s %s</th></tr>\n ",month,year);
+  every(iplog,iplogs)
+   if(iplog->traffic)
+   {
+    fprintf(f,"<tr><td align=\"right\">%d</td><th>%s</td><td align=\"right\">%ld MB</td><th align=\"right\">%ld GB</th></tr>\n",i++,iplog->name,iplog->traffic,iplog->traffic>>10);
+    total+=iplog->traffic>>10;
+   }
+  fprintf(f,"<tr><th colspan=\"3\" align=\"left\">Total:</th><th align=\"right\">%ld GB</th></tr>\n",total);
+  fputs("</table>\n",f);
+  fclose(f);
+  puts(" done.");
+ }
+}
+\r
+\r
 /*-----------------------------------------------------------------*/\r
 /* Are you looking for int main (int argc, char **argv) ? :-))     */\r
 /*-----------------------------------------------------------------*/\r
@@ -658,6 +776,9 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   argument("-9") { just_flush=9; }\r
   argument("-p") { just_preview=1; }\r
   argument("-n") { nodelay=1; }\r
+  argument("-l") { parse_ip_log(argc,argv); exit(0); }\r
+  argument("-m") { parse_ip_log(argc,argv); exit(0); }\r
+  argument("-y") { parse_ip_log(argc,argv); exit(0); }\r
   argument("-?") { help(); exit(0); }\r
   argument("--help") { help(); exit(0); }\r
   argument("-v") { exit(0); } \r
@@ -731,8 +852,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
     ip->min=atoi(substring);\r
     if(ip->min<=0)\r
     {\r
-     puts("Illegal value of minimum bandwidth: 0 kbps.");\r
-     reject_config_and_exit(hosts);\r
+     printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kbps\n",s\r
+     ip->min=free_min;\r
     }\r
     if(ip->max<=ip->min)\r
     {\r
@@ -1242,7 +1363,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
    \r
    if(!just_preview)\r
    {\r
-    sprintf(str,"%s%s.log",log_dir,ip->name);\r
+    sprintf(str,"%s/%s.log",log_dir,ip->name);\r
     iplog=fopen(str,"a");\r
     if(iplog)\r
     {\r
This page took 0.159887 seconds and 4 git commands to generate.