Skip to content

Commit

Permalink
- Added custom access log (also added per request %CPU and memory mes…
Browse files Browse the repository at this point in the history
…urement)
  • Loading branch information
Jérôme Loyet committed Jun 23, 2011
1 parent 12081f5 commit bbe8518
Show file tree
Hide file tree
Showing 23 changed files with 746 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -178,6 +178,8 @@ PHP NEWS
. Added master rlimit_files and rlimit_core in the global configuration
settings. (fat)
. Removed pid in debug logs written by chrildren processes. (fat)
. Added custom access log (also added per request %CPU and memory
mesurement). (fat)

- Reflection extension:
. Fixed bug #54347 (reflection_extension does not lowercase module function
Expand Down
30 changes: 30 additions & 0 deletions sapi/fpm/config.m4
Expand Up @@ -340,6 +340,33 @@ AC_DEFUN([AC_FPM_LQ],
])
dnl }}}

AC_DEFUN([AC_FPM_SYSCONF],
[
AC_MSG_CHECKING([for sysconf])
AC_TRY_COMPILE([ #include <unistd.h> ], [sysconf(_SC_CLK_TCK);], [
AC_DEFINE([HAVE_SYSCONF], 1, [do we have sysconf?])
AC_MSG_RESULT([yes])
], [
AC_MSG_RESULT([no])
])
])
dnl }}}

AC_DEFUN([AC_FPM_TIMES],
[
AC_MSG_CHECKING([for times])
AC_TRY_COMPILE([ #include <sys/times.h> ], [struct tms t; times(&t);], [
AC_DEFINE([HAVE_TIMES], 1, [do we have times?])
AC_MSG_RESULT([yes])
], [
AC_MSG_RESULT([no])
])
])
dnl }}}


AC_MSG_CHECKING(for FPM build)
if test "$PHP_FPM" != "no"; then
AC_MSG_RESULT($PHP_FPM)
Expand All @@ -350,6 +377,8 @@ if test "$PHP_FPM" != "no"; then
AC_FPM_TRACE
AC_FPM_BUILTIN_ATOMIC
AC_FPM_LQ
AC_FPM_SYSCONF
AC_FPM_TIMES

PHP_ARG_WITH(fpm-user,,
[ --with-fpm-user[=USER] Set the user for php-fpm to run as. (default: nobody)], nobody, no)
Expand Down Expand Up @@ -403,6 +432,7 @@ if test "$PHP_FPM" != "no"; then
fpm/fpm_conf.c \
fpm/fpm_env.c \
fpm/fpm_events.c \
fpm/fpm_log.c \
fpm/fpm_main.c \
fpm/fpm_php.c \
fpm/fpm_php_trace.c \
Expand Down
14 changes: 13 additions & 1 deletion sapi/fpm/fpm/fastcgi.c
Expand Up @@ -149,6 +149,8 @@ static int is_fastcgi = 0;
static int in_shutdown = 0;
static in_addr_t *allowed_clients = NULL;

static sa_t client_sa;

#ifdef _WIN32

static DWORD WINAPI fcgi_shutdown_thread(LPVOID arg)
Expand Down Expand Up @@ -833,7 +835,9 @@ int fcgi_accept_request(fcgi_request *req)
FCGI_LOCK(req->listen_socket);
req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
FCGI_UNLOCK(req->listen_socket);
if (req->fd >= 0 && allowed_clients) {

client_sa = sa;
if (sa.sa.sa_family == AF_INET && req->fd >= 0 && allowed_clients) {
int n = 0;
int allowed = 0;

Expand Down Expand Up @@ -1118,6 +1122,14 @@ void fcgi_free_mgmt_var_cb(void * ptr)
pefree(*var, 1);
}

char *fcgi_get_last_client_ip() /* {{{ */
{
if (client_sa.sa.sa_family == AF_UNIX) {
return NULL;
}
return inet_ntoa(client_sa.sa_inet.sin_addr);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
Expand Down
2 changes: 2 additions & 0 deletions sapi/fpm/fpm/fastcgi.h
Expand Up @@ -135,6 +135,8 @@ int fcgi_flush(fcgi_request *req, int close);
void fcgi_set_mgmt_var(const char * name, size_t name_len, const char * value, size_t value_len);
void fcgi_free_mgmt_var_cb(void * ptr);

char *fcgi_get_last_client_ip();

/*
* Local variables:
* tab-width: 4
Expand Down
4 changes: 3 additions & 1 deletion sapi/fpm/fpm/fpm.c
Expand Up @@ -19,6 +19,7 @@
#include "fpm_conf.h"
#include "fpm_worker_pool.h"
#include "fpm_stdio.h"
#include "fpm_log.h"
#include "zlog.h"

struct fpm_globals_s fpm_globals = {
Expand All @@ -45,8 +46,9 @@ int fpm_init(int argc, char **argv, char *config, char *prefix, int test_conf) /
}
fpm_globals.prefix = prefix;

if (0 > fpm_php_init_main() ||
if (0 > fpm_php_init_main() ||
0 > fpm_stdio_init_main() ||
0 > fpm_log_init_main() ||
0 > fpm_conf_init_main(test_conf) ||
0 > fpm_unix_init_main() ||
0 > fpm_pctl_init_main() ||
Expand Down
2 changes: 2 additions & 0 deletions sapi/fpm/fpm/fpm_children.c
Expand Up @@ -27,6 +27,7 @@
#include "fpm_env.h"
#include "fpm_shm_slots.h"
#include "fpm_status.h"
#include "fpm_log.h"

#include "zlog.h"

Expand Down Expand Up @@ -146,6 +147,7 @@ static void fpm_child_init(struct fpm_worker_pool_s *wp) /* {{{ */
fpm_globals.max_requests = wp->config->pm_max_requests;

if (0 > fpm_stdio_init_child(wp) ||
0 > fpm_log_init_child(wp) ||
0 > fpm_status_init_child(wp) ||
0 > fpm_unix_init_child(wp) ||
0 > fpm_signals_init_child() ||
Expand Down
40 changes: 37 additions & 3 deletions sapi/fpm/fpm/fpm_conf.c
Expand Up @@ -41,6 +41,7 @@
#include "fpm_sockets.h"
#include "fpm_shm.h"
#include "fpm_status.h"
#include "fpm_log.h"
#include "zlog.h"

static int fpm_conf_load_ini_file(char *filename TSRMLS_DC);
Expand Down Expand Up @@ -102,6 +103,8 @@ static struct ini_value_parser_s ini_fpm_pool_options[] = {
{ "pm.status_path", &fpm_conf_set_string, WPO(pm_status_path) },
{ "ping.path", &fpm_conf_set_string, WPO(ping_path) },
{ "ping.response", &fpm_conf_set_string, WPO(ping_response) },
{ "access.log", &fpm_conf_set_string, WPO(access_log) },
{ "access.format", &fpm_conf_set_string, WPO(access_format) },
{ 0, 0, 0 }
};

Expand Down Expand Up @@ -414,6 +417,8 @@ int fpm_worker_pool_config_free(struct fpm_worker_pool_config_s *wpc) /* {{{ */
free(wpc->chdir);
free(wpc->slowlog);
free(wpc->prefix);
free(wpc->access_log);
free(wpc->access_format);

return 0;
}
Expand Down Expand Up @@ -663,6 +668,13 @@ static int fpm_conf_process_all_pools() /* {{{ */
/* memset(&fpm_status.last_update, 0, sizeof(fpm_status.last_update)); */
}

if (wp->config->access_log && *wp->config->access_log) {
fpm_evaluate_full_path(&wp->config->access_log, wp, NULL, 0);
if (!wp->config->access_format) {
wp->config->access_format = strdup("%R - %u %t \"%m %r\" %s");
}
}

if (wp->config->chroot && *wp->config->chroot) {

fpm_evaluate_full_path(&wp->config->chroot, wp, NULL, 1);
Expand Down Expand Up @@ -770,8 +782,10 @@ int fpm_conf_write_pid() /* {{{ */
}
/* }}} */

static int fpm_conf_post_process() /* {{{ */
static int fpm_conf_post_process(TSRMLS_D) /* {{{ */
{
struct fpm_worker_pool_s *wp;

if (fpm_global_config.pid_file) {
fpm_evaluate_full_path(&fpm_global_config.pid_file, NULL, PHP_LOCALSTATEDIR, 0);
}
Expand All @@ -786,7 +800,25 @@ static int fpm_conf_post_process() /* {{{ */
return -1;
}

return fpm_conf_process_all_pools();
if (0 > fpm_log_open(0)) {
return -1;
}

if (0 > fpm_conf_process_all_pools()) {
return -1;
}

for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
if (!wp->config->access_log || !*wp->config->access_log) {
continue;
}
if (0 > fpm_log_write(wp->config->access_format TSRMLS_CC)) {
zlog(ZLOG_ERROR, "[pool %s] wrong format for access.format '%s'", wp->config->name, wp->config->access_format);
return -1;
}
}

return 0;
}
/* }}} */

Expand Down Expand Up @@ -1147,6 +1179,8 @@ static void fpm_conf_dump() /* {{{ */
zlog(ZLOG_NOTICE, "\tpm.status_path = %s", STR2STR(wp->config->pm_status_path));
zlog(ZLOG_NOTICE, "\tping.path = %s", STR2STR(wp->config->ping_path));
zlog(ZLOG_NOTICE, "\tping.response = %s", STR2STR(wp->config->ping_response));
zlog(ZLOG_NOTICE, "\taccess.log = %s", STR2STR(wp->config->access_log));
zlog(ZLOG_NOTICE, "\taccess.format = %s", STR2STR(wp->config->access_format));
zlog(ZLOG_NOTICE, "\tcatch_workers_output = %s", BOOL2STR(wp->config->catch_workers_output));
zlog(ZLOG_NOTICE, "\trequest_terminate_timeout = %ds", wp->config->request_terminate_timeout);
zlog(ZLOG_NOTICE, "\trequest_slowlog_timeout = %ds", wp->config->request_slowlog_timeout);
Expand Down Expand Up @@ -1212,7 +1246,7 @@ int fpm_conf_init_main(int test_conf) /* {{{ */
return -1;
}

if (0 > fpm_conf_post_process()) {
if (0 > fpm_conf_post_process(TSRMLS_C)) {
zlog(ZLOG_ERROR, "failed to post process the configuration");
return -1;
}
Expand Down
2 changes: 2 additions & 0 deletions sapi/fpm/fpm/fpm_conf.h
Expand Up @@ -57,6 +57,8 @@ struct fpm_worker_pool_config_s {
int pm_max_spare_servers;
char *ping_path;
char *ping_response;
char *access_log;
char *access_format;
char *listen_address;
int listen_backlog;
char *listen_owner;
Expand Down
18 changes: 14 additions & 4 deletions sapi/fpm/fpm/fpm_events.c
Expand Up @@ -21,6 +21,7 @@
#include "fpm_children.h"
#include "zlog.h"
#include "fpm_clock.h"
#include "fpm_log.h"

#define fpm_event_set_timeout(ev, now) timeradd(&(now), &(ev)->frequency, &(ev)->timeout);

Expand Down Expand Up @@ -55,8 +56,8 @@ static void fpm_event_cleanup(int which, void *arg) /* {{{ */
static void fpm_got_signal(struct fpm_event_s *ev, short which, void *arg) /* {{{ */
{
char c;
int res;
int fd = ev->fd;;
int res, ret;
int fd = ev->fd;

do {
do {
Expand Down Expand Up @@ -93,10 +94,19 @@ static void fpm_got_signal(struct fpm_event_s *ev, short which, void *arg) /* {{
case '1' : /* SIGUSR1 */
zlog(ZLOG_DEBUG, "received SIGUSR1");
if (0 == fpm_stdio_open_error_log(1)) {
zlog(ZLOG_NOTICE, "log file re-opened");
zlog(ZLOG_NOTICE, "error log file re-opened");
} else {
zlog(ZLOG_ERROR, "unable to re-opened log file");
zlog(ZLOG_ERROR, "unable to re-opened error log file");
}

ret = fpm_log_open(1);
if (ret == 0) {
zlog(ZLOG_NOTICE, "access log file re-opened");
} else if (ret == -1) {
zlog(ZLOG_ERROR, "unable to re-opened access log file");
}
/* else no access log are set */

break;
case '2' : /* SIGUSR2 */
zlog(ZLOG_DEBUG, "received SIGUSR2");
Expand Down

0 comments on commit bbe8518

Please sign in to comment.