Esempio n. 1
0
void FCGX_Free(FCGX_Request * request, int close)
{
	if (request == NULL)
		return;

	FCGX_FreeStream(&request->in);
	FCGX_FreeStream(&request->out);
	FCGX_FreeStream(&request->err);
	FreeParams(&request->paramsPtr);

	if (close) {
		OS_IpcClose(request->ipcFd);
		request->ipcFd = -1;
	}
}
int main (int argc, char **argv) {
    int fcgiSock = -1;
    FCGX_Stream *paramsStream;
    char *pool = NULL;
    char *content, *data;
    int type, count;
    struct json_object  *obj, *slaveobj;

    /* Set signal handling and alarm */
    if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    /* Connect to fcgi server */
    fcgiSock = mp_fcgi_connect(fcgisocket);

    /* Prepare Begin Request */
    FCGI_BeginRequestBody body;
    body.roleB1 = 0x00;
    body.roleB0 = FCGI_RESPONDER;
    body.flags  = 0x000;
    memset(body.reserved, 0, sizeof(body.reserved));
    mp_fcgi_write(fcgiSock, 42, FCGI_BEGIN_REQUEST, (char*)&body, sizeof(body));

    /* Set FCGI Params */
    paramsStream = FCGX_CreateWriter(fcgiSock, 1, 8192, FCGI_PARAMS);
    mp_fcgi_putkv(paramsStream, "REQUEST_METHOD", "GET");
    mp_fcgi_putkv(paramsStream, "SCRIPT_NAME", query);
    mp_fcgi_putkv(paramsStream, "SCRIPT_FILENAME", query);
    mp_fcgi_putkv(paramsStream, "QUERY_STRING", "json&");
    FCGX_FClose(paramsStream);
    FCGX_FreeStream(&paramsStream);

    /* Start request processing by stdin closing */
    mp_fcgi_write(fcgiSock, 42, FCGI_STDIN, NULL, 0);

    /* Wait for answer */
    data = NULL;
    do {
        content = NULL;
        type = mp_fcgi_read(fcgiSock, &content, &count);
        if (type == FCGI_STDOUT)
            data = content;
        else if (content)
            free(content);
    } while (type != FCGI_END_REQUEST);

    /* Skip http headers */
    content = data;
    do {
        (void)strsep(&data, "\n");
    } while (data && data[0] != '\r');

    /* Parse JSON */
    obj = mp_json_tokener_parse(data);

    /* Read pool name */
    mp_json_object_object_get(obj, "pool", &slaveobj);
    pool = mp_strdup(json_object_get_string(slaveobj));

    /* Read accepted connections */
    mp_json_object_object_get(obj, "accepted conn", &slaveobj);
    mp_perfdata_int("accepted_conn", json_object_get_int(slaveobj), "c", NULL);

    /* Read listen queue */
    mp_json_object_object_get(obj, "listen queue", &slaveobj);
    mp_perfdata_int("listen_queue", json_object_get_int(slaveobj), "", NULL);

    /* Read idle processes */
    mp_json_object_object_get(obj, "idle processes", &slaveobj);
    mp_perfdata_int("idle_processes", json_object_get_int(slaveobj), "", NULL);

    /* Read active processes */
    mp_json_object_object_get(obj, "active processes", &slaveobj);
    mp_perfdata_int("active_processes", json_object_get_int(slaveobj), "", NULL);

    free(content);
    json_object_put(obj);

    ok("PHP-FPM: %s", pool);
}