Use parameters list when converting monitor parameters to json

The json conversion now processes the general monitor parameters similarly
to the module-specific settings.
This commit is contained in:
Esa Korhonen
2018-08-09 11:52:15 +03:00
parent 1572e088bf
commit eab0a22518
5 changed files with 67 additions and 54 deletions

View File

@ -31,6 +31,7 @@
#include <fstream>
#include <functional>
#include <map>
#include <numeric>
#include <set>
#include <string>
@ -3360,51 +3361,75 @@ void config_add_defaults(CONFIG_CONTEXT *ctx, const MXS_MODULE_PARAM *params)
}
}
static json_t* param_value_json(const MXS_CONFIG_PARAMETER* param,
const MXS_MODULE* mod)
/**
* Convert a config value to a json object.
*
* @param param The parameter value to convert
* @param param_info Type information for the parameter
* @return Json integer, boolean or string
*/
static json_t* param_value_to_json(const MXS_CONFIG_PARAMETER* param, const MXS_MODULE_PARAM* param_info)
{
ss_dassert(strcmp(param->name, param_info->name) == 0);
json_t* rval = NULL;
for (int i = 0; mod->parameters[i].name; i++)
switch (param_info->type)
{
if (strcmp(mod->parameters[i].name, param->name) == 0)
{
switch (mod->parameters[i].type)
{
case MXS_MODULE_PARAM_COUNT:
case MXS_MODULE_PARAM_INT:
rval = json_integer(strtol(param->value, NULL, 10));
break;
case MXS_MODULE_PARAM_COUNT:
case MXS_MODULE_PARAM_INT:
rval = json_integer(strtol(param->value, NULL, 10));
break;
case MXS_MODULE_PARAM_BOOL:
rval = json_boolean(config_truth_value(param->value));
break;
case MXS_MODULE_PARAM_BOOL:
rval = json_boolean(config_truth_value(param->value));
break;
default:
rval = json_string(param->value);
break;
}
}
default:
rval = json_string(param->value);
break;
}
return rval;
}
void config_add_module_params_json(const MXS_MODULE* mod, MXS_CONFIG_PARAMETER* parameters,
const MXS_MODULE_PARAM* type_params, json_t* output)
void config_add_module_params_json(const MXS_CONFIG_PARAMETER* parameters, const MXS_MODULE_PARAM* param_info,
const MXS_MODULE_PARAM* ignored_params, json_t* output)
{
set<string> param_set;
for (int i = 0; type_params[i].name; i++)
set<string> ignore_set;
if (ignored_params)
{
param_set.insert(type_params[i].name);
for (int i = 0; ignored_params[i].name; i++)
{
ignore_set.insert(ignored_params[i].name);
}
}
for (MXS_CONFIG_PARAMETER* p = parameters; p; p = p->next)
// The parameter values are added to the json output in the order they are in 'param_info'.
// Add the config values to a map and loop through 'param_info'.
// 'parameters' is a linked list while the others are null-terminated arrays.
std::map<string, const MXS_CONFIG_PARAMETER*> param_values_map;
for (const MXS_CONFIG_PARAMETER* param = parameters; param; param = param->next)
{
if (param_set.find(p->name) == param_set.end())
ss_dassert(param_values_map.count(param->name) == 0);
param_values_map[param->name] = param;
}
for (int i = 0; param_info[i].name; i++)
{
if (ignore_set.count(param_info[i].name) == 0)
{
json_object_set_new(output, p->name, param_value_json(p, mod));
auto item = param_values_map.find(param_info[i].name);
if (item != param_values_map.end())
{
json_object_set_new(output, param_info[i].name,
param_value_to_json(item->second, &param_info[i]));
}
else
{
// The parameter was not set in config and does not have a default value.
// Print a null value.
json_object_set_new(output, param_info[i].name, json_null());
}
}
}
}

View File

@ -401,7 +401,7 @@ json_t* filter_parameters_to_json(const SFilterDef& filter)
/** Add custom module parameters */
const MXS_MODULE* mod = get_module(filter->module.c_str(), MODULE_FILTER);
config_add_module_params_json(mod, filter->parameters, config_filter_params, rval);
config_add_module_params_json(filter->parameters, mod->parameters, config_filter_params, rval);
return rval;
}

View File

@ -161,17 +161,15 @@ bool config_create_ssl(const char* name, MXS_CONFIG_PARAMETER* params,
bool config_have_required_ssl_params(CONFIG_CONTEXT *obj);
/**
* @brief Add non-standard module type parameters to a JSON object
* @brief Add non-standard configuration parameters to a JSON object
*
* @param mod Module whose parameters are inspected
* @param parameters List of configuration parameters for the module
* @param type_params NULL terminated list of default module type parameters
* @param output Output JSON object where the parameters are added
* @param parameters List of configuration parameter values
* @param param_info Configuration parameter type information
* @param ignored_params List of parameters which should not be added to the output
* @param output Output JSON object where the parameters are added
*/
void config_add_module_params_json(const MXS_MODULE* mod,
MXS_CONFIG_PARAMETER* parameters,
const MXS_MODULE_PARAM* type_params,
json_t* output);
void config_add_module_params_json(const MXS_CONFIG_PARAMETER* parameters, const MXS_MODULE_PARAM* param_info,
const MXS_MODULE_PARAM* ignored_params, json_t* output);
/**
* @brief Convert object names to correct format

View File

@ -1790,22 +1790,12 @@ static const char* monitor_state_to_string(int state)
json_t* monitor_parameters_to_json(const MXS_MONITOR* monitor)
{
json_t* rval = json_object();
json_object_set_new(rval, CN_USER, json_string(monitor->user));
json_object_set_new(rval, CN_PASSWORD, json_string(monitor->password));
json_object_set_new(rval, CN_MONITOR_INTERVAL, json_integer(monitor->interval));
json_object_set_new(rval, CN_BACKEND_CONNECT_TIMEOUT, json_integer(monitor->connect_timeout));
json_object_set_new(rval, CN_BACKEND_READ_TIMEOUT, json_integer(monitor->read_timeout));
json_object_set_new(rval, CN_BACKEND_WRITE_TIMEOUT, json_integer(monitor->write_timeout));
json_object_set_new(rval, CN_BACKEND_CONNECT_ATTEMPTS, json_integer(monitor->connect_attempts));
json_object_set_new(rval, CN_JOURNAL_MAX_AGE, json_integer(monitor->journal_max_age));
// TODO: print disk_space_threshold
json_object_set_new(rval, CN_DISK_SPACE_CHECK_INTERVAL, json_integer(monitor->disk_space_check_interval));
json_object_set_new(rval, CN_SCRIPT, json_string(monitor->script));
json_object_set_new(rval, CN_SCRIPT_TIMEOUT, json_integer(monitor->script_timeout));
// TODO: print events
/** Add custom module parameters */
// The 'monitor->parameters' contain some items which are printed specially and should be ignored here.
static const MXS_MODULE_PARAM already_printed[] = {{CN_TYPE}, {CN_MODULE}, {CN_SERVERS}};
config_add_module_params_json(monitor->parameters, config_monitor_params, already_printed, rval);
/** Add module-specific parameters */
const MXS_MODULE* mod = get_module(monitor->module_name, MODULE_MONITOR);
config_add_module_params_json(mod, monitor->parameters, config_monitor_params, rval);
config_add_module_params_json(monitor->parameters, mod->parameters, NULL, rval);
return rval;
}

View File

@ -2001,7 +2001,7 @@ json_t* service_parameters_to_json(const SERVICE* service)
/** Add custom module parameters */
const MXS_MODULE* mod = get_module(service->routerModule, MODULE_ROUTER);
config_add_module_params_json(mod, service->svc_config_param, config_service_params, rval);
config_add_module_params_json(service->svc_config_param, mod->parameters, config_service_params, rval);
return rval;
}