Commit b443828c authored by Marco Mariani's avatar Marco Mariani

netdrive: use json not eval, fixed XML report, --verbose

parent 487a225a
...@@ -27,10 +27,10 @@ ...@@ -27,10 +27,10 @@
# #
############################################################################## ##############################################################################
import argparse import argparse
import json
import os.path import os.path
import slapos.slap.slap import slapos.slap.slap
import sqlite3 import sqlite3
import sys
import xmlrpclib import xmlrpclib
from datetime import datetime, date from datetime import datetime, date
...@@ -67,6 +67,7 @@ def parseArgumentTuple(): ...@@ -67,6 +67,7 @@ def parseArgumentTuple():
help="If True, send report per day at mid-night. " help="If True, send report per day at mid-night. "
"Otherwise send report instantly.", "Otherwise send report instantly.",
default=False) default=False)
parser.add_argument('--verbose', action='store_true')
option = parser.parse_args() option = parser.parse_args()
# Build option_dict # Build option_dict
...@@ -129,6 +130,9 @@ class NetDriveUsageReporter(object): ...@@ -129,6 +130,9 @@ class NetDriveUsageReporter(object):
current_timestamp = datetime.now() current_timestamp = datetime.now()
d = current_timestamp - last_timestamp d = current_timestamp - last_timestamp
if d.seconds < self.report_interval: if d.seconds < self.report_interval:
if self.verbose:
print 'sleeping', interval
print '%s < %s' % (d.seconds, self.report_interval)
sleep(interval) sleep(interval)
continue continue
self.insertUsageReport(monitor, last_timestamp.isoformat(), d.seconds) self.insertUsageReport(monitor, last_timestamp.isoformat(), d.seconds)
...@@ -141,7 +145,7 @@ class NetDriveUsageReporter(object): ...@@ -141,7 +145,7 @@ class NetDriveUsageReporter(object):
def insertUsageReport(self, monitor, start, duration): def insertUsageReport(self, monitor, start, duration):
q = self._db.execute q = self._db.execute
for r in eval(monitor.netdrive_usage()): for r in json.loads(monitor.netdrive_usage()):
q( "INSERT INTO net_drive_usage " q( "INSERT INTO net_drive_usage "
"(config_id, domain_user, drive_letter, remote_folder, " "(config_id, domain_user, drive_letter, remote_folder, "
" start, duration, usage_bytes )" " start, duration, usage_bytes )"
...@@ -167,6 +171,8 @@ class NetDriveUsageReporter(object): ...@@ -167,6 +171,8 @@ class NetDriveUsageReporter(object):
# (Optional) Move all the reported data to histroy table # (Optional) Move all the reported data to histroy table
today = date.today().isoformat() today = date.today().isoformat()
if (not self.batch) or self._report_date < today: if (not self.batch) or self._report_date < today:
if self.verbose:
print 'Creating report'
self._postData(self.generateDailyReport(self._config_id, self._postData(self.generateDailyReport(self._config_id,
self.computer_id, self.computer_id,
self._report_date)) self._report_date))
...@@ -221,7 +227,7 @@ class NetDriveUsageReporter(object): ...@@ -221,7 +227,7 @@ class NetDriveUsageReporter(object):
movement = etree.Element('movement') movement = etree.Element('movement')
element = etree.Element("resource") element = etree.Element("resource")
element.text = r[0] element.text = r[1]
movement.append(element) movement.append(element)
element = etree.Element("title") element = etree.Element("title")
...@@ -229,15 +235,15 @@ class NetDriveUsageReporter(object): ...@@ -229,15 +235,15 @@ class NetDriveUsageReporter(object):
movement.append(element) movement.append(element)
element = etree.Element("reference") element = etree.Element("reference")
element.text = etree.Element("domain_user"), element.text = r[0]
movement.append(element) movement.append(element)
element = etree.Element("reference") # element = etree.Element("reference")
element.text = report_date # element.text = report_date
movement.append(element) # movement.append(element)
element = etree.Element("quantity") element = etree.Element("quantity")
element.text = str(r[1] * r[2]) element.text = str(r[2] * r[3])
movement.append(element) movement.append(element)
element = etree.Element("price") element = etree.Element("price")
......
#!/usr/bin/python #!/usr/bin/python
import json
import logging import logging
import os import os
import sys import sys
...@@ -33,7 +34,7 @@ class SlapNodeMonitor(object): ...@@ -33,7 +34,7 @@ class SlapNodeMonitor(object):
for k in self.drivelist: for k in self.drivelist:
r = netuse.usageReport(k[0]) r = netuse.usageReport(k[0])
result.append((k[3], k[0], k[1], r[0], r[1])) result.append((k[3], k[0], k[1], r[0], r[1]))
return repr(result) return json.dumps(result)
def run(self): def run(self):
# Create server # Create server
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment