[asterisk-commits] dlee: branch dlee/stasis-demo r386043 - /team/dlee/stasis-demo/res/res_statsd.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Apr 18 17:35:12 CDT 2013


Author: dlee
Date: Thu Apr 18 17:35:11 2013
New Revision: 386043

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=386043
Log:
Fixed up some config bugs

Modified:
    team/dlee/stasis-demo/res/res_statsd.c

Modified: team/dlee/stasis-demo/res/res_statsd.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-demo/res/res_statsd.c?view=diff&rev=386043&r1=386042&r2=386043
==============================================================================
--- team/dlee/stasis-demo/res/res_statsd.c (original)
+++ team/dlee/stasis-demo/res/res_statsd.c Thu Apr 18 17:35:11 2013
@@ -67,8 +67,6 @@
 
 /*! Socket for sending statd messages */
 static int socket_fd = -1;
-/*! Statsd server address:port. */
-struct ast_sockaddr statsd_server;
 
 /*! \brief Global configuration options for statsd client. */
 struct conf_global_options {
@@ -91,6 +89,14 @@
 /*! \brief Locking container for safe configuration access. */
 static AO2_GLOBAL_OBJ_STATIC(confs);
 
+static void conf_server(const struct conf *cfg, struct ast_sockaddr *addr)
+{
+	*addr = cfg->global->statsd_server;
+	if (ast_sockaddr_port(addr) == 0) {
+		ast_sockaddr_set_port(addr, DEFAULT_STATSD_PORT);
+	}
+}
+
 static const char *type_to_str(enum ast_statsd_type type);
 
 void AST_OPTIONAL_API_NAME(ast_statsd_log_full)(const char *metric_name,
@@ -99,10 +105,14 @@
 	RAII_VAR(struct conf *, cfg, NULL, ao2_cleanup);
 	RAII_VAR(struct ast_str *, msg, NULL, ast_free);
 	size_t len;
+	struct ast_sockaddr statsd_server;
 
 	if (socket_fd == -1) {
 		return;
 	}
+
+	cfg = ao2_global_obj_ref(confs);
+	conf_server(cfg, &statsd_server);
 
 	/* Rates <= 0.0 never get logged.
 	 * Rates >= 1.0 always get logged.
@@ -136,6 +146,7 @@
 
 	len = ast_str_strlen(msg);
 
+	ast_debug(6, "send: %s\n", ast_str_buffer(msg));
 	ast_sendto(socket_fd, ast_str_buffer(msg), len, 0, &statsd_server);
 }
 
@@ -203,19 +214,6 @@
 {
 	RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup);
 	return cfg->global->enabled;
-}
-
-static int conf_server(struct ast_sockaddr *addr)
-{
-	RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup);
-	/* This should really do a name lookup, in which case it may fail.
-	 * For now, it always works.
-	 */
-	*addr = cfg->global->statsd_server;
-	if (ast_sockaddr_port(addr) == 0) {
-		ast_sockaddr_set_port(addr, DEFAULT_STATSD_PORT);
-	}
-	return 0;
 }
 
 static const char *type_to_str(enum ast_statsd_type type)
@@ -241,29 +239,29 @@
 {
 	RAII_VAR(struct conf *, cfg, ao2_global_obj_ref(confs), ao2_cleanup);
 	char *server;
-	int ret;
+	struct ast_sockaddr statsd_server;
 
 	ast_assert(is_enabled());
 
-	ast_debug(3, "Starting up statsd client.\n");
-	ret = conf_server(&statsd_server);
-	if (ret != 0) {
-		ast_log(LOG_ERROR, "Failed to find statsd server\n");
-		return AST_MODULE_LOAD_DECLINE;
-	}
+	ast_debug(3, "Configuring statsd client.\n");
+
+	if (socket_fd == -1) {
+		ast_debug(3, "Creating statsd socket.\n");
+		socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+		if (socket_fd == -1) {
+			perror("Error creating statsd socket");
+			return -1;
+		}
+	}
+
+	conf_server(cfg, &statsd_server);
 	server = ast_sockaddr_stringify_fmt(&statsd_server,
 		AST_SOCKADDR_STR_DEFAULT);
 	ast_debug(3, "  statsd server = %s.\n", server);
-
-	if (socket_fd == -1) {
-		socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
-		if (socket_fd == -1) {
-			perror("Error creating statsd socket");
-			return AST_MODULE_LOAD_FAILURE;
-		}
-	}
-
-	return AST_MODULE_LOAD_SUCCESS;
+	ast_debug(3, "  add newline = %s\n", AST_YESNO(cfg->global->add_newline));
+	ast_debug(3, "  prefix = %s\n", cfg->global->prefix);
+
+	return 0;
 }
 
 static void statsd_shutdown(void)
@@ -283,11 +281,11 @@
 	}
 
 	aco_option_register(&cfg_info, "enabled", ACO_EXACT, global_options,
-		"yes", OPT_BOOL_T, 1,
+		"no", OPT_BOOL_T, 1,
 		FLDSET(struct conf_global_options, enabled));
 
 	aco_option_register(&cfg_info, "add_newline", ACO_EXACT, global_options,
-		"yes", OPT_BOOL_T, 1,
+		"no", OPT_BOOL_T, 1,
 		FLDSET(struct conf_global_options, add_newline));
 
 	aco_option_register(&cfg_info, "server", ACO_EXACT, global_options,
@@ -296,7 +294,7 @@
 
 	aco_option_register(&cfg_info, "prefix", ACO_EXACT, global_options,
 		"", OPT_CHAR_ARRAY_T, 0,
-		FLDSET(struct conf_global_options, prefix));
+		CHARFLDSET(struct conf_global_options, prefix));
 
 	if (aco_process_config(&cfg_info, 0)) {
 		aco_info_destroy(&cfg_info);
@@ -307,7 +305,11 @@
 		return AST_MODULE_LOAD_SUCCESS;
 	}
 
-	return statsd_init();
+	if (statsd_init() != 0) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
+
+	return AST_MODULE_LOAD_SUCCESS;
 }
 
 static int unload_module(void)
@@ -320,22 +322,16 @@
 
 static int reload_module(void)
 {
-	char was_enabled = is_enabled();
-
 	if (aco_process_config(&cfg_info, 1)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
 
-	if (!was_enabled && is_enabled()) {
+	if (is_enabled()) {
 		return statsd_init();
-	}
-
-	if (was_enabled && !is_enabled()) {
+	} else {
 		statsd_shutdown();
 		return AST_MODULE_LOAD_SUCCESS;
 	}
-
-	return AST_MODULE_LOAD_SUCCESS;
 }
 
 /* The priority of this module is set to be as low as possible, since it could




More information about the asterisk-commits mailing list