def test_collect_only_alive_interfaces(self): container = SystemContainer() container.discover_objects() os_obj = container.objects.values().pop() collector = SystemMetricsCollector(object=os_obj) collector.collect() collector.collect() # double collect is needed, because otherwise we do not collect metrics properly # get interfaces info all_interfaces = netifaces.interfaces() alive_interfaces = set() down_interfaces = set() for interface_name, interface in psutil.net_if_stats().iteritems(): if interface.isup: alive_interfaces.add(interface_name) else: down_interfaces.add(interface_name) # check collected_metrics = os_obj.statsd.current net_metrics_found = False for metric in collected_metrics['counter'].keys(): if metric.startswith('system.net.') and '|' in metric: net_metrics_found = True metric_name, label_name = metric.split('|') assert_that(all_interfaces, has_item(label_name)) assert_that(alive_interfaces, has_item(label_name)) assert_that(down_interfaces, not_(has_item(label_name))) assert_that(net_metrics_found, equal_to(True))
def test_flush_aggregation(self): container = SystemContainer() container.discover_objects() os_obj = container.objects.values().pop() collector = SystemMetricsCollector(object=os_obj) collector.collect() collector.collect() # double collect is needed, because otherwise we do not collect metrics properly flush = collector.statsd.flush() for type in flush['metrics'].keys(): # e.g. 'timer', 'counter', 'gauge', 'average' for key in flush['metrics'][type].keys(): # Make sure there is only one item per item in the flush. assert_that(len(flush['metrics'][type][key]), equal_to(1))