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

Scott Emidy asteriskteam at digium.com
Wed Jul 29 13:26:40 CDT 2015


Scott Emidy has uploaded a new change for review.

  https://gerrit.asterisk.org/986

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/ari_model_validators.c
M res/ari/ari_model_validators.h
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
8 files changed, 313 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/86/986/2

diff --git a/include/asterisk/logger.h b/include/asterisk/logger.h
index 2215115..01c87d4 100644
--- a/include/asterisk/logger.h
+++ b/include/asterisk/logger.h
@@ -260,6 +260,22 @@
 unsigned int ast_verbose_get_by_module(const char *module) __attribute__((deprecated));
 
 /*!
+ * \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 1 on success
+ * \retval 0 on failure
+ */
+int ast_logger_get_channels(int (*logentry)(const char *channel, const char *type,
+                                            const char *status, const char *configuration,
+                                            void *data),
+                            void *data);
+
+/*!
  * \brief Register a new logger level
  * \param name The name of the level to be registered
  * \retval -1 if an error occurs
diff --git a/main/logger.c b/main/logger.c
index f84221f..853a737 100644
--- a/main/logger.c
+++ b/main/logger.c
@@ -1125,6 +1125,42 @@
 	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);
+
+    AST_RWLIST_RDLOCK(&logchannels);
+
+    if(!configs) {
+        return 0;
+    }
+
+    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]);
+            }
+        }
+
+        logentry(chan->filename, chan->type == LOGTYPE_CONSOLE ? "Console" :
+                        (chan->type == LOGTYPE_SYSLOG ? "Syslog" : "File"),
+                        chan->disabled ? "Disabled" : "Enabled",
+                        ast_str_buffer(configs), data);
+    }
+    AST_RWLIST_UNLOCK(&logchannels);
+    ast_free(configs);
+    configs = NULL;
+    return 1;
+}
+
 struct verb {
 	void (*verboser)(const char *string);
 	AST_LIST_ENTRY(verb) list;
diff --git a/res/ari/ari_model_validators.c b/res/ari/ari_model_validators.c
index 6675896..7461175 100644
--- a/res/ari/ari_model_validators.c
+++ b/res/ari/ari_model_validators.c
@@ -362,6 +362,92 @@
 	return ast_ari_validate_config_tuple;
 }
 
+int ast_ari_validate_log_channel(struct ast_json *json)
+{
+	int res = 1;
+	struct ast_json_iter *iter;
+	int has_channel = 0;
+	int has_configuration = 0;
+	int has_status = 0;
+	int has_type = 0;
+
+	for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
+		if (strcmp("channel", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_channel = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI LogChannel field channel failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("configuration", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_configuration = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI LogChannel field configuration failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("status", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_status = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI LogChannel field status failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("type", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_type = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI LogChannel field type failed validation\n");
+				res = 0;
+			}
+		} else
+		{
+			ast_log(LOG_ERROR,
+				"ARI LogChannel has undocumented field %s\n",
+				ast_json_object_iter_key(iter));
+			res = 0;
+		}
+	}
+
+	if (!has_channel) {
+		ast_log(LOG_ERROR, "ARI LogChannel missing required field channel\n");
+		res = 0;
+	}
+
+	if (!has_configuration) {
+		ast_log(LOG_ERROR, "ARI LogChannel missing required field configuration\n");
+		res = 0;
+	}
+
+	if (!has_status) {
+		ast_log(LOG_ERROR, "ARI LogChannel missing required field status\n");
+		res = 0;
+	}
+
+	if (!has_type) {
+		ast_log(LOG_ERROR, "ARI LogChannel missing required field type\n");
+		res = 0;
+	}
+
+	return res;
+}
+
+ari_validator ast_ari_validate_log_channel_fn(void)
+{
+	return ast_ari_validate_log_channel;
+}
+
 int ast_ari_validate_module(struct ast_json *json)
 {
 	int res = 1;
diff --git a/res/ari/ari_model_validators.h b/res/ari/ari_model_validators.h
index e122ded..1803f57 100644
--- a/res/ari/ari_model_validators.h
+++ b/res/ari/ari_model_validators.h
@@ -225,6 +225,24 @@
 ari_validator ast_ari_validate_config_tuple_fn(void);
 
 /*!
+ * \brief Validator for LogChannel.
+ *
+ * Details of an Asterisk log channel
+ *
+ * \param json JSON object to validate.
+ * \returns True (non-zero) if valid.
+ * \returns False (zero) if invalid.
+ */
+int ast_ari_validate_log_channel(struct ast_json *json);
+
+/*!
+ * \brief Function pointer to ast_ari_validate_log_channel().
+ *
+ * See \ref ast_ari_model_validators.h for more details.
+ */
+ari_validator ast_ari_validate_log_channel_fn(void);
+
+/*!
  * \brief Validator for Module.
  *
  * Details of an Asterisk module
@@ -1283,6 +1301,11 @@
  * ConfigTuple
  * - attribute: string (required)
  * - value: string (required)
+ * LogChannel
+ * - channel: string (required)
+ * - configuration: string (required)
+ * - status: string (required)
+ * - type: string (required)
  * Module
  * - description: string (required)
  * - name: string (required)
diff --git a/res/ari/resource_asterisk.c b/res/ari/resource_asterisk.c
index 2b6b6bc..d2db5c0 100644
--- a/res/ari/resource_asterisk.c
+++ b/res/ari/resource_asterisk.c
@@ -427,6 +427,35 @@
 	return 1;
 }
 
+/*!
+ * \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 0 if no resource exists
+ * \retval 1 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 0;
+    }
+    ast_json_array_append(log_data_list, logger_info);
+    return 1;
+}
+
 void ast_ari_asterisk_list_modules(struct ast_variable *headers,
 	struct ast_ari_asterisk_list_modules_args *args,
 	struct ast_ari_response *response)
@@ -627,6 +656,18 @@
 	ast_ari_response_no_content(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)
+{
+    struct ast_json *json;
+
+    json = ast_json_array_create();
+    ast_logger_get_channels(&process_log_list, json);
+
+    ast_ari_response_ok(response, json);
+}
+
 void ast_ari_asterisk_get_global_var(struct ast_variable *headers,
 	struct ast_ari_asterisk_get_global_var_args *args,
 	struct ast_ari_response *response)
diff --git a/res/ari/resource_asterisk.h b/res/ari/resource_asterisk.h
index 1afc093..960b982 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_get_global_var() */
 struct ast_ari_asterisk_get_global_var_args {
 	/*! The variable to get */
diff --git a/res/res_ari_asterisk.c b/res/res_ari_asterisk.c
index ea8ddbb..c283552 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_get_global_var_parse_body(
 	struct ast_json *body,
 	struct ast_ari_asterisk_get_global_var_args *args)
@@ -989,6 +1040,15 @@
 	.children = { &asterisk_modules_moduleName, }
 };
 /*! \brief REST handler for /api-docs/asterisk.{format} */
+static struct stasis_rest_handlers asterisk_logging = {
+	.path_segment = "logging",
+	.callbacks = {
+		[AST_HTTP_GET] = ast_ari_asterisk_list_log_channels_cb,
+	},
+	.num_children = 0,
+	.children = {  }
+};
+/*! \brief REST handler for /api-docs/asterisk.{format} */
 static struct stasis_rest_handlers asterisk_variable = {
 	.path_segment = "variable",
 	.callbacks = {
@@ -1003,8 +1063,8 @@
 	.path_segment = "asterisk",
 	.callbacks = {
 	},
-	.num_children = 4,
-	.children = { &asterisk_config,&asterisk_info,&asterisk_modules,&asterisk_variable, }
+	.num_children = 5,
+	.children = { &asterisk_config,&asterisk_info,&asterisk_modules,&asterisk_logging,&asterisk_variable, }
 };
 
 static int load_module(void)
diff --git a/rest-api/api-docs/asterisk.json b/rest-api/api-docs/asterisk.json
index 2705f45..046c8d3 100644
--- a/rest-api/api-docs/asterisk.json
+++ b/rest-api/api-docs/asterisk.json
@@ -296,6 +296,18 @@
 				}
 			]
 		},
+        {
+            "path": "/asterisk/logging",
+            "description": "Asterisk log channels",
+            "operations": [
+                {
+                    "httpMethod": "GET",
+                    "summary": "Gets Asterisk log channel information.",
+                    "nickname": "listLogChannels",
+                    "responseClass": "List[LogChannel]"
+                }
+            ]
+        },
 		{
 			"path": "/asterisk/variable",
 			"description": "Global variables",
@@ -533,6 +545,32 @@
 				}
 			}
 		},
+        "LogChannel": {
+            "id": "LogChannel",
+            "description": "Details of an Asterisk log channel",
+            "properties": {
+                "channel": {
+                    "type": "string",
+                    "description": "The log channel path",
+                    "required": true
+                },
+                "type": {
+                    "type": "string",
+                    "description": "Types of logs for the log channel",
+                    "required": true
+                },
+                "status": {
+                    "type": "string",
+                    "description": "Whether or not a log type is enabled",
+                    "required": true
+                },
+                "configuration": {
+                    "type": "string",
+                    "description": "The various log levels",
+                    "required": true
+                }
+            }
+        },
 		"Variable": {
 			"id": "Variable",
 			"description": "The value of a channel variable",

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7bb08b93e3b938c991f3f56cc5d188654768a808
Gerrit-PatchSet: 2
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Scott Emidy <jemidy at digium.com>



More information about the asterisk-code-review mailing list