From 583ca0059efeb78cf940253da093bea052d169c0 Mon Sep 17 00:00:00 2001 From: Tristan Michelet Date: Tue, 13 May 2014 15:52:23 +0000 Subject: [PATCH 1/8] HAProxy: gauge missing statuses with zero --- checks.d/haproxy.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/checks.d/haproxy.py b/checks.d/haproxy.py index 59a8aa6144..eb04104934 100644 --- a/checks.d/haproxy.py +++ b/checks.d/haproxy.py @@ -19,6 +19,10 @@ class Services(object): BACKEND = 'BACKEND' FRONTEND = 'FRONTEND' ALL = (BACKEND, FRONTEND) + ALL_STATUSES = ( + 'up', 'up_1/3', 'up_2/3', 'open', 'no_check', 'down', 'down_1/2', + 'maint', 'nolb' + ) class HAProxy(AgentCheck): def __init__(self, name, init_config, agentConfig): @@ -195,10 +199,10 @@ def _process_status_metric(self, hosts_statuses, collect_status_metrics_by_host) service, status = host_status status = status.lower() - tags = ['status:%s' % status, 'service:%s' % service] + tags = ['service:%s' % service] if collect_status_metrics_by_host: tags.append('backend:%s' % hostname) - self.gauge("haproxy.count_per_status", count, tags=tags) + self._gauge_all_statuses("haproxy.count_per_status", count, status, tags=tags) if 'up' in status: agg_statuses[service]['available'] += count @@ -210,6 +214,13 @@ def _process_status_metric(self, hosts_statuses, collect_status_metrics_by_host) tags = ['status:%s' % status, 'service:%s' % service] self.gauge("haproxy.count_per_status", count, tags=tags) + def _gauge_all_statuses(self, metric_name, count, status, tags): + self.gauge(metric_name, count, tags + ['status:%s' % status]) + for state in Services.ALL_STATUSES: + if state != status: + self.gauge(metric_name, 0, tags + ['status:%s' % state]) + + def _process_metrics(self, data, url): """ Data is a dictionary related to one host From f4254e3a7dbf7a82a39944de1731f8768fb4aeef Mon Sep 17 00:00:00 2001 From: tristan Date: Fri, 16 May 2014 10:50:36 -0400 Subject: [PATCH 2/8] Remove unused imports and statuses --- checks.d/haproxy.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/checks.d/haproxy.py b/checks.d/haproxy.py index eb04104934..3e3e0b4710 100644 --- a/checks.d/haproxy.py +++ b/checks.d/haproxy.py @@ -1,9 +1,7 @@ -import urlparse import urllib2 -import socket from checks import AgentCheck -from util import json, headers +from util import headers import time @@ -20,8 +18,7 @@ class Services(object): FRONTEND = 'FRONTEND' ALL = (BACKEND, FRONTEND) ALL_STATUSES = ( - 'up', 'up_1/3', 'up_2/3', 'open', 'no_check', 'down', 'down_1/2', - 'maint', 'nolb' + 'up', 'open', 'no_check', 'down', 'maint', 'nolb' ) class HAProxy(AgentCheck): From 93b6954e7140f9eda8dc2d44313d0e91a16d80c6 Mon Sep 17 00:00:00 2001 From: tristan Date: Fri, 16 May 2014 10:56:54 -0400 Subject: [PATCH 3/8] Bugfix: update is for == --- checks.d/haproxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checks.d/haproxy.py b/checks.d/haproxy.py index 3e3e0b4710..23decae1dd 100644 --- a/checks.d/haproxy.py +++ b/checks.d/haproxy.py @@ -183,7 +183,7 @@ def _should_process(self, data_dict, collect_aggregates_only): if self._is_aggregate(data_dict): return True return False - elif data_dict['svname'] is Services.BACKEND: + elif data_dict['svname'] == Services.BACKEND: return False return True From 52f0317f86c3da4d2f23a9f401f1e67e1b8287a8 Mon Sep 17 00:00:00 2001 From: tristan Date: Fri, 16 May 2014 12:20:36 -0400 Subject: [PATCH 4/8] Update haproxy events title --- checks.d/haproxy.py | 4 ++-- emitter.py | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/checks.d/haproxy.py b/checks.d/haproxy.py index 23decae1dd..2acd183a11 100644 --- a/checks.d/haproxy.py +++ b/checks.d/haproxy.py @@ -271,13 +271,13 @@ def _process_event(self, data, url): def _create_event(self, status, hostname, lastchg, service_name): if status == "DOWN": alert_type = "error" - title = "HAProxy %s front-end reported %s %s" % (service_name, hostname, status) + title = "HAProxy %s reported %s %s" % (service_name, hostname, status) else: if status == "UP": alert_type = "success" else: alert_type = "info" - title = "HAProxy %s front-end reported %s back and %s" % (service_name, hostname, status) + title = "HAProxy %s reported %s back and %s" % (service_name, hostname, status) return { 'timestamp': int(time.time() - lastchg), diff --git a/emitter.py b/emitter.py index c7ff1acb36..a7832e9ef3 100644 --- a/emitter.py +++ b/emitter.py @@ -18,7 +18,7 @@ def get_http_library(proxy_settings, use_forwarder): else: # Python version < 2.6.3 import urllib2proxy as urllib2 - return urllib2 + return urllib2 def post_headers(agentConfig, payload): return { @@ -52,6 +52,7 @@ def http_emitter(message, log, agentConfig): urllib2 = get_http_library(proxy_settings, agentConfig['use_forwarder']) try: + import pdb; pdb.set_trace() request = urllib2.Request(url, zipped, headers) # Do the request, log any errors opener = get_opener(log, proxy_settings, agentConfig['use_forwarder'], urllib2) @@ -75,13 +76,13 @@ def get_opener(log, proxy_settings, use_forwarder, urllib2): log.debug("Not using proxy settings") else: proxy_url = '%s:%s' % (proxy_settings['host'], proxy_settings['port']) - + if proxy_settings.get('user') is not None: proxy_auth = proxy_settings['user'] if proxy_settings.get('password') is not None: proxy_auth = '%s:%s' % (proxy_auth, proxy_settings['password']) proxy_url = '%s@%s' % (proxy_auth, proxy_url) - + proxy = {'https': proxy_url} log.debug("Using proxy settings %s" % proxy) From 422b30211247ef5ca8c58ddd3044b1bf98469c8c Mon Sep 17 00:00:00 2001 From: tristan Date: Fri, 16 May 2014 12:20:48 -0400 Subject: [PATCH 5/8] Add open to available statuses --- checks.d/haproxy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checks.d/haproxy.py b/checks.d/haproxy.py index 2acd183a11..5e28c03897 100644 --- a/checks.d/haproxy.py +++ b/checks.d/haproxy.py @@ -201,7 +201,7 @@ def _process_status_metric(self, hosts_statuses, collect_status_metrics_by_host) tags.append('backend:%s' % hostname) self._gauge_all_statuses("haproxy.count_per_status", count, status, tags=tags) - if 'up' in status: + if 'up' in status or 'open' in status: agg_statuses[service]['available'] += count if 'down' in status or 'maint' in status or 'nolb' in status: agg_statuses[service]['unavailable'] += count From dc4cd9c36cbbdde1ff1e3ec9d06389a592f3131a Mon Sep 17 00:00:00 2001 From: tristan Date: Fri, 16 May 2014 12:24:09 -0400 Subject: [PATCH 6/8] HAPROXY Process events and host metrics only for needed services --- checks.d/haproxy.py | 13 +++++++------ emitter.py | 1 - 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/checks.d/haproxy.py b/checks.d/haproxy.py index 5e28c03897..73750e35a6 100644 --- a/checks.d/haproxy.py +++ b/checks.d/haproxy.py @@ -120,16 +120,17 @@ def _process_data( self._update_data_dict(data_dict, back_or_front) - self._update_hosts_statuses_if_needed( - collect_status_metrics, collect_status_metrics_by_host, - data_dict, hosts_statuses - ) if self._should_process(data_dict, collect_aggregates_only): + # update status + self._update_hosts_statuses_if_needed( + collect_status_metrics, collect_status_metrics_by_host, + data_dict, hosts_statuses + ) # Send the list of data to the metric and event callbacks self._process_metrics(data_dict, url) - if process_events: - self._process_event(data_dict, url) + if process_events: + self._process_event(data_dict, url) if collect_status_metrics: self._process_status_metric(hosts_statuses, collect_status_metrics_by_host) diff --git a/emitter.py b/emitter.py index a7832e9ef3..65bf0a9db5 100644 --- a/emitter.py +++ b/emitter.py @@ -52,7 +52,6 @@ def http_emitter(message, log, agentConfig): urllib2 = get_http_library(proxy_settings, agentConfig['use_forwarder']) try: - import pdb; pdb.set_trace() request = urllib2.Request(url, zipped, headers) # Do the request, log any errors opener = get_opener(log, proxy_settings, agentConfig['use_forwarder'], urllib2) From 7062ae8778c4640e26bca2971ed1ac3682cfec99 Mon Sep 17 00:00:00 2001 From: tristan Date: Fri, 16 May 2014 13:32:13 -0400 Subject: [PATCH 7/8] Remove HAProxy from event's title --- checks.d/haproxy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checks.d/haproxy.py b/checks.d/haproxy.py index 73750e35a6..66574cd841 100644 --- a/checks.d/haproxy.py +++ b/checks.d/haproxy.py @@ -272,13 +272,13 @@ def _process_event(self, data, url): def _create_event(self, status, hostname, lastchg, service_name): if status == "DOWN": alert_type = "error" - title = "HAProxy %s reported %s %s" % (service_name, hostname, status) + title = "%s reported %s %s" % (service_name, hostname, status) else: if status == "UP": alert_type = "success" else: alert_type = "info" - title = "HAProxy %s reported %s back and %s" % (service_name, hostname, status) + title = "%s reported %s back and %s" % (service_name, hostname, status) return { 'timestamp': int(time.time() - lastchg), From cca8a44b941283fabef42801af6cade23b957218 Mon Sep 17 00:00:00 2001 From: Tristan Michelet Date: Mon, 19 May 2014 19:23:17 +0000 Subject: [PATCH 8/8] Update haproxy test --- tests/test_haproxy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_haproxy.py b/tests/test_haproxy.py index fe92c7d150..9d20ed1a3f 100644 --- a/tests/test_haproxy.py +++ b/tests/test_haproxy.py @@ -89,7 +89,7 @@ def testCheck(self): self.assertTrue(len(metrics) > 0) self.assertEquals(len([t for t in metrics - if t[0] == "haproxy.backend.bytes.in_rate"]), 4, metrics) + if t[0] == "haproxy.backend.bytes.in_rate"]), 3, metrics) self.assertEquals(len([t for t in metrics if t[0] == "haproxy.frontend.session.current"]), 1, metrics) @@ -151,7 +151,7 @@ def testOpenConfig(self): self.assertTrue(len(metrics) > 0) self.assertEquals(len([t for t in metrics - if t[0] == "haproxy.backend.bytes.in_rate"]), 4, metrics) + if t[0] == "haproxy.backend.bytes.in_rate"]), 3, metrics) self.assertEquals(len([t for t in metrics if t[0] == "haproxy.frontend.session.current"]), 1, metrics)