Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[haproxy] Count available/unavailable backends #1915

Merged
merged 2 commits into from
Sep 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions checks.d/haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ def _process_data(self, data, collect_aggregates_only, process_events, url=None,
services_incl_filter=services_incl_filter,
services_excl_filter=services_excl_filter
)
self._process_backend_hosts_metric(
self.hosts_statuses,
services_incl_filter=services_incl_filter,
services_excl_filter=services_excl_filter
)

return data

Expand Down Expand Up @@ -247,6 +252,38 @@ def _tag_match_patterns(self, tag, filters):
return True
return False

def _process_backend_hosts_metric(self, hosts_statuses, services_incl_filter=None,
services_excl_filter=None):
agg_statuses = defaultdict(lambda: {'available': 0, 'unavailable': 0})
for host_status, count in hosts_statuses.iteritems():
try:
service, hostname, status = host_status
except Exception:
service, status = host_status

if self._is_service_excl_filtered(service, services_incl_filter, services_excl_filter):
continue
status = status.lower()
if 'up' in status:
agg_statuses[service]['available'] += count
elif 'down' in status or 'maint' in status or 'nolb' in status:
agg_statuses[service]['unavailable'] += count
else:
# create the entries for this service anyway
agg_statuses[service]

for service in agg_statuses:
tags = ['service:%s' % service]
self.gauge(
'haproxy.backend_hosts',
agg_statuses[service]['available'],
tags=tags + ['available:true'])
self.gauge(
'haproxy.backend_hosts',
agg_statuses[service]['unavailable'],
tags=tags + ['available:false'])
return agg_statuses

def _process_status_metric(self, hosts_statuses, collect_status_metrics_by_host,
services_incl_filter=None, services_excl_filter=None):
agg_statuses = defaultdict(lambda: {'available': 0, 'unavailable': 0})
Expand Down
14 changes: 14 additions & 0 deletions tests/checks/integration/test_haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def test_count_per_statuses(self):
b,i-1,0,0,0,1,,1,1,0,,0,,0,0,0,0,UP,1,1,0,0,1,1,30,,1,3,1,,70,,2,0,,1,1,,0,,,,,,,0,,,,0,0,
b,i-2,0,0,1,1,,1,1,0,,0,,0,0,0,0,UP,1,1,0,0,0,1,0,,1,3,2,,71,,2,0,,1,1,,0,,,,,,,0,,,,0,0,
b,i-3,0,0,0,1,,1,1,0,,0,,0,0,0,0,UP,1,1,0,0,0,1,0,,1,3,3,,70,,2,0,,1,1,,0,,,,,,,0,,,,0,0,
b,i-4,0,0,0,1,,1,1,0,,0,,0,0,0,0,DOWN,1,1,0,0,0,1,0,,1,3,3,,70,,2,0,,1,1,,0,,,,,,,0,,,,0,0,
b,i-5,0,0,0,1,,1,1,0,,0,,0,0,0,0,MAINT,1,1,0,0,0,1,0,,1,3,3,,70,,2,0,,1,1,,0,,,,,,,0,,,,0,0,
b,BACKEND,0,0,1,2,0,421,1,0,0,0,,0,0,0,0,UP,6,6,0,,0,1,0,,1,3,0,,421,,1,0,,1,,,,,,,,,,,,,,0,0,
""".split('\n')

Expand All @@ -229,9 +231,19 @@ def test_count_per_statuses(self):
expected_hosts_statuses = defaultdict(int)
expected_hosts_statuses[('b', 'OPEN')] = 1
expected_hosts_statuses[('b', 'UP')] = 3
expected_hosts_statuses[('b', 'DOWN')] = 1
expected_hosts_statuses[('b', 'MAINT')] = 1
expected_hosts_statuses[('a', 'OPEN')] = 1
self.assertEquals(self.check.hosts_statuses, expected_hosts_statuses)

# backend hosts
agg_statuses = self.check._process_backend_hosts_metric(expected_hosts_statuses)
expected_agg_statuses = {
'a': {'available': 0, 'unavailable': 0},
'b': {'available': 3, 'unavailable': 2},
}
self.assertEquals(expected_agg_statuses, dict(agg_statuses))

# with collect_aggregates_only set to True
self.check._process_data(data, True, True, collect_status_metrics=True,
collect_status_metrics_by_host=False)
Expand All @@ -246,6 +258,8 @@ def test_count_per_statuses(self):
expected_hosts_statuses[('b', 'i-1', 'UP')] = 1
expected_hosts_statuses[('b', 'i-2', 'UP')] = 1
expected_hosts_statuses[('b', 'i-3', 'UP')] = 1
expected_hosts_statuses[('b', 'i-4', 'DOWN')] = 1
expected_hosts_statuses[('b', 'i-5', 'MAINT')] = 1
self.assertEquals(self.check.hosts_statuses, expected_hosts_statuses)

self.check._process_data(data, True, True, collect_status_metrics=True,
Expand Down