def __handle_stat(self, stat, id, topic, container_name): try: read_dt = parser.parse(stat["read"]) timestamp = int((time.mktime(read_dt.timetuple()) + (read_dt.microsecond / 1000000.0)) * 1000) memory_usage = float(stat["memory_stats"]["usage"]) / float(stat["memory_stats"]["limit"]) Metric.create(topic=topic, container=container_name, timestamp=timestamp, name="memory", value=memory_usage) # Calculate CPU usage. The docker API returns the number of cycles consumed # by the container and the number of cycles consumed by the system. We need # to take the difference over time and divide them to retrieve the usage # percentage. total_usage = float(stat["cpu_stats"]["cpu_usage"]["total_usage"]) system_usage = float(stat["cpu_stats"]["system_cpu_usage"]) if id in self.id_to_cpu: usage_diff = total_usage - self.id_to_cpu[id]["total"] system_diff = system_usage - self.id_to_cpu[id]["system"] if usage_diff >= 0: usage_pct = usage_diff / system_diff else: usage_pct = 0.0 Metric.create(topic=topic, container=container_name, timestamp=timestamp, name="cpu", value=usage_pct) self.id_to_cpu[id] = {"total": total_usage, "system": system_usage} except: # We don't want to kill the stat thread, and we don't really mind # if some statistics aren't saved properly pass
def get_metrics(registration_id): # Collect and return the following metrics: # - Current number of containers running for this registration # - Average CPU usage for this registration # - Average memory usage for this registration # - Average processing time for this registration # - Number of events per day for the last week result = {} registration = Registration.get(Registration.id == registration_id) stats = Metric.select( Metric.name, fn.AVG(Metric.value).alias('average') ).where( Metric.topic == registration.topic, Metric.container == registration.container ).group_by(Metric.name) # Add to the result object for stat in stats: result[stat.name] = stat.average # Use the beginning of the day as a starting point (for full # days) today_dt = datetime.datetime.combine( datetime.date.today(), datetime.time.min ) today = int(time.mktime(today_dt.timetuple()) * 1000) # Get the starting point (one week ago) one_week_ago = today - (7 * MILLISECONDS_IN_DAY) events = Event.select( # In order to query by date intervals, we need to divide by one day # worth of milliseconds, round to the nearest whole number, then # multiply the milliseconds back in. This will create intervals and # normalize the returned values. (fn.ROUND(Event.timestamp/MILLISECONDS_IN_DAY) * MILLISECONDS_IN_DAY).alias('date'), fn.COUNT(Event.id).alias('events') ).where( Event.topic == registration.topic, Event.container == registration.container, Event.timestamp >= one_week_ago ).group_by('date') # Add to the result object result['events'] = {} for event in events: result['events'][int(event.date)] = event.events avg_duration = Event.select(fn.AVG(Event.duration)).where( Event.topic == registration.topic, Event.container == registration.container ).scalar() result['duration'] = avg_duration result['containers'] = docker_agent.container_count(registration.topic, registration.container) return result
def get_metrics(registration_id): # Collect and return the following metrics: # - Current number of containers running for this registration # - Average CPU usage for this registration # - Average memory usage for this registration # - Average processing time for this registration # - Number of events per day for the last week result = {} registration = Registration.get(Registration.id == registration_id) stats = Metric.select( Metric.name, fn.AVG(Metric.value).alias('average')).where( Metric.topic == registration.topic, Metric.container == registration.container).group_by(Metric.name) # Add to the result object for stat in stats: result[stat.name] = stat.average # Use the beginning of the day as a starting point (for full # days) today_dt = datetime.datetime.combine(datetime.date.today(), datetime.time.min) today = int(time.mktime(today_dt.timetuple()) * 1000) # Get the starting point (one week ago) one_week_ago = today - (7 * MILLISECONDS_IN_DAY) events = Event.select( # In order to query by date intervals, we need to divide by one day # worth of milliseconds, round to the nearest whole number, then # multiply the milliseconds back in. This will create intervals and # normalize the returned values. (fn.ROUND(Event.timestamp / MILLISECONDS_IN_DAY) * MILLISECONDS_IN_DAY ).alias('date'), fn.COUNT(Event.id).alias('events')).where( Event.topic == registration.topic, Event.container == registration.container, Event.timestamp >= one_week_ago).group_by('date') # Add to the result object result['events'] = {} for event in events: result['events'][int(event.date)] = event.events avg_duration = Event.select(fn.AVG(Event.duration)).where( Event.topic == registration.topic, Event.container == registration.container).scalar() result['duration'] = avg_duration result['containers'] = docker_agent.container_count( registration.topic, registration.container) return result
def __handle_stat(self, stat, id, topic, container_name): try: read_dt = parser.parse(stat['read']) timestamp = int((time.mktime(read_dt.timetuple()) + (read_dt.microsecond / 1000000.0)) * 1000) memory_usage = float(stat['memory_stats']['usage']) / float( stat['memory_stats']['limit']) Metric.create(topic=topic, container=container_name, timestamp=timestamp, name='memory', value=memory_usage) # Calculate CPU usage. The docker API returns the number of cycles consumed # by the container and the number of cycles consumed by the system. We need # to take the difference over time and divide them to retrieve the usage # percentage. total_usage = float(stat['cpu_stats']['cpu_usage']['total_usage']) system_usage = float(stat['cpu_stats']['system_cpu_usage']) if id in self.id_to_cpu: usage_diff = total_usage - self.id_to_cpu[id]['total'] system_diff = system_usage - self.id_to_cpu[id]['system'] if usage_diff >= 0: usage_pct = usage_diff / system_diff else: usage_pct = 0.0 Metric.create(topic=topic, container=container_name, timestamp=timestamp, name='cpu', value=usage_pct) self.id_to_cpu[id] = {'total': total_usage, 'system': system_usage} except: # We don't want to kill the stat thread, and we don't really mind # if some statistics aren't saved properly pass