From ac53def0095bd16229f78d4ba733546c9558b02d Mon Sep 17 00:00:00 2001 From: Achim Rosenhagen Date: Fri, 23 Aug 2013 12:08:00 +0200 Subject: [PATCH 1/8] Add check for php-fpm status --- checks.d/php.py | 129 ++++++++++++++++++++++++++++++++++++++++ conf.d/php.yaml.example | 13 ++++ 2 files changed, 142 insertions(+) create mode 100755 checks.d/php.py create mode 100755 conf.d/php.yaml.example diff --git a/checks.d/php.py b/checks.d/php.py new file mode 100755 index 0000000000..15d61edc6d --- /dev/null +++ b/checks.d/php.py @@ -0,0 +1,129 @@ +import re +import time +import urllib2 + +from util import headers +from checks import AgentCheck + +class Php(AgentCheck): + """Tracks basic php-fpm metrics via the status module + * accepted conn + * listen queue + * max listen queue + * listen queue len + * idle processes + * active processes + * total processes + * max active processes + * max children reached + * slow requests + + Requires php-fpm pools to have the status option. + See http://www.php.net/manual/de/install.fpm.configuration.php#pm.status-path for more details + + """ + def check(self, instance): + if 'php_status_url' not in instance: + raise Exception('php instance missing "php_status_url" value.') + tags = instance.get('tags', []) + + self._get_metrics(instance['php_status_url'], tags) + + def _get_metrics(self, url, tags): + req = urllib2.Request(url, None, headers(self.agentConfig)) + request = urllib2.urlopen(req) + response = request.read() + + # accepted conn + parsed = re.search(r'accepted conn:\s+(\d+)', response) + if parsed: + accepted_conn = int(parsed.group(1)) + self.increment("php.accepted_conn", accepted_conn, tags=tags) + + # listen queue + parsed = re.search(r'listen queue:\s+(\d+)', response) + if parsed: + listen_queue = int(parsed.group(1)) + self.gauge("php.listen_queue", listen_queue, tags=tags) + + # max listen queue + parsed = re.search(r'max listen queue:\s+(\d+)', response) + if parsed: + max_listen_queue = int(parsed.group(1)) + self.gauge("php.max_listen_queue", max_listen_queue, tags=tags) + + # listen queue len + parsed = re.search(r'listen queue len:\s+(\d+)', response) + if parsed: + listen_queue_len = int(parsed.group(1)) + self.gauge("php.listen_queue_len", listen_queue_len, tags=tags) + + # idle processes + parsed = re.search(r'idle processes:\s+(\d+)', response) + if parsed: + idle_processes = int(parsed.group(1)) + self.gauge("php.idle_processes", idle_processes, tags=tags) + + # active processes + parsed = re.search(r'active processes:\s+(\d+)', response) + if parsed: + active_processes = int(parsed.group(1)) + self.gauge("php.active_processes", active_processes, tags=tags) + + # total processes + parsed = re.search(r'total processes:\s+(\d+)', response) + if parsed: + total_processes = int(parsed.group(1)) + self.gauge("php.total_processes", total_processes, tags=tags) + + # max active processes + parsed = re.search(r'max active processes:\s+(\d+)', response) + if parsed: + max_active_processes = int(parsed.group(1)) + self.gauge("php.max_active_processes", max_active_processes, tags=tags) + + # max children reached + parsed = re.search(r'max children reached:\s+(\d+)', response) + if parsed: + max_children_reached = int(parsed.group(1)) + self.gauge("php.max_children_reached", max_children_reached, tags=tags) + + # slow requests + parsed = re.search(r'slow requests:\s+(\d+)', response) + if parsed: + slow_requests = int(parsed.group(1)) + self.increment("php.slow_requests", slow_requests, tags=tags) + + @staticmethod + def parse_agent_config(agentConfig): + instances = [] + + # Try loading from the very old format + php_url = agentConfig.get("php_status_url", None) + if php_url is not None: + instances.append({ + 'php_status_url': php_url + }) + + # Try the older multi-instance style + # php_status_url_1: http://www.example.com/php_status:first_tag + # php_status_url_2: http://www.example2.com/php_status:8080:second_tag + # php_status_url_2: http://www.example3.com/php_status:third_tag + def load_conf(index=1): + instance = agentConfig.get("php_status_url_%s" % index, None) + if instance is not None: + instance = instance.split(":") + instances.append({ + 'php_status_url': ":".join(instance[:-1]), + 'tags': ['instance:%s' % instance[-1]] + }) + load_conf(index+1) + + load_conf() + + if not instances: + return False + + return { + 'instances': instances + } diff --git a/conf.d/php.yaml.example b/conf.d/php.yaml.example new file mode 100755 index 0000000000..0dd353f367 --- /dev/null +++ b/conf.d/php.yaml.example @@ -0,0 +1,13 @@ +init_config: + +instances: + # For every instance, you have an `php_status_url` and (optionally) + # a list of tags. + + - php_status_url: http://example.com/php_status/ + tags: + - instance:foo + + - php_status_url: http://example2.com:1234/php_status/ + tags: + - instance:bar From 5dfda7c7f5e3648d985935d9baa059808f8a5a47 Mon Sep 17 00:00:00 2001 From: Achim Rosenhagen Date: Fri, 23 Aug 2013 15:44:56 +0200 Subject: [PATCH 2/8] rewritten to mirror apache check code style (for better maintainability) --- checks.d/php.py | 158 +++++++++++++++++----------------------- conf.d/php.yaml.example | 11 +-- 2 files changed, 71 insertions(+), 98 deletions(-) diff --git a/checks.d/php.py b/checks.d/php.py index 15d61edc6d..0ad8933df3 100755 --- a/checks.d/php.py +++ b/checks.d/php.py @@ -22,108 +22,86 @@ class Php(AgentCheck): See http://www.php.net/manual/de/install.fpm.configuration.php#pm.status-path for more details """ + + GAUGES = { + 'listen_queue': 'php.listen_queue', + 'max_listen_queue': 'php.max_listen_queue', + 'listen_queue_len': 'php.listen_queue_len', + 'idle_processes': 'php.idle_processes', + 'active_processes': 'php.active_processes', + 'total_processes': 'php.total_processes', + 'max_active_processes': 'php.max_active_processes', + 'max_children_reached': 'php.max_children_reached' + } + + RATES = { + + } + + COUNTERS = { + 'accepted_conn': 'php.accepted_conn', + 'slow_requests': 'php.slow_requests' + } + def check(self, instance): if 'php_status_url' not in instance: raise Exception('php instance missing "php_status_url" value.') - tags = instance.get('tags', []) - self._get_metrics(instance['php_status_url'], tags) + url = self.assumed_url.get(instance['php_status_url'], instance['php_status_url']) + + tags = instance.get('tags', []) - def _get_metrics(self, url, tags): - req = urllib2.Request(url, None, headers(self.agentConfig)) + req = urllib2.Request(url, None, + headers(self.agentConfig)) + if 'php_status_user' in instance and 'php_status_password' in instance: + auth_str = '%s:%s' % (instance['php_status_user'], instance['php_status_password']) + encoded_auth_str = base64.encodestring(auth_str) + req.add_header("Authorization", "Basic %s" % encoded_auth_str) request = urllib2.urlopen(req) response = request.read() - # accepted conn - parsed = re.search(r'accepted conn:\s+(\d+)', response) - if parsed: - accepted_conn = int(parsed.group(1)) - self.increment("php.accepted_conn", accepted_conn, tags=tags) - - # listen queue - parsed = re.search(r'listen queue:\s+(\d+)', response) - if parsed: - listen_queue = int(parsed.group(1)) - self.gauge("php.listen_queue", listen_queue, tags=tags) - - # max listen queue - parsed = re.search(r'max listen queue:\s+(\d+)', response) - if parsed: - max_listen_queue = int(parsed.group(1)) - self.gauge("php.max_listen_queue", max_listen_queue, tags=tags) - - # listen queue len - parsed = re.search(r'listen queue len:\s+(\d+)', response) - if parsed: - listen_queue_len = int(parsed.group(1)) - self.gauge("php.listen_queue_len", listen_queue_len, tags=tags) - - # idle processes - parsed = re.search(r'idle processes:\s+(\d+)', response) - if parsed: - idle_processes = int(parsed.group(1)) - self.gauge("php.idle_processes", idle_processes, tags=tags) - - # active processes - parsed = re.search(r'active processes:\s+(\d+)', response) - if parsed: - active_processes = int(parsed.group(1)) - self.gauge("php.active_processes", active_processes, tags=tags) - - # total processes - parsed = re.search(r'total processes:\s+(\d+)', response) - if parsed: - total_processes = int(parsed.group(1)) - self.gauge("php.total_processes", total_processes, tags=tags) - - # max active processes - parsed = re.search(r'max active processes:\s+(\d+)', response) - if parsed: - max_active_processes = int(parsed.group(1)) - self.gauge("php.max_active_processes", max_active_processes, tags=tags) - - # max children reached - parsed = re.search(r'max children reached:\s+(\d+)', response) - if parsed: - max_children_reached = int(parsed.group(1)) - self.gauge("php.max_children_reached", max_children_reached, tags=tags) - - # slow requests - parsed = re.search(r'slow requests:\s+(\d+)', response) - if parsed: - slow_requests = int(parsed.group(1)) - self.increment("php.slow_requests", slow_requests, tags=tags) + metric_count = 0 + # Loop through and extract the numerical values + for line in response.split('\n'): + values = line.split(': ') + if len(values) == 2: # match + metric, value = values + try: + value = float(value) + except ValueError: + continue + + # Send metric as a gauge, if applicable + if metric in self.GAUGES: + metric_count += 1 + metric_name = self.GAUGES[metric] + self.gauge(metric_name, value, tags=tags) + + # Send metric as a rate, if applicable + if metric in self.RATES: + metric_count += 1 + metric_name = self.RATES[metric] + self.rate(metric_name, value, tags=tags) + + # Send metric as a increment, if applicable + if metric in self.COUNTERS: + metric_count += 1 + metric_name = self.COUNTERS[metric] + self.increment(metric_name, value, tags=tags) + + if metric_count == 0: + if self.assumed_url.get(instance['php_status_url'], None) is None and url[-5:] != '?auto': + self.assumed_url[instance['php_status_url']]= '%s?auto' % url + self.warning("Assuming url was not correct. Trying to add ?auto suffix to the url") + self.check(instance) + else: + raise Exception("No metrics were fetched for this instance. Make sure that %s is the proper url." % instance['php_status_url']) @staticmethod def parse_agent_config(agentConfig): - instances = [] - - # Try loading from the very old format - php_url = agentConfig.get("php_status_url", None) - if php_url is not None: - instances.append({ - 'php_status_url': php_url - }) - - # Try the older multi-instance style - # php_status_url_1: http://www.example.com/php_status:first_tag - # php_status_url_2: http://www.example2.com/php_status:8080:second_tag - # php_status_url_2: http://www.example3.com/php_status:third_tag - def load_conf(index=1): - instance = agentConfig.get("php_status_url_%s" % index, None) - if instance is not None: - instance = instance.split(":") - instances.append({ - 'php_status_url': ":".join(instance[:-1]), - 'tags': ['instance:%s' % instance[-1]] - }) - load_conf(index+1) - - load_conf() - - if not instances: + if not agentConfig.get('php_status_url'): return False return { - 'instances': instances + 'instances': [{'php_status_url': agentConfig.get('php_status_url')}] } diff --git a/conf.d/php.yaml.example b/conf.d/php.yaml.example index 0dd353f367..9ef53ad525 100755 --- a/conf.d/php.yaml.example +++ b/conf.d/php.yaml.example @@ -1,13 +1,8 @@ init_config: instances: - # For every instance, you have an `php_status_url` and (optionally) - # a list of tags. - - - php_status_url: http://example.com/php_status/ + - php_status_url: http://example.com/php_status + # php_status_user: example_user + # php_status_password: example_password tags: - instance:foo - - - php_status_url: http://example2.com:1234/php_status/ - tags: - - instance:bar From e9ed390542376565971b926b79f73f8e2eba1e95 Mon Sep 17 00:00:00 2001 From: Achim Rosenhagen Date: Fri, 23 Aug 2013 18:19:20 +0200 Subject: [PATCH 3/8] fixed missing init function --- checks.d/php.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/checks.d/php.py b/checks.d/php.py index 0ad8933df3..761804db0c 100755 --- a/checks.d/php.py +++ b/checks.d/php.py @@ -43,6 +43,10 @@ class Php(AgentCheck): 'slow_requests': 'php.slow_requests' } + def __init__(self, name, init_config, agentConfig, instances=None): + AgentCheck.__init__(self, name, init_config, agentConfig, instances) + self.assumed_url = {} + def check(self, instance): if 'php_status_url' not in instance: raise Exception('php instance missing "php_status_url" value.') From d5b0794d8d94f3e61956b42b679ef78f11109662 Mon Sep 17 00:00:00 2001 From: Achim Rosenhagen Date: Fri, 23 Aug 2013 18:26:36 +0200 Subject: [PATCH 4/8] fixed dependencies --- checks.d/php.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/checks.d/php.py b/checks.d/php.py index 761804db0c..0172772021 100755 --- a/checks.d/php.py +++ b/checks.d/php.py @@ -1,6 +1,5 @@ -import re -import time import urllib2 +import base64 from util import headers from checks import AgentCheck From ff483cfa0dbf85a5998fc2d3aaa2f00feff8d562 Mon Sep 17 00:00:00 2001 From: Achim Rosenhagen Date: Sat, 24 Aug 2013 17:48:17 +0200 Subject: [PATCH 5/8] fixed naming of gauges --- checks.d/php.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/checks.d/php.py b/checks.d/php.py index 0172772021..2b3f1e0ae2 100755 --- a/checks.d/php.py +++ b/checks.d/php.py @@ -23,14 +23,14 @@ class Php(AgentCheck): """ GAUGES = { - 'listen_queue': 'php.listen_queue', - 'max_listen_queue': 'php.max_listen_queue', - 'listen_queue_len': 'php.listen_queue_len', - 'idle_processes': 'php.idle_processes', - 'active_processes': 'php.active_processes', - 'total_processes': 'php.total_processes', - 'max_active_processes': 'php.max_active_processes', - 'max_children_reached': 'php.max_children_reached' + 'listen queue': 'php.listen_queue', + 'max listen queue': 'php.max_listen_queue', + 'listen queue len': 'php.listen_queue_len', + 'idle processes': 'php.idle_processes', + 'active processes': 'php.active_processes', + 'total processes': 'php.total_processes', + 'max active processes': 'php.max_active_processes', + 'max children reached': 'php.max_children_reached' } RATES = { @@ -38,8 +38,8 @@ class Php(AgentCheck): } COUNTERS = { - 'accepted_conn': 'php.accepted_conn', - 'slow_requests': 'php.slow_requests' + 'accepted conn': 'php.accepted_conn', + 'slow requests': 'php.slow_requests' } def __init__(self, name, init_config, agentConfig, instances=None): @@ -107,4 +107,4 @@ def parse_agent_config(agentConfig): return { 'instances': [{'php_status_url': agentConfig.get('php_status_url')}] - } + } \ No newline at end of file From e734f8e59057c45f3c4b8608201584ae8644204d Mon Sep 17 00:00:00 2001 From: Achim Rosenhagen Date: Mon, 26 Aug 2013 18:18:56 +0200 Subject: [PATCH 6/8] remove unnecessary code (aka refactoring) --- checks.d/php.py | 32 +------------------------------- 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/checks.d/php.py b/checks.d/php.py index 2b3f1e0ae2..b32381b5ce 100755 --- a/checks.d/php.py +++ b/checks.d/php.py @@ -33,10 +33,6 @@ class Php(AgentCheck): 'max children reached': 'php.max_children_reached' } - RATES = { - - } - COUNTERS = { 'accepted conn': 'php.accepted_conn', 'slow requests': 'php.slow_requests' @@ -63,7 +59,6 @@ def check(self, instance): request = urllib2.urlopen(req) response = request.read() - metric_count = 0 # Loop through and extract the numerical values for line in response.split('\n'): values = line.split(': ') @@ -74,37 +69,12 @@ def check(self, instance): except ValueError: continue - # Send metric as a gauge, if applicable - if metric in self.GAUGES: - metric_count += 1 - metric_name = self.GAUGES[metric] - self.gauge(metric_name, value, tags=tags) - # Send metric as a rate, if applicable if metric in self.RATES: - metric_count += 1 metric_name = self.RATES[metric] self.rate(metric_name, value, tags=tags) # Send metric as a increment, if applicable if metric in self.COUNTERS: - metric_count += 1 metric_name = self.COUNTERS[metric] - self.increment(metric_name, value, tags=tags) - - if metric_count == 0: - if self.assumed_url.get(instance['php_status_url'], None) is None and url[-5:] != '?auto': - self.assumed_url[instance['php_status_url']]= '%s?auto' % url - self.warning("Assuming url was not correct. Trying to add ?auto suffix to the url") - self.check(instance) - else: - raise Exception("No metrics were fetched for this instance. Make sure that %s is the proper url." % instance['php_status_url']) - - @staticmethod - def parse_agent_config(agentConfig): - if not agentConfig.get('php_status_url'): - return False - - return { - 'instances': [{'php_status_url': agentConfig.get('php_status_url')}] - } \ No newline at end of file + self.increment(metric_name, value, tags=tags) \ No newline at end of file From 6c42861032873b5f6df284ecd9e668d41328143a Mon Sep 17 00:00:00 2001 From: Achim Rosenhagen Date: Thu, 3 Jul 2014 12:33:13 +0200 Subject: [PATCH 7/8] fix self.GAUGES and self.gauge --- checks.d/php.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/checks.d/php.py b/checks.d/php.py index b32381b5ce..b93ec464b6 100755 --- a/checks.d/php.py +++ b/checks.d/php.py @@ -70,9 +70,9 @@ def check(self, instance): continue # Send metric as a rate, if applicable - if metric in self.RATES: - metric_name = self.RATES[metric] - self.rate(metric_name, value, tags=tags) + if metric in self.GAUGES: + metric_name = self.GAUGES[metric] + self.gauge(metric_name, value, tags=tags) # Send metric as a increment, if applicable if metric in self.COUNTERS: From 83dbc64d9f6f865cd04eb171c3c830593bdc97fb Mon Sep 17 00:00:00 2001 From: Achim Rosenhagen Date: Wed, 23 Jul 2014 19:37:54 +0200 Subject: [PATCH 8/8] update php check; include php_ping check; --- checks.d/php.py | 117 +++++++++++++++++++++++++++++-------------- checks.d/php_ping.py | 70 ++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 37 deletions(-) create mode 100755 checks.d/php_ping.py diff --git a/checks.d/php.py b/checks.d/php.py index b93ec464b6..6b65d07e20 100755 --- a/checks.d/php.py +++ b/checks.d/php.py @@ -1,8 +1,12 @@ +# stdlib +import re import urllib2 -import base64 +import urlparse +# project from util import headers from checks import AgentCheck +from checks.utils import add_basic_auth class Php(AgentCheck): """Tracks basic php-fpm metrics via the status module @@ -22,43 +26,75 @@ class Php(AgentCheck): """ - GAUGES = { - 'listen queue': 'php.listen_queue', - 'max listen queue': 'php.max_listen_queue', - 'listen queue len': 'php.listen_queue_len', - 'idle processes': 'php.idle_processes', - 'active processes': 'php.active_processes', - 'total processes': 'php.total_processes', - 'max active processes': 'php.max_active_processes', - 'max children reached': 'php.max_children_reached' - } - - COUNTERS = { - 'accepted conn': 'php.accepted_conn', - 'slow requests': 'php.slow_requests' - } - - def __init__(self, name, init_config, agentConfig, instances=None): - AgentCheck.__init__(self, name, init_config, agentConfig, instances) - self.assumed_url = {} - def check(self, instance): if 'php_status_url' not in instance: raise Exception('php instance missing "php_status_url" value.') - - url = self.assumed_url.get(instance['php_status_url'], instance['php_status_url']) - tags = instance.get('tags', []) + + response, content_type = self._get_data(instance) + metrics = self.parse_text(response, tags) + + funcs = { + 'gauge': self.gauge, + 'rate': self.rate, + 'increment': self.increment + } + for row in metrics: + try: + name, value, tags, metric_type = row + func = funcs[metric_type] + func(name, value, tags) + except Exception: + self.log.error(u'Could not submit metric: %s' % repr(row)) - req = urllib2.Request(url, None, - headers(self.agentConfig)) + def _get_data(self, instance): + url = instance.get('php_status_url') + req = urllib2.Request(url, None, headers(self.agentConfig)) if 'php_status_user' in instance and 'php_status_password' in instance: - auth_str = '%s:%s' % (instance['php_status_user'], instance['php_status_password']) - encoded_auth_str = base64.encodestring(auth_str) - req.add_header("Authorization", "Basic %s" % encoded_auth_str) - request = urllib2.urlopen(req) - response = request.read() + add_basic_auth(req, instance['php_status_user'], instance['php_status_password']) + + # Submit a service check for status page availability. + parsed_url = urlparse.urlparse(url) + php_ping_host = parsed_url.hostname + php_ping_port = parsed_url.port or 80 + service_check_name = 'php_status.can_connect' + service_check_tags = ['host:%s' % php_ping_host, 'port:%s' % php_ping_port] + try: + response = urllib2.urlopen(req) + except Exception: + self.service_check(service_check_name, AgentCheck.CRITICAL) + raise + else: + self.service_check(service_check_name, AgentCheck.OK) + + body = response.read() + resp_headers = response.info() + return body, resp_headers.get('Content-Type', 'text/plain') + + @classmethod + def parse_text(cls, response, tags): + GAUGES = { + 'listen queue': 'php.listen_queue', + 'max listen queue': 'php.max_listen_queue', + 'listen queue len': 'php.listen_queue_len', + 'idle processes': 'php.idle_processes', + 'active processes': 'php.active_processes', + 'total processes': 'php.total_processes', + 'max active processes': 'php.max_active_processes', + 'max children reached': 'php.max_children_reached' + } + + RATES = { + + } + + INCREMENTS = { + 'accepted conn': 'php.accepted_conn', + 'slow requests': 'php.slow_requests' + } + + output = [] # Loop through and extract the numerical values for line in response.split('\n'): values = line.split(': ') @@ -69,12 +105,19 @@ def check(self, instance): except ValueError: continue + # Send metric as a gauge, if applicable + if metric in GAUGES: + metric_name = GAUGES[metric] + output.append((metric_name, value, tags, 'gauge')) + # Send metric as a rate, if applicable - if metric in self.GAUGES: - metric_name = self.GAUGES[metric] - self.gauge(metric_name, value, tags=tags) + if metric in RATES: + metric_name = RATES[metric] + output.append((metric_name, value, tags, 'rate')) # Send metric as a increment, if applicable - if metric in self.COUNTERS: - metric_name = self.COUNTERS[metric] - self.increment(metric_name, value, tags=tags) \ No newline at end of file + if metric in INCREMENTS: + metric_name = INCREMENTS[metric] + output.append((metric_name, value, tags, 'increment')) + + return output \ No newline at end of file diff --git a/checks.d/php_ping.py b/checks.d/php_ping.py new file mode 100755 index 0000000000..81300ea110 --- /dev/null +++ b/checks.d/php_ping.py @@ -0,0 +1,70 @@ +# stdlib +import re +import urllib2 +import urlparse + +# project +from util import headers +from checks import AgentCheck +from checks.utils import add_basic_auth + +class PhpPing(AgentCheck): + """Monitors php-fpm status via ping-url + + Requires php-fpm pools to have the status option. + See http://www.php.net/manual/de/install.fpm.configuration.php#ping.path for more details + + """ + + def check(self, instance): + if 'php_ping_url' not in instance: + raise Exception('php instance missing "php_ping_url" value.') + tags = instance.get('tags', []) + + response, content_type = self._get_data(instance) + metrics = self.parse_status(response, tags) + + funcs = { + 'gauge': self.gauge, + 'rate': self.rate + } + for row in metrics: + try: + name, value, tags, metric_type = row + func = funcs[metric_type] + func(name, value, tags) + except Exception: + self.log.error(u'Could not submit metric: %s' % repr(row)) + + def _get_data(self, instance): + url = instance.get('php_ping_url') + req = urllib2.Request(url, None, headers(self.agentConfig)) + if 'php_ping_user' in instance and 'php_ping_password' in instance: + add_basic_auth(req, instance['php_ping_user'], instance['php_ping_password']) + + # Submit a service check for status page availability. + parsed_url = urlparse.urlparse(url) + php_ping_host = parsed_url.hostname + php_ping_port = parsed_url.port or 80 + service_check_name = 'php_ping.can_connect' + service_check_tags = ['host:%s' % php_ping_host, 'port:%s' % php_ping_port] + try: + response = urllib2.urlopen(req) + except Exception: + self.service_check(service_check_name, AgentCheck.CRITICAL) + raise + else: + self.service_check(service_check_name, AgentCheck.OK) + + body = response.read() + resp_headers = response.info() + return body, resp_headers.get('Content-Type', 'text/plain') + + @classmethod + def parse_status(cls, raw, tags): + output = [] + parsed = re.search(r'pong', raw) + if parsed: + output.append(('php.ping', 1, tags, 'gauge')) + + return output