Skip to content

Instantly share code, notes, and snippets.

@radist2s
Last active December 17, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radist2s/5631217 to your computer and use it in GitHub Desktop.
Save radist2s/5631217 to your computer and use it in GitHub Desktop.
ztc php-fpm loging via http; add to config (/etc/ztc/php-fpm.conf) "proto=http"
#!/usr/bin/env python
"""
<description>
This file is part of ZTC and distributed under the same license.
http://bitbucket.org/rvs/ztc/
Copyright (c) 2011 Vladimir Rusinov <vladimir@greenmice.info>
"""
import time
import urllib2
from ztc.check import ZTCCheck, CheckFail
import ztc.lib.flup_fcgi_client as fcgi_client
class PHPFPMCheck(ZTCCheck):
name = "php-fpm"
OPTPARSE_MIN_NUMBER_OF_ARGS = 1
OPTPARSE_MAX_NUMBER_OF_ARGS = 2
def _myinit(self):
self.protocol_type = self.config.get('proto', 'fcgi')
self.fcgi_port = self.config.get('fpm_port', 9000)
self.fcgi_host = self.config.get('fpm_host', '127.0.0.1')
def _get(self, metric, *arg, **kwarg):
if metric == 'ping':
return self.ping
elif metric == 'status':
m = arg[0]
return self.get_status(m)
else:
raise CheckFail("uncknown metric")
def _load_page(self, url):
""" load fastcgi page """
if self.protocol_type == 'http':
http_url = "http://%s:%s%s" % (
self.fcgi_host,
self.fcgi_port,
url
)
self.logger.debug("opening url %s" % http_url)
try:
u = urllib2.urlopen(http_url, None, 1)
except TypeError:
u = urllib2.urlopen(http_url, None)
ret = u.read()
u.close()
return '200', [], ret, ''
else:
try:
fcgi = fcgi_client.FCGIApp(host = self.fcgi_host,
port = self.fcgi_port)
env = {
'SCRIPT_FILENAME': url,
'QUERY_STRING': '',
'REQUEST_METHOD': 'GET',
'SCRIPT_NAME': url,
'REQUEST_URI': url,
'GATEWAY_INTERFACE': 'CGI/1.1',
'SERVER_SOFTWARE': 'ztc',
'REDIRECT_STATUS': '200',
'CONTENT_TYPE': '',
'CONTENT_LENGTH': '0',
#'DOCUMENT_URI': url,
'DOCUMENT_ROOT': '/',
'DOCUMENT_ROOT': '/var/www/',
#'SERVER_PROTOCOL' : ???
'REMOTE_ADDR': '127.0.0.1',
'REMOTE_PORT': '123',
'SERVER_ADDR': self.fcgi_host,
'SERVER_PORT': str(self.fcgi_port),
'SERVER_NAME': self.fcgi_host
}
ret = fcgi(env)
return ret
except:
self.logger.exception('fastcgi load failed')
return '500', [], '', ''
@property
def ping(self):
""" calls php-fpm ping resource """
st = time.time()
code, headers, out, err = self._load_page('/ping')
if code.startswith('200') and out == 'pong':
return time.time() - st
else:
self.logger.error('ping: got response, but not correct')
return 0
def get_status(self, metric):
""" get php-fpm status metric """
metric = metric.replace('_', ' ')
page = self.get_status_page()
if not page:
raise CheckFail("unable to get status page")
for line in page.splitlines():
if line.startswith(metric):
return line.split()[-1]
# no such metric found
raise CheckFail("no such metric found")
def get_status_page(self):
""" return php-ftm status page text """
code, headers, out, err = self._load_page('/status')
if code.startswith('200'):
return out
else:
self.logger.error('ping: got response, but not correct')
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment