From d8d292348e65ddfede88a2772cb6f666d7e96d5a Mon Sep 17 00:00:00 2001 From: "roberto@precise64" Date: Thu, 24 May 2012 20:40:56 +0200 Subject: [PATCH] refactored snmp --- master.c | 35 +---------------------------------- snmp.c | 41 +++++++++++++++++++++++++++++++++++++++++ uwsgi.h | 2 ++ 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/master.c b/master.c index 1984b7e5..6029cba9 100644 --- a/master.c +++ b/master.c @@ -535,40 +535,7 @@ int master_loop(char **argv, char **environ) { #endif #ifdef UWSGI_SNMP - if (uwsgi.snmp) { - if (uwsgi.snmp_community) { - if (strlen(uwsgi.snmp_community) > 72) { - uwsgi_log("*** warning the supplied SNMP community string will be truncated to 72 chars ***\n"); - memcpy(uwsgi.shared->snmp_community, uwsgi.snmp_community, 72); - } - else { - memcpy(uwsgi.shared->snmp_community, uwsgi.snmp_community, strlen(uwsgi.snmp_community) + 1); - } - } - - uwsgi.shared->snmp_gvalue[0].type = SNMP_COUNTER64; - uwsgi.shared->snmp_gvalue[0].val = &uwsgi.workers[0].requests; - - for (i = 0; i < uwsgi.numproc; i++) { - uwsgi.shared->snmp_gvalue[30 + i].type = SNMP_COUNTER64; - uwsgi.shared->snmp_gvalue[30 + i].val = &uwsgi.workers[i + 1].requests; - } - - if (uwsgi.snmp_addr) { - snmp_fd = bind_to_udp(uwsgi.snmp_addr, 0, 0); - if (snmp_fd < 0) { - uwsgi_log("unable to bind to udp socket. SNMP service will be disabled.\n"); - } - else { - uwsgi_log("SNMP server enabled on %s\n", uwsgi.snmp_addr); - event_queue_add_fd_read(uwsgi.master_queue, snmp_fd); - } - } - else { - uwsgi_log("SNMP agent enabled.\n"); - } - - } + snmp_fd = uwsgi_setup_snmp(); #endif diff --git a/snmp.c b/snmp.c index 1cb27b51..5f1aa905 100644 --- a/snmp.c +++ b/snmp.c @@ -364,6 +364,47 @@ void uwsgi_opt_snmp_community(char *opt, char *value, void *foobar) { uwsgi.snmp = 1; uwsgi.snmp_community = value; } + +int uwsgi_setup_snmp(void) { + int snmp_fd = -1; + int i; + if (uwsgi.snmp) { + if (uwsgi.snmp_community) { + if (strlen(uwsgi.snmp_community) > 72) { + uwsgi_log("*** warning the supplied SNMP community string will be truncated to 72 chars ***\n"); + memcpy(uwsgi.shared->snmp_community, uwsgi.snmp_community, 72); + } + else { + memcpy(uwsgi.shared->snmp_community, uwsgi.snmp_community, strlen(uwsgi.snmp_community) + 1); + } + } + + uwsgi.shared->snmp_gvalue[0].type = SNMP_COUNTER64; + uwsgi.shared->snmp_gvalue[0].val = &uwsgi.workers[0].requests; + + for (i = 0; i < uwsgi.numproc; i++) { + uwsgi.shared->snmp_gvalue[30 + i].type = SNMP_COUNTER64; + uwsgi.shared->snmp_gvalue[30 + i].val = &uwsgi.workers[i + 1].requests; + } + + if (uwsgi.snmp_addr) { + snmp_fd = bind_to_udp(uwsgi.snmp_addr, 0, 0); + if (snmp_fd < 0) { + uwsgi_log("unable to bind to udp socket. SNMP service will be disabled.\n"); + } + else { + uwsgi_log("SNMP server enabled on %s\n", uwsgi.snmp_addr); + event_queue_add_fd_read(uwsgi.master_queue, snmp_fd); + } + } + else { + uwsgi_log("SNMP agent enabled.\n"); + } + + } + + return snmp_fd; +} #else #warning "*** SNMP support is disabled ***" #endif diff --git a/uwsgi.h b/uwsgi.h index 3485188c..d1ec18e4 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1737,6 +1737,8 @@ struct uwsgi_snmp_custom_value { uint64_t val; }; +int uwsgi_setup_snmp(void); + struct uwsgi_snmp_server_value { uint8_t type; uint64_t *val;