[Asterisk-code-review] ARI: Retrieve existing log channels (asterisk[13])

Joshua Colp asteriskteam at digium.com
Fri Aug 7 16:09:40 CDT 2015


Joshua Colp has submitted this change and it was merged.

Change subject: ARI: Retrieve existing log channels
......................................................................


ARI: Retrieve existing log channels

An http request can be sent to get the existing Asterisk logs.

The command "curl -v -u user:pass -X GET 'http://localhost:8088
/ari/asterisk/logging'" can be run in the terminal to access the
newly implemented functionality.

* Retrieve all existing log channels

ASTERISK-25252

Change-Id: I7bb08b93e3b938c991f3f56cc5d188654768a808
---
M include/asterisk/logger.h
M main/logger.c
M res/ari/resource_asterisk.c
M res/ari/resource_asterisk.h
M res/res_ari_asterisk.c
M rest-api/api-docs/asterisk.json
6 files changed, 190 insertions(+), 7 deletions(-)

Approvals:
  Anonymous Coward #1000019: Verified
  Ashley Sanders: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved



diff --git a/include/asterisk/logger.h b/include/asterisk/logger.h
index 18701a1..d06aecf 100644
--- a/include/asterisk/logger.h
+++ b/include/asterisk/logger.h
@@ -99,6 +99,21 @@
 	__attribute__((format(printf, 6, 7)));
 
 /*!
+ * \brief Retrieve the existing log channels
+ * \param logentry A callback to an updater function
+ * \param data Data passed into the callback for manipulation
+ *
+ * For each of the logging channels, logentry will be executed with the
+ * channel file name, log type, status of the log, and configuration levels.
+ *
+ * \retval 0 on success
+ * \retval 1 on failure
+ * \retval -2 on allocation error
+ */
+int ast_logger_get_channels(int (*logentry)(const char *channel, const char *type,
+	const char *status, const char *configuration, void *data), void *data);
+
+/*!
  * \brief Create a log channel
  *
  * \param log_channel Log channel to create
diff --git a/main/logger.c b/main/logger.c
index c856e72..6e540cb 100644
--- a/main/logger.c
+++ b/main/logger.c
@@ -936,11 +936,11 @@
 int ast_logger_rotate_channel(const char *log_channel)
 {
 	struct logchannel *f;
-	int success = 0;
+	int success = AST_LOGGER_FAILURE;
 
 	struct ast_str *filename = ast_str_create(64);
 	if (!filename) {
-		return -1;
+		return AST_LOGGER_ALLOC_ERROR;
 	}
 
 	ast_str_append(&filename, 0, "%s/%s", ast_config_AST_LOG_DIR, log_channel);
@@ -960,7 +960,7 @@
 			f->fileptr = NULL;
 			if (strcmp(ast_str_buffer(filename), f->filename) == 0) {
 				rotate_file(f->filename);
-				success = 1;
+				success = AST_LOGGER_SUCCESS;
 			}
 		}
 	}
@@ -1015,6 +1015,48 @@
 	return CLI_SUCCESS;
 }
 
+int ast_logger_get_channels(int (*logentry)(const char *channel, const char *type,
+	const char *status, const char *configuration, void *data), void *data)
+{
+	struct logchannel *chan;
+	struct ast_str *configs = ast_str_create(64);
+	int res = AST_LOGGER_SUCCESS;
+
+	if (!configs) {
+		return AST_LOGGER_ALLOC_ERROR;
+	}
+
+	AST_RWLIST_RDLOCK(&logchannels);
+	AST_RWLIST_TRAVERSE(&logchannels, chan, list) {
+		unsigned int level;
+
+		ast_str_reset(configs);
+
+		for (level = 0; level < ARRAY_LEN(levels); level++) {
+			if ((chan->logmask & (1 << level)) && levels[level]) {
+				ast_str_append(&configs, 0, "%s ", levels[level]);
+			}
+		}
+
+		res = logentry(chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" :
+			(chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"), chan->disabled ?
+			"Disabled" : "Enabled", ast_str_buffer(configs), data);
+
+		if (res) {
+			AST_RWLIST_UNLOCK(&logchannels);
+			ast_free(configs);
+			configs = NULL;
+			return AST_LOGGER_FAILURE;
+		}
+	}
+	AST_RWLIST_UNLOCK(&logchannels);
+
+	ast_free(configs);
+	configs = NULL;
+
+	return AST_LOGGER_SUCCESS;
+}
+
 /*! \brief CLI command to show logging system configuration */
 static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
diff --git a/res/ari/resource_asterisk.c b/res/ari/resource_asterisk.c
index 070a1aa..30684d2 100644
--- a/res/ari/resource_asterisk.c
+++ b/res/ari/resource_asterisk.c
@@ -628,6 +628,57 @@
 	ast_ari_response_no_content(response);
 }
 
+/*!
+ * \brief Process logger information and append to a json array
+ * \param channel Resource logger channel name path
+ * \param type Resource log type
+ * \param status Resource log status
+ * \param configuration Resource logger levels
+ * \param log_data_list Resource array
+ *
+ * \retval -1 if no resource exists
+ * \retval 0 if resource exists
+ */
+static int process_log_list(const char *channel, const char *type,
+	const char *status, const char *configuration, void *log_data_list)
+{
+	struct ast_json *logger_info;
+
+	logger_info = ast_json_pack("{s: s, s: s, s: s, s: s}",
+		"channel", channel, "type", type, "status", status, "configuration",
+		configuration);
+
+	if (!logger_info) {
+		return AST_LOGGER_FAILURE;
+	}
+
+	ast_json_array_append(log_data_list, logger_info);
+	return AST_LOGGER_SUCCESS;
+}
+
+void ast_ari_asterisk_list_log_channels(struct ast_variable *headers,
+	struct ast_ari_asterisk_list_log_channels_args *args,
+	struct ast_ari_response *response)
+{
+	struct ast_json *json;
+	int res;
+
+	json = ast_json_array_create();
+	res = ast_logger_get_channels(&process_log_list, json);
+
+	if (res == AST_LOGGER_FAILURE) {
+		ast_ari_response_error(response, 500, "Internal Server Error",
+			"Response body is not valid");
+		return;
+	} else if (res == AST_LOGGER_ALLOC_ERROR) {
+		ast_ari_response_error(response, 500, "Internal Server Error",
+			"Allocation Failed");
+		return;
+	}
+
+	ast_ari_response_ok(response, json);
+}
+
 void ast_ari_asterisk_add_log(struct ast_variable *headers,
 	struct ast_ari_asterisk_add_log_args *args,
 	struct ast_ari_response *response)
@@ -659,18 +710,18 @@
 	struct ast_ari_asterisk_rotate_log_args *args,
 	struct ast_ari_response *response)
 {
-	int success;
+	int res;
 
 	ast_assert(response != NULL);
 
-	success = ast_logger_rotate_channel(args->log_channel_name);
+	res = ast_logger_rotate_channel(args->log_channel_name);
 
-	if (success == 0) {
+	if (res == AST_LOGGER_FAILURE) {
 		ast_ari_response_error(
 			response, 404, "Not Found",
 			"Log channel does not exist");
 		return;
-	} else if (success == -1) {
+	} else if (res == AST_LOGGER_ALLOC_ERROR) {
 		ast_ari_response_error(
 			response, 500, "Internal Server Error",
 			"Allocation failed");
diff --git a/res/ari/resource_asterisk.h b/res/ari/resource_asterisk.h
index 5f84d07..a4a7da0 100644
--- a/res/ari/resource_asterisk.h
+++ b/res/ari/resource_asterisk.h
@@ -194,6 +194,17 @@
  * \param[out] response HTTP response
  */
 void ast_ari_asterisk_reload_module(struct ast_variable *headers, struct ast_ari_asterisk_reload_module_args *args, struct ast_ari_response *response);
+/*! Argument struct for ast_ari_asterisk_list_log_channels() */
+struct ast_ari_asterisk_list_log_channels_args {
+};
+/*!
+ * \brief Gets Asterisk log channel information.
+ *
+ * \param headers HTTP headers
+ * \param args Swagger parameters
+ * \param[out] response HTTP response
+ */
+void ast_ari_asterisk_list_log_channels(struct ast_variable *headers, struct ast_ari_asterisk_list_log_channels_args *args, struct ast_ari_response *response);
 /*! Argument struct for ast_ari_asterisk_add_log() */
 struct ast_ari_asterisk_add_log_args {
 	/*! The log channel to add */
diff --git a/res/res_ari_asterisk.c b/res/res_ari_asterisk.c
index 08ec951..fe6e3d3 100644
--- a/res/res_ari_asterisk.c
+++ b/res/res_ari_asterisk.c
@@ -721,6 +721,57 @@
 fin: __attribute__((unused))
 	return;
 }
+/*!
+ * \brief Parameter parsing callback for /asterisk/logging.
+ * \param get_params GET parameters in the HTTP request.
+ * \param path_vars Path variables extracted from the request.
+ * \param headers HTTP headers.
+ * \param[out] response Response to the HTTP request.
+ */
+static void ast_ari_asterisk_list_log_channels_cb(
+	struct ast_tcptls_session_instance *ser,
+	struct ast_variable *get_params, struct ast_variable *path_vars,
+	struct ast_variable *headers, struct ast_ari_response *response)
+{
+	struct ast_ari_asterisk_list_log_channels_args args = {};
+	RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
+#if defined(AST_DEVMODE)
+	int is_valid;
+	int code;
+#endif /* AST_DEVMODE */
+
+	ast_ari_asterisk_list_log_channels(headers, &args, response);
+#if defined(AST_DEVMODE)
+	code = response->response_code;
+
+	switch (code) {
+	case 0: /* Implementation is still a stub, or the code wasn't set */
+		is_valid = response->message == NULL;
+		break;
+	case 500: /* Internal Server Error */
+	case 501: /* Not Implemented */
+		is_valid = 1;
+		break;
+	default:
+		if (200 <= code && code <= 299) {
+			is_valid = ast_ari_validate_list(response->message,
+				ast_ari_validate_log_channel_fn());
+		} else {
+			ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/logging\n", code);
+			is_valid = 0;
+		}
+	}
+
+	if (!is_valid) {
+		ast_log(LOG_ERROR, "Response validation failed for /asterisk/logging\n");
+		ast_ari_response_error(response, 500,
+			"Internal Server Error", "Response validation failed");
+	}
+#endif /* AST_DEVMODE */
+
+fin: __attribute__((unused))
+	return;
+}
 int ast_ari_asterisk_add_log_parse_body(
 	struct ast_json *body,
 	struct ast_ari_asterisk_add_log_args *args)
@@ -1228,6 +1279,7 @@
 static struct stasis_rest_handlers asterisk_logging = {
 	.path_segment = "logging",
 	.callbacks = {
+		[AST_HTTP_GET] = ast_ari_asterisk_list_log_channels_cb,
 	},
 	.num_children = 1,
 	.children = { &asterisk_logging_logChannelName, }
diff --git a/rest-api/api-docs/asterisk.json b/rest-api/api-docs/asterisk.json
index 6b2c896..44bf024 100644
--- a/rest-api/api-docs/asterisk.json
+++ b/rest-api/api-docs/asterisk.json
@@ -297,6 +297,18 @@
 			]
 		},
 		{
+			"path": "/asterisk/logging",
+			"description": "Asterisk log channels",
+			"operations": [
+				{
+					"httpMethod": "GET",
+					"summary": "Gets Asterisk log channel information.",
+					"nickname": "listLogChannels",
+					"responseClass": "List[LogChannel]"
+				}
+			]
+		},
+		{
 			"path": "/asterisk/logging/{logChannelName}",
 			"description": "Asterisk log channel",
 			"operations": [

-- 
To view, visit https://gerrit.asterisk.org/986
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7bb08b93e3b938c991f3f56cc5d188654768a808
Gerrit-PatchSet: 14
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Scott Emidy <jemidy at digium.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: Ashley Sanders <asanders at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Mark Michelson <mmichelson at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Scott Emidy <jemidy at digium.com>



More information about the asterisk-code-review mailing list