import sys
import getopt
import sqlite3
-from julian import datetimeToJulian
+import julian
from DatabaseAccess import DatabaseAccess
+DEBUG = False
+
class DatabaseCommander:
"""
swarms and client_sessions table handling methods
def add_client_session(self, swarm_id, client_name, system_os,
system_os_version, system_ram, system_cpu, public_ip,
- public_port, ds_limit, us_limit, start_time):
+ public_port, ds_limit, us_limit, date, time):
+ jd = float(julian.stringToJulian(date, time))
client_id = self.dba.select_btclient_id_by_name(client_name)
self.dba.insert_client_sessions(swarm_id, client_id,
system_os, system_os_version, system_ram, system_cpu,
- public_ip, public_port, ds_limit, us_limit, start_time)
+ public_ip, public_port, ds_limit, us_limit, jd)
def show_swarms(self, swarm_id = -1):
self.dba.select_swarms(True, swarm_id)
print "\t-l\t\tlist entry/entries from database"
print "\t--delete"
print "\t-d\t\tdelete entry from database"
+ print "\t--read"
+ print "\t-r\t\tread information from standard input"
print "target:"
print "\tswarm\t\tswarm entries"
print "\tsession\t\tclient sessions"
print "\t--id"
print "\t-i\t\tswarm_id or client_session_id"
print "options:"
+ print "\t--sep"
+ print "\t-m\t\tuse separator string instead of standard comma (,)"
print "\t--swarm id"
print "\t-s id\t\tspecify swarm_id"
print "\t--client name"
print "\t-h\t\t\tprint this help screen"
+def read_swarms(dbc, sep = None):
+ if sep == None:
+ sep = ','
+
+ while 1:
+ line = sys.stdin.readline().strip()
+ if line:
+ message_array = line.split(sep)
+
+ swarm_name = message_array[0].strip()
+ filesize = int(message_array[1].strip())
+ purpose = message_array[2].strip()
+ source = message_array[3].strip()
+
+ dbc.add_swarm(swarm_name, filesize, purpose, source)
+ else:
+ break
+
+def read_client_sessions(dbc, sep = None):
+ if sep == None:
+ sep = ','
+
+ while 1:
+ line = sys.stdin.readline().strip()
+ if line:
+ message_array = line.split(sep)
+
+ swarm_id = int(message_array[0].strip())
+ client_name = message_array[1].strip()
+ system_os = message_array[2].strip()
+ system_os_version = message_array[3].strip()
+ system_ram = int(message_array[4].strip())
+ system_cpu = int(message_array[5].strip())
+ public_ip = message_array[6].strip()
+ public_port = int(message_array[7].strip())
+ ds_limit = int(message_array[8].strip())
+ us_limit = int(message_array[9].strip())
+ timestamp = message_array[10].strip().split(' ')
+ date = timestamp[0]
+ time = timestamp[1]
+
+ if DEBUG == True:
+ print "(%d, %s, %s, %s, %d, %d, %s, %d, %d, %d, %s, %s)" %(swarm_id, client_name, system_os, system_os_version, system_ram, system_cpu, public_ip, public_port, ds_limit, us_limit, date, time)
+
+ dbc.add_client_session(swarm_id, client_name, system_os,
+ system_os_version, system_ram, system_cpu, public_ip, public_port, ds_limit, us_limit, date, time)
+ else:
+ break
+
+
def main():
"""
Command line interface for database handling. Allows insertion,
"""
try:
- opts, args = getopt.getopt(sys.argv[1:], "ha:l:d:s:c:i:", ["help",
- "add=", "list=", "delete=", "swarm=", "client=", "id="])
+ opts, args = getopt.getopt(sys.argv[1:], "ha:l:d:r:m:s:c:i:", ["help",
+ "add=", "list=", "delete=", "read=", "sep=", "swarm=", "client=", "id="])
except getopt.GetoptError, err:
print str(err)
usage()
client = None
client_session_list_option = None
database = None
+ sep = None
for o, a in opts:
if o in ("-h", "--help"):
elif o in ("-l", "--list"):
action = "list"
target = a
+ elif o in ("-r", "--read"):
+ action = "read"
+ target = a
elif o in ("-i", "--id"):
try:
id = int(a)
sys.exit(2)
elif o in ("-c", "--client"):
client = a
+ elif o in ("-m", "--sep"):
+ sep = a
else:
assert False, "unhandled option"
print "Error: invalid target ", target, "."
sys.exit(2)
- if id != None and action == "add":
- print "Error: add action doesn't use an id field"
+ if id != None and (action == "add" or action == "read"):
+ print "Error: %s action doesn't use an id field" % action
sys.exit(2)
- if target == "swarm" and id == None and action != "add":
- print "Error: no swarm_id specified for 'swarm' target."
+ if target == "swarm" and id == None and (action == "list" or action == "delete"):
+ print "Error: no swarm_id specified for 'swarm' target and action %s." % action
sys.exit(2)
if target == "session" and (action == "list" or action == "delete"):
dbc.delete_swarm(id)
if action == "list":
dbc.show_swarms(id)
+ if action == "read":
+ read_swarms(dbc, sep)
if target == "session":
if action == "add":
os_version = sys.stdin.readline().strip()
print "system RAM (in MB): ",
ram = sys.stdin.readline().strip()
- print "system CPU (in KHz): ",
+ print "system CPU (in MHz): ",
cpu = sys.stdin.readline().strip()
print "public IP (dotted decimal format): ",
ip = sys.stdin.readline().strip()
us_limit = sys.stdin.readline().strip()
print "start time (YYYY-MM-DD HH:MM:SS): ",
start_time = sys.stdin.readline().strip().split(' ')
- jd = float(datetimeToJulian(start_time[0], start_time[1]));
dbc.add_client_session(swarm_id, client_name, system_os,
- os_version, ram, cpu, ip, port, ds_limit, us_limit, jd)
+ os_version, ram, cpu, ip, port, ds_limit, us_limit, date, time)
if action == "delete":
if client_session_list_option == "id":
dbc.delete_client_session_by_id(id)
dbc.show_client_sessions_by_id(id)
elif client_session_list_option == "swarm":
dbc.show_client_sessions_by_swarm(swarm, client)
+ if action == "read":
+ read_client_sessions(dbc, sep)
if __name__ == "__main__":
sys.exit(main())