示例#1
0
    def test_add_measurement_more_than_limit(self):
        # adding more measurements than the limit will keep only the newest measurements within this limit
        num_entries = self.max_entries_per_node * 2
        for i in range(0, num_entries):
            payload = dumps({'k': str(i)})

            redis_adapter.add_measurement(some_node_id, payload)

        node_key = raw_redis.keys('*%s*' % some_node_id)[0]

        self.assertEqual(
            raw_redis.llen(node_key), self.max_entries_per_node,
            "Redis contains more than the allowed limit of measurements")

        oldest = loads(raw_redis.lindex(node_key, 0))
        newest = loads(
            raw_redis.lindex(node_key, self.max_entries_per_node - 1))

        expected_payload_oldest = str(num_entries - self.max_entries_per_node)
        expected_payload_newest = str(num_entries - 1)

        self.assertEqual(
            oldest['k'], expected_payload_oldest,
            "The oldest element in a dequeue should be at index 0")

        self.assertEqual(
            newest['k'], expected_payload_newest,
            "The newest element in a dequeue should be at the end of the dequeue"
        )
示例#2
0
def forget_host(hostname):
    print("Forgetting %s" % hostname)
    redis_adapter.forget_node(hostname)
    flash(
        "Node %s was forgotten. No measurements from this node will be saved and it will be hidden from the dashboard."
        % hostname, 'warning')
    return ''
示例#3
0
    def test_get_all_for_node(self):
        num_items = 10
        for index in range(0, num_items):
            redis_adapter.add_measurement(some_node_id,
                                          dumps({'k': str(index)}))
        node_key = raw_redis.keys('*%s*' % some_node_id)[0]

        all_measurements = redis_adapter.get_all_for_node(some_node_id)
        self.assertEqual(len(all_measurements), num_items)

        for index, payload in enumerate(
                redis_adapter.get_all_for_node(some_node_id)):
            self.assertEqual(payload['k'], str(index))
示例#4
0
def add_measurement():
	"""
	This is the endpoint that nodes call - they send us their current status in the form of a hardware_objects_lib.Computer
	object. Internally we enqueue each such Computer object for each node that has called this api.
	POST request with form data in the form:
	{
		"payload": <json string of a Computer object>
	}
	:return:
		- on success - http code 200
		- on ignored - http code 202. if the node was marked as one which should be forgotten from the management dashboard
		- on empty input data - http code 400
		- on failure to deserialize the json - http code 500
	"""
	computer_json = request.form.get('payload')
	if not computer_json:
		return fail_with("Empty input data.", 400)

	try:
		# we don't actually need the Computer object now.
		# we just made sure we can deserialize it
		computer = reconstruct_computer(computer_json)
	except Exception as ex:
		return fail_with("Cannot deserialize the input", 500)
	if ComputerMeasurementsRedisAdapter.add_measurement(node_identifier=computer.hostname, computer_json=computer_json):
		return 'ok'
	else:
		return fail_with('measurements for node %s are ignored.' % computer.hostname, code=202)
示例#5
0
def delete_host_readings(hostname):
    print("Deleting %s's readings" % hostname)
    if redis_adapter.delete_node(hostname):
        flash("All readings for host %s were deleted " % hostname, 'warning')
    else:
        flash("No readings were found for host %s" % hostname, 'danger')
    return ''
示例#6
0
    def test_add_measurement_simple(self):
        payload_json = dumps({'key1': 'val1'})

        redis_adapter.add_measurement(some_node_id, payload_json)
        matching_keys = raw_redis.keys('*%s*' % some_node_id)

        self.assertEqual(len(matching_keys), 1,
                         'Only one key is expected to be in redis now.')
        matching_key = matching_keys[0]
        self.assertTrue(
            some_node_id in matching_key,
            "The name of the host is expected to be in the redis key")

        self.assertEqual(
            raw_redis.llen(matching_key), 1,
            "The list against for this node should contain only one element now"
        )
示例#7
0
    def test_add_measurement_dequeue(self):
        # we add at end of the list
        payload_first = dumps({'k1': 'v1'})
        payload_second = dumps({'k2': 'v2'})

        redis_adapter.add_measurement(some_node_id, payload_first)
        redis_adapter.add_measurement(some_node_id, payload_second)

        node_key = raw_redis.keys('*%s*' % some_node_id)[0]

        assert raw_redis.llen(
            node_key) == 2, 'sanity checking the number of elements'

        second = loads(raw_redis.lindex(node_key, 1))
        self.assertEqual(
            'v2', second['k2'],
            'the newest added element to the queue was not at added at the end'
        )
示例#8
0
def unforget_host(hostname):
    print("Unforgetting %s" % hostname)
    if redis_adapter.unforget_node(hostname):
        flash(
            "Host %s was unforgotten - new measurements from it will be saved"
            % hostname, 'success')
    else:
        flash("Host %s was never forgotten" % hostname, 'danger')
    return ''
示例#9
0
def node_info(node_identifier, auto_reloading_kwargs={}):
    computer_measurements = ComputerMeasurementsRedisAdapter.get_all_for_node(
        node_identifier, deserialize=True)

    newest_computer_measurement = computer_measurements[-1]
    is_dead = is_node_dead(newest_computer_measurement)
    older_computers = list(reversed(computer_measurements[0:-1]))

    return render_template('single_computer.html',
                           computer=newest_computer_measurement,
                           older_computers=older_computers,
                           is_dead=is_dead,
                           **auto_reloading_kwargs)
示例#10
0
def index(auto_reloading_kwargs={}):
    all_host_names = ComputerMeasurementsRedisAdapter.get_all_monitored_hostnames(
    )
    all_host_names = list(sorted(all_host_names))
    dead_nodes_objects = []
    dead_nodes_names = []
    computers = []
    for index, host_name in enumerate(all_host_names):
        node = ComputerMeasurementsRedisAdapter.get_newest_computer_measurement_from_redis(
            host_name, deserialize=True)
        entry = (host_name, node)
        computers.append(entry)
        if is_node_dead(node):
            dead_nodes_objects.append(entry)
            dead_nodes_names.append(host_name)

    return render_template('dashboard.html',
                           host_names=all_host_names,
                           computers=computers,
                           dead_host_objects=dead_nodes_objects,
                           dead_host_names=dead_nodes_names,
                           **auto_reloading_kwargs)
示例#11
0
def manage():

    all_host_names = redis_adapter.get_all_monitored_hostnames()
    newest_measurement_from = [
        redis_adapter.get_newest_computer_measurement_from_redis(
            host).time_now_utc for host in all_host_names
    ]

    forgotten_hosts = redis_adapter.get_forgotten_hosts()

    server_settings = {
        'NODE_GONE_AFTER': current_app.config['NODE_GONE_AFTER'],
        "MAX_ENTRIES_PER_NODE": current_app.config['MAX_ENTRIES_PER_NODE'],
        "REDIS_HOST": current_app.config['REDIS_HOST'],
        "REDIS_PORT": current_app.config['REDIS_PORT'],
        "REDIS_DB": current_app.config['REDIS_DB'],
    }

    return render_template('admin/admin_dash.html',
                           all_host_names=all_host_names,
                           forgotten_hosts=forgotten_hosts,
                           server_settings=server_settings,
                           newest_measurement_from=newest_measurement_from)
示例#12
0
def get_all_measurements(node_identifier):
	# TODO serializing a serialized string
	import json
	return json.dumps(ComputerMeasurementsRedisAdapter.get_all_for_node(node_identifier, deserialize=False))
示例#13
0
def newest(node_identifier):
	newest_entry_json = ComputerMeasurementsRedisAdapter.get_newest_computer_measurement_from_redis(
		node_identifier=node_identifier,
		deserialize=False)

	return newest_entry_json or fail_with("No entries for %s" % node_identifier, 400)