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

Add check for php-fpm status #630

Closed
wants to merge 12 commits into from
110 changes: 110 additions & 0 deletions checks.d/php.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import urllib2
import base64

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

"""

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 = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete that variable


}

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', [])

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()

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can delete taht section as self.RATES is empty

if metric in self.RATES:
metric_count += 1
metric_name = self.RATES[metric]
self.rate(metric_name, value, tags=tags)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be self.GAUGES and self.gauge. Not self.RATES/self.rate


# 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':
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the php_status_url expecting ?auto at the end to have a computer readable version of the metrics ?
If not you can get rid of that part and of the constructor

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):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementing this method is not needed here.
This method is intended for backward compatibilty purposes that is not required here.

if not agentConfig.get('php_status_url'):
return False

return {
'instances': [{'php_status_url': agentConfig.get('php_status_url')}]
}
8 changes: 8 additions & 0 deletions conf.d/php.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
init_config:

instances:
- php_status_url: http://example.com/php_status
# php_status_user: example_user
# php_status_password: example_password
tags:
- instance:foo