Beispiel #1
0
    def _read(self):
        if self.socket_file is not None:
            fcgi = fcgi_client.FCGIApp(connect=self.socket_file)
        elif self.host is not None and self.port is not None:
            fcgi = fcgi_client.FCGIApp(host=self.host, port=self.port)
        else:
            raise Exception("No socket_file, host or port given as parameter")

        if self.uri is None:
            raise Exception("No valid URI given")

        env = {
            'SCRIPT_FILENAME': self.uri,
            'QUERY_STRING': '',
            'REQUEST_METHOD': 'GET',
            'SCRIPT_NAME': self.uri,
            'REQUEST_URI': self.uri,
            'GATEWAY_INTERFACE': 'CGI/1.1',
            'SERVER_SOFTWARE': 'zems',
            'REDIRECT_STATUS': '200',
            'CONTENT_TYPE': '',
            'CONTENT_LENGTH': '0',
            # 'DOCUMENT_URI': url,
            'DOCUMENT_ROOT': '/var/www/'
        }

        return fcgi(env)
Beispiel #2
0
def _make_fcgi_request(config, section, request_path):
    """ load fastcgi page """
    try:
        listen = config.get(section, 'listen')
        if listen[0] == '/':
            #its unix socket
            fcgi = fcgi_client.FCGIApp(connect=listen)
        else:
            if listen.find(':') != -1:
                _listen = listen.split(':')
                fcgi = fcgi_client.FCGIApp(host=_listen[0], port=_listen[1])
            else:
                fcgi = fcgi_client.FCGIApp(port=listen, host='127.0.0.1')

        env = {
            'SCRIPT_FILENAME': request_path,
            'QUERY_STRING': '',
            'REQUEST_METHOD': 'GET',
            'SCRIPT_NAME': request_path,
            'REQUEST_URI': request_path,
            '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/'
        }
        ret = fcgi(env)
        return ret
    except Exception as e:
        print str(e)
        print "exception "
        return '500', [], '', str(e)
Beispiel #3
0
    def load_url(self, url, content='', remote_addr='127.0.0.1', cookies=None):
        """Loads URL via FastCGI interface.

        :param url: URL to access, can optionally include a (GET) query string
        :param content: Content to submit with request (i.e. POST data)
        :param remote_addr: IP address of remote end to pass to script
        :param cookies: String with cookies in 'a=b;' format
        :rtype: Tuple: status header, headers, output, error message
        """
        if hasattr(self, 'fcgi_sock'):
            fcgi = fcgi_client.FCGIApp(connect=self.fcgi_sock)
        else:
            fcgi = fcgi_client.FCGIApp(host=self.fcgi_host,
                                       port=self.fcgi_port)

        try:
            script_name, query_string = url.split('?')
        except ValueError:
            script_name = url
            query_string = ''

        env = {
            'SCRIPT_FILENAME': '%s%s' % (self.document_root, script_name),
            'QUERY_STRING': query_string,
            'REQUEST_METHOD': 'GET' if not content else 'POST',
            'SCRIPT_NAME': script_name,
            'REQUEST_URI': url,
            'GATEWAY_INTERFACE': 'CGI/1.1',
            'SERVER_SOFTWARE': 'zomg',
            'REDIRECT_STATUS': '200',
            'CONTENT_TYPE': 'application/x-www-form-urlencoded',
            # This number below needs to be the number of bytes in the
            # content (POST data)
            'CONTENT_LENGTH': str(len(content)),
            #'DOCUMENT_URI': url,
            'DOCUMENT_ROOT': self.document_root,
            #'SERVER_PROTOCOL' : ???
            'REMOTE_ADDR': remote_addr,
            'REMOTE_PORT': '0',
            'SERVER_ADDR': self.fcgi_host,
            'SERVER_PORT': str(self.fcgi_port),
            'SERVER_NAME': self.fcgi_host,
        }

        if cookies:
            env['HTTP_COOKIE'] = cookies

        ret = fcgi(env, content)
        return ret
Beispiel #4
0
def load_page(fcgi_host, fcgi_port, script, query):
    """ load fastcgi page """
    try:
        fcgi = fcgi_client.FCGIApp(host=fcgi_host, port=fcgi_port)
        env = {
            'SCRIPT_FILENAME': script,
            'QUERY_STRING': query,
            'REQUEST_METHOD': 'GET',
            'SCRIPT_NAME': script,
            'REQUEST_URI': script + '?' + query,
            'GATEWAY_INTERFACE': 'CGI/1.1',
            'SERVER_SOFTWARE': '',
            'REDIRECT_STATUS': '200',
            'CONTENT_TYPE': '',
            'CONTENT_LENGTH': '0',
            'DOCUMENT_ROOT': '/qgisserver/',
            'SERVER_ADDR': fcgi_host,
            'SERVER_PORT': str(fcgi_port),
            'SERVER_PROTOCOL': 'HTTP/1.0',
            'SERVER_NAME': fcgi_host
        }
        ret = fcgi(env)
        return ret
    except:
        print 'fastcgi load failed'
        return '500', [], '', ''
Beispiel #5
0
 def set_params(host, port):
     FCGIProxyHandler.host = host
     FCGIProxyHandler.port = port
     FCGIProxyHandler.fcgi = fcgi_client.FCGIApp(host=host, port=port)
Beispiel #6
0
def get_php_data(instance_name='', *args):
    """
    get monitor data from nginx
    @param instance_name: 'http://ip:port/nginx-status active'
    @return: dict
    """
    # /app/bin/zbxmon --service nginx --item reading --instance 10.10.60.211/8888
    if len(instance_name.split('/')) == 2:
        instance_ip = instance_name.split('/')[0]
        instance_port = instance_name.split('/')[1]
    elif len(instance_name.split('/')) > 2:
        instance_ip = instance_name.split('/')[0]
        instance_port = '/'
        for i in instance_name.split('/')[1:]:
            if i:
                instance_port += i + '/'
        instance_port = instance_port.rstrip('/')
    host_info = discovery_php(ALL=True)
    # print host_info
    # {0: {'pong_path': 'pong', 'ping_path': '/ping', 'status_path': '/status', 'socket': '/dev/shm/php-fpm.socket'}}
    for i in host_info.keys():
        if len(host_info[i]) == 5:
            if instance_ip == host_info[i][
                    'ip'] and instance_port == host_info[i]['port']:
                status_path = host_info[i]['status_path']
                ping_path = host_info[i]['ping_path']
                pong_path = host_info[i]['pong_path']
                fcgi = fcgi_client.FCGIApp(host=host_info[i]['ip'],
                                           port=host_info[i]['port'])
                env = {
                    'SCRIPT_NAME': status_path,
                    'SCRIPT_FILENAME': status_path,
                    'QUERY_STRING': 'json',
                    'REQUEST_METHOD': 'GET'
                }
                alive_env = {
                    'SCRIPT_NAME': ping_path,
                    'SCRIPT_FILENAME': ping_path,
                    'QUERY_STRING': 'json',
                    'REQUEST_METHOD': 'GET'
                }
        if len(host_info[i]) == 4:
            if instance_port == host_info[i]['socket']:
                status_path = host_info[i]['status_path']
                ping_path = host_info[i]['ping_path']
                pong_path = host_info[i]['pong_path']
                fcgi = fcgi_client.FCGIApp(connect=host_info[i]['socket'])
                env = {
                    'SCRIPT_NAME': status_path,
                    'SCRIPT_FILENAME': status_path,
                    'QUERY_STRING': 'json',
                    'REQUEST_METHOD': 'GET'
                }
                alive_env = {
                    'SCRIPT_NAME': ping_path,
                    'SCRIPT_FILENAME': ping_path,
                    'QUERY_STRING': 'json',
                    'REQUEST_METHOD': 'GET'
                }

    code, headers, out, err = fcgi(env)
    out = out.lstrip('{').rstrip('}')
    l_out = out.split(',')
    d_out = {}
    for i in l_out:
        d_out[i.split(':')[0].strip('"')] = i.split(':')[1].strip('"')

    alive_code, alive_headers, alive_out, alive_err = fcgi(alive_env)
    alive_out = alive_out.lstrip('{').rstrip('}').strip()

    # {"pool":"www","process manager":"dynamic","start time":1447740439,"start since":13513,"accepted conn":17,"listen queue":0,"max listen queue":0,"listen queue len":0,"idle processes":4,"active processes":1,"total processes":5,"max active processes":1,"max children reached":0,"slow requests":0}
    result = {}
    if "start since" in d_out:
        result['start_since'] = int(d_out["start since"])
    if "accepted conn" in d_out:
        result['accepted_conn'] = int(d_out["accepted conn"])
    if "listen queue" in d_out:
        result['listen_queue'] = int(d_out["listen queue"])
    if "max listen queue" in d_out:
        result['max_listen_queue'] = int(d_out["max listen queue"])
    if "listen queue len" in d_out:
        result['listen_queue_len'] = int(d_out["listen queue len"])
    if "idle processes" in d_out:
        result['idle_processes'] = int(d_out["idle processes"])
    if "active processes" in d_out:
        result['active_processes'] = int(d_out["active processes"])
    if "total processes" in d_out:
        result['total_processes'] = int(d_out["total processes"])
    if "max active processes" in d_out:
        result['max_active_processes'] = int(d_out["max active processes"])
    if "max children reached" in d_out:
        result['max_children_reached'] = int(d_out["max children reached"])
    if "slow requests" in d_out:
        result['slow_requests'] = int(d_out["slow requests"])
    if alive_out == pong_path:
        result['alive'] = 'up'
    else:
        result['alive'] = 'down'

    return result