def update_results(client_id, data): # Add results to cmd table - used by agent server when receiving new result from client con = db_connect(DATABASE_FILE) db_query(con,"""UPDATE CMD SET RESULT='{}' WHERE CLIENT_ID={} and RESULT='' LIMIT 1;""".format(data, client_id)) if data != "check-in": logger("CMD: {} returned {}".format(get_hostname(con,client_id), b64decode(data).decode('utf-8'))) con.close()
def post_command(client_id, username, command): # Post a command from the admin http server con = db_connect(DATABASE_FILE) admin_id = get_adminid(con,username) hostname = get_hostname(con, client_id) db_query(con, """INSERT INTO CMD (CLIENT_ID, ADMIN_ID, TIME, COMMAND, RESULT) VALUES ({},{},'{}','{}','');""".format(client_id, admin_id, log_time(),command)) logger("CMD: {} executed a command on {} ({})".format(username, hostname, b64decode(command).decode('utf-8'))) con.close()
def admin_logout(con, username): # Set user status as "Inactive" on logout admin_id = get_adminid(con, username) db_query( con, """UPDATE ADMIN SET STATUS='Inactive' WHERE ADMIN_ID={}""".format( admin_id)) logger("Admin: {} Logged out".format(username)) return
def admin_logout(username): # Set user status as "Inactive" on logout con = db_connect(DATABASE_FILE) admin_id = get_adminid(con, username) db_query(con, 'UPDATE ADMIN SET STATUS="Inactive" WHERE ADMIN_ID={}'.format(admin_id)) logger("Admin: {} Logged out".format(username)) con.close() return
def post_command(con, client_id, username, command): # Post a command from the admin http server admin_id = get_adminid(con, username) hostname = get_hostname(con, client_id) db_query( con, """INSERT INTO CMD (CLIENT_ID, ADMIN_ID, TIME, COMMAND, RESULT) VALUES ({},{},'{}','{}','');""" .format(client_id, admin_id, log_time(), command)) logger("CMD: {} executed a command on {} ({})".format( username, hostname, cmd_decode(command).strip()))
def update_results(con, client_id, data): # Add results to cmd table - used by agent server when receiving new result from client try: db_query( con, """UPDATE CMD SET RESULT='{}' WHERE CLIENT_ID={} and RESULT='' LIMIT 1;""" .format(data, client_id)) logger("CMD: {} returned {}".format(get_hostname(con, client_id), cmd_decode(data).strip())) except Exception as e: #print(e) pass
def update_admin(username, password, status): # update user pwd or add new user con = db_connect(DATABASE_FILE) id = get_adminid(con, username) password = md5(password.encode('utf-8')).hexdigest() if id: db_query(con, 'UPDATE ADMIN SET USERNAME="******",PASSWORD="******",LAST_LOGIN="******",STATUS="{}" WHERE ADMIN_ID={};'.format(username,password,log_time(),status,id)) logger("Admin: {} record updated".format(username)) else: db_query(con, 'INSERT INTO ADMIN (USERNAME,PASSWORD,LAST_LOGIN,STATUS) VALUES ("{}","{}","{}","{}");'.format(username,password,log_time(),status)) logger("Admin: {} user added to database".format(username)) con.close() return
def init_db(): try: # check if db file exists: if not path.exists(DATABASE_FILE): logger("Database file created") con = db_connect(DATABASE_FILE) create_tables(con) default_admin(con) con.close() return True except Exception as e: logger("Error in init_db: {}".format(str(e))) return False
def update_client(ip, hostname, os, status, pid, client_type, protocol): try: con = db_connect(DATABASE_FILE) id = get_clientid(con, hostname, pid) if id: db_query(con, 'UPDATE CLIENT SET IP="{}",HOSTNAME="{}",OS="{}",PID="{}",TYPE="{}",PROTOCOL="{}",LAST_CHECKIN="{}",STATUS="{}" WHERE CLIENT_ID={};'.format(ip,hostname,os,pid,client_type,protocol,log_time(),status,id)) if status == 'Inactive': logger("CLIENT: {} record updated ({}, {}, {})".format(hostname, ip, os, status)) else: db_query(con, 'INSERT INTO CLIENT (IP,HOSTNAME,OS,PID,TYPE,PROTOCOL,LAST_CHECKIN,STATUS) VALUES ("{}","{}","{}","{}","{}","{}","{}","{}");'.format(ip,hostname,os,pid,client_type,protocol,log_time(),status)) logger("CLIENT: New Connection from: {} ({}, {}, {})".format(hostname, ip, os, status)) id = get_clientid(con, hostname, pid) con.close() return id except Exception as e: print(e)
def admin_login(username, password): # used for login page to authenticate users result = False con = db_connect(DATABASE_FILE) password = md5(password.encode('utf-8')).hexdigest() try: id = get_adminid(con, username) passwd = db_query(con, 'SELECT PASSWORD FROM ADMIN WHERE ADMIN_ID={};'.format(id)) if str(password) == str(passwd[0][0]): valid_login(con, id) # Set user as active in DB result = True except Exception as e: pass con.close() logger("Admin: {} Login attempt: {}".format(username, str(result))) return result
def update_admin(con, username, password, status): # update user pwd or add new user id = get_adminid(con, username) password = md5(password.encode('utf-8')).hexdigest() if id: db_query( con, """UPDATE ADMIN SET USERNAME='******', PASSWORD='******', LAST_LOGIN='******', STATUS='{}' WHERE ADMIN_ID={};""" .format(username, password, log_time(), status, id)) logger("Admin: {} record updated".format(username)) else: db_query( con, """INSERT INTO ADMIN (USERNAME, PASSWORD, LAST_LOGIN, STATUS) VALUES ('{}','{}','{}','{}');""" .format(username, password, log_time(), status)) logger("Admin: {} user added to database".format(username)) return
def __init__(self, name = 'newcurrent'): cb = Couchbase(util.DB_IP+":"+util.DB_PORT, "Administrator", "couchbase") self.log = logger ("database") try: bucket = cb.create(name, ram_quota_mb=300, replica=1) except: bucket = cb[name] self.bucket = bucket
def update_client(con, ip, hostname, os, status, pid, client_type, protocol): id = get_clientid(con, hostname, pid) if id: db_query( con, """UPDATE CLIENT SET IP='{}', HOSTNAME='{}', OS='{}', PID='{}', TYPE='{}', PROTOCOL='{}', LAST_CHECKIN='{}', STATUS='{}' WHERE CLIENT_ID={};""" .format(ip, hostname, os, pid, client_type, protocol, log_time(), status, id)) if status == 'Inactive': logger("CLIENT: {} record updated ({}, {}, {})".format( hostname, ip, os, status)) else: db_query( con, """INSERT INTO CLIENT (IP, HOSTNAME, OS, PID, TYPE, PROTOCOL, LAST_CHECKIN, STATUS) VALUES ('{}','{}','{}','{}','{}','{}','{}','{}');""" .format(ip, hostname, os, pid, client_type, protocol, log_time(), status)) logger("CLIENT: New Connection from: {} ({}, {}, {})".format( hostname, ip, os, status)) id = get_clientid(con, hostname, pid) return id
from server.couchbase import store from server.logger import logger log = logger("Create View") def generate_view_document ( doc_name, map_function, view_name, reduce_function=None, reduce=False ): # check if there are entries for same document_id log.debug("Generating view function: %s with Map function as: %s " %(doc_name, map_function)) if(not reduce): design = { 'views': { view_name: { 'map':map_function, } } } else: design = { 'views': { view_name: { 'map':map_function, 'reduce': reduce_function } } } document_id = '_design/'+ doc_name store.store_document(document_id, design) def create_view():
def __init__(self, time_period): self.time_period = time_period self.log = logger("Schedule") self.q= Queue() self.node_ip6q = Queue() self.trace_node= Queue()
import json import urllib2 from common import nodelist from server import constants from server.couchbase import util from server.logger import logger log = logger("controller") def update_node_list(): nodes =[] url = 'http://'+ util.CONTROLLER_IP + '/api/nodes/' request = urllib2.Request(url) response= None try: response = urllib2.urlopen(request) except: log.error("Error in http request") response = None if(response): log.info("Updating Node list from Controller") nodes_uri = json.loads(response.read()) log.debug("Nodes list obtained from controller: "+ str(nodes_uri)) for dict_node_uri in nodes_uri: node_uri= dict_node_uri['uri'] node_ip6= get_node_ip_from_node_uri(node_uri)
import datetime import time import calendar import server.constants from server.couchbase import fetchdocument, util from server.logger import logger log = logger("Document Parser") def get_value( document, value_type): ''' Get value from variable name in server.constants if it exists, otherwise Get value directly from attributes. Used for attributes with dynamic names. Example: value of bytesent for a particular interface ''' value = None if(len(document)>0): log.debug("Attempting to get value from document %s for value %s" %(document,value_type)) try: new_value_type = 'server.constants.' + value_type attributes = eval(new_value_type) log.debug("Path for value is %s" %attributes) except: log.debug("Exception in attempting to evaluate value of " + value_type) log.debug("Reset given value to attributes") attributes = value_type attribute_list = attributes.split('.') value = document for attribute in attribute_list:
from server.couchbase import store from server.couchbase import util from server import rename from server.couchbase import documentparser from server.logger import logger import errno import urllib2 import json import socket log = logger("Collect") socket.setdefaulttimeout(40) class Collect: # TODO: IP, PORT,Last_seen_sequence_number needs to be read from class variable. Currently fetching from util for testing def __init__(self, name, port, ip=None,type = None, sequence = 0): self.name = name self.port = port self.sequence= sequence self.ip = ip self.type = type self.value= {} self.most_recent_absolute_timestamp = 0.0 self.most_recent_doc= {} def generate_url(self):
import json import numpy import time from common import nodelist from server import constants from server.couchbase import store, util, fetchdocument from server.couchbase.views import util as view_util from server.couchbase import documentparser from server.logger import logger from collections import namedtuple log = logger("Get View") node_treemap = namedtuple("node_treemap", 'cpu_usage memory_usage data_sent data_received') def get_view_node_id_attribute_json( node_id, value_type, limit): #for given node_id get value_type ordered by time (most recent first) log.debug("Get view by node ID for node: %s" %node_id) db = store.get_bucket() str_startkey = "[\"" + node_id + "\",{}]" str_endkey = "[\"" + node_id + "\"]" view_by_node_id = db.view('_design/node-timestamp/_view/get_node-timestamp', startkey=str_startkey, endkey = str_endkey, descending = True, include_docs= True, limit=limit) all_values = [] for node in view_by_node_id:
def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue self.log = logger.logger("Parallel Collect Network trace")
def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue self.log = logger.logger("Parallel Collect synthesized")
import ast import json from couchbase.client import Couchbase from server.couchbase import store from server.couchbase import util from server.logger import logger log = logger("Fetch Document") def fetch_most_recent_document(node_id): log.debug("Fetching most recent document for node: %s" %node_id) db = store.get_bucket() most_recent_timestamp = fetch_most_recent_timestamp(node_id,db) doc_id = str (node_id) + "-" + str(most_recent_timestamp) try: str_doc= db[doc_id][2] except: return {} document = json.loads(str_doc) log.debug("Document fetched is: " + str(document)) return document # fetch from reference document def fetch_most_recent_timestamp(node_id, db): log.debug("Fetching most recent timestamp for node: %s" %node_id) doc_id = str(node_id) +"-most_recent_timestamp" try:
import os import pickle import shelve import logging from couchbase.client import Couchbase from server import constants from server.couchbase import util from server.logger import logger log = logger("store") class Database(object): def __init__(self, name = 'newcurrent'): cb = Couchbase(util.DB_IP+":"+util.DB_PORT, "Administrator", "couchbase") self.log = logger ("database") try: bucket = cb.create(name, ram_quota_mb=300, replica=1) except: bucket = cb[name] self.bucket = bucket def store_document(self, doc_id, values): self.log.debug("Storing Document with DocID: %s" %doc_id) self.bucket[doc_id] = values def update_document(self, doc_id, values):