gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r14487 - gauger


From: gnunet
Subject: [GNUnet-SVN] r14487 - gauger
Date: Tue, 22 Feb 2011 23:25:22 +0100

Author: bartpolot
Date: 2011-02-22 23:25:22 +0100 (Tue, 22 Feb 2011)
New Revision: 14487

Added:
   gauger/.htaccess
Modified:
   gauger/README
   gauger/explore.php
   gauger/gauger-cli.py
   gauger/gauger.h
   gauger/gauger.py
   gauger/plot.php
Log:
Changed py client arg interface, included explicit unit and [id]
Added a new C macro and change old one to adapt to new client arg interface
Changed py server and php to use common config file and dedicated dir for data
Added .htaccess to avoid exposing passwords


Added: gauger/.htaccess
===================================================================
--- gauger/.htaccess                            (rev 0)
+++ gauger/.htaccess    2011-02-22 22:25:22 UTC (rev 14487)
@@ -0,0 +1,4 @@
+<Files ~ "^.*\.conf">
+Order deny,allow
+Deny from all
+</Files>
\ No newline at end of file

Modified: gauger/README
===================================================================
--- gauger/README       2011-02-22 15:22:00 UTC (rev 14486)
+++ gauger/README       2011-02-22 22:25:22 UTC (rev 14487)
@@ -33,14 +33,22 @@
 =====
 
  * gauger.conf: Server configuration
-  Should contain usernames, passwords and the desired hostname for each
-  machine authorized to log data to the server, one per line.
-  Should be placed in same directory as gauger.py
-  Lines starting with # are ignored. Example:
-  #USER PASS HOSTNAME
-  user 123456 foo
-  user2 god bar
+  Should be placed in same directory as gauger.py. Should contain two 
sections, 
+  one with directories and one for authentication.
+  * Section dir
+   Should contain a path where to put the data logged from clients
+  * Section hosts
+   Should contain "username,password" and the desired hostname for each
+   machine authorized to log data to the server, one per line. Username and
+   password should be separated by a comma and contain no blank spaces.
+  Example:  
+  [dir]
+  data=my_data_directory
 
+  [hosts]
+  user,123456=foo
+  user2,pass=testhost
+
  * gauger-cli.conf: Client configuration
   Should contain one line with the server hostname, user and password.
   Should be placed in the same directory as gauger-cli.py or in the user's
@@ -55,19 +63,27 @@
 
  * Server
   Run directly in data directory with ./gauger.py > gauger.log
+  When manual changes has been done to the raw data files, it's possible
+  to regenerate the .dat plotting source files with: ./gauger.py --refresh
 
  * Client
    * Command line
-    invoke with ./gauger-cli.py COUNTER_NAME COUNTER_VALUE
+    invoke with ./gauger-cli.py -n COUNTER_NAME -d COUNTER_VALUE -u unit [-i 
ID]
+    Run ./gauger-cli.py -h for help
    * C program
      * Include "gauger.h"
-     * Call the macro GAUGER("COUNTER_NAME", COUNTER_VALUE)
+     * Call the macro GAUGER("COUNTER_NAME", COUNTER_VALUE, "COUNTER_UNIT")
+       to obtain the ID for the data from the svn repository
+      or
+     * Call the macro GAUGER_ID("COUNTER_NAME", COUNTER_VALUE, "COUNTER_UNIT", 
"ID")
+       to explicitly specify the ID for the data
      * Example:
 
         #include <gauger.h>
 
         int main(int argc, char *argv[]) {
-            GAUGER("counter1", 100)
+            GAUGER("counter1", 100, "kb/s")
+            GAUGER_ID("temperature", 50, "C", "365")
             return 0;
         }
 

Modified: gauger/explore.php
===================================================================
--- gauger/explore.php  2011-02-22 15:22:00 UTC (rev 14486)
+++ gauger/explore.php  2011-02-22 22:25:22 UTC (rev 14487)
@@ -1,6 +1,8 @@
 <?php
 
-$DATADIR = "data/";
+$CONF = parse_ini_file("gauger.conf");
+$DATADIR = $CONF["data"] ? $CONF["data"] : "data/";
+if ($DATADIR[strlen($DATADIR)-1] != '/') $DATADIR .= '/';
 
 function get_counter_name($s) {
     $s = str_replace('-SLASH-', '/', $s);
@@ -56,6 +58,6 @@
     }
 }
 $d->close();
-echo '<pre>';
-print_r(parse_ini_file("bo.conf"));
-die();
+// echo '<pre>';
+// print_r(parse_ini_file("gauger.conf"));
+// die();

Modified: gauger/gauger-cli.py
===================================================================
--- gauger/gauger-cli.py        2011-02-22 15:22:00 UTC (rev 14486)
+++ gauger/gauger-cli.py        2011-02-22 22:25:22 UTC (rev 14487)
@@ -26,6 +26,7 @@
 import sys
 import os
 import subprocess
+import argparse
 
 def svnversion():
     p = subprocess.Popen("svnversion", shell=True,
@@ -38,11 +39,22 @@
         return ""
     return stdout[0:x-1]
 
+def process_arguments():
+    parser = argparse.ArgumentParser(description='Gauger client.')
+    parser.add_argument('-i', '--id',
+        help="identifier of the data, default: svn revision")
+    parser.add_argument('-n', '--name', required=True,
+        help="name of the counter")
+    parser.add_argument('-d', '--data', required=True,
+        help="value of the data itself")
+    parser.add_argument('-u', '--unit',
+        help="unit in which the data is expressed")
+    args = parser.parse_args()
+    return args
 
-if (len(sys.argv) != 3):
-  print "usage:", sys.argv[0], "counter value"
-  exit(1)
 
+args = process_arguments()
+
 try:
   configfile = open("gauger-cli.conf", "r")
   remotehost, username, password = configfile.readline().strip().split()
@@ -54,13 +66,19 @@
   except:
     print "please put 'remotehost username password' in gauger-cli.conf"
     exit(1)
-  
-revision = svnversion()
 
+if(args.id):
+    revision = args.id
+else:
+    revision = svnversion()
+
 if (not revision):
   print "please make sure to be in a svn respoitory and have svn installed"
+  print "or specify an identifier at runtime with the --id option"
   exit(1)
 
+if(args.unit):
+    args.name = args.name + "_" + args.unit
 
 s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 try:
@@ -69,5 +87,6 @@
   print "ERROR: Couldn't connect to", remotehost
   exit(1)
 l1 = [username, password, revision]
-l1.extend(sys.argv[1:])
+l1.append(args.name);
+l1.append(args.data);
 s1.send(",".join(l1))

Modified: gauger/gauger.h
===================================================================
--- gauger/gauger.h     2011-02-22 15:22:00 UTC (rev 14486)
+++ gauger/gauger.h     2011-02-22 22:25:22 UTC (rev 14487)
@@ -5,6 +5,8 @@
 #include <stdio.h>
 #include <sys/wait.h>
 
-#define GAUGER(counter, value) {char __gauger_s[32];pid_t 
__gauger_p;if(!(__gauger_p=fork())){if(!fork()){sprintf(__gauger_s,"%llu", 
(unsigned long long) (value));execlp("gauger-cli.py","gauger-cli.py",counter, 
__gauger_s,(char*)NULL);perror("gauger");_exit(1);}else{_exit(0);}}else{waitpid(__gauger_p,NULL,0);}}
+#define GAUGER(counter, value, unit) {char* __gauger_v[8];char 
__gauger_s[32];pid_t __gauger_p; 
if(!(__gauger_p=fork())){if(!fork()){sprintf(__gauger_s,"%llu", (unsigned long 
long) (value));__gauger_v[0] = "gauger-cli.py";__gauger_v[1] = 
"-n";__gauger_v[2] = counter;__gauger_v[3] = "-d";__gauger_v[4] = 
__gauger_s;__gauger_v[5] = "-u";__gauger_v[6] = unit;__gauger_v[7] = (char 
*)NULL;execvp("gauger-cli.py",__gauger_v);perror("gauger");_exit(1);}else{_exit(0);}}else{waitpid(__gauger_p,NULL,0);}}
 
+#define GAUGER_ID(counter, value, unit, id) {char* __gauger_v[10];char 
__gauger_s[32];pid_t __gauger_p; 
if(!(__gauger_p=fork())){if(!fork()){sprintf(__gauger_s,"%llu", (unsigned long 
long) (value));__gauger_v[0] = "gauger-cli.py";__gauger_v[1] = 
"-n";__gauger_v[2] = counter;__gauger_v[3] = "-d";__gauger_v[4] = 
__gauger_s;__gauger_v[5] = "-u";__gauger_v[6] = unit;__gauger_v[7] = 
"-i";__gauger_v[8] = id;__gauger_v[9] = (char 
*)NULL;execvp("gauger-cli.py",__gauger_v);perror("gauger");_exit(1);}else{_exit(0);}}else{waitpid(__gauger_p,NULL,0);}}
+
 #endif

Modified: gauger/gauger.py
===================================================================
--- gauger/gauger.py    2011-02-22 15:22:00 UTC (rev 14486)
+++ gauger/gauger.py    2011-02-22 22:25:22 UTC (rev 14487)
@@ -26,7 +26,56 @@
 import os
 import glob
 import math
+import ConfigParser
+import argparse
 
+DATADIR="data/"
+
+def check_dir(path):
+    path = path.strip("/")
+    if (not os.path.exists(path)):
+        os.mkdir(path)
+        print "Created new host directory: ", path
+    if (not os.access(path, os.W_OK) or not os.path.isdir(path)):
+        print "Not a directory or not writable: ", path
+        return False
+    return True
+
+
+def process_arguments():
+    parser = argparse.ArgumentParser(description='Gauger server.')
+    parser.add_argument('--refresh', action='store_true',
+        help="refresh all .dat files and exit")
+    args = parser.parse_args()
+    return args
+
+def process_configuration():
+    global DATADIR
+    parser = ConfigParser.ConfigParser()
+    parser.read(["gauger.conf"])
+    try:
+        DATADIR=parser.get("dir", "data")
+        DATADIR = DATADIR.strip('" ')
+        if(not DATADIR.endswith('/')):
+            DATADIR += '/'
+        if(not check_dir(DATADIR)):
+            print "ERROR: data dir not accessible"
+            exit (1)
+        print "Using datadir:", DATADIR
+    except ConfigParser.NoSectionError:
+        pass
+    except ConfigParser.NoOptionError:
+        pass
+
+
+def get_all_data_files():
+    files = []
+    for d in glob.glob(DATADIR+"*/"):
+        for c in [x for x in glob.glob(d+"*") if x.find(".") == -1 and 
x.find("~") == -1]:
+            files.append(c)
+    return files
+
+
 def getminmax(s):
     try:
         f = open(s+".dat", "r")
@@ -50,49 +99,65 @@
     f.close()
     return l,h
 
+
+def refresh_all_dat():
+    """ Scans the raw data to regenrate the .dat files """
+    for f in get_all_data_files():
+        print f
+        fp = open(f, "r+")
+        add_data_to_file(fp, "", "")
+        fp.close()
+    create_global_range()
+
+
 def create_global_range():
     """ Scans the data to find the lowest and highest x values """
-    for d in glob.glob("*/"):
-        for c in [x for x in glob.glob(d+"*") if x.find(".") == -1 and 
x.find("~") == -1]:
-            try:
-                l,h = getminmax(c)
-            except:
-                continue
-            try:
-                if(l < xrange_min):
-                    xrange_min = l
-            except NameError:
+    for c in get_all_data_files():
+        try:
+            l,h = getminmax(c)
+        except:
+            continue
+        try:
+            if(l < xrange_min):
                 xrange_min = l
-            try:
-                if(h > xrange_max):
-                    xrange_max = h
-            except NameError:
+        except NameError:
+            xrange_min = l
+        try:
+            if(h > xrange_max):
                 xrange_max = h
-            
-    f = open("global_range.dat", "w");
+        except NameError:
+            xrange_max = h
+    f = open(DATADIR+"global_range.dat", "w");
     f.write("%d %d" % (xrange_min, xrange_max));
     f.close();
+    return xrange_min,xrange_max;
 
-def updateplot(host, data):
-  """ Regenerates the plot for a given host / counter combination """
-  # os.system("./plot.sh %s %s" % (host, data))
+def get_global_range():
+    try:
+        f = open(DATADIR+"global_range.dat", "r");
+        xrange_min,xrange_max = f.readline().split(' ');
+        xrange_min = int(xrange_min);
+        xrange_max = int(xrange_max);
+        f.close();
+    except (IOError, ValueError):
+        xrange_min,xrange_max = create_global_range()
+    return xrange_min,xrange_max;
 
-
 def gethostfromlogin(user, password):
   try:
-    file = open("gauger.conf", "r")
+    f = open("gauger.conf", "r")
   except:
-    print "ERROR: please create 'gauger.conf' with 'user password hostname'"
+    print "ERROR: please create 'gauger.conf' with server configuration"
     return ''
-  for line in file.readlines():
-    if (line[0] == '#' or line[0] == ' '):
-      continue
-    u, p, h = line.split()
-    if(u == user and p == password):
-      file.close()
-      return h
-  file.close()
-  return ''
+  parser = ConfigParser.ConfigParser()
+  parser.readfp(f)
+  f.close()
+  try:
+    return parser.get("hosts", user+','+password)
+  except ConfigParser.NoSectionError:
+    return ''
+  except ConfigParser.NoOptionError:
+    return ''
 
 
 def add_data_to_file(datafile, revision, result):
@@ -138,16 +203,17 @@
   processedfile.close()
 
 
+
 """ ******************* MAIN ******************************** """
-try:
-    f = open("global_range.dat", "r");
-    xrange_min,xrange_max = f.readline().split(' ');
-    xrange_min = int(xrange_min);
-    xrange_max = int(xrange_max);
-    f.close();
-except (IOError, ValueError):
-    create_global_range()
+args = process_arguments()
+process_configuration()
 
+xrange_min,xrange_max = get_global_range()
+
+if(args.refresh):
+    refresh_all_dat()
+    exit(0)
+
 s1 = socket(AF_INET, SOCK_STREAM)
 s1.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
 s1.bind(("0.0.0.0", 10111))
@@ -167,11 +233,8 @@
   if (not hostname):
     print "ERROR: username/password not correct"
     continue
-  if (not os.path.exists(hostname)):
-    os.mkdir(hostname)
-    print "Created new host directory: ", hostname
-  if (not os.access(hostname, os.W_OK) or not os.path.isdir(hostname)):
-    print "Not a directory or not writable: ", hostname
+  hostname = DATADIR+hostname
+  if (not check_dir(hostname)):
     continue
 
   dataname = dataname.replace('/', '-SLASH-');
@@ -181,11 +244,10 @@
       datafile = open(hostname + '/' + dataname, "w+")
   add_data_to_file(datafile, revision, result);
   datafile.close()
-  updateplot(hostname, dataname)
   revision = int(revision);
   if(revision > xrange_max or revision < xrange_min):
     xrange_max = max(xrange_max, revision);
     xrange_min = min(xrange_min, revision);
-    f = open("global_range.dat", "w");
+    f = open(DATADIR+"global_range.dat", "w");
     f.write("%d %d" % (xrange_min, xrange_max));
     f.close();

Modified: gauger/plot.php
===================================================================
--- gauger/plot.php     2011-02-22 15:22:00 UTC (rev 14486)
+++ gauger/plot.php     2011-02-22 22:25:22 UTC (rev 14487)
@@ -1,5 +1,37 @@
 <?php
 
+/** Function build_gnuplot_file
+ * @param $cmd: header, terminal size and plot range
+ * @param $rangecmd: xtics range description
+ * @param $plotcmd: data description
+ * @return: gnuplot file with inline data
+ */
+function build_gnuplot_file($cmd, $rangecmd, $plotcmd) {
+    // TODO: get all files used in plot
+    $files = explode(",", $plotcmd);
+    foreach ($files as $i => $file) {
+        //$files[$i] = preg_replace('/[^"]*"\([^"]*\).*/', "\1", $file);
+        echo "$file -> " . preg_replace('/[^"]+"([^"]+).*/', '\1#', $file) . 
"\n";
+    }
+    print_r($files);
+    die();
+    foreach($files as $file) {
+        //include data inline
+        //substitute dataname with index
+    }
+    return "";
+}
+
+/** Function build_gnuplot_command
+ * @param $cmd: header, terminal size and plot range
+ * @param $rangecmd: xtics range description
+ * @param $plotcmd: data description
+ * @return: gnuplot file with inline data
+ */
+function build_gnuplot_command($cmd, $rangecmd, $plotcmd) {
+    return "echo '$cmd $rangecmd $plotcmd' | gnuplot";
+}
+
 /** Function plot
  * @param h: host name 
  * @param g: graph name
@@ -96,10 +128,13 @@
         die();
     }
 
-    $cmd = "echo '$cmd $rangecmd $plotcmd' | gnuplot";
-    if(get_param('raw_cmd') == 'true')
+    if(get_param('raw_cmd') == 'true') {
+        $cmd = build_gnuplot_file($cmd, $rangecmd, $plotcmd);
         die($cmd);
-    $out = shell_exec($cmd);
+    } else {
+        $cmd = build_gnuplot_command($cmd, $rangecmd, $plotcmd);
+        $out = shell_exec($cmd);
+    }
 
     return $out;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]