Example #1
0
    def get_reply_latency(self,
                          ip_src,
                          ip_dst,
                          default: int = 0,
                          mode: str = None):
        """
        Gets the minimum and the maximum reply latency for all the connections of a specific IP.

        :param ip_src: The source IP for which to retrieve the reply latency.
        :param ip_dst: The destination IP for which to retrieve the reply latency.
        :param default: The default value to return if no latency could be calculated.
        :param mode: either "local" or "public"
        :return minimum and maximum latency
        """
        if not mode:
            mode = Util.get_network_mode(ip_src, ip_dst)
        minimum = {"local": 900, "public": 3000}

        if default != 0:
            minimum[mode] = default

        result = self.statistics.process_db_query\
                ("SELECT minLatency, maxLatency FROM ip_statistics WHERE ipAddress in ('{0}, {1}');".
                 format(ip_src, ip_dst))

        min_latency = minimum[mode]
        max_latency = minimum[mode]

        for ip in result:
            # retrieve minimum latency
            if ip[0]:
                retrieved = ip[0]
            else:
                retrieved = np.median(self.all_min_latencies)
            min_latency = min(min_latency, retrieved)

            # retrieve maximum latency
            if ip[1]:
                retrieved = ip[1]
            else:
                retrieved = np.median(self.all_max_latencies)
            max_latency = min(max_latency, retrieved)

        min_latency = int(
            min_latency) * 10**-6  # convert from micro to seconds
        max_latency = int(max_latency) * 10**-6
        return min_latency, max_latency
Example #2
0
    def get_remaining_bandwidth(self,
                                timestamp: int = 0,
                                ip_src: str = "",
                                ip_dst: str = "",
                                custom_max_bandwidth: float = 0,
                                custom_bandwidth_local: float = 0,
                                custom_bandwidth_public: float = 0):
        """
        This function calculates the remaining bandwidth based on the maximum bandwidth available and the kbytes already
        sent inside the interval corresponding to the timestamp given.

        !!! custom_max_bandwidth is mutually exclusive to custom_bandwidth_local and/or custom_bandwidth_public
        :param timestamp: the timestamp of the current packet
        :param ip_src: the source IP
        :param ip_dst: the destination IP
        :param custom_max_bandwidth: maximum bandwidth to be set as a hard limit, discarding the pcaps bandwidth
        :param custom_bandwidth_local: bandwidth minimum for local traffic
        :param custom_bandwidth_public: bandwidth minimum for public traffic
        :return: the remaining bandwidth in kbyte/s
        """
        mode = Util.get_network_mode(ip_src, ip_dst)

        if custom_max_bandwidth != 0:
            bandwidth = custom_max_bandwidth
        else:
            bandwidth = self.statistics.get_kbyte_rate(
                mode, custom_bandwidth_local, custom_bandwidth_public)

        remaining_bandwidth = bandwidth

        current_table = self.statistics.stats_db.get_current_interval_statistics_table(
        )
        kbytes_sent, interval = self.statistics.get_interval_stat(
            table_name=current_table, field="kbytes", timestamp=timestamp)
        if not kbytes_sent:
            kbytes_sent = 0
        kbytes_sent = kbytes_sent

        duration = self.statistics.get_current_interval_len()
        used_bandwidth = float((kbytes_sent * 1000) / duration)
        remaining_bandwidth -= used_bandwidth
        return remaining_bandwidth, interval