-
Notifications
You must be signed in to change notification settings - Fork 814
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] unix socket stats urls #3005
Changes from 6 commits
0a08eac
d8fd6e8
e88d6ef
213cd48
067f487
5d56b23
e04a4c8
512c872
1ceeaec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,12 @@ | |
from collections import defaultdict | ||
import copy | ||
import re | ||
import socket | ||
import time | ||
try: | ||
import urlparse | ||
except LoadError: | ||
import urllib.parse as urlparse | ||
|
||
# 3rd party | ||
import requests | ||
|
@@ -97,8 +102,20 @@ def __init__(self, name, init_config, agentConfig, instances=None): | |
|
||
def check(self, instance): | ||
url = instance.get('url') | ||
username = instance.get('username') | ||
password = instance.get('password') | ||
self.log.debug('Processing HAProxy data for %s' % url) | ||
|
||
parsed_url = urlparse.urlparse(url) | ||
|
||
if parsed_url.scheme == 'unix': | ||
data = self._fetch_socket_data(parsed_url.path) | ||
|
||
else: | ||
username = instance.get('username') | ||
password = instance.get('password') | ||
verify = not _is_affirmative(instance.get('disable_ssl_validation', False)) | ||
|
||
data = self._fetch_url_data(url, username, password, verify) | ||
|
||
collect_aggregates_only = _is_affirmative( | ||
instance.get('collect_aggregates_only', True) | ||
) | ||
|
@@ -127,12 +144,6 @@ def check(self, instance): | |
|
||
custom_tags = instance.get('tags', []) | ||
|
||
verify = not _is_affirmative(instance.get('disable_ssl_validation', False)) | ||
|
||
self.log.debug('Processing HAProxy data for %s' % url) | ||
|
||
data = self._fetch_data(url, username, password, verify) | ||
|
||
process_events = instance.get('status_check', self.init_config.get('status_check', False)) | ||
|
||
self._process_data( | ||
|
@@ -147,19 +158,38 @@ def check(self, instance): | |
custom_tags=custom_tags, | ||
) | ||
|
||
def _fetch_data(self, url, username, password, verify): | ||
''' Hit a given URL and return the parsed json ''' | ||
def _fetch_url_data(self, url, username, password, verify): | ||
''' Hit a given http url and return the stats lines ''' | ||
# Try to fetch data from the stats URL | ||
|
||
auth = (username, password) | ||
url = "%s%s" % (url, STATS_URL) | ||
|
||
self.log.debug("HAProxy Fetching haproxy search data from: %s" % url) | ||
self.log.debug("Fetching haproxy stats from url: %s" % url) | ||
|
||
response = requests.get(url, auth=auth, headers=headers(self.agentConfig), verify=verify, timeout=self.default_integration_http_timeout) | ||
response.raise_for_status() | ||
|
||
return response.content.splitlines() | ||
|
||
def _fetch_socket_data(self, socket_path): | ||
''' Hit a given stats socket and return the stats lines ''' | ||
|
||
self.log.debug("Fetching haproxy stats from socket: %s" % socket_path) | ||
|
||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | ||
sock.connect(socket_path) | ||
sock.send("show stat\r\n") | ||
|
||
response = "" | ||
output = sock.recv(8192) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you define this number as a constant on the top of the module? Like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
while output: | ||
response += output.decode("ASCII") | ||
output = sock.recv(8192) | ||
|
||
r = requests.get(url, auth=auth, headers=headers(self.agentConfig), verify=verify, timeout=self.default_integration_http_timeout) | ||
r.raise_for_status() | ||
sock.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of this was borrowed from https://github.com/neurogeek/haproxyctl/blob/master/haproxy/conn.py#L51-L83 |
||
|
||
return r.content.splitlines() | ||
return response.splitlines() | ||
|
||
def _process_data(self, data, collect_aggregates_only, process_events, url=None, | ||
collect_status_metrics=False, collect_status_metrics_by_host=False, | ||
|
@@ -428,8 +458,11 @@ def _process_metrics(self, data, url, services_incl_filter=None, | |
hostname = data['svname'] | ||
service_name = data['pxname'] | ||
back_or_front = data['back_or_front'] | ||
tags = ["type:%s" % back_or_front, "instance_url:%s" % url] | ||
tags.append("service:%s" % service_name) | ||
tags = [ | ||
"type:%s" % back_or_front, | ||
"instance_url:%s" % url, | ||
"service:%s" % service_name, | ||
] | ||
tags.extend(custom_tags) | ||
|
||
if self._is_service_excl_filtered(service_name, services_incl_filter, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,10 @@ instances: | |
# username: username | ||
# password: password | ||
# | ||
# or, with a unix stats or admin socket: | ||
# | ||
# url: unix:///var/run/haproxy.sock | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: could you add a leading
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, that looks odd to me, but updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah at first I thought it'd be clearer but doesn't seem so... |
||
# | ||
# The (optional) `status_check` paramater will instruct the check to | ||
# send events on status changes in the backend. This is DEPRECATED in | ||
# favor creation a monitor on the service check status and will be | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't actually need this since the agent ships with python 2.7 (the exception name should be
ImportError
anyways, tests are failing on this).simply
import urlparse
is ok.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Too much Ruby on the brain.)