From 0b6d7159823d30b86e5f47e0b7b9a82aec35aa77 Mon Sep 17 00:00:00 2001 From: "roberto@debian32.local" Date: Mon, 21 Nov 2011 09:42:48 +0100 Subject: [PATCH] refactored ping and nagios --- plugins/nagios/nagios.c | 37 ++++++++++++------------ plugins/ping/ping_plugin.c | 26 ++++++++--------- plugins/python/python_plugin.c | 6 ++-- plugins/rrdtool/rrdtool.c | 2 +- protocol.c | 52 ++++++++++++++++++++++++++++++++++ signal.c | 22 ++------------ utils.c | 4 +-- uwsgi.c | 27 +++++++----------- uwsgi.h | 3 ++ 9 files changed, 104 insertions(+), 75 deletions(-) diff --git a/plugins/nagios/nagios.c b/plugins/nagios/nagios.c index b459eb50..76b64a54 100644 --- a/plugins/nagios/nagios.c +++ b/plugins/nagios/nagios.c @@ -15,49 +15,48 @@ struct option nagios_options[] = { int nagios() { - char *tcp_port; - struct wsgi_request nagios_req; -// connect and send + struct uwsgi_header uh; + char *buf = NULL; if (!use_nagios) { return 1; } + if (!uwsgi.sockets) { fprintf(stdout, "UWSGI UNKNOWN: you have specified an invalid socket\n"); exit(3); } - tcp_port = strchr(uwsgi.sockets->name, ':'); - if (tcp_port == NULL) { - fprintf(stdout, "UWSGI UNKNOWN: you have specified an invalid socket\n"); - exit(3); - } - tcp_port[0] = 0; - int fd = connect_to_tcp(uwsgi.sockets->name, atoi(tcp_port + 1), uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0); + int fd = uwsgi_connect(uwsgi.sockets->name, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], 0); if (fd < 0) { fprintf(stdout, "UWSGI CRITICAL: could not connect() to workers\n"); exit(2); } - nagios_req.uh.modifier1 = UWSGI_MODIFIER_PING; - nagios_req.uh.pktsize = 0; - nagios_req.uh.modifier2 = 0; - if (write(fd, &nagios_req.uh, 4) != 4) { + + uh.modifier1 = UWSGI_MODIFIER_PING; + uh.pktsize = 0; + uh.modifier2 = 0; + if (write(fd, &uh, 4) != 4) { uwsgi_error("write()"); fprintf(stdout, "UWSGI CRITICAL: could not send ping packet to workers\n"); exit(2); } - nagios_req.poll.fd = fd; - nagios_req.poll.events = POLLIN; - if (!uwsgi_parse_packet(&nagios_req, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) { + int ret = uwsgi_read_response(fd, &uh, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], &buf); + + if (ret == -2) { fprintf(stdout, "UWSGI CRITICAL: timed out waiting for response\n"); exit(2); } + else if (ret == -1) { + fprintf(stdout, "UWSGI CRITICAL: error reading response\n"); + exit(2); + } else { - if (nagios_req.uh.pktsize > 0) { - fprintf(stdout, "UWSGI WARNING: %.*s\n", nagios_req.uh.pktsize, nagios_req.buffer); + if (uh.pktsize > 0 && buf) { + fprintf(stdout, "UWSGI WARNING: %.*s\n", uh.pktsize, buf); exit(1); } else { diff --git a/plugins/ping/ping_plugin.c b/plugins/ping/ping_plugin.c index e6f3af81..0ebc8b76 100644 --- a/plugins/ping/ping_plugin.c +++ b/plugins/ping/ping_plugin.c @@ -15,8 +15,8 @@ struct option uwsgi_ping_options[] = { static void ping() { - struct wsgi_request ping_req; - char *buf = uwsgi_malloc(uwsgi.buffer_size); + struct uwsgi_header uh; + char *buf = NULL; // use a 3 secs timeout by default if (!uping.ping_timeout) uping.ping_timeout = 3; @@ -28,25 +28,22 @@ static void ping() { exit(1); } - memset(&ping_req, 0, sizeof(struct wsgi_request)); - ping_req.uh.modifier1 = UWSGI_MODIFIER_PING; - ping_req.uh.pktsize = 0; - ping_req.uh.modifier2 = 0; - if (write(fd, &ping_req.uh, 4) != 4) { + uh.modifier1 = UWSGI_MODIFIER_PING; + uh.pktsize = 0; + uh.modifier2 = 0; + + if (write(fd, &uh, 4) != 4) { uwsgi_error("write()"); exit(2); } - ping_req.poll.fd = fd; - ping_req.poll.events = POLLIN; - ping_req.buffer = buf; - - if (!uwsgi_parse_packet(&ping_req, uping.ping_timeout)) { + int ret = uwsgi_read_response(fd, &uh, uping.ping_timeout, &buf); + if (ret < 0) { exit(1); } else { - if (ping_req.uh.pktsize > 0) { - uwsgi_log("[WARNING] node %s message: %.*s\n", uping.ping, ping_req.uh.pktsize, buf); + if (uh.pktsize > 0) { + uwsgi_log("[WARNING] node %s message: %.*s\n", uping.ping, uh.pktsize, buf); exit(2); } else { @@ -98,6 +95,7 @@ int uwsgi_ping_manage_options(int i, char *optarg) { switch(i) { case LONG_ARGS_PING: + uwsgi.no_initial_output = 1; uwsgi.no_server = 1; uping.ping = optarg; return 1; diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 1467a286..80ff09f7 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -106,9 +106,9 @@ int uwsgi_python_init() { #ifndef UWSGI_PYPY char *pyversion = strchr(Py_GetVersion(), '\n'); - uwsgi_log("Python version: %.*s %s\n", pyversion-Py_GetVersion(), Py_GetVersion(), Py_GetCompiler()+1); + uwsgi_log_initial("Python version: %.*s %s\n", pyversion-Py_GetVersion(), Py_GetVersion(), Py_GetCompiler()+1); #else - uwsgi_log("PyPy version: %s\n", PYPY_VERSION); + uwsgi_log_initial("PyPy version: %s\n", PYPY_VERSION); #endif #ifndef UWSGI_PYPY @@ -160,7 +160,7 @@ int uwsgi_python_init() { up.reset_ts = simple_reset_ts; - uwsgi_log("Python main interpreter initialized at %p\n", up.main_thread); + uwsgi_log_initial("Python main interpreter initialized at %p\n", up.main_thread); return 1; diff --git a/plugins/rrdtool/rrdtool.c b/plugins/rrdtool/rrdtool.c index a187e3dc..28966157 100644 --- a/plugins/rrdtool/rrdtool.c +++ b/plugins/rrdtool/rrdtool.c @@ -46,7 +46,7 @@ int rrdtool_init() { if (!u_rrd.max_ds) u_rrd.max_ds = 30; - uwsgi_log("*** RRDtool library available at %p ***\n", u_rrd.lib); + uwsgi_log_initial("*** RRDtool library available at %p ***\n", u_rrd.lib); return 0; } diff --git a/protocol.c b/protocol.c index 3cd17e40..f6aeae09 100644 --- a/protocol.c +++ b/protocol.c @@ -342,6 +342,58 @@ ssize_t uwsgi_send_message(int fd, uint8_t modifier1, uint8_t modifier2, char *m return ret; } +int uwsgi_read_response(int fd, struct uwsgi_header *uh, int timeout, char **buf) { + + char *ptr = (char *) uh; + size_t remains = 4; + int ret = -1; + int rlen; + ssize_t len; + + while(remains > 0) { + rlen = uwsgi_waitfd(fd, timeout); + if (rlen > 0) { + len = read(fd, ptr, remains); + if (len <= 0) break; + remains -= len; + ptr += len; + if (remains == 0) { + ret = uh->modifier2; + break; + } + continue; + } + // timed out ? + else if (ret == 0) ret = -2; + break; + } + + if (buf && uh->pktsize > 0) { + *buf = uwsgi_malloc(uh->pktsize); + remains = uh->pktsize; + ptr = *buf; + ret = -1; + while(remains > 0) { + rlen = uwsgi_waitfd(fd, timeout); + if (rlen > 0) { + len = read(fd, ptr, remains); + if (len <= 0) break; + remains -= len; + ptr += len; + if (remains == 0) { + ret = uh->modifier2; + break; + } + continue; + } + // timed out ? + else if (ret == 0) ret = -2; + break; + } + } + + return ret; +} int uwsgi_parse_packet(struct wsgi_request *wsgi_req, int timeout) { int rlen; diff --git a/signal.c b/signal.c index 79d98f46..605037c7 100644 --- a/signal.c +++ b/signal.c @@ -232,8 +232,6 @@ void create_signal_pipe(int *sigpipe) { int uwsgi_remote_signal_send(char *addr, uint8_t sig) { struct uwsgi_header uh; - size_t remains = 4; - char *ptr = (char *) &uh; uh.modifier1 = 110; uh.pktsize = 0; @@ -248,26 +246,10 @@ int uwsgi_remote_signal_send(char *addr, uint8_t sig) { return -1; } - while(remains > 0) { - int rlen = uwsgi_waitfd(fd, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]); - if (rlen > 0) { - ssize_t len = read(fd, ptr, remains); - if (len <= 0) { - break; - } - remains -= len; - ptr += len; - if (remains == 0) { - close(fd); - return uh.modifier2; - } - continue; - } - break; - } + int ret = uwsgi_read_response(fd, &uh, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], NULL); close(fd); - return -1; + return ret; } diff --git a/utils.c b/utils.c index aaa5daaa..a8403544 100644 --- a/utils.c +++ b/utils.c @@ -470,7 +470,7 @@ void uwsgi_as_root() { if (!getuid()) { if (!uwsgi.master_as_root && !uwsgi.uidname) { - uwsgi_log("uWSGI running as root, you can use --uid/--gid/--chroot options\n"); + uwsgi_log_initial("uWSGI running as root, you can use --uid/--gid/--chroot options\n"); } #ifdef UWSGI_CAP @@ -571,7 +571,7 @@ void uwsgi_as_root() { } if (!getuid()) { - uwsgi_log("*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** \n"); + uwsgi_log_initial("*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** \n"); } #ifdef UWSGI_CAP diff --git a/uwsgi.c b/uwsgi.c index 87ab0e46..2804b97d 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -1428,27 +1428,26 @@ int main(int argc, char *argv[], char *envp[]) { uwsgi.binary_path = uwsgi_str(uwsgi.argv[0]); } - if (!uwsgi.no_initial_output) { + if (uwsgi.shared->options[UWSGI_OPTION_CGI_MODE] == 0) { - uwsgi_log("*** Starting uWSGI %s (%dbit) on [%.*s] ***\n", UWSGI_VERSION, (int) (sizeof(void *)) * 8, 24, ctime((const time_t *) &uwsgi.start_tv.tv_sec)); + uwsgi_log_initial("*** Starting uWSGI %s (%dbit) on [%.*s] ***\n", UWSGI_VERSION, (int) (sizeof(void *)) * 8, 24, ctime((const time_t *) &uwsgi.start_tv.tv_sec)); } else { - uwsgi_log("*** Starting uWSGI %s (CGI mode) (%dbit) on [%.*s] ***\n", UWSGI_VERSION, (int) (sizeof(void *)) * 8, 24, ctime((const time_t *) &uwsgi.start_tv.tv_sec)); + uwsgi_log_initial("*** Starting uWSGI %s (CGI mode) (%dbit) on [%.*s] ***\n", UWSGI_VERSION, (int) (sizeof(void *)) * 8, 24, ctime((const time_t *) &uwsgi.start_tv.tv_sec)); } #ifdef UWSGI_DEBUG uwsgi_log("***\n*** You are running a DEBUG version of uWSGI, please disable debug in your build profile and recompile it ***\n***\n"); #endif - uwsgi_log("compiled with version: %s on %s\n", __VERSION__, UWSGI_BUILD_DATE); + uwsgi_log_initial("compiled with version: %s on %s\n", __VERSION__, UWSGI_BUILD_DATE); #ifdef __BIG_ENDIAN__ - uwsgi_log("*** big endian arch detected ***\n"); + uwsgi_log_initial("*** big endian arch detected ***\n"); #endif - } - uwsgi_log("current working directory: %s\n", uwsgi.cwd); + uwsgi_log_initial("current working directory: %s\n", uwsgi.cwd); if (uwsgi.screen_session) { uwsgi_log("*** running under screen session %s ***\n", uwsgi.screen_session); @@ -1467,7 +1466,7 @@ int main(int argc, char *argv[], char *envp[]) { fclose(pidfile); } - uwsgi_log("detected binary path: %s\n", uwsgi.binary_path); + uwsgi_log_initial("detected binary path: %s\n", uwsgi.binary_path); struct uwsgi_socket *shared_sock = uwsgi.shared_sockets; while (shared_sock) { @@ -1616,11 +1615,9 @@ int uwsgi_start(void *v_argv) { fclose(pidfile2); } - if (!uwsgi.no_initial_output) { if (!uwsgi.master_process) { - uwsgi_log("*** WARNING: you are running uWSGI without its master process manager ***\n"); + uwsgi_log_initial("*** WARNING: you are running uWSGI without its master process manager ***\n"); } - } #ifndef __OpenBSD__ if (uwsgi.rl.rlim_max > 0) { @@ -1645,16 +1642,14 @@ int uwsgi_start(void *v_argv) { } if (!getrlimit(RLIMIT_AS, &uwsgi.rl)) { //check for overflow - if (uwsgi.rl.rlim_max != (rlim_t) RLIM_INFINITY && !uwsgi.no_initial_output) { - uwsgi_log("your process address space limit is %lld bytes (%lld MB)\n", (long long) uwsgi.rl.rlim_max, (long long) uwsgi.rl.rlim_max / 1024 / 1024); + if (uwsgi.rl.rlim_max != (rlim_t) RLIM_INFINITY) { + uwsgi_log_initial("your process address space limit is %lld bytes (%lld MB)\n", (long long) uwsgi.rl.rlim_max, (long long) uwsgi.rl.rlim_max / 1024 / 1024); } } #endif - if (!uwsgi.no_initial_output) { - uwsgi_log("your memory page size is %d bytes\n", uwsgi.page_size); - } + uwsgi_log_initial("your memory page size is %d bytes\n", uwsgi.page_size); if (uwsgi.buffer_size > 65536) { uwsgi_log("invalid buffer size.\n"); diff --git a/uwsgi.h b/uwsgi.h index 55c6211c..233d573e 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -11,6 +11,7 @@ extern "C" { #define UMAX64_STR "18446744073709551616" #define uwsgi_error(x) uwsgi_log("%s: %s [%s line %d]\n", x, strerror(errno), __FILE__, __LINE__); +#define uwsgi_log_initial if (!uwsgi.no_initial_output) uwsgi_log #define uwsgi_fatal_error(x) uwsgi_error(x); exit(1); #define uwsgi_error_open(x) uwsgi_log("open(\"%s\"): %s [%s line %d]\n", x, strerror(errno), __FILE__, __LINE__); #define uwsgi_req_error(x) if (wsgi_req->uri_len > 0 && wsgi_req->method_len > 0 && wsgi_req->remote_addr_len > 0) uwsgi_log_verbose("%s: %s [%s line %d] during %.*s %.*s (%.*s)\n", x, strerror(errno), __FILE__, __LINE__,\ @@ -2470,6 +2471,8 @@ void uwsgi_configure(void); void cluster_setup(void); void manage_cluster_announce(char *, uint16_t, char *, uint16_t, void *); +int uwsgi_read_response(int, struct uwsgi_header *, int, char **); + #ifdef __linux__ #ifdef MADV_MERGEABLE void uwsgi_linux_ksm_map(void);