예제 #1
0
    def load_state(self, system: Union[System, GitHubRepo]) \
            -> Union[System, GitHubRepo]:
        # If Redis is down, the data passed as default will be stored as
        # the system state.

        self.logger.debug("Loading the state of %s from Redis", system)
        redis_hash = Keys.get_hash_parent(system.parent_id)
        system_id = system.system_id

        # Below, we will try and get the data stored in redis and store it
        # in the system's state. If the data from Redis cannot be obtained, the
        # state won't be updated.

        # Load process_cpu_seconds_total from Redis
        state_process_cpu_seconds_total = system.process_cpu_seconds_total
        redis_process_cpu_seconds_total = self.redis.hget(
            redis_hash, Keys.get_system_process_cpu_seconds_total(system_id),
            state_process_cpu_seconds_total)
        process_cpu_seconds_total = \
            convert_to_float_if_not_none(redis_process_cpu_seconds_total, None)
        system.set_process_cpu_seconds_total(process_cpu_seconds_total)

        # Load process_memory_usage from Redis
        state_process_memory_usage = system.process_memory_usage
        redis_process_memory_usage = self.redis.hget(
            redis_hash, Keys.get_system_process_memory_usage(system_id),
            state_process_memory_usage)
        process_memory_usage = \
            convert_to_float_if_not_none(redis_process_memory_usage, None)
        system.set_process_memory_usage(process_memory_usage)

        # Load virtual_memory_usage from Redis
        state_virtual_memory_usage = system.virtual_memory_usage
        redis_virtual_memory_usage = self.redis.hget(
            redis_hash, Keys.get_system_virtual_memory_usage(system_id),
            state_virtual_memory_usage)
        virtual_memory_usage = \
            convert_to_float_if_not_none(redis_virtual_memory_usage, None)
        system.set_virtual_memory_usage(virtual_memory_usage)

        # Load open_file_descriptors from Redis
        state_open_file_descriptors = system.open_file_descriptors
        redis_open_file_descriptors = self.redis.hget(
            redis_hash, Keys.get_system_open_file_descriptors(system_id),
            state_open_file_descriptors)
        open_file_descriptors = \
            convert_to_float_if_not_none(redis_open_file_descriptors, None)
        system.set_open_file_descriptors(open_file_descriptors)

        # Load system_cpu_usage from Redis
        state_system_cpu_usage = system.system_cpu_usage
        redis_system_cpu_usage = self.redis.hget(
            redis_hash, Keys.get_system_system_cpu_usage(system_id),
            state_system_cpu_usage)
        system_cpu_usage = \
            convert_to_float_if_not_none(redis_system_cpu_usage, None)
        system.set_system_cpu_usage(system_cpu_usage)

        # Load system_ram_usage from Redis
        state_system_ram_usage = system.system_ram_usage
        redis_system_ram_usage = self.redis.hget(
            redis_hash, Keys.get_system_system_ram_usage(system_id),
            state_system_ram_usage)
        system_ram_usage = \
            convert_to_float_if_not_none(redis_system_ram_usage, None)
        system.set_system_ram_usage(system_ram_usage)

        # Load system_storage_usage from Redis
        state_system_storage_usage = system.system_storage_usage
        redis_system_storage_usage = self.redis.hget(
            redis_hash, Keys.get_system_system_storage_usage(system_id),
            state_system_storage_usage)
        system_storage_usage = \
            convert_to_float_if_not_none(redis_system_storage_usage, None)
        system.set_system_storage_usage(system_storage_usage)

        # Load network_transmit_bytes_per_second from Redis
        state_network_transmit_bytes_per_second = \
            system.network_transmit_bytes_per_second
        redis_network_transmit_bytes_per_second = self.redis.hget(
            redis_hash,
            Keys.get_system_network_transmit_bytes_per_second(system_id),
            state_network_transmit_bytes_per_second)
        network_transmit_bytes_per_second = \
            convert_to_float_if_not_none(
                redis_network_transmit_bytes_per_second, None)
        system.set_network_transmit_bytes_per_second(
            network_transmit_bytes_per_second)

        # Load network_receive_bytes_per_second from Redis
        state_network_receive_bytes_per_second = \
            system.network_receive_bytes_per_second
        redis_network_receive_bytes_per_second = self.redis.hget(
            redis_hash,
            Keys.get_system_network_receive_bytes_per_second(system_id),
            state_network_receive_bytes_per_second)
        network_receive_bytes_per_second = \
            convert_to_float_if_not_none(
                redis_network_receive_bytes_per_second, None)
        system.set_network_receive_bytes_per_second(
            network_receive_bytes_per_second)

        # Load network_transmit_bytes_total from Redis
        state_network_transmit_bytes_total = system.network_transmit_bytes_total
        redis_network_transmit_bytes_total = self.redis.hget(
            redis_hash, Keys.get_system_network_transmit_bytes_total(system_id),
            state_network_transmit_bytes_total)
        network_transmit_bytes_total = \
            convert_to_float_if_not_none(redis_network_transmit_bytes_total,
                                         None)
        system.set_network_transmit_bytes_total(network_transmit_bytes_total)

        # Load network_receive_bytes_total from Redis
        state_network_receive_bytes_total = system.network_receive_bytes_total
        redis_network_receive_bytes_total = self.redis.hget(
            redis_hash, Keys.get_system_network_receive_bytes_total(system_id),
            state_network_receive_bytes_total)
        network_receive_bytes_total = \
            convert_to_float_if_not_none(redis_network_receive_bytes_total,
                                         None)
        system.set_network_receive_bytes_total(network_receive_bytes_total)

        # Load disk_io_time_seconds_in_interval from Redis
        state_disk_io_time_seconds_in_interval = \
            system.disk_io_time_seconds_in_interval
        redis_disk_io_time_seconds_in_interval = self.redis.hget(
            redis_hash,
            Keys.get_system_disk_io_time_seconds_in_interval(system_id),
            state_disk_io_time_seconds_in_interval)
        disk_io_time_seconds_in_interval = \
            convert_to_float_if_not_none(
                redis_disk_io_time_seconds_in_interval, None)
        system.set_disk_io_time_seconds_in_interval(
            disk_io_time_seconds_in_interval)

        # Load disk_io_time_seconds_total from Redis
        state_disk_io_time_seconds_total = system.disk_io_time_seconds_total
        redis_disk_io_time_seconds_total = self.redis.hget(
            redis_hash, Keys.get_system_disk_io_time_seconds_total(system_id),
            state_disk_io_time_seconds_total)
        disk_io_time_seconds_total = \
            convert_to_float_if_not_none(redis_disk_io_time_seconds_total, None)
        system.set_disk_io_time_seconds_total(disk_io_time_seconds_total)

        # Load last_monitored from Redis
        state_last_monitored = system.last_monitored
        redis_last_monitored = self.redis.hget(
            redis_hash, Keys.get_system_last_monitored(system_id),
            state_last_monitored)
        last_monitored = convert_to_float_if_not_none(redis_last_monitored,
                                                      None)
        system.set_last_monitored(last_monitored)

        # Load went_down_at from Redis
        state_went_down_at = system.went_down_at
        redis_went_down_at = self.redis.hget(
            redis_hash, Keys.get_system_went_down_at(system_id),
            state_went_down_at)
        went_down_at = convert_to_float_if_not_none(redis_went_down_at, None)
        system.set_went_down_at(went_down_at)

        self.logger.debug(
            "Restored %s state: _process_cpu_seconds_total=%s, "
            "_process_memory_usage=%s, _virtual_memory_usage=%s, "
            "_open_file_descriptors=%s, _system_cpu_usage=%s, "
            "_system_ram_usage=%s, _system_storage_usage=%s, "
            "_network_transmit_bytes_per_second=%s, "
            "_network_receive_bytes_per_second=%s, "
            "_network_transmit_bytes_total=%s, "
            "_network_receive_bytes_total=%s, "
            "_disk_io_time_seconds_in_interval=%s, "
            "_disk_io_time_seconds_total=%s, _last_monitored=%s, "
            "_went_down_at=%s", system, process_cpu_seconds_total,
            process_memory_usage, virtual_memory_usage, open_file_descriptors,
            system_cpu_usage, system_ram_usage, system_storage_usage,
            network_transmit_bytes_per_second, network_receive_bytes_per_second,
            network_transmit_bytes_total, network_receive_bytes_total,
            disk_io_time_seconds_in_interval, disk_io_time_seconds_total,
            last_monitored, went_down_at)

        return system
예제 #2
0
    def _process_redis_result_store(self, data: Dict) -> None:
        meta_data = data['meta_data']
        system_name = meta_data['system_name']
        system_id = meta_data['system_id']
        parent_id = meta_data['system_parent_id']
        metrics = data['data']

        self.logger.debug(
            'Saving %s state: _process_cpu_seconds_total=%s, '
            '_process_memory_usage=%s, _virtual_memory_usage=%s, '
            '_open_file_descriptors=%s, _system_cpu_usage=%s, '
            '_system_ram_usage=%s, _system_storage_usage=%s, '
            '_network_transmit_bytes_per_second=%s, '
            '_network_receive_bytes_per_second=%s, '
            '_network_receive_bytes_total=%s, '
            '_network_transmit_bytes_total=%s, '
            '_disk_io_time_seconds_total=%s, '
            '_disk_io_time_seconds_in_interval=%s, _went_down_at=%s, '
            '_last_monitored=%s', system_name,
            metrics['process_cpu_seconds_total'],
            metrics['process_memory_usage'], metrics['virtual_memory_usage'],
            metrics['open_file_descriptors'], metrics['system_cpu_usage'],
            metrics['system_ram_usage'], metrics['system_storage_usage'],
            metrics['network_transmit_bytes_per_second'],
            metrics['network_receive_bytes_per_second'],
            metrics['network_receive_bytes_total'],
            metrics['network_transmit_bytes_total'],
            metrics['disk_io_time_seconds_total'],
            metrics['disk_io_time_seconds_in_interval'],
            metrics['went_down_at'], meta_data['last_monitored'])

        self.redis.hset_multiple(
            Keys.get_hash_parent(parent_id), {
                Keys.get_system_process_cpu_seconds_total(system_id):
                str(metrics['process_cpu_seconds_total']),
                Keys.get_system_process_memory_usage(system_id):
                str(metrics['process_memory_usage']),
                Keys.get_system_virtual_memory_usage(system_id):
                str(metrics['virtual_memory_usage']),
                Keys.get_system_open_file_descriptors(system_id):
                str(metrics['open_file_descriptors']),
                Keys.get_system_system_cpu_usage(system_id):
                str(metrics['system_cpu_usage']),
                Keys.get_system_system_ram_usage(system_id):
                str(metrics['system_ram_usage']),
                Keys.get_system_system_storage_usage(system_id):
                str(metrics['system_storage_usage']),
                Keys.get_system_network_transmit_bytes_per_second(system_id):
                str(metrics['network_transmit_bytes_per_second']),
                Keys.get_system_network_receive_bytes_per_second(system_id):
                str(metrics['network_receive_bytes_per_second']),
                Keys.get_system_network_receive_bytes_total(system_id):
                str(metrics['network_receive_bytes_total']),
                Keys.get_system_network_transmit_bytes_total(system_id):
                str(metrics['network_transmit_bytes_total']),
                Keys.get_system_disk_io_time_seconds_total(system_id):
                str(metrics['disk_io_time_seconds_total']),
                Keys.get_system_disk_io_time_seconds_in_interval(system_id):
                str(metrics['disk_io_time_seconds_in_interval']),
                Keys.get_system_went_down_at(system_id):
                str(metrics['went_down_at']),
                Keys.get_system_last_monitored(system_id):
                str(meta_data['last_monitored']),
            })