diff --git a/src/mod/event_handlers/mod_kazoo/Makefile.am b/src/mod/event_handlers/mod_kazoo/Makefile.am index 516c192c93..668d9ffed2 100644 --- a/src/mod/event_handlers/mod_kazoo/Makefile.am +++ b/src/mod/event_handlers/mod_kazoo/Makefile.am @@ -12,6 +12,7 @@ mod_kazoo_la_SOURCES += kazoo_message.c mod_kazoo_la_SOURCES += kazoo_ei_config.c kazoo_ei_utils.c kazoo_event_stream.c mod_kazoo_la_SOURCES += kazoo_fetch_agent.c kazoo_node.c mod_kazoo_la_SOURCES += kazoo_endpoints.c +mod_kazoo_la_SOURCES += kz_node.c mod_kazoo_la_CFLAGS = $(AM_CFLAGS) @ERLANG_CFLAGS@ -D_REENTRANT -DERLANG_VERSION=@ERLANG_VERSION@ -DERLANG_MAJOR=@ERLANG_MAJOR@ -DERLANG_MINOR=@ERLANG_MINOR@ mod_kazoo_la_LIBADD = $(KAZOO_DEFS) $(switch_builddir)/libfreeswitch.la diff --git a/src/mod/event_handlers/mod_kazoo/kazoo_utils.c b/src/mod/event_handlers/mod_kazoo/kazoo_utils.c index deb33e1a03..f4050bb34a 100644 --- a/src/mod/event_handlers/mod_kazoo/kazoo_utils.c +++ b/src/mod/event_handlers/mod_kazoo/kazoo_utils.c @@ -445,16 +445,28 @@ SWITCH_DECLARE(char *) kz_event_expand(const char *in) return ret; } -char *kazoo_expand_header(switch_memory_pool_t *pool, switch_event_t *event, char *val) +SWITCH_DECLARE(char *) kz_expand(const char *in) +{ + switch_event_t *event = NULL; + char *ret = NULL; + kz_switch_core_base_headers_for_expand(&event); + ret = kz_event_expand_headers_check(event, in, NULL, NULL, 0); + switch_event_destroy(&event); + return ret; +} + +SWITCH_DECLARE(char *) kz_expand_pool(switch_memory_pool_t *pool, const char *in) { char *expanded; char *dup = NULL; - expanded = kz_event_expand_headers(event, val); + if(!(expanded = kz_expand(in))) { + return NULL; + } dup = switch_core_strdup(pool, expanded); - if (expanded != val) { - free(expanded); + if (expanded != in) { + switch_safe_free(expanded); } return dup; @@ -609,6 +621,17 @@ char * kz_expand_vars_pool(char *xml_str, switch_memory_pool_t *pool) { } +switch_status_t kz_json_api(const char * command, cJSON *args, cJSON **res) +{ + switch_status_t status = SWITCH_STATUS_SUCCESS; + cJSON *req = cJSON_CreateObject(); + cJSON_AddItemToObject(req, "command", cJSON_CreateString(command)); + cJSON_AddItemToObject(req, "data", args ? args : cJSON_CreateObject()); + status = switch_json_api_execute(req, NULL, res); + cJSON_Delete(req); + return status; +} + /* For Emacs: * Local Variables: * mode:c diff --git a/src/mod/event_handlers/mod_kazoo/kz_node.c b/src/mod/event_handlers/mod_kazoo/kz_node.c new file mode 100644 index 0000000000..2da5c4a658 --- /dev/null +++ b/src/mod/event_handlers/mod_kazoo/kz_node.c @@ -0,0 +1,89 @@ +#include "mod_kazoo.h" + +static int kz_nodes_module_names_array_callback(void *pArg, const char *module_name) +{ + cJSON *json = (cJSON *) pArg; + if(!strstr(module_name, "CORE")) { + cJSON_AddItemToArray(json, cJSON_CreateString(module_name)); + } + return 0; +} + +void kz_nodes_collect_media_role(cJSON *container) +{ + cJSON *retval = NULL; + if(kz_json_api("sofia.status.info", NULL, &retval) == SWITCH_STATUS_SUCCESS) { + if(retval != NULL && (!(retval->type & cJSON_NULL))) { + cJSON_AddItemToObject(container, "Media", cJSON_Duplicate(retval, 1)); + } + } + if(retval) { + cJSON_Delete(retval); + } +} + +void kz_nodes_collect_modules(cJSON *container) +{ + cJSON *modules = cJSON_CreateObject(); + cJSON *loaded = cJSON_CreateArray(); + cJSON *available = cJSON_CreateArray(); + switch_loadable_module_enumerate_available(SWITCH_GLOBAL_dirs.mod_dir, kz_nodes_module_names_array_callback, available); + switch_loadable_module_enumerate_loaded(kz_nodes_module_names_array_callback, loaded); + cJSON_AddItemToObject(modules, "available", available); + cJSON_AddItemToObject(modules, "loaded", loaded); + cJSON_AddItemToObject(container, "Modules", modules); +} + +void kz_nodes_collect_runtime(cJSON *container) +{ + cJSON *retval = NULL; + if(kz_json_api("status", NULL, &retval) == SWITCH_STATUS_SUCCESS) { + if(retval != NULL && (!(retval->type & cJSON_NULL))) { + cJSON_AddItemToObject(container, "Runtime-Info", cJSON_Duplicate(retval, 1)); + } + } + if(retval) { + cJSON_Delete(retval); + } +} + +void kz_nodes_collect_apps(cJSON *container) +{ + cJSON *apps = cJSON_CreateObject(); + cJSON *app = cJSON_CreateObject(); + cJSON_AddItemToObject(app, "Uptime", cJSON_CreateNumber(switch_core_uptime())); + cJSON_AddItemToObject(apps, "freeswitch", app); + cJSON_AddItemToObject(container, "WhApps", apps); +} + +void kz_nodes_collect_roles(cJSON *container) +{ + cJSON *roles = cJSON_CreateObject(); + cJSON_AddItemToObject(container, "Roles", roles); + kz_nodes_collect_media_role(roles); +} + +cJSON * kz_node_create() +{ + cJSON *node = cJSON_CreateObject(); + + kz_nodes_collect_apps(node); + kz_nodes_collect_runtime(node); + kz_nodes_collect_modules(node); + kz_nodes_collect_roles(node); + + return node; +} + +SWITCH_STANDARD_JSON_API(kz_node_info_json_function) +{ + cJSON * ret = kz_node_create(); + *json_reply = ret; + return SWITCH_STATUS_SUCCESS; +} + +void add_kz_node(switch_loadable_module_interface_t **module_interface) +{ + switch_json_api_interface_t *json_api_interface = NULL; + SWITCH_ADD_JSON_API(json_api_interface, "node.info", "JSON node API", kz_node_info_json_function, ""); +} diff --git a/src/mod/event_handlers/mod_kazoo/mod_kazoo.c b/src/mod/event_handlers/mod_kazoo/mod_kazoo.c index 13ab8de247..ca0538039b 100644 --- a/src/mod/event_handlers/mod_kazoo/mod_kazoo.c +++ b/src/mod/event_handlers/mod_kazoo/mod_kazoo.c @@ -77,6 +77,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_kazoo_load) { /* add our endpoints */ add_kz_endpoints(module_interface); + /* add our kz_node api */ + add_kz_node(module_interface); + /* add tweaks */ kz_tweaks_start(); diff --git a/src/mod/event_handlers/mod_kazoo/mod_kazoo.h b/src/mod/event_handlers/mod_kazoo/mod_kazoo.h index 2b02ee973a..bbf511a280 100644 --- a/src/mod/event_handlers/mod_kazoo/mod_kazoo.h +++ b/src/mod/event_handlers/mod_kazoo/mod_kazoo.h @@ -39,7 +39,6 @@ void remove_cli_api(); SWITCH_DECLARE(switch_status_t) kz_switch_core_merge_variables(switch_event_t *event); SWITCH_DECLARE(switch_status_t) kz_switch_core_base_headers_for_expand(switch_event_t **event); void kz_check_set_profile_var(switch_channel_t *channel, char* var, char *val); -char *kazoo_expand_header(switch_memory_pool_t *pool, switch_event_t *event, char *val); char* kz_switch_event_get_first_of(switch_event_t *event, const char *list[]); SWITCH_DECLARE(switch_status_t) kz_switch_event_add_variable_name_printf(switch_event_t *event, switch_stack_t stack, const char *val, const char *fmt, ...); void kz_xml_process(switch_xml_t cfg); @@ -47,6 +46,9 @@ void kz_event_decode(switch_event_t *event); char * kz_expand_vars(char *xml_str); char * kz_expand_vars_pool(char *xml_str, switch_memory_pool_t *pool); SWITCH_DECLARE(char *) kz_event_expand_headers(switch_event_t *event, const char *in); +SWITCH_DECLARE(char *) kz_expand(const char *in); +SWITCH_DECLARE(char *) kz_expand_pool(switch_memory_pool_t *pool, const char *in); +switch_status_t kz_json_api(const char * command, cJSON *args, cJSON **res); /* kazoo_endpoints.c */ void add_kz_endpoints(switch_loadable_module_interface_t **module_interface); @@ -56,6 +58,9 @@ void add_kz_endpoints(switch_loadable_module_interface_t **module_interface); void kz_tweaks_start(); void kz_tweaks_stop(); +/* kazoo_node.c */ +void add_kz_node(switch_loadable_module_interface_t **module_interface); + SWITCH_MODULE_LOAD_FUNCTION(mod_kazoo_load); SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_kazoo_shutdown);