コード例 #1
0
ファイル: test_queue.py プロジェクト: Fiware/cloud.Facts
    def testChannelNone(self):
        """ Test if method fails when tries to publish a message with channel equal to None.
        """
        queue = myqueue()

        queue.channel = None

        expectedvalue = "AMQP channel not properly created..."

        try:
            queue.publish_message("tenantid", "hola")
        except (Exception), err:
            self.assertEqual(expectedvalue, err.message)
コード例 #2
0
    def testChannelNone(self):
        """ Test if method fails when tries to publish a message with channel equal to None.
        """
        queue = myqueue()

        queue.channel = None

        expectedvalue = "AMQP channel not properly created..."

        try:
            queue.publish_message("tenantid", "hola")
        except (Exception), err:
            self.assertEqual(expectedvalue, err.message)
コード例 #3
0
def process_request(request, tenantid, serverid):
    """Get the parsed contents of the form data

    :param string request:     The information of the received request
    :param string serverid:    the id of the monitored instance (server)

    :return: True
    """
    json = request.json
    if request.remote_addr:
        message = "[{}] received {} from ip {}:{}"\
            .format("-", json, request.environ[REMOTE_ADDR], request.environ[REMOTE_PORT])
    else:
        message = "[{}] received {} from test client"\
            .format("-", json)
    logging.info(message)

    key = ['contextResponses', 'contextElement', 'attributes']

    # Check that it contains the previous keys
    try:
        jsoncheck.checkit(json, key, 0)
    except NotFound as err:
        logging.error(err)
        raise err

    # Extract the list of attributes from the NGSI message
    attrlist = request.json[CONTEXT_RESPONSES][0][CONTEXT_ELEMENT][
        CONTEXT_ATTRIBUTES]

    data = list()

    for item in attrlist:
        name = item[CONTEXT_ATTRIBUTES_NAME]
        value = item[CONTEXT_ATTRIBUTES_VALUE]

        # Obtain the information of used memory and cpu
        if name == 'usedMemPct':
            verify_values(name, value)
            mem = float(value)
        elif name == 'cpuLoadPct':
            verify_values(name, value)
            cpu = float(value)
        elif name == 'netLoadPct':
            verify_values(name, value)
            net = float(value)
        elif name == 'freeSpacePct':
            verify_values(name, value)
            hdd = float(value)

    data.insert(len(data), cpu)
    data.insert(len(data), mem)
    data.insert(len(data), hdd)
    data.insert(len(data), net)

    # fix the first value of the list with the server identity
    data.insert(0, str(serverid))

    # fix the last value with the current date and time
    data.insert(
        len(fact_attributes) - 1,
        datetime.datetime.today().isoformat())

    # Check data coherency of time stamps
    # +1 is needed because the fact is not already added to the queue.
    # It checks that queue will have at least 2 facts.
    if len(mredis.range(tenantid, serverid)) + 1 >= 2:
        mredis.check_time_stamps(tenantid, serverid,
                                 mredis.range(tenantid, serverid), data)

    # Get the windowsize for the tenant from a redis queue
    windowsize = mredis.get_windowsize(tenantid)
    if windowsize == []:
        windowsize = myClotoDBClient.get_window_size(tenantid)
        mredis.insert_window_size(tenantid, windowsize)

    # Insert the result into the queue system
    mredis.insert(tenantid, serverid, data)
    logging.info(data)

    # If the queue has the number of facts defined by the windows size, it returns the
    # last window-size values (range) and calculates the media of them (in terms of memory and cpu)
    lo = mredis.media(mredis.range(tenantid, serverid), windowsize)

    # If the number of facts is lt window size, the previous operation returns a null lists
    if len(lo) != 0:
        try:
            rabbit = myqueue()
            if len(lo) == 1:
                lo.data = lo.data[0]
            message = "{\"serverId\": \"%s\", \"cpu\": %s, \"mem\": %s, \"hdd\": %s, \"net\": %s, \"time\": \"%s\"}" \
                      % (lo.data[0][1:-1], lo.data[1], lo.data[2], lo.data[3], lo.data[4], lo.data[5])

            logging_message = "[{}] sending message {}".format("-", message)

            logging.info(logging_message)

            # Send the message to the RabbitMQ components.
            result = rabbit.publish_message(tenantid,
                                            message)  # @UnusedVariable

        except Exception as ex:
            raise ex

    return True
コード例 #4
0
ファイル: server.py プロジェクト: geonexus/fiware-facts
    if windowsize == []:
        windowsize = myClotoDBClient.get_window_size(tenantid)
        mredis.insert_window_size(tenantid, windowsize)

     # Insert the result into the queue system
    mredis.insert(tenantid, serverid, data)
    logging.info(data)

    # If the queue has the number of facts defined by the windows size, it returns the
    # last window-size values (range) and calculates the media of them (in terms of memory and cpu)
    lo = mredis.media(mredis.range(tenantid, serverid), windowsize)

    # If the number of facts is lt window size, the previous operation returns a null lists
    if len(lo) != 0:
        try:
            rabbit = myqueue()
            if len(lo) == 1:
                lo.data = lo.data[0]
            message = "{\"serverId\": \"%s\", \"cpu\": %s, \"mem\": %s, \"hdd\": %s, \"net\": %s, \"time\": \"%s\"}" \
                      % (lo.data[0][1:-1], lo.data[1], lo.data[2], lo.data[3], lo.data[4], lo.data[5])

            logging_message = "[{}] sending message {}".format("-", message)

            logging.info(logging_message)

            # Send the message to the RabbitMQ components.
            result = rabbit.publish_message(tenantid, message)  # @UnusedVariable

        except Exception:
            return False
コード例 #5
0
ファイル: server.py プロジェクト: Fiware/cloud.Facts
def process_request(request, tenantid, serverid):
    """Get the parsed contents of the form data

    :param string request:     The information of the received request
    :param string serverid:    the id of the monitored instance (server)

    :return: True
    """
    json = request.json
    if request.remote_addr:
        message = "[{}] received {} from ip {}:{}"\
            .format("-", json, request.environ[REMOTE_ADDR], request.environ[REMOTE_PORT])
    else:
        message = "[{}] received {} from test client"\
            .format("-", json)
    logging.info(message)

    key = ['contextResponses', 'contextElement', 'attributes']

    # Check that it contains the previous keys
    try:
        jsoncheck.checkit(json, key, 0)
    except NotFound as err:
        logging.error(err)
        raise err

    # Extract the list of attributes from the NGSI message
    attrlist = request.json[CONTEXT_RESPONSES][0][CONTEXT_ELEMENT][CONTEXT_ATTRIBUTES]

    data = list()

    for item in attrlist:
        name = item[CONTEXT_ATTRIBUTES_NAME]
        value = item[CONTEXT_ATTRIBUTES_VALUE]

        # Obtain the information of used memory and cpu
        if name == 'usedMemPct':
            verify_values(name, value)
            mem = float(value)
        elif name == 'cpuLoadPct':
            verify_values(name, value)
            cpu = float(value)
        elif name == 'netLoadPct':
            verify_values(name, value)
            net = float(value)
        elif name == 'freeSpacePct':
            verify_values(name, value)
            hdd = float(value)

    data.insert(len(data), cpu)
    data.insert(len(data), mem)
    data.insert(len(data), hdd)
    data.insert(len(data), net)

    # fix the first value of the list with the server identity
    data.insert(0, str(serverid))

    # fix the last value with the current date and time
    data.insert(len(fact_attributes) - 1, datetime.datetime.today().isoformat())

    # Check data coherency of time stamps
    # +1 is needed because the fact is not already added to the queue.
    # It checks that queue will have at least 2 facts.
    if len(mredis.range(tenantid, serverid)) + 1 >= 2:
        mredis.check_time_stamps(tenantid, serverid, mredis.range(tenantid, serverid), data)

    # Get the windowsize for the tenant from a redis queue
    windowsize = mredis.get_windowsize(tenantid)
    if windowsize == []:
        windowsize = myClotoDBClient.get_window_size(tenantid)
        mredis.insert_window_size(tenantid, windowsize)

     # Insert the result into the queue system
    mredis.insert(tenantid, serverid, data)
    logging.info(data)

    # If the queue has the number of facts defined by the windows size, it returns the
    # last window-size values (range) and calculates the media of them (in terms of memory and cpu)
    lo = mredis.media(mredis.range(tenantid, serverid), windowsize)

    # If the number of facts is lt window size, the previous operation returns a null lists
    if len(lo) != 0:
        try:
            rabbit = myqueue()
            if len(lo) == 1:
                lo.data = lo.data[0]
            message = "{\"serverId\": \"%s\", \"cpu\": %s, \"mem\": %s, \"hdd\": %s, \"net\": %s, \"time\": \"%s\"}" \
                      % (lo.data[0][1:-1], lo.data[1], lo.data[2], lo.data[3], lo.data[4], lo.data[5])

            logging_message = "[{}] sending message {}".format("-", message)

            logging.info(logging_message)

            # Send the message to the RabbitMQ components.
            result = rabbit.publish_message(tenantid, message)  # @UnusedVariable

        except Exception as ex:
            raise ex

    return True