ahem
[svn/Prometheus-QoS/.git] / prometheus.c
index 8e74a443ca609dde4a661a566c2672af51bcca35..5b90528c7bad21025eb4c6bd12c08ae29f9ff623 100644 (file)
@@ -7,8 +7,8 @@
 /*  Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf  */\r
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */\r
 \r
-/* Modified: xChaos, 20080407\r
-             ludva, 20071227\r
+/* Modified: xChaos, 20080422\r
+             ludva, 20080415\r
 \r
    Prometheus QoS is free software; you can redistribute it and/or\r
    modify it under the terms of the GNU General Public License as \r
 \r
 #include "cll1-0.6.h"\r
 \r
-const char *version="0.7.9.1"; /*0.7.9 will be last development, 0.8.0 first stable */\r
+const char *version = "0.7.9-c"; \r
 \r
-/* ======= All path names are defined hear (for RPM patch) =======  */\r
+/* Version numbers: 0.7.9 will be last development ("beta"), 0.8.0 first stable */\r
+/* Debian(RPM) package versions/patchlevels: 0.7.9-2, 0.8.0-1, 0.8.0-2, etc. */\r
+/* C source code development versions ("beta"): 0.7.9-a, 0.8.1-b, etc. */\r
+/* C source code release versions: 0.8.0, 0.8.2, 0.8.4, etc. */\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
+const char *stats_html_signature = "<small>Statistics generated by Prometheus QoS version %s<br>GPL+Copyright(C)2005-2008 Michael Polak, <a href=\"http://www.arachne.cz/\">Arachne Labs</a></small>\n";\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
-char *ls              = "/bin/ls"; /* this is not user configurable :-) */\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
+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
+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
@@ -435,7 +442,7 @@ void get_config(char *config_filename)
  /* are supplied values meaningful ?*/\r
  if(line<=0 || up<=0)\r
  {\r
-  puts("Illegal value of wan bandwidth: 0 kbps.");\r
+  puts("Illegal value of LAN or WAN bandwidth: 0 kbps.");\r
   reject_config_and_exit(config_filename);\r
  }\r
 }\r
@@ -547,7 +554,6 @@ void get_traffic_statistics(void)
     }  \r
   }\r
 \r
-\r
  free(cmd);\r
 }\r
  \r
@@ -632,17 +638,17 @@ struct IpLog
 {\r
  char *name;\r
  long traffic;\r
+ long guaranted;\r
  list(IpLog);\r
 } *iplog,*iplogs;\r
 \r
 void parse_ip_log(int argc, char **argv) \r
 {\r
- char *month,*year,*str,*name,*ptr,*ptr2;\r
- long traffic,traffic_month,total=0;\r
- int col,col2,y_ok,m_ok,accept_month,i=1,any_month=0;\r
- char mstr[4],ystr[5];\r
- FILE *f;\r
\r
+ char *month, *year, *str, *name, *ptr, *ptr2;\r
+ long traffic, traffic_month, total=0, guaranted;\r
+ int col, col2, y_ok, m_ok, accept_month, i=1, any_month=0;\r
+ char mstr[4], ystr[5];\r
+ FILE *f; \r
  string(str,STRLEN);\r
 \r
  if(argv[1][1]=='l') /* -l */\r
@@ -683,6 +689,7 @@ void parse_ip_log(int argc, char **argv)
 \r
  sprintf(str,"%s %s/*.log",ls,log_dir);\r
  shell(str);\r
+\r
  input(str,STRLEN)\r
  {\r
   ptr=strrchr(str,'\n');\r
@@ -690,31 +697,46 @@ void parse_ip_log(int argc, char **argv)
   printf("Parsing %s ...",str);\r
   accept_month=0;\r
   traffic_month=0;\r
+  guaranted = 0;\r
   parse(str)\r
   {\r
    y_ok=m_ok=0;  \r
    valid_columns(ptr,_,'\t',col) switch(col)\r
    {\r
-    case 2: name=ptr;break;\r
-    case 3: traffic=atol(ptr);break;\r
-    case 7: valid_columns(ptr2,ptr,' ',col2) switch(col2)\r
-            {\r
-             case 2: if(any_month || eq(ptr2,month)) m_ok=1; break;\r
-             case 5: if(eq(ptr2,year)) y_ok=1; break;\r
-            }\r
+    case 2: name = ptr;break;\r
+    case 3: traffic = atol(ptr);break;\r
+    /* column number   - was 7, now 10...*/\r
+    case 7:\r
+    case 8:\r
+    case 9:\r
+    case 10: if (isalpha(*ptr)) /* character, not numeric string = date, just one*/\r
+             {\r
+               valid_columns(ptr2,ptr,' ',col2) switch(col2)\r
+               {\r
+                case 2: if(any_month || eq(ptr2,month)) m_ok = 1; break;\r
+                case 5: if(eq(ptr2,year)) y_ok = 1; break;\r
+               }\r
+             }\r
+             else\r
+             {\r
+               if(col == 7) guaranted = atol(ptr);\r
+             }\r
    }\r
+   \r
    if(y_ok && m_ok) \r
    {\r
-    traffic_month+=traffic;\r
-    accept_month=1;\r
+    traffic_month += traffic;\r
+    accept_month = 1;\r
    }\r
   }\r
   done;\r
+\r
   if(accept_month)\r
   {\r
    create(iplog,IpLog);\r
-   iplog->name=name;\r
-   iplog->traffic=traffic_month;\r
+   iplog->name = name;\r
+   iplog->guaranted = guaranted;\r
+   iplog->traffic = traffic_month;\r
    insert(iplog,iplogs,desc_order_by,traffic);\r
    printf(" %ld MB\n",iplog->traffic);\r
   }\r
@@ -726,15 +748,17 @@ void parse_ip_log(int argc, char **argv)
  f=fopen(str,"w");\r
  if(f)\r
  {\r
-  fprintf(f,"<table border><tr><th colspan=\"4\">Data transfers - %s %s</th></tr>\n ",month,year);\r
+  fprintf(f,"<table border><tr><th colspan=\"4\">Data transfers - %s %s</th><th align=\"right\">Min.speed</th></tr>\n ",month,year);\r
   every(iplog,iplogs)\r
    if(iplog->traffic)\r
    {\r
-    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);\r
+    fprintf(f,"<tr><td align=\"right\">%d</td><th>%s</td><td align=\"right\">%ld MB</td><th align=\"right\">%ld GB</th><th align=\"right\">%ld kbps</th></tr>\n",\r
+              i++, iplog->name, iplog->traffic, iplog->traffic>>10, iplog->guaranted);\r
     total+=iplog->traffic>>10;\r
    }\r
-  fprintf(f,"<tr><th colspan=\"3\" align=\"left\">Total:</th><th align=\"right\">%ld GB</th></tr>\n",total);\r
-  fputs("</table>\n",f);\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
+  fputs("</table>\n", f);\r
+  fprintf(f, stats_html_signature, version);\r
   fclose(f);\r
   puts(" done.");\r
  }\r
@@ -755,7 +779,9 @@ program
  int parent=1;\r
  int just_flush=0;\r
  int nodelay=0;\r
- int just_preview=0;                   /* preview - generate just stats */\r
+ int just_preview=0;                /* preview - generate just stats */\r
+ int just_logs=0;                   /* just parse logs */\r
\r
  char *chain_forward, *chain_postrouting;\r
  char *althosts=NULL;\r
   \r
@@ -776,9 +802,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") { just_preview=666; }\r
-  argument("-m") { just_preview=666; }\r
-  argument("-y") { just_preview=666; }\r
+  argument("-l") { just_logs=1; }\r
+  argument("-m") { just_logs=1; }\r
+  argument("-y") { just_logs=1; }\r
   argument("-?") { help(); exit(0); }\r
   argument("--help") { help(); exit(0); }\r
   argument("-v") { exit(0); } \r
@@ -795,10 +821,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  /*-----------------------------------------------------------------*/\r
  get_config(config);\r
  \r
- if(just_preview == 666)\r
+ if(just_logs)\r
  {\r
-    parse_ip_log(argc,argv); \r
-    exit(0); \r
+    parse_ip_log(argc,argv);\r
+    exit(0);\r
  }\r
 \r
  if(althosts) hosts=althosts;\r
@@ -1373,8 +1399,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
     iplog=fopen(str,"a");\r
     if(iplog)\r
     {\r
-     fprintf(iplog,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%s",\r
-                    time(NULL),ip->name,ip->traffic,ip->direct,ip->proxy,ip->upload,ip->min,ip->min,d); /* d = date*/\r
+     fprintf(iplog,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%s",\r
+                    time(NULL),ip->name,ip->traffic,ip->direct,ip->proxy,ip->upload,ip->min,ip->max,ip->desired,d); /* d = date*/\r
      fclose(iplog);\r
     }\r
    }\r
@@ -1444,9 +1470,9 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
 \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
-   fputs("</table>\n",f);\r
+   fputs("</table>\n", f);\r
   }\r
-  fprintf(f,"<small>Statistics generated by Prometheus QoS version %s<br>GPL+Copyright(C)2005-2008 Michael Polak, <a href=\"http://www.arachne.cz/\">Arachne Labs</a></small>\n",version);\r
+  fprintf(f, stats_html_signature, version);\r
   fclose(f);\r
  }\r
 \r
This page took 0.174307 seconds and 4 git commands to generate.