def create_category(self, tag, args, norm=False): value = None params = None if type(args) == type( str() ): value = args params = [tag, args] else: value = args[0] params = [tag] + list(args) functions = [] norm_coef_f = lambda entity, functions: max([f(entity) for f in functions]) norm_f = lambda entity, f, norm_coef_f: f(entity)/norm_coef_f(entity) for opt in self.opts: f = self.create_f(*params, opt=opt) functions.append(f) self.columns.append(("{} {}".format(value, opt), f)) if norm: norm_coef_f = p(norm_coef_f, functions=functions) for opt in self.opts: f = self.create_f(*params, opt=opt) f_n = p(norm_f, f=f, norm_coef_f=norm_coef_f) self.columns.append(("{} norm {}".format(value, opt), f_n))
def test_jenkins(version): with run_service("jenkins", buildargs={ "JENKINS_VERSION": version, "JENKINS_PORT": "8080" }) as jenkins_container: host = container_ip(jenkins_container) config = jenkins_config.substitute(host=host, port=8080) assert wait_for(p(tcp_socket_open, host, 8080), 60), "service not listening on port" assert wait_for( p(http_status, url= "http://{0}:8080/metrics/33DD8B2F1FD645B814993275703F_EE1FD4D4E204446D5F3200E0F6-C55AC14E/ping/" .format(host), status=[200]), 120), "service didn't start" with run_agent(config) as [backend, _, _]: assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "jenkins")), "Didn't get jenkins datapoints"
def connect(self, name, timeout, version=None): print("\nConnecting to %s container ..." % name) assert wait_for( p(container_is_running, self.host_client, name), timeout_seconds=timeout, interval_seconds=2), ("timed out waiting for container %s!" % name) self.container = self.host_client.containers.get(name) self.load_kubeconfig(timeout=timeout) self.client = self.get_client() self.get_bootstrapper() self.name = name self.k8s_version = version
def test_docker_stops_watching_old_containers(): with run_service("nginx") as nginx_container: with run_agent(""" monitors: - type: docker-container-stats """) as [backend, get_output, _]: assert wait_for(p(has_datapoint_with_dim, backend, "container_id", nginx_container.id)), "Didn't get nginx datapoints" nginx_container.stop(timeout=10) time.sleep(3) backend.datapoints.clear() assert ensure_always(lambda: not has_datapoint_with_dim(backend, "container_id", nginx_container.id))
def test_statsd_monitor(): """ Test basic functionality """ with run_agent(""" monitors: - type: collectd/statsd listenAddress: localhost listenPort: 8125 counterSum: true """) as [backend, _, _]: assert wait_for(p(udp_port_open_locally, 8125)), "statsd port never opened!" send_udp_message("localhost", 8125, "statsd.[foo=bar,dim=val]test:1|g") assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "statsd")),\ "Didn't get statsd datapoints" assert wait_for(p(has_datapoint_with_metric_name, backend, "gauge.statsd.test")),\ "Didn't get statsd.test metric" assert wait_for(p(has_datapoint_with_dim, backend, "foo", "bar")),\ "Didn't get foo dimension"
def test_overlapping_filter_with_monitor_type2(): """ Test overlapping filters with different negation. Blacklist is favored """ with Agent.run(""" monitors: - type: memory - type: collectd/uptime metricsToExclude: - metricName: uptime monitorType: collectd/uptime - metricName: uptime negated: true monitorType: collectd/uptime """) as agent: assert wait_for( p(has_datapoint, agent.fake_services, metric_name="memory.used")) assert wait_for( p(has_datapoint, agent.fake_services, metric_name="memory.free")) assert ensure_always( p(has_no_datapoint, agent.fake_services, metric_name="uptime"), 5)
def get_container(self, client, timeout=30): assert wait_for(p(has_pod, self.container_name, namespace=self.namespace), timeout_seconds=timeout), ( 'timed out waiting for "%s" pod!' % self.container_name ) pods = get_all_pods_starting_with_name(self.container_name, namespace=self.namespace) assert len(pods) == 1, 'multiple pods found with name "%s"!\n%s' % ( self.container_name, "\n".join([p.metadata.name for p in pods]), ) self.container = client.containers.get( pods[0].status.container_statuses[0].container_id.replace("docker:/", "") )
def test_chef(base_image, init_system): dockerfile = DOCKERFILES_DIR / f"Dockerfile.{base_image}" with run_init_system_image(base_image, path=REPO_ROOT_DIR, dockerfile=dockerfile) as [cont, backend]: try: # install latest agent run_chef_client(cont) assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "host-metadata") ), "Datapoints didn't come through" finally: print("Agent log:") print_lines(get_agent_logs(cont, init_system))
def test_does_not_set_hostname_on_monitor_if_not_host_specific(): with Agent.run( """ hostname: acmeinc.com monitors: - type: cpu disableHostDimensions: true - type: memory """ ) as agent: assert wait_for( p(has_datapoint, agent.fake_services, dimensions={"host": "acmeinc.com"}, metric_name="memory.utilization") ), "Didn't get overridden hostname in datapoint" assert wait_for(p(has_datapoint, agent.fake_services, metric_name="cpu.utilization")), "Didn't get cpu metric" assert ensure_always( lambda: not has_datapoint( agent.fake_services, metric_name="cpu.utilization", dimensions={"host": "acmeinc.com"} ) ), "Got overridden hostname in cpu datapoint"
def test_interior_globbing(): with run_container(ETCD2_IMAGE, command=ETCD_COMMAND) as etcd: assert wait_for(p(container_cmd_exit_0, etcd, "/etcdctl ls"), 30), "etcd didn't start" create_path(etcd, "/env", "prod") create_path(etcd, "/services/cpu/monitor", "- type: collectd/cpu") create_path(etcd, "/services/signalfx/monitor", "- type: collectd/signalfx-metadata") final_conf = INTERNAL_GLOB_CONFIG.substitute(endpoint="%s:2379" % container_ip(etcd)) with Agent.run(final_conf) as agent: assert wait_for( p(has_event_with_dim, agent.fake_services, "plugin", "signalfx-metadata")), "Datapoints didn't come through" create_path(etcd, "/services/uptime/monitor", "- type: collectd/uptime") assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "plugin", "uptime")), "didn't get uptime datapoints"
def run(config, metrics): with run_service("apache") as apache_container: host = container_ip(apache_container) config = config.format(host=host) assert wait_for(p(tcp_socket_open, host, 80), 60), "service didn't start" with Agent.run(config) as agent: verify(agent, metrics) assert has_datapoint_with_dim( agent.fake_services, "plugin", "apache"), "Didn't get apache datapoints"
def test_basic_service_discovery(): with Agent.run(CONFIG) as agent: with run_service("nginx", name="nginx-basic-discovery"): assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "container_name", "nginx-basic-discovery") ), "Didn't get nginx datapoints" # Let nginx be removed by docker observer and collectd restart time.sleep(5) agent.fake_services.reset_datapoints() assert ensure_always( lambda: not has_datapoint_with_dim(agent.fake_services, "container_name", "nginx-basic-discovery"), 10 )
def test_elasticsearch_with_cluster_option(): with run_service("elasticsearch/6.4.2", environment=ENV) as es_container: host = container_ip(es_container) check_service_status(host) agent_config = AGENT_CONFIG_TEMPLATE.format( host=host, flag="cluster: testCluster1") with Agent.run(agent_config) as agent: assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "plugin", "elasticsearch")), "Didn't get elasticsearch datapoints" assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "plugin_instance", "testCluster1") ), "Cluster name not picked from read callback" # make sure all plugin_instance dimensions were overridden by the cluster option assert not wait_for( p(has_datapoint_with_dim, agent.fake_services, "plugin_instance", "testCluster"), 10 ), "plugin_instance dimension not overridden by cluster option" assert not has_log_message(agent.output.lower(), "error"), "error found in agent output!"
def test_with_default_config_2_0_2(): with run_service("elasticsearch/2.0.2") as es_container: host = container_ip(es_container) check_service_status(host) agent_config = AGENT_CONFIG_TEMPLATE.format(host=host, flag="") with Agent.run(agent_config) as agent: assert wait_for( p(any_metric_has_any_dim_key, agent.fake_services, METADATA.included_metrics, METADATA.dims)), "Didn't get all default dimensions" assert not has_log_message(agent.output.lower(), "error"), "error found in agent output!"
def test_kubernetes_cluster_in_k8s(k8s_cluster): config = """ monitors: - type: kubernetes-cluster """ yamls = [SCRIPT_DIR / "resource_quota.yaml", TEST_SERVICES_DIR / "nginx/nginx-k8s.yaml"] with k8s_cluster.create_resources(yamls): with k8s_cluster.run_agent(agent_yaml=config) as agent: for metric in get_default_monitor_metrics_from_selfdescribe("kubernetes-cluster"): if "replication_controller" in metric: continue assert wait_for(p(has_datapoint, agent.fake_services, metric_name=metric))
def test_sql(image): expected_metrics = get_monitor_metrics_from_selfdescribe( "telegraf/sqlserver") expected_dims = get_monitor_dims_from_selfdescribe("telegraf/sqlserver") with run_container(image, environment={ "ACCEPT_EULA": "Y", "MSSQL_PID": "Developer", "SA_PASSWORD": "******" }) as test_container: host = container_ip(test_container) config = MONITOR_CONFIG.substitute(host=host) assert wait_for(p(tcp_socket_open, host, 1433), 60), "service not listening on port" with Agent.run(config) as agent: assert wait_for( p(has_any_metric_or_dim, agent.fake_services, expected_metrics, expected_dims), timeout_seconds=60 ), "timed out waiting for metrics and/or dimensions!"
def test_conviva_multi_metric(): with Agent.run( dedent(f""" monitors: - type: conviva pulseUsername: {{"#from": "env:CONVIVA_PULSE_USERNAME"}} pulsePassword: {{"#from": "env:CONVIVA_PULSE_PASSWORD"}} metricConfigs: - metricParameter: concurrent_plays - metricParameter: plays """), debug=CONVIVA_DEBUG, ) as agent: assert wait_for( p(has_datapoint_with_metric_name, agent.fake_services, "conviva.concurrent_plays"), 60), "Didn't get conviva datapoints for metric concurrent_plays" assert wait_for( p(has_datapoint_with_metric_name, agent.fake_services, "conviva.plays"), 60), "Didn't get conviva datapoints for metric plays"
def test_health_checker_http(): with run_service("nginx") as nginx_container: host = container_ip(nginx_container) assert wait_for(p(tcp_socket_open, host, 80), 60), "service didn't start" with run_agent( string.Template( dedent( """ monitors: - type: collectd/health-checker host: $host port: 80 path: /nonexistent """ ) ).substitute(host=host) ) as [backend, _, _]: assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "health_checker") ), "Didn't get health_checker datapoints"
def test_snmp(): # snmp-simulator source is available at: https://github.com/xeemetric/snmp-simulator # the hard coded uptime OID used in this test was fetched using the following command # $ snmpget -On -v2c -c public <snmp simulator host>:161 system.sysUpTime.0 with run_container("xeemetric/snmp-simulator") as test_container: host = container_ip(test_container) config = MONITOR_CONFIG.substitute(host=host) with Agent.run(config) as agent: assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "plugin", "telegraf-snmp")), "didn't get database io datapoints"
def patch_resource(body, api_client, namespace=None, timeout=K8S_CREATE_TIMEOUT): name = body["metadata"]["name"] kind = body["kind"] # The namespace in the resource body always takes precidence namespace = body.get("metadata", {}).get("namespace", namespace) resource = getattr(api_client, "patch_namespaced_" + camel_case_to_snake_case(kind))( name=name, body=body, namespace=namespace ) assert wait_for( p(has_resource, name, kind, api_client, namespace=namespace), timeout_seconds=timeout ), 'timed out waiting for %s "%s" to be patched!' % (kind, name) return resource
def test_kubelet_stats_extra(k8s_cluster): config = f""" monitors: - type: kubelet-stats kubeletAPI: skipVerify: true authType: serviceAccount extraMetrics: - {CUSTOM_METRIC} """ with k8s_cluster.run_agent(agent_yaml=config) as agent: assert wait_for(p(has_datapoint, agent.fake_services, metric_name=CUSTOM_METRIC))
def test_docker_envvar_dimensions(): with run_service("nginx", environment={"APP": "myserver"}): with run_agent(""" monitors: - type: docker-container-stats envToDimensions: APP: app """) as [backend, _, _]: assert wait_for( p(has_datapoint_with_dim, backend, "app", "myserver")), "Didn't get datapoint with service app"
def test_docker_observer_use_host_bindings(): with run_service("nginx", name="nginx-non-host-binding", labels={"mylabel": "non-host-binding"}): with run_service( "nginx", name="nginx-with-host-binding", labels={"mylabel": "with-host-binding"}, ports={"80/tcp": ("127.0.0.1", 0)}, ) as container_bind: with Agent.run( HOST_BINDING_CONFIG.substitute( port=container_bind.attrs["NetworkSettings"]["Ports"] ["80/tcp"][0]["HostPort"])) as agent: assert not wait_for( p(has_datapoint_with_dim, agent.fake_services, "mydim", "non-host-binding")), "Didn't get custom label dimension" assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "mydim", "with-host-binding") ), "Didn't get custom label dimension"
def test_interior_globbing(): with run_container(ETCD2_IMAGE, command=ETCD_COMMAND) as etcd: assert wait_for(p(container_cmd_exit_0, etcd, "/etcdctl ls"), 5), "etcd didn't start" create_path(etcd, "/env", "prod") create_path(etcd, "/services/cpu/monitor", "- type: collectd/cpu") create_path(etcd, "/services/signalfx/monitor", "- type: collectd/signalfx-metadata") final_conf = internal_glob_config.substitute(endpoint="%s:2379" % container_ip(etcd)) with run_agent(final_conf) as [backend, get_output, _]: assert wait_for( p(has_event_with_dim, backend, "plugin", "signalfx-metadata")), "Datapoints didn't come through" create_path(etcd, "/services/uptime/monitor", "- type: collectd/uptime") assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "uptime")), "didn't get uptime datapoints"
def test_docker_label_dimensions(): with run_service("nginx", labels={"app": "myserver"}): with run_agent(""" monitors: - type: docker-container-stats labelsToDimensions: app: service """) as [backend, _, _]: assert wait_for( p(has_datapoint_with_dim, backend, "service", "myserver")), "Didn't get datapoint with service dim"
def test_jenkins(version): with run_service("jenkins", buildargs={"JENKINS_VERSION": version, "JENKINS_PORT": "8080"}) as jenkins_container: host = container_ip(jenkins_container) config = dedent( f""" monitors: - type: collectd/jenkins host: {host} port: 8080 metricsKey: {METRICS_KEY} """ ) assert wait_for(p(tcp_socket_open, host, 8080), 60), "service not listening on port" assert wait_for( p(http_status, url=f"http://{host}:8080/metrics/{METRICS_KEY}/ping/", status=[200]), 120 ), "service didn't start" with Agent.run(config) as agent: assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "plugin", "jenkins") ), "Didn't get jenkins datapoints"
def test_basic_service_discovery(): with run_agent(config) as [backend, get_output, _]: with run_service("nginx", name="nginx-discovery") as nginx_container: assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "nginx")), "Didn't get nginx datapoints" # Let nginx be removed by docker observer and collectd restart time.sleep(5) backend.datapoints.clear() assert ensure_always( lambda: not has_datapoint_with_dim(backend, "plugin", "nginx"), 10) assert not has_log_message(get_output(), "error")
def create_daemonset(body, namespace=None, timeout=K8S_CREATE_TIMEOUT): name = body["metadata"]["name"] api = api_client_from_version(body["apiVersion"]) daemonset = create_resource(body, api, namespace=namespace, timeout=timeout) assert wait_for( p(daemonset_is_ready, name, namespace=namespace), timeout_seconds=timeout), ( 'timed out waiting for daemonset "%s" to be ready!' % name) return daemonset
def test_all_kafka_monitors(version): with run_kafka(version) as kafka: kafka_host = container_ip(kafka) with run_container( kafka.image.id, environment={ "JMX_PORT": "8099", "START_AS": "producer", "KAFKA_BROKER": "%s:9092" % (kafka_host, ) }, ) as kafka_producer: kafkaproducerhost = container_ip(kafka_producer) assert wait_for(p(tcp_socket_open, kafkaproducerhost, 8099), 60), "kafka producer jmx didn't start" with run_container( kafka.image.id, environment={ "JMX_PORT": "9099", "START_AS": "consumer", "KAFKA_BROKER": "%s:9092" % (kafka_host, ) }, ) as kafka_consumer: kafkaconsumerhost = container_ip(kafka_consumer) assert wait_for(p(tcp_socket_open, kafkaconsumerhost, 9099), 60), "kafka consumer jmx didn't start" with Agent.run( textwrap.dedent(""" monitors: - type: collectd/kafka host: {0} port: 7099 clusterName: testCluster - type: collectd/kafka_producer host: {1} port: 8099 - type: collectd/kafka_consumer host: {2} port: 9099 """.format(kafka_host, kafkaproducerhost, kafkaconsumerhost))) as agent: assert wait_for( p(has_datapoint_with_metric_name, agent.fake_services, "gauge.kafka-active-controllers"), timeout_seconds=60, ), "Didn't get kafka datapoints" assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "cluster", "testCluster"), timeout_seconds=60 ), "Didn't get cluster dimension from kafka datapoints" assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "client-id", "console-producer"), timeout_seconds=60, ), "Didn't get client-id dimension from kafka_producer datapoints" assert wait_for( p(has_datapoint_with_dim, agent.fake_services, "client-id", "consumer-1"), timeout_seconds=60 ), "Didn't get client-id dimension from kafka_consumer datapoints"
def test_puppet_on_windows(puppet_version): run_win_puppet_setup(puppet_version) with ensure_fake_backend() as backend: try: monitors = [{"type": "host-metadata"}] run_win_puppet_agent(backend, monitors, INITIAL_VERSION, STAGE) assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "host-metadata")), "Datapoints didn't come through" # upgrade agent run_win_puppet_agent(backend, monitors, UPGRADE_VERSION, STAGE) backend.reset_datapoints() assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "host-metadata")), "Datapoints didn't come through" # downgrade agent run_win_puppet_agent(backend, monitors, INITIAL_VERSION, STAGE) backend.reset_datapoints() assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "host-metadata")), "Datapoints didn't come through" # change agent config monitors = [{"type": "internal-metrics"}] run_win_puppet_agent(backend, monitors, INITIAL_VERSION, STAGE) backend.reset_datapoints() assert wait_for( p(has_datapoint_with_metric_name, backend, "sfxagent.datapoints_sent") ), "Didn't get internal metric datapoints" finally: print("\nDatapoints received:") for dp in backend.datapoints: print_dp_or_event(dp) print("\nEvents received:") for event in backend.events: print_dp_or_event(event) print(f"\nDimensions set: {backend.dims}")
def test_docker_observer_labels_multiple_monitors_per_port(): """ Test that we can configure multiple monitors per port using labels """ with run_agent( dedent(""" observers: - type: docker """)) as [backend, _, _]: with run_service( "nginx", name="nginx-multi-monitors", labels={ "agent.signalfx.com.monitorType.80": "collectd/nginx", "agent.signalfx.com.config.80.intervalSeconds": "1", "agent.signalfx.com.config.80.extraDimensions": "{app: nginx}", "agent.signalfx.com.monitorType.80-nginx2": "collectd/nginx", "agent.signalfx.com.config.80-nginx2.intervalSeconds": "1", "agent.signalfx.com.config.80-nginx2.extraDimensions": "{app: other}", }, ): assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "nginx")), "Didn't get nginx datapoints" assert wait_for(p(has_datapoint_with_dim, backend, "app", "nginx")), "Didn't get extra dims" assert wait_for(p(has_datapoint_with_dim, backend, "app", "other")), "Didn't get extra dims" # Let nginx be removed by docker observer and collectd restart time.sleep(5) backend.reset_datapoints() assert ensure_always( lambda: not has_datapoint_with_dim(backend, "container_name", "nginx-multi-monitors"), 10)
def create_f(self, *args, **kwargs): is_meta = False (tag, value, title, value_1, value_2, op) = [None for i in range(6)] opt = kwargs['opt'] if len(args) == 2: (tag, value) = args elif len(args) == 5: is_meta = True (tag, title, value_1, value_2, op) = args results_obj = lambda entity, opt: entity.instances['clang'][opt].results tag_f = lambda entity, tag, r: r(entity)[tag].parsed_data mother_f = lambda entity, value, tag: tag(entity)[value] r = p(results_obj, opt=opt) e = p(tag_f, tag=tag, r=r) f = None if is_meta: meta_f = lambda entity, value_1, value_2: op(value_1(entity), value_2(entity)) f1 = p(mother_f, tag=e, value=value_1) f2 = p(mother_f, tag=e, value=value_2) f = p(meta_f, value_1=f1, value_2=f2) else: f = p(mother_f, tag=e, value=value) return f
def expression_sensitivity_analysis(method, model, expression_profile, metrics, condition_exchanges=None, condition_knockouts=None, growth_rates=None, growth_rates_std=None, growth_reaction=None, **kwargs): assert isinstance(model, Model) assert isinstance(expression_profile, ExpressionProfile) if condition_exchanges is None: condition_exchanges = {} if condition_knockouts is None: condition_knockouts = {} data_frames = [DataFrame(columns=["x", "y", "condition"]) for _ in metrics] for condition in expression_profile.conditions: with TimeMachine() as tm: for exchange_condition, ex in six.iteritems(condition_exchanges): if exchange_condition == condition: tm(do=p(setattr, ex, "lower_bound", -10), undo=p(setattr, ex, "lower_bound", ex.lower_bound)) else: tm(do=p(setattr, ex, "lower_bound", 0), undo=p(setattr, ex, "lower_bound", ex.lower_bound)) for knockout in condition_knockouts.get(condition, []): model.reactions.get_by_id(knockout).knock_out(tm) if growth_reaction is not None: growth_rate = growth_rates[condition] growth_std = growth_rates_std[condition] if isinstance(growth_reaction, str): growth_reaction = model.reactions.get_by_id(growth_reaction) tm(do=p(setattr, growth_reaction, "lower_bound", growth_rate-growth_std), undo=p(setattr, growth_reaction, "lower_bound", growth_reaction.upper_bound)) tm(do=p(setattr, growth_reaction, "upper_bound", growth_rate+growth_std), undo=p(setattr, growth_reaction, "upper_bound", growth_reaction.upper_bound)) min_val, max_val = expression_profile.minmax(condition) binwidth = expression_profile.binwidth(condition) for i, exp in enumerate(range(min_val, max_val, binwidth)): res = method(model, expression_profile=expression_profile, condition=condition, cutoff=exp, **kwargs) for j, metric in enumerate(metrics): data_frames[j].loc[i] = [exp, getattr(res, metric), metric] for i, metric in enumerate(metrics): plotting.line(data_frames[i], x=metric, y="cutoff")
def sum_of_squares(x): return sum(xi ** 2 for xi in v) def difference_quotient(f,x,h): return (f(x+h) - f(x))/h def square(x): return x * x def derivative(x): return 2 * x derivative_estimate = p(difference_quotient, square, h=.00001) x = range(-10,10) # plt.scatter(x, [derivative(y) for y in x]) # plt.scatter(x, [derivative_estimate(y) for y in x]) # plt.legend(loc=9) # plt.show() #ith partial dq def partial_difference_quotient(f,v,i,h): w = [vj + (h if j == i else 0) for j, v_j in enumerate(v)] return (f(w)-f(v))/h def estimate_gradient(f, v, h=.00001): return [partial_difference_quotient(f,v,i,h)