Пример #1
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 (Exception), err:
        logging.error(err)
        return False
Пример #2
0
    def test_unknowjsonmessage(self):
        """ Check that a not expected message is recognized
        """
        key = ['contextResponses', 'contextElement', 'attributes']

        expectedvalue = "Invalid json message. We cannot obtain the key: contextResponses"

        try:
            jsoncheck.checkit(mydict2, key, 0)
        except (Exception), err:
            self.assertEqual(expectedvalue, err.message)
Пример #3
0
    def test_unknowkeyvalue(self):
        """ Check that not all keys are content in the dict¡onary
        """
        key = ['contextResponses', 'context', 'attributes']

        expectedvalue = "Invalid json message. We cannot obtain the key: context"

        try:
            jsoncheck.checkit(mydict1, key, 0)
        except (Exception), err:
            self.assertEqual(expectedvalue, err.message)
Пример #4
0
    def test_unknowjsonmessage(self):
        """ Check that a not expected message is recognized
        """
        key = ['contextResponses', 'contextElement', 'attributes']

        expectedvalue = "Invalid json message. We cannot obtain the key: contextResponses"

        try:
            jsoncheck.checkit(mydict2, key, 0)
        except (Exception), err:
            self.assertEqual(expectedvalue, err.message)
Пример #5
0
    def test_unknowkeyvalue(self):
        """ Check that not all keys are content in the dict¡onary
        """
        key = ['contextResponses', 'context', 'attributes']

        expectedvalue = "Invalid json message. We cannot obtain the key: context"

        try:
            jsoncheck.checkit(mydict1, key, 0)
        except (Exception), err:
            self.assertEqual(expectedvalue, err.message)
Пример #6
0
    def test_dictwithonekey(self):
        """Check that an invalid json with only one valid key is recognized
        """
        key = ['contextResponses', 'contextElement', 'attributes']

        expectedvalue = "Invalid json message. We expected " \
                        "'['contextResponses', 'context', 'attributes']' keys but " \
                        "obtain only 'contextResponses' key"

        try:
            jsoncheck.checkit(mydict3, key, 0)
        except (Exception), err:
            self.assertEqual(expectedvalue, err.message)
Пример #7
0
    def test_dictwithonekey(self):
        """Check that an invalid json with only one valid key is recognized
        """
        key = ['contextResponses', 'contextElement', 'attributes']

        expectedvalue = "Invalid json message. We expected " \
                        "'['contextResponses', 'context', 'attributes']' keys but " \
                        "obtain only 'contextResponses' key"

        try:
            jsoncheck.checkit(mydict3, key, 0)
        except (Exception), err:
            self.assertEqual(expectedvalue, err.message)
Пример #8
0
    def test_wellformedmessage(self):
        """ Check that a correct message is recognize
        """
        key = ['contextResponses', 'contextElement', 'attributes']

        expectedvalue = ""
        result = jsoncheck.checkit(mydict1, key, 0)
        self.assertEqual(result, None)
Пример #9
0
    def test_wellformedmessage(self):
        """ Check that a correct message is recognize
        """
        key = ['contextResponses', 'contextElement', 'attributes']

        expectedvalue = ""
        result = jsoncheck.checkit(mydict1, key, 0)
        self.assertEqual(result, None)
Пример #10
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
Пример #11
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