Update basic monitor parameters

All of the monitor parameters are now kept up to date when they are
altered. This simplifies the parameter list generation for the REST API.
This commit is contained in:
Markus Mäkelä
2018-08-14 15:08:51 +03:00
parent 4c8aee2b61
commit 419aefb173
3 changed files with 48 additions and 72 deletions

View File

@ -491,116 +491,96 @@ bool runtime_alter_server(SERVER *server, const char *key, const char *value)
bool runtime_alter_monitor(MXS_MONITOR *monitor, const char *key, const char *value)
{
mxs::SpinLockGuard guard(crt_lock);
bool valid = false;
mxs::SpinLockGuard guard(crt_lock);;
const MXS_MODULE *mod = get_module(monitor->module_name, MODULE_MONITOR);
if (!config_param_is_valid(config_monitor_params, key, value, NULL) &&
!config_param_is_valid(mod->parameters, key, value, NULL))
{
config_runtime_error("Invalid monitor parameter: %s", key);
return false;
}
else if (!value[0])
{
config_runtime_error("Empty value for parameter: %s", key);
return false;
}
monitor_stop(monitor);
monitor_set_parameter(monitor, key, value);
if (strcmp(key, CN_USER) == 0)
{
valid = true;
monitor_add_user(monitor, value, monitor->password);
}
else if (strcmp(key, CN_PASSWORD) == 0)
{
valid = true;
monitor_add_user(monitor, monitor->user, value);
}
else if (strcmp(key, CN_MONITOR_INTERVAL) == 0)
{
long ival = get_positive_int(value);
if (ival)
if (auto ival = get_positive_int(value))
{
valid = true;
monitor_set_interval(monitor, ival);
}
}
else if (strcmp(key, CN_BACKEND_CONNECT_TIMEOUT) == 0)
{
long ival = get_positive_int(value);
if (ival)
if (auto ival = get_positive_int(value))
{
valid = true;
monitor_set_network_timeout(monitor, MONITOR_CONNECT_TIMEOUT, ival, CN_BACKEND_CONNECT_TIMEOUT);
monitor_set_network_timeout(monitor, MONITOR_CONNECT_TIMEOUT, ival,
CN_BACKEND_CONNECT_TIMEOUT);
}
}
else if (strcmp(key, CN_BACKEND_WRITE_TIMEOUT) == 0)
{
long ival = get_positive_int(value);
if (ival)
if (auto ival = get_positive_int(value))
{
valid = true;
monitor_set_network_timeout(monitor, MONITOR_WRITE_TIMEOUT, ival, CN_BACKEND_WRITE_TIMEOUT);
monitor_set_network_timeout(monitor, MONITOR_WRITE_TIMEOUT, ival,
CN_BACKEND_WRITE_TIMEOUT);
}
}
else if (strcmp(key, CN_BACKEND_READ_TIMEOUT) == 0)
{
long ival = get_positive_int(value);
if (ival)
if (auto ival = get_positive_int(value))
{
valid = true;
monitor_set_network_timeout(monitor, MONITOR_READ_TIMEOUT, ival, CN_BACKEND_READ_TIMEOUT);
monitor_set_network_timeout(monitor, MONITOR_READ_TIMEOUT, ival,
CN_BACKEND_READ_TIMEOUT);
}
}
else if (strcmp(key, CN_BACKEND_CONNECT_ATTEMPTS) == 0)
{
long ival = get_positive_int(value);
if (ival)
if (auto ival = get_positive_int(value))
{
valid = true;
monitor_set_network_timeout(monitor, MONITOR_CONNECT_ATTEMPTS, ival, CN_BACKEND_CONNECT_ATTEMPTS);
monitor_set_network_timeout(monitor, MONITOR_CONNECT_ATTEMPTS, ival,
CN_BACKEND_CONNECT_ATTEMPTS);
}
}
else if (strcmp(key, CN_JOURNAL_MAX_AGE) == 0)
{
long ival = get_positive_int(value);
if (ival)
if (auto ival = get_positive_int(value))
{
valid = true;
monitor_set_journal_max_age(monitor, ival);
}
}
else if (strcmp(key, CN_SCRIPT_TIMEOUT) == 0)
{
long ival = get_positive_int(value);
if (ival)
if (auto ival = get_positive_int(value))
{
valid = true;
monitor_set_script_timeout(monitor, ival);
}
}
else if (config_param_is_valid(mod->parameters, key, value, NULL))
{
/** We're modifying module specific parameters and we need to stop the monitor */
monitor_stop(monitor);
if (monitor_remove_parameter(monitor, key) || value[0])
{
/** Either we're removing an existing parameter or adding a new one */
valid = true;
if (value[0])
{
MXS_CONFIG_PARAMETER p = {};
p.name = const_cast<char*>(key);
p.value = const_cast<char*>(value);
monitor_add_parameters(monitor, &p);
}
}
monitor_start(monitor, monitor->parameters);
}
if (valid)
{
monitor_serialize(monitor);
MXS_NOTICE("Updated monitor '%s': %s=%s", monitor->name, key, value);
}
else
{
config_runtime_error("Invalid monitor parameter: %s", key);
// This should be a module specific parameter
ss_dassert(config_param_is_valid(mod->parameters, key, value, NULL));
}
return valid;
monitor_serialize(monitor);
monitor_start(monitor, monitor->parameters);
MXS_NOTICE("Updated monitor '%s': %s=%s", monitor->name, key, value);
return true;
}
bool runtime_alter_service(Service *service, const char* zKey, const char* zValue)

View File

@ -118,6 +118,7 @@ void monitor_remove_server(MXS_MONITOR *mon, SERVER *server);
void monitor_add_user(MXS_MONITOR *, const char *, const char *);
void monitor_add_parameters(MXS_MONITOR *monitor, MXS_CONFIG_PARAMETER *params);
bool monitor_remove_parameter(MXS_MONITOR *monitor, const char *key);
void monitor_set_parameter(MXS_MONITOR *monitor, const char* key, const char* value);
void monitor_set_interval (MXS_MONITOR *, unsigned long);
bool monitor_set_network_timeout(MXS_MONITOR *, int, int, const char*);

View File

@ -882,6 +882,15 @@ void monitor_add_parameters(MXS_MONITOR *monitor, MXS_CONFIG_PARAMETER *params)
spinlock_release(&monitor->lock);
}
void monitor_set_parameter(MXS_MONITOR *monitor, const char* key, const char* value)
{
monitor_remove_parameter(monitor, key);
MXS_CONFIG_PARAMETER p = {};
p.name = const_cast<char*>(key);
p.value = const_cast<char*>(value);
monitor_add_parameters(monitor, &p);
}
bool monitor_remove_parameter(MXS_MONITOR *monitor, const char *key)
{
MXS_CONFIG_PARAMETER *prev = NULL;
@ -1797,20 +1806,6 @@ json_t* monitor_parameters_to_json(const MXS_MONITOR* monitor)
const MXS_MODULE* mod = get_module(monitor->module_name, MODULE_MONITOR);
config_add_module_params_json(monitor->parameters, mod->parameters, NULL, rval);
// As the parameters of the monitor aren't updated after startup, these
// need to be set explicitly
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));
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));
return rval;
}