Exemplo n.º 1
0
 def computeMu(self, k):
     functionName = self.computeMu.__name__
     helpers.entrylog(log, functionName)
     self.mu3[k+1] = max(0, self.mu3[k] + self.Kmu3*(self.P_Gmin-self.P_G[k]))
     self.mu4[k+1] = max(0, self.mu4[k] + self.Kmu4*(self.P_G[k]-self.P_Gmax))
     log.info("Computed Mu. k=%d, mu3:%f, mu4:%f", k+1, self.mu3[k+1], self.mu4[k+1])
     helpers.exitlog(log, functionName)
Exemplo n.º 2
0
    def getData(self,
                msg,
                agents=None,
                nodes=None,
                filters=dict(),
                timestampChunks=None,
                visited=set()):
        """
            Request to fetch data
        """
        functionName = self.getData.__name__
        helpers.entrylog(log, functionName, locals())

        agents_ = helpers.toSet(agents)
        nodes_ = helpers.toSet(nodes)

        if not nodes_:
            nodes_ = config.getTopoGraph().nodes()

        if not agents_:
            if nodes:
                agents_ = self.getSensorAgents(nodes[0])
            else:
                raise AttributeError(
                    "Cannot query for an empty set of collections.")

        if timestampChunks == None:
            timestampChunks = [(0, time.time())]

        data = dict()
        for agent in agents_:
            data[agent] = dict()
            for node in nodes_:
                filters_copy = filters.copy()
                filters_copy['host'] = node
                nodedata = []
                for tsChunk in timestampChunks:
                    nodedata = nodedata + database.getData(
                        agent, filters_copy, tsChunk, database.configHost(),
                        database.ROUTER_SERVER_PORT)
                data[agent][node] = nodedata

        args = {
            "agents": agents,
            "nodes": nodes,
            "filters": filters,
            "timestampChunks": timestampChunks,
            "visited": visited,
            "data": data
        }
        call = {'version': 1.0, 'method': 'putData', 'args': args}
        log.debug('Creating data message')
        msg = MAGIMessage(nodes=msg.src,
                          docks='dataman',
                          contenttype=MAGIMessage.PICKLE,
                          data=pickle.dumps(call))
        log.debug('Sending message')
        self.messenger.send(msg)

        helpers.exitlog(log, functionName)
Exemplo n.º 3
0
def isShardRegistered(dbHost,
                      configHost,
                      dbPort=DATABASE_SERVER_PORT,
                      configPort=ROUTER_SERVER_PORT,
                      block=True):
    """
        Check if given mongo db host is registered as a shard
    """
    functionName = isShardRegistered.__name__
    helpers.entrylog(log, functionName, locals())

    connection = getConnection(host=configHost, port=configPort)
    log.info("Checking if database server is registered as a shard")
    while True:
        try:
            if connection.config.shards.find({
                    "host": "%s:%d" % (dbHost, dbPort)
            }).count() != 0:
                helpers.exitlog(log, functionName)
                return True
        except:
            pass
        if not block:
            helpers.exitlog(log, functionName)
            return False
        time.sleep(1)
Exemplo n.º 4
0
 def stopAgent(self, msg):
     functionName = self.stopAgent.__name__
     helpers.entrylog(log, functionName, level=logging.INFO)
     self.active = False
     if self.thread:
         self.thread.join()
     helpers.exitlog(log, functionName, level=logging.INFO)
Exemplo n.º 5
0
    def link_up_tevc(self, msg, linkName, timing):
        functionName = self.link_up.__name__
        helpers.entrylog(log, functionName, locals())

        if timing == 0:
            timing = "now"
        else:
            timing = "+" + str(timing)

        cmd = "/usr/testbed/bin/tevc -e %s %s %s up" % (testbed.eid, timing,
                                                        linkName)
        log.info("Running cmd: %s" % (cmd))

        process = Popen(cmd.split(), stdout=PIPE, stderr=PIPE)
        returncode = process.wait()
        stdout, stderr = process.communicate()

        if returncode == 0:
            log.info(stdout)
            log.info("Link '" + linkName + "' brought up.")
        else:
            log.error(stderr)
            log.error("Error in bringing link '" + linkName +
                      "' up. Error code %d" % (returncode))

        return True
Exemplo n.º 6
0
    def initCommClient(self, address, replyHandler):
        functionName = self.initCommClient.__name__
        helpers.entrylog(log, functionName, level=logging.INFO)
        
        self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.settimeout(TXTIMEOUT)
        
        retries = 0        
        while not self.connected:
            log.info("Trying to connect to server, attempt #%d..." % (retries+1))
            try:
                self.sock.connect((address, PORT))
                self.connected = True
                log.info("Connected to server")
            except socket.error as e:
                retries += 1
                log.info("Socket timed out, exception: %s" % repr(e))
                time.sleep(0.1 + (random.random()*0.3))

        data = json.dumps({'src': self.clientId})
        self.sock.send(data)
        
        self.active = True
        thread = Thread(name="ClientHandler for " + self.clientId, target=self.ClientHandler, args=(replyHandler,))
        thread.start()
        
        helpers.exitlog(log, functionName, level=logging.INFO)
        return thread
Exemplo n.º 7
0
    def initCommClient(self, address, replyHandler):
        functionName = self.initCommClient.__name__
        helpers.entrylog(log, functionName, level=logging.INFO)

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.settimeout(TXTIMEOUT)

        retries = 0
        while not self.connected:
            log.info("Trying to connect to server, attempt #%d..." %
                     (retries + 1))
            try:
                self.sock.connect((address, PORT))
                self.connected = True
                log.info("Connected to server")
            except socket.error as e:
                retries += 1
                log.info("Socket timed out, exception: %s" % repr(e))
                time.sleep(0.1 + (random.random() * 0.3))

        data = json.dumps({'src': self.clientId})
        self.sock.send(data)

        self.active = True
        thread = Thread(name="ClientHandler for " + self.clientId,
                        target=self.ClientHandler,
                        args=(replyHandler, ))
        thread.start()

        helpers.exitlog(log, functionName, level=logging.INFO)
        return thread
Exemplo n.º 8
0
  def terminateserver(self): 
      functionName = self.terminateserver.__name__
      helpers.entrylog(log, functionName, level=logging.INFO)
 
      self.commServer.stop() 
      helpers.exitlog(log, functionName, level=logging.INFO)
      return True 
Exemplo n.º 9
0
	def startClient(self, msg, server, port=None, time=10, bw=None):
		functionName = self.startClient.__name__
		helpers.entrylog(log, functionName, locals())
		
		if not server:
			raise AttributeError("iperf server not set")
		
		if port == None:
			port = self.port
		
		iperfCmd = "iperf -c %s -u -p %d -t %d" %(server, port, time)
		if bw:
			iperfCmd = iperfCmd + " -b %d" %port

		log.info("Running: %s" %iperfCmd)
		
		p = Popen(iperfCmd.split(), stdout=PIPE, stderr=PIPE)
		
		if p.wait():
			log.error('Could not start iperf')
			err = p.communicate()[1]
			log.error(err)
			return False
		
		log.info('Successfully running iperf client')
		
		out = p.communicate()[0]
		f = open(self.clientFile, 'w')
		f.write(out)
		f.close()
		
		log.info('iperf client successfully completed')
		return True
Exemplo n.º 10
0
    def startHping(self, msg, dest, port=None, interval=None):
        functionName = self.startHping.__name__
        helpers.entrylog(log, functionName, locals())

        if not dest:
            raise AttributeError("Destination not set")

        hpingCmd = "sudo hping3 %s --udp -q" % dest
        if port:
            hpingCmd = hpingCmd + " -p %d" % port
        if interval:
            hpingCmd = hpingCmd + " -i %d" % interval
        else:
            hpingCmd = hpingCmd + " --flood"

        log.info("Running %s" % hpingCmd)

        self.process = Popen(hpingCmd.split(), stdout=PIPE, stderr=PIPE)
        time.sleep(1)

        if self.process.poll() != None:
            log.error('Could not start hping')
            err = self.process.communicate()[1]
            log.error(err)
            return False

        log.info('hping3 started with process id %d' % (self.process.pid))
        return True
Exemplo n.º 11
0
def moveChunk(db,
              collection,
              host,
              collector,
              configHost,
              configPort=ROUTER_SERVER_PORT):
    """
        Shard, split and move a given collection to the corresponding collector
    """
    functionName = moveChunk.__name__
    helpers.entrylog(log, functionName, locals())

    adminConnection = getConnection(host=configHost, port=configPort)

    log.info("Trying to move chunk %s:%s to %s" %
             (host, collection, collector))

    while True:
        try:
            log.info("Enabling sharding %s.%s" % (db, collection))
            adminConnection.admin.command('enablesharding',
                                          '%s.%s' % (db, collection))
            log.info("Sharding enabled successfully.")
            break
        except pymongo.errors.OperationFailure, e:
            log.error(str(e))  #sharding might already be enabled
            if "already enabled" in str(e):
                break
            time.sleep(0.2)
Exemplo n.º 12
0
 def computeMu(self, k):
     functionName = self.computeMu.__name__
     helpers.entrylog(log, functionName)
     self.mu5[k+1] = max(0, self.mu5[k] + self.Kmu5*(-self.P_D[k] - self.K_E*self.E_D[k] + self.P_Dmin))
     self.mu6[k+1] = max(0, self.mu6[k] + self.Kmu6*(self.P_D[k] + self.K_E*self.E_D[k] - self.P_Dmax))
     log.info("Computed Mu. k=%d, mu5:%f, mu6:%f", k+1, self.mu5[k+1], self.mu6[k+1])
     helpers.exitlog(log, functionName)
Exemplo n.º 13
0
	def startHping(self, msg, dest, port=None, interval=None):
		functionName = self.startHping.__name__
		helpers.entrylog(log, functionName, locals())
		
		if not dest:
			raise AttributeError("Destination not set")
		
		hpingCmd = "sudo hping3 %s --udp -q" %dest
		if port:
			hpingCmd = hpingCmd + " -p %d" %port
		if interval:
			hpingCmd = hpingCmd + " -i %d" %interval
		else:
			hpingCmd = hpingCmd + " --flood"
			
		log.info("Running %s" %hpingCmd)
		
		self.process = Popen(hpingCmd.split(), stdout=PIPE, stderr=PIPE)
		time.sleep(1)
		
		if self.process.poll() != None:
			log.error('Could not start hping')
			err = self.process.communicate()[1]
			log.error(err)
			return False
		
		log.info('hping3 started with process id %d' %(self.process.pid))
		return True
Exemplo n.º 14
0
 def stopAgent(self, msg):
     functionName = self.stopAgent.__name__
     helpers.entrylog(log, functionName, level=logging.INFO)
     self.active = False
     if self.thread:
         self.thread.join()
     helpers.exitlog(log, functionName, level=logging.INFO)
Exemplo n.º 15
0
    def startClient(self, msg, server, port=None, time=10, bw=None):
        functionName = self.startClient.__name__
        helpers.entrylog(log, functionName, locals())

        if not server:
            raise AttributeError("iperf server not set")

        if port == None:
            port = self.port

        iperfCmd = "iperf -c %s -u -p %d -t %d" % (server, port, time)
        if bw:
            iperfCmd = iperfCmd + " -b %d" % port

        log.info("Running: %s" % iperfCmd)

        p = Popen(iperfCmd.split(), stdout=PIPE, stderr=PIPE)

        if p.wait():
            log.error('Could not start iperf')
            err = p.communicate()[1]
            log.error(err)
            return False

        log.info('Successfully running iperf client')

        out = p.communicate()[0]
        f = open(self.clientFile, 'w')
        f.write(out)
        f.close()

        log.info('iperf client successfully completed')
        return True
Exemplo n.º 16
0
	def getStatus(self, msg, groupMembership=False, agentInfo=False):
		"""
			gives the group membership and agent information: 
			pid, agentname, threadId
        """ 
		functionName = self.getStatus.__name__
		helpers.entrylog(log, functionName, locals())
		result = dict()
		result['status'] = True
		
		if groupMembership:
			groupMembership = dict(self.messaging.groupMembership)
			result['groupMembership'] = groupMembership
			
		if agentInfo:
			agentInfo = []
			processId = os.getpid()
			for tAgent in self.staticAgents + self.threadAgents:
				agentInfo.append({"name": tAgent.agentname, 
								  "processId": processId, 
								  "threadId": tAgent.tid})
			for name in self.pAgentPids.keys():
				agentInfo.append({"name": name, 
								  "processId": self.pAgentPids[name]})
			result['agentInfo'] = agentInfo
		
		self.messaging.send(MAGIMessage(nodes=msg.src, docks=msg.srcdock, 
									    contenttype=MAGIMessage.YAML, 
									    data=yaml.safe_dump(result)))	
		helpers.exitlog(log, functionName)
Exemplo n.º 17
0
def registerShard(dbHost, configHost, dbPort=DATABASE_SERVER_PORT, configPort=ROUTER_SERVER_PORT, block=True, timeout=TIMEOUT):
    """
        Function to register a database server as a shard in the database cluster
    """
    functionName = registerShard.__name__
    helpers.entrylog(log, functionName, locals())
    
    if not block:
        timeout = 0
    elif timeout <= 0:
        timeout = sys.maxint
        
    start = time.time()
    stop = start + timeout
    
    log.info("Trying to register %s:%d as a shard on %s:%d" %(dbHost, dbPort, configHost, configPort))
    connection = getConnection(host=configHost, port=configPort, timeout=timeout) #check if mongos is up and connect to it
    getConnection(host=dbHost, port=DATABASE_SERVER_PORT, timeout=timeout) #check if mongod is up
    
    while time.time() < stop:
        if call("""mongo --host %s --eval "sh.addShard('%s:%d')" """ %(configHost, dbHost, dbPort), shell=True):
            log.debug("Failed to add shard. Will retry.")
            time.sleep(1)
            continue
        if connection.config.shards.find({"host": "%s:%d" % (dbHost, dbPort)}).count() == 0:
            log.debug("Failed to add shard. Will retry.")
            time.sleep(1)
            continue
        log.info("Registered %s as a shard on %s" %(dbHost, configHost))
        helpers.exitlog(log, functionName)
        return
    
    log.error("Cannot add the required shard")
    helpers.exitlog(log, functionName)
    raise pymongo.errors.PyMongoError("Cannot add the required shard")
Exemplo n.º 18
0
	def reboot(self, msg, distributionDir=None, noUpdate=False, noInstall=False, expConf=None, nodeConf=None):
		"""
		    reinvokes magi_bootstrap, the boostrap script invokes stop() and does a clean shutdown and then 
		    restarts 
		"""
		functionName = self.reboot.__name__
		helpers.entrylog(log, functionName, locals())
		
		if not distributionDir:
			distributionDir = config.getDistDir()
		rebootCmd = "sudo %s/magi_bootstrap.py -p %s" %(distributionDir, distributionDir)
		if noUpdate:
			rebootCmd += ' --noupdate'
		if noInstall:
			rebootCmd += ' --noinstall'
		
		if not expConf and not nodeConf:
			nodeConf = config.getNodeConfFile()
				
		if expConf:
			rebootCmd += ' --expconf %s' %(expConf)
		if nodeConf:
			rebootCmd += ' --nodeconf %s' %(nodeConf)
		
		log.info("Rebooting: %s" %(rebootCmd))
		
		self.messaging.send(MAGIMessage(nodes=msg.src, 
										docks=msg.srcdock, 
										contenttype=MAGIMessage.YAML, 
										data=yaml.safe_dump({'status' : True})))
		
		Popen(rebootCmd.split())
		
		helpers.exitlog(log, functionName)	
Exemplo n.º 19
0
 def initCommClient(self, address, port, replyHandler):
     functionName = self.initCommClient.__name__
     helpers.entrylog(log, functionName, level=logging.INFO)
     
     self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.sock.settimeout(TXTIMEOUT)
     
     retries = 0        
     while not self.connected:
         log.info("Trying to connect to server, attempt #%d..." % (retries+1))
         try:
             log.info("Trying to connect to server %s..." % (address))
             self.sock.connect((address, port))
             self.connected = True
             log.info("Connected to server")
         except socket.error as e:
             retries += 1
             log.info("Socket timed out, exception: %s" % repr(e))
             time.sleep(0.1 + (random.random()*0.3))
             if retries == 10:
                 log.info("Failed to connect after ten retires...  %s" % repr(e))
                 return 
     
     # Now that a connection is established with the server start the 
     # server management thread so that client and asynchronously send and recv 
     self.active = True
     thread = Thread(name="ClientHandler for " + str(self.clientId), target=self.ClientHandler, args=(replyHandler,))
     thread.start()
     
     helpers.exitlog(log, functionName, level=logging.INFO)
     return thread
Exemplo n.º 20
0
    def terminateserver(self):
        functionName = self.terminateserver.__name__
        helpers.entrylog(log, functionName, level=logging.INFO)

        self.commServer.stop()
        helpers.exitlog(log, functionName, level=logging.INFO)
        return True
Exemplo n.º 21
0
def getCollection(agentName,
                  hostName,
                  connection=None,
                  dbHost='localhost',
                  dbPort=DATABASE_SERVER_PORT):
    """
        Function to get a pointer to a given agent data collection
    """
    functionName = getCollection.__name__
    helpers.entrylog(log, functionName, locals())

    global collectionCache

    if connection:
        if not isinstance(connection, Connection):
            raise TypeError("Invalid connection instance")
        dbHost = connection.host
        dbPort = connection.port

    if (agentName, dbHost, dbPort) not in collectionCache:
        collectionCache[(agentName, hostName, dbHost,
                         dbPort)] = Collection(agentName=agentName,
                                               hostName=hostName,
                                               connection=connection,
                                               dbHost=dbHost,
                                               dbPort=dbPort)

    helpers.exitlog(log, functionName)
    return collectionCache[(agentName, hostName, dbHost, dbPort)]
Exemplo n.º 22
0
 def sendTheta(self, k):
     functionName = self.sendTheta.__name__
     helpers.entrylog(log, functionName)
     theta = self.theta[:, k]
     kwargs = {'method' : 'receiveTheta', 'args' : {'k' : k, 'theta' : theta}, 'version' : 1.0}
     msg = MAGIMessage(nodes="grid", docks="dmm_dock", data=yaml.dump(kwargs), contenttype=MAGIMessage.YAML)
     self.messenger.send(msg)
     helpers.exitlog(log, functionName)
Exemplo n.º 23
0
 def sendGradF(self, k):
     functionName = self.sendGradF.__name__
     helpers.entrylog(log, functionName)
     pdr = self.P_D[k]
     grad_f = self.grad_f[k]
     log.info("Sending grad_f to ISO: %f, P_D: %f (k=%d)", grad_f, pdr, k)
     self.commClient.sendData({'k': k, 'pdr': pdr, 'grad_f': grad_f})
     helpers.exitlog(log, functionName)
Exemplo n.º 24
0
 def sendRho(self, k):
     functionName = self.sendRho.__name__
     helpers.entrylog(log, functionName, locals())
     kwargs = {'method' : 'receiveRho', 'args' : {'k' : k, 'rho' : self.rho[k]}, 'version' : 1.0}
     log.debug("Sending rho: %s", kwargs['args'])
     msg = MAGIMessage(nodes="iso", docks="dmm_dock", data=yaml.dump(kwargs), contenttype=MAGIMessage.YAML)
     self.messenger.send(msg)
     helpers.exitlog(log, functionName) 
Exemplo n.º 25
0
 def sendGradF(self, k):
     functionName = self.sendGradF.__name__
     helpers.entrylog(log, functionName)
     pdr = self.P_D[k]
     grad_f = self.grad_f[k]
     log.info("Sending grad_f to ISO: %f, P_D: %f (k=%d)", grad_f, pdr, k)
     self.commClient.sendData({'k' : k, 'pdr': pdr, 'grad_f' : grad_f})
     helpers.exitlog(log, functionName)
Exemplo n.º 26
0
 def sendFreqErrorNotice(self, status):
     functionName = self.sendFreqErrorNotice.__name__
     helpers.entrylog(log, functionName, locals())
     kwargs = {'method' : 'receiveFreqErrorNotice', 'args' : {'status' : status}, 'version' : 1.0}
     log.info("Sending freqErrorNotice: %s", kwargs['args'])
     msg = MAGIMessage(nodes="iso", docks="dmm_dock", data=yaml.dump(kwargs), contenttype=MAGIMessage.YAML)
     self.messenger.send(msg)
     helpers.exitlog(log, functionName)
Exemplo n.º 27
0
 def requestPdr(self, k):
     functionName = self.requestPdr.__name__
     helpers.entrylog(log, functionName)
     log.debug("Requesting Pdr, k:%d", k)
     kwargs = {'method' : 'sendPdr', 'args' : {'k' : k}, 'version' : 1.0}
     msg = MAGIMessage(groups="dr_group", docks="dmm_dock", data=yaml.dump(kwargs), contenttype=MAGIMessage.YAML)
     self.messenger.send(msg)
     helpers.exitlog(log, functionName)
Exemplo n.º 28
0
 def testInt(self, msg, a, b):
     functionName = self.testInt.__name__
     helpers.entrylog(log, functionName, locals(), logging.INFO)
     log.info("a: %d" % (a))
     log.info("b: %d" % (b))
     result = a + b
     log.info("result: %d" % (result))
     helpers.exitlog(log, functionName, result, logging.INFO)
     return result
Exemplo n.º 29
0
 def sendPdr(self, k):
     functionName = self.sendPdr.__name__
     helpers.entrylog(log, functionName)
     for agentIndex in range(0, self.N_dem):
         node = "dr-"+str(agentIndex)
         pdr = self.P_D[agentIndex, k]
         log.debug("Sending P_D, dst: %s, k:%d, pdr:%f", node, k, pdr)
         self.commServer.sendData(node, {'k' : k, 'pdr' : pdr})
     helpers.exitlog(log, functionName)
Exemplo n.º 30
0
 def testInt(self, msg, a, b):
     functionName = self.testInt.__name__
     helpers.entrylog(log, functionName, locals(), logging.INFO)
     log.info("a: %d" %(a))
     log.info("b: %d" %(b))
     result = a + b
     log.info("result: %d" %(result))
     helpers.exitlog(log, functionName, result, logging.INFO)
     return result
Exemplo n.º 31
0
 def sendPdr(self, k):
     functionName = self.sendPdr.__name__
     helpers.entrylog(log, functionName)
     for agentIndex in range(0, self.N_dem):
         node = "dr-" + str(agentIndex)
         pdr = self.P_D[agentIndex, k]
         log.debug("Sending P_D, dst: %s, k:%d, pdr:%f", node, k, pdr)
         self.commServer.sendData(node, {'k': k, 'pdr': pdr})
     helpers.exitlog(log, functionName)
Exemplo n.º 32
0
 def computeMu(self, k):
     functionName = self.computeMu.__name__
     helpers.entrylog(log, functionName)
     self.mu3[k + 1] = max(
         0, self.mu3[k] + self.Kmu3 * (self.P_Gmin - self.P_G[k]))
     self.mu4[k + 1] = max(
         0, self.mu4[k] + self.Kmu4 * (self.P_G[k] - self.P_Gmax))
     log.info("Computed Mu. k=%d, mu3:%f, mu4:%f", k + 1, self.mu3[k + 1],
              self.mu4[k + 1])
     helpers.exitlog(log, functionName)
Exemplo n.º 33
0
    def runserver(self):
        functionName = self.runserver.__name__ + "on port " + str(self.port) 
        helpers.entrylog(log, functionName, level=logging.INFO)

        self.commServer = ServerCommService()
        self.commServer.initCommServer(self.port, self.responseHandler)
            
        helpers.exitlog(log, functionName, level=logging.INFO)

        return True 
Exemplo n.º 34
0
    def runserver(self):
        functionName = self.runserver.__name__ + "on port " + str(self.port)
        helpers.entrylog(log, functionName, level=logging.INFO)

        self.commServer = ServerCommService()
        self.commServer.initCommServer(self.port, self.responseHandler)

        helpers.exitlog(log, functionName, level=logging.INFO)

        return True
Exemplo n.º 35
0
 def __init__(self, triggerData):
     functionName = self.__init__.__name__
     helpers.entrylog(log, functionName, locals())
 
     Trigger.__init__(self, triggerData)
     self.event = triggerData.pop('event')
     self.nodes = helpers.toSet(triggerData.pop('nodes', None))
     self.count = triggerData.pop('count', max(len(self.nodes), 1))
     self.args = triggerData
     if not self.args:
         self.args = {'retVal' : True}
Exemplo n.º 36
0
    def __init__(self, triggerData):
        functionName = self.__init__.__name__
        helpers.entrylog(log, functionName, locals())

        Trigger.__init__(self, triggerData)
        self.event = triggerData.pop('event')
        self.nodes = helpers.toSet(triggerData.pop('nodes', None))
        self.count = triggerData.pop('count', max(len(self.nodes), 1))
        self.args = triggerData
        if not self.args:
            self.args = {'retVal': True}
Exemplo n.º 37
0
 def sendPg(self, k):
     functionName = self.sendPg.__name__
     helpers.entrylog(log, functionName)
     for agentIndex in range(0, self.N_gen):
         if not self.statusGens[agentIndex]:
             continue  # inactive generator
         node = "gen-" + str(agentIndex)
         pg = self.P_G[agentIndex, k]
         log.debug("Sending P_G, dst: %s, k:%d, pg:%f", node, k, pg)
         self.commServer.sendData(node, {'k': k, 'pg': pg})
     helpers.exitlog(log, functionName)
Exemplo n.º 38
0
    def setupDBLogHandler(self):
        functionName = self.setupDBLogHandler.__name__
        helpers.entrylog(log, functionName, locals())

        log.info("Setting up database log handler")
        from magi.util.databaseLogHandler import DatabaseHandler
        self.dbLogHandler = DatabaseHandler.to(level=logging.INFO)
        rootLogger = logging.getLogger()
        rootLogger.addHandler(self.dbLogHandler)

        helpers.exitlog(log, functionName)
Exemplo n.º 39
0
 def sendEdr(self, k):
     functionName = self.sendEdr.__name__
     helpers.entrylog(log, functionName)
     for agentIndex in range(0, self.N_dem):
         node = "dr-"+str(agentIndex)
         edr = self.E_D[agentIndex, k]
         log.debug("Sending E_D, Node: %s, k:%d, E_D:%f", node, k, edr)
         kwargs = {'method' : 'receiveEdr', 'args' : {'k' : k, 'edr' : edr}, 'version' : 1.0}
         msg = MAGIMessage(nodes=node, docks="dmm_dock", data=yaml.dump(kwargs), contenttype=MAGIMessage.YAML)
         self.messenger.send(msg)
     helpers.exitlog(log, functionName) 
Exemplo n.º 40
0
 def setupDBLogHandler(self):
     functionName = self.setupDBLogHandler.__name__
     helpers.entrylog(log, functionName, locals())
     
     log.info("Setting up database log handler")
     from magi.util.databaseLogHandler import DatabaseHandler
     self.dbLogHandler = DatabaseHandler.to(level=logging.INFO)
     rootLogger = logging.getLogger()
     rootLogger.addHandler(self.dbLogHandler)
     
     helpers.exitlog(log, functionName)
Exemplo n.º 41
0
 def sendPg(self, k):
     functionName = self.sendPg.__name__
     helpers.entrylog(log, functionName)
     for agentIndex in range(0, self.N_gen):
         if not self.statusGens[agentIndex]:
             continue # inactive generator
         node = "gen-"+str(agentIndex)
         pg = self.P_G[agentIndex, k]
         log.debug("Sending P_G, dst: %s, k:%d, pg:%f", node, k, pg)
         self.commServer.sendData(node, {'k' : k, 'pg' : pg})
     helpers.exitlog(log, functionName)
Exemplo n.º 42
0
 def sendPdr(self, msg, k):
     functionName = self.sendPdr.__name__
     helpers.entrylog(log, functionName)
     self.currentGridTs = k
     if k > self.lastPdrRcvdTs:
         self.P_D[k] = self.P_D[self.lastPdrRcvdTs]
     pdr = self.P_D[k]
     log.info("Sending P_D to grid agent: %f (k=%d)", pdr, k)
     kwargs = {'method' : 'receivePdr', 'args' : {'k' : k, 'pdr' : pdr}, 'version' : 1.0}
     msg = MAGIMessage(nodes="grid", docks="dmm_dock", data=yaml.dump(kwargs), contenttype=MAGIMessage.YAML)
     self.messenger.send(msg)
     helpers.exitlog(log, functionName)
Exemplo n.º 43
0
 def computeMu(self, k):
     functionName = self.computeMu.__name__
     helpers.entrylog(log, functionName)
     self.mu5[k + 1] = max(
         0, self.mu5[k] + self.Kmu5 *
         (-self.P_D[k] - self.K_E * self.E_D[k] + self.P_Dmin))
     self.mu6[k + 1] = max(
         0, self.mu6[k] + self.Kmu6 *
         (self.P_D[k] + self.K_E * self.E_D[k] - self.P_Dmax))
     log.info("Computed Mu. k=%d, mu5:%f, mu6:%f", k + 1, self.mu5[k + 1],
              self.mu6[k + 1])
     helpers.exitlog(log, functionName)
Exemplo n.º 44
0
 def getData(self, msg, agents=None, nodes=None, filters=dict(), timestampChunks=None, visited=set()):
     """
         Request to fetch data
     """
     functionName = self.getData.__name__
     helpers.entrylog(log, functionName, locals())
     
     agents_ = helpers.toSet(agents)
     nodes_ = helpers.toSet(nodes)
     
     if not nodes_:
         nodes_ = config.getTopoGraph().nodes()
         
     if not agents_:
         if nodes:
             agents_ = self.getSensorAgents(nodes[0])
         else:
             raise AttributeError("Cannot query for an empty set of collections.")
     
     if timestampChunks == None:
         timestampChunks = [(0, time.time())]
     
     data = dict()
     for agent in agents_:
         data[agent] = dict()
         for node in nodes_:
             filters_copy = filters.copy()
             filters_copy['host'] = node
             nodedata = []
             for tsChunk in timestampChunks:
                 nodedata = nodedata + database.getData(agent, 
                                                        filters_copy, 
                                                        tsChunk, 
                                                        database.configHost(), 
                                                        database.ROUTER_SERVER_PORT)
             data[agent][node] = nodedata
     
     args = {
         "agents": agents,
         "nodes": nodes,
         "filters": filters,
         "timestampChunks": timestampChunks,
         "visited": visited,
         "data": data
     }
     call = {'version': 1.0, 'method': 'putData', 'args': args}
     log.debug('Creating data message')
     msg = MAGIMessage(nodes=msg.src, docks='dataman', contenttype=MAGIMessage.PICKLE, data=pickle.dumps(call))
     log.debug('Sending message')
     self.messenger.send(msg)
     
     helpers.exitlog(log, functionName)
Exemplo n.º 45
0
 def digest(self, *args):
     functionName = self.digest.__name__
     helpers.entrylog(log, functionName, locals())
     m = hashlib.md5()
     for arg in args:
         if type(arg) is set:
             arg = list(arg)
         if type(arg) is list:
             arg.sort()
         m.update(str(arg))
     result = m.hexdigest()
     helpers.exitlog(log, functionName, result)
     return result
Exemplo n.º 46
0
 def getCollector(self, node, agentName):
     """
         Internal function to fetch the collector for a given node and agent
     """
     functionName = self.getCollector.__name__
     helpers.entrylog(log, functionName, locals())
     
     node = node.split(".")[0]
     
     sensorToCollectorMap = database.getSensorToCollectorMap()
     result = sensorToCollectorMap.get(node, sensorToCollectorMap.get('__ALL__'))
     helpers.exitlog(log, functionName, result)
     return result
Exemplo n.º 47
0
 def putCollectionMetadata(self, msg, collectionMetadata):
     """
         Response for collector information request
     """
     functionName = self.putCollectionMetadata.__name__
     helpers.entrylog(log, functionName, locals())
     
     self.collectionMetadata[msg.src] = CachedItem(msg.src, collectionMetadata)
     queryHash = self.digest("CollectionMetadata", msg.src)
     self.events[queryHash].set()
     self.events[queryHash].clear()
     
     helpers.exitlog(log, functionName)
Exemplo n.º 48
0
 def digest(self, *args):
     functionName = self.digest.__name__
     helpers.entrylog(log, functionName, locals())
     m = hashlib.md5()
     for arg in args:
         if type(arg) is set:
             arg = list(arg)
         if type(arg) is list:
             arg.sort()
         m.update(str(arg))
     result = m.hexdigest()
     helpers.exitlog(log, functionName, result)
     return result
Exemplo n.º 49
0
	def stopServer(self, msg):
		functionName = self.stopServer.__name__
		helpers.entrylog(log, functionName, locals())
		
		if self.serverProcess:
			self.serverProcess.terminate()
		
		out = self.serverProcess.communicate()[0]
		f = open(self.serverFile, 'w')
		f.write(out)
		f.close()
		
		return True
Exemplo n.º 50
0
    def stopServer(self, msg):
        functionName = self.stopServer.__name__
        helpers.entrylog(log, functionName, locals())

        if self.serverProcess:
            self.serverProcess.terminate()

        out = self.serverProcess.communicate()[0]
        f = open(self.serverFile, 'w')
        f.write(out)
        f.close()

        return True
Exemplo n.º 51
0
 def sendGradF(self, k):
     functionName = self.sendGradF.__name__
     helpers.entrylog(log, functionName, locals())
     pg = self.P_G[k]
     grad_f = self.grad_f[k]
     self.collection.insert({'k': k, 'gradFDeception': self.gradFDeception})
     if self.gradFDeceptionAttack:
         log.info("gradFDeception grad_f: %f, deception: %f (k=%d)", grad_f,
                  self.gradFDeception, k)
         grad_f += self.gradFDeception
         self.gradFDeception += self.gradFDeceptionInc
     log.info("Sending grad_f to ISO: %f, P_G: %f (k=%d)", grad_f, pg, k)
     self.commClient.sendData({'k': k, 'pg': pg, 'grad_f': grad_f})
     helpers.exitlog(log, functionName)
Exemplo n.º 52
0
	def archive(self, msg, destinationDir=config.getTempDir()):
		""" 
            Tars the log directory" 
        """ 
		functionName = self.archive.__name__
		helpers.entrylog(log, functionName, locals())
		logDir = config.getLogDir()
		logTar = tarfile.open(name=os.path.join(destinationDir, 
								"logs_%s.tar.gz"%(datetime.datetime.now()
												.strftime("%Y%m%d_%H%M%S"))), 
							  mode='w:gz')
		logTar.add(logDir, arcname=os.path.basename(logDir))
		logTar.close()
		helpers.exitlog(log, functionName)