From 78b737bec1310b1819a74febb5d6b997a1cbad97 Mon Sep 17 00:00:00 2001
From: Alain Takoudjou <talino@tiolive.com>
Date: Mon, 2 Feb 2015 15:38:07 +0000
Subject: [PATCH] monitor-fix: get collector database path from configuration
 file

---
 stack/monitor/buildout.cfg                    |  6 ++--
 stack/monitor/collect.py                      | 16 ++++------
 stack/monitor/monitor.cfg.in                  |  6 ++--
 .../webfile-directory/ressources.cgi.in       | 29 +++++++++++++++----
 4 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/stack/monitor/buildout.cfg b/stack/monitor/buildout.cfg
index d05ad958b..28e729717 100644
--- a/stack/monitor/buildout.cfg
+++ b/stack/monitor/buildout.cfg
@@ -46,7 +46,7 @@ recipe = slapos.recipe.template
 url = ${:_profile_base_location_}/monitor.cfg.in
 output = ${buildout:directory}/monitor.cfg
 filename = monitor.cfg
-md5sum = 05ed0063a8de43d3711b23605cdab4d7
+md5sum = 87d7c22ed77a0e77b06b6c7869024b74
 mode = 0644
 
 [monitor-bin]
@@ -104,7 +104,7 @@ mode = 0644
 recipe = hexagonit.recipe.download
 url = ${:_profile_base_location_}/webfile-directory/${:filename}
 download-only = true
-md5sum = 739a6e470ef174b9d0b523aa349860cb
+md5sum = 6aed44f5048b58cc01408caf265a3c4d
 filename = ressources.cgi.in
 mode = 0644
 
@@ -173,7 +173,7 @@ mode = 0644
 recipe = hexagonit.recipe.download
 url = ${:_profile_base_location_}/${:filename}
 download-only = true
-md5sum = b0b22c8c2fd8dfbf6ee7271aaa09a7d2
+md5sum = 08c31783601214109e803ab69ea83aa1
 filename = collect.py
 mode = 0644
 
diff --git a/stack/monitor/collect.py b/stack/monitor/collect.py
index 2abdd5223..631c5ca17 100644
--- a/stack/monitor/collect.py
+++ b/stack/monitor/collect.py
@@ -34,13 +34,9 @@ from datetime import datetime, timedelta
 
 class Database:
 
-  database_name = "collector.db"
-  table_list = ["user", "computer", "system", "disk", \
-                 "temperature", "heating"]
-
-  def __init__(self, directory = None):
-    assert self.database_name is not None
-    self.uri = os.path.join(directory, self.database_name)
+  def __init__(self, db_path = None):
+    assert os.path.exists(db_path) and os.path.isfile(db_path)
+    self.uri = db_path
     self.connection = None
     self.cursor = None
 
@@ -109,7 +105,7 @@ class Database:
     self.close()
 
     if len(sample_amount) and len(memory_sum):
-      return round(memory_sum[0][0]/sample_amount[0][0], 2)
+      return round(memory_sum[0][0]/(sample_amount[0][0]*1024*1024.0), 2)
 
   def getPartitionConsumption(self, partition_id, where=""):
     self.connect()
@@ -132,7 +128,7 @@ group by pid order by cpu_result desc""" % (
       comsumption_list.append([result[6], round((result[1]/count), 2),
                     round((result[2]/count), 2),
                     round(result[3], 2), round((result[4]/count), 2),
-                    round((result[5]/count), 2)])
+                    round((result[5]/(count*1024*1024.0)), 2)])
     self.close()
     return comsumption_list
   
@@ -158,6 +154,6 @@ date='%s' and partition='%s' and (time between '%s' and '%s') %s""" % (
             'cpu_time': round(result[2][0], 2),
             'cpu_num_threads': round(result[3][0], 2),
             'memory_percent': round(result[4][0], 2),
-            'memory_rss': round(result[5][0], 2)}
+            'memory_rss': round(result[5][0]/(1024*1024.0), 2)}
             
     return None
diff --git a/stack/monitor/monitor.cfg.in b/stack/monitor/monitor.cfg.in
index 0073ee636..587ece2ec 100644
--- a/stack/monitor/monitor.cfg.in
+++ b/stack/monitor/monitor.cfg.in
@@ -200,13 +200,12 @@ recipe = slapos.recipe.template:jinja2
 template = ${ressources-cgi:location}/${ressources-cgi:filename}
 rendered = $${monitor-directory:monitoring-cgi}/$${:filename}
 filename = ressources.cgi
-mode = 0744
-# XXX - We need to find a proper way to set db_path here, maybe by using zero-parameters ??
+mode = $${deploy-settings-cgi:mode}
 context =
   key monitor_bin monitor-parameters:executable
   raw python_executable ${buildout:directory}/bin/${extra-eggs:interpreter}
   key root_folder buildout:directory
-  raw db_path /srv/slapgrid/var/data-log/
+  raw config_cfg $${buildout:directory}/$${public:filename}
 
 [make-rss]
 recipe = slapos.recipe.template:jinja2
@@ -345,6 +344,7 @@ name = example.com
 recipe = slapos.cookbook:zero-knowledge.write
 filename = knowledge0.cfg
 status-history-length = 5
+collect-db-path = /srv/slapgrid/var/data-log/collector.db
 
 [zero-parameters]
 recipe = slapos.cookbook:zero-knowledge.read
diff --git a/stack/monitor/webfile-directory/ressources.cgi.in b/stack/monitor/webfile-directory/ressources.cgi.in
index da6068460..94a3e6b4d 100644
--- a/stack/monitor/webfile-directory/ressources.cgi.in
+++ b/stack/monitor/webfile-directory/ressources.cgi.in
@@ -7,16 +7,32 @@ import os
 import pwd
 from time import strftime
 from datetime import datetime
+import ConfigParser
 import collect
 
 cgitb.enable(display=0, logdir="/tmp/cgi.log")
 form = cgi.FieldStorage()
-db_path = "{{ db_path }}"
+config_file = "{{ config_cfg }}"
 action = form.getvalue("action", "")
 home = "{{ root_folder }}".strip()
+db_path = ""
+if not os.path.exists(config_file):
+  print """<html><head></head>
+  <body><h2>Could not find database path in configuration file.
+  the file %s might not exist.</h2></body></html>""" % config_file
+  exit(0)
+parser = ConfigParser.ConfigParser()
+parser.read(config_file)
+try:
+  db_path = parser.get('public', 'collect-db-path')
+except ConfigParser.NoOptionError, e:
+  print """<html><head></head>
+  <body><h2>Could not find database path in configuration file.
+  <br/>%s</h2></body></html>""" % str(e)
+  exit(0)
 
 if action:
-  db = collect.Database(directory=db_path)
+  db = collect.Database(db_path=db_path)
   stat_info = os.stat(home)
   partition_user = pwd.getpwuid(stat_info.st_uid)[0]
   result_dict = {}
@@ -44,6 +60,7 @@ else:
 .tg .tg-s6z2 td{text-align:center}
 .tg .tg-zapm{background-color:#f9f9f9;}
 .tg .tg-4eph{background-color:#f9f9f9}
+.tg tr:hover td{background-color: #FAFAFA}
 .head{
 	background-color:#0078e7;
 	border:0px solid #ffffff;
@@ -98,7 +115,7 @@ $(document).ready(function () {
       table1 += '<th class="tg-s6z2">Threads</th><th class="tg-s6z2">Memory Usage</th>';
       table1 += '<th class="tg-s6z2">Memory %</th></tr>';
       
-      table3 = '<tr><th class="tg-s6z2">CPU Load Average</th>';
+      table3 = '<tr><th class="tg-s6z2">CPU Load Average %</th>';
       table3 += '<th class="tg-s6z2">Memory Consumption Average</th></tr>';
       
       line2 = "<tr class='tg-4eph tg-s6z2'>"
@@ -106,7 +123,7 @@ $(document).ready(function () {
       line2 += "<td>" + result['status']['cpu_percent'] + "</td>";
       line2 += "<td>" + result['status']['cpu_time'] + "</td>";
       line2 += "<td>" + result['status']['cpu_num_threads'] + "</td>";
-      line2 += "<td>" + result['status']['memory_rss'] + "</td>";
+      line2 += "<td>" + result['status']['memory_rss'] + " Mb</td>";
       line2 += "<td>" + result['status']['memory_percent'] + "</td>";
       line2 += "</tr>";
       for (var i=0; i<consump.length; i++) {
@@ -117,14 +134,14 @@ $(document).ready(function () {
         line += "<td>" + consump[i][1] + "</td>";
         line += "<td>" + consump[i][2] + "</td>";
         line += "<td>" + consump[i][3] + "</td>";
-        line += "<td>" + consump[i][5] + "</td>";
+        line += "<td>" + consump[i][5] + " Mb</td>";
         line += "<td>" + consump[i][4] + "</td>";
         line += "</tr>";
       }
       
       table3 += "<tr class='tg-4eph tg-s6z2'>"
       table3 += "<td>" + result['cpu-load'] + "</td>";
-      table3 += "<td>" + result['memory'] + "</td></tr></table>";
+      table3 += "<td>" + result['memory'] + " Mb</td></tr></table>";
       
       $("#box3").html(table3);
       $("#box2").html(table2 + line2 + '</table>');
-- 
2.30.9