diff options
author | Costa Tsaousis <costa@netdata.cloud> | 2024-01-23 20:20:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-23 20:20:41 +0200 |
commit | f466b8aef52c1ea394651f5fe6b4586a5e39e5af (patch) | |
tree | 96130302033cc5df6b2a5683931138c72e4ab88e /libnetdata/json/json-c-parser-inline.h | |
parent | 33412db1f50d833e56889ea4762725dfde5d6d8f (diff) |
DYNCFG: dynamically configured alerts (#16779)
* cleanup alerts
* fix references
* fix references
* fix references
* load alerts once and apply them to each node
* simplify health_create_alarm_entry()
* Compile without warnings with compiler flags:
-Wall -Wextra -Wformat=2 -Wshadow -Wno-format-nonliteral -Winit-self
* code re-organization and cleanup
* generate patterns when applying prototypes; give unique dyncfg names to all alerts
* eval expressions keep the source and the parsed_as as STRING pointers
* renamed host to node in dyncfg ids
* renamed host to node in dyncfg ids
* add all cloud roles to the list of parsed X-Netdata-Role header and also default to member access level
* working functionality
* code re-organization: moved health event-loop to a new file, moved health globals to health.c
* rrdcalctemplate is removed; alert_cfg is removed; foreach dimension is removed; RRDCALCs are now instanciated only when they are linked to RRDSETs
* dyncfg alert prototypes initialization for alerts
* health dyncfg split to separate file
* cleanup not-needed code
* normalize matches between parsing and json
* also detect !* for disabled alerts
* dyncfg capability disabled
* Store alert config part1
* Add rrdlabels_common_count
* wip health variables lookup without indexes
* Improve rrdlabels_common_count by reusing rrdlabels_find_label_with_key_unsafe with an additional parameter
* working variables with runtime lookup
* working variables with runtime lookup
* delete rrddimvar and rrdfamily index
* remove rrdsetvar; now all variables are in RRDVARs inside hosts and charts
* added /api/v1/variable that resolves a variable the same way alerts do
* remove rrdcalc from eval
* remove debug code
* remove duplicate assignment
* Fix memory leak
* all alert variables are now handled by alert_variable_lookup() and EVAL is now independent of alerts
* hide all internal structures of EVAL
* Enable -Wformat flag
Signed-off-by: Tasos Katsoulas <tasos@netdata.cloud>
* Adjust binding for calculation, warning, critical
* Remove unused macro
* Update config hash id
* use the right info and summary in alerts log
* use synchronous queries for alerts
* Handle cases when config_hash_id is missing from health_log
* remove deadlock from health worker
* parsing to json payload for health alert prototypes
* cleaner parsing and avoiding memory leaks in case of duplicate members in json
* fix left-over rename of function
* Keep original lookup field to send to the cloud
Cleanup / rename function to store config
Remove unused DEFINEs, functions
* Use ac->lookup
* link jobs to the host when the template is registered; do not accept running a function without a host
* full dyncfg support for health alerts, except action TEST
* working dyncfg additions, updates, removals
* fixed missing source, wrong status updates
* add alerts by type, component, classification, recipient and module at the /api/v2/alerts endpoint
* fix dyncfg unittest
* rename functions
* generalize the json-c parser macros and move them to libnetdata
* report progress when enabling and disabling dyncfg templates
* moved rrdcalc and rrdvar to health
* update alarms
* added schema for alerts; separated alert_action_options from rrdr_options; restructured the json payload for alerts
* enable parsed json alerts; allow sending back accepted but disabled
* added format_version for alerts payload; enables/disables status now is also inheritted by the status of the rules; fixed variable names in json output
* remove the RRDHOST pointer from DYNCFG
* Fix command field submitted to the cloud
* do not send updates to creation requests, for DYNCFG jobs
---------
Signed-off-by: Tasos Katsoulas <tasos@netdata.cloud>
Co-authored-by: Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com>
Co-authored-by: Tasos Katsoulas <tasos@netdata.cloud>
Co-authored-by: ilyam8 <ilya@netdata.cloud>
Diffstat (limited to 'libnetdata/json/json-c-parser-inline.h')
-rw-r--r-- | libnetdata/json/json-c-parser-inline.h | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/libnetdata/json/json-c-parser-inline.h b/libnetdata/json/json-c-parser-inline.h new file mode 100644 index 0000000000..dba57757cb --- /dev/null +++ b/libnetdata/json/json-c-parser-inline.h @@ -0,0 +1,154 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef NETDATA_JSON_C_PARSER_INLINE_H +#define NETDATA_JSON_C_PARSER_INLINE_H + +#define JSONC_PARSE_BOOL_OR_ERROR_AND_RETURN(jobj, path, member, dst, error) do { \ + json_object *_j; \ + if (json_object_object_get_ex(jobj, member, &_j) && json_object_is_type(_j, json_type_boolean)) \ + dst = json_object_get_boolean(_j); \ + else { \ + buffer_sprintf(error, "missing or invalid type for '%s.%s' boolean", path, member); \ + return false; \ + } \ +} while(0) + +#define JSONC_PARSE_TXT2STRING_OR_ERROR_AND_RETURN(jobj, path, member, dst, error, required) do { \ + json_object *_j; \ + if (json_object_object_get_ex(jobj, member, &_j) && json_object_is_type(_j, json_type_string)) { \ + string_freez(dst); \ + dst = string_strdupz(json_object_get_string(_j)); \ + } \ + else if(required) { \ + buffer_sprintf(error, "missing or invalid type for '%s.%s' string", path, member); \ + return false; \ + } \ +} while(0) + +#define JSONC_PARSE_TXT2PATTERN_OR_ERROR_AND_RETURN(jobj, path, member, dst, error) do { \ + json_object *_j; \ + if (json_object_object_get_ex(jobj, member, &_j) && json_object_is_type(_j, json_type_string)) { \ + string_freez(dst); \ + const char *_v = json_object_get_string(_j); \ + if(strcmp(_v, "*") == 0) \ + dst = NULL; \ + else \ + dst = string_strdupz(_v); \ + } \ + else { \ + buffer_sprintf(error, "missing or invalid type for '%s.%s' string", path, member); \ + return false; \ + } \ +} while(0) + +#define JSONC_PARSE_TXT2EXPRESSION_OR_ERROR_AND_RETURN(jobj, path, member, dst, error) do { \ + json_object *_j; \ + if (json_object_object_get_ex(jobj, member, &_j) && json_object_is_type(_j, json_type_string)) { \ + const char *_t = json_object_get_string(_j); \ + if(_t && *_t && strcmp(_t, "*") != 0) { \ + const char *_failed_at = NULL; \ + int _err = 0; \ + expression_free(dst); \ + dst = expression_parse(_t, &_failed_at, &_err); \ + if(!dst) { \ + buffer_sprintf(error, "expression '%s.%s' has a non-parseable expression '%s': %s at '%s'", \ + path, member, _t, expression_strerror(_err), _failed_at); \ + return false; \ + } \ + } \ + } \ + else { \ + buffer_sprintf(error, "missing or invalid type for '%s.%s' expression", path, member); \ + return false; \ + } \ +} while(0) + +#define JSONC_PARSE_ARRAY_OF_TXT2BITMAP_OR_ERROR_AND_RETURN(jobj, path, member, converter, dst, error) do { \ + json_object *_jarray; \ + if (json_object_object_get_ex(jobj, member, &_jarray) && json_object_is_type(_jarray, json_type_array)) { \ + size_t _num_options = json_object_array_length(_jarray); \ + dst = 0; \ + for (size_t _i = 0; _i < _num_options; ++_i) { \ + json_object *_joption = json_object_array_get_idx(_jarray, _i); \ + if (!json_object_is_type(_joption, json_type_string)) { \ + buffer_sprintf(error, "invalid type for '%s.%s' at index %zu", path, member, _i); \ + return false; \ + } \ + const char *_option_str = json_object_get_string(_joption); \ + typeof(dst) _bit = converter(_option_str); \ + if (_bit == 0) { \ + buffer_sprintf(error, "unknown option '%s' in '%s.%s' at index %zu", _option_str, path, member, _i); \ + return false; \ + } \ + dst |= _bit; \ + } \ + } else { \ + buffer_sprintf(error, "missing or invalid type for '%s.%s' array", path, member); \ + return false; \ + } \ +} while(0) + + +#define JSONC_PARSE_TXT2ENUM_OR_ERROR_AND_RETURN(jobj, path, member, converter, dst, error) do { \ + json_object *_j; \ + if (json_object_object_get_ex(jobj, member, &_j) && json_object_is_type(_j, json_type_string)) \ + dst = converter(json_object_get_string(_j)); \ + else { \ + buffer_sprintf(error, "missing or invalid type (expected text value) for '%s.%s' enum", path, member); \ + return false; \ + } \ +} while(0) + +#define JSONC_PARSE_INT_OR_ERROR_AND_RETURN(jobj, path, member, dst, error) do { \ + json_object *_j; \ + if (json_object_object_get_ex(jobj, member, &_j)) { \ + if (_j != NULL && json_object_is_type(_j, json_type_int)) \ + dst = json_object_get_int(_j); \ + else if (_j != NULL && json_object_is_type(_j, json_type_double)) \ + dst = (typeof(dst))json_object_get_double(_j); \ + else if (_j == NULL) \ + dst = 0; \ + else { \ + buffer_sprintf(error, "not supported type (expected int) for '%s.%s'", path, member); \ + return false; \ + } \ + } else { \ + buffer_sprintf(error, "missing or invalid type (expected double value or null) for '%s.%s'", path, member); \ + return false; \ + } \ +} while(0) + +#define JSONC_PARSE_DOUBLE_OR_ERROR_AND_RETURN(jobj, path, member, dst, error) do { \ + json_object *_j; \ + if (json_object_object_get_ex(jobj, member, &_j)) { \ + if (_j != NULL && json_object_is_type(_j, json_type_double)) \ + dst = json_object_get_double(_j); \ + else if (_j != NULL && json_object_is_type(_j, json_type_int)) \ + dst = (typeof(dst))json_object_get_int(_j); \ + else if (_j == NULL) \ + dst = NAN; \ + else { \ + buffer_sprintf(error, "not supported type (expected double) for '%s.%s'", path, member); \ + return false; \ + } \ + } else { \ + buffer_sprintf(error, "missing or invalid type (expected double value or null) for '%s.%s'", path, member); \ + return false; \ + } \ +} while(0) + +#define JSONC_PARSE_SUBOBJECT(jobj, path, member, dst, callback, error) do { \ + json_object *_j; \ + if (json_object_object_get_ex(jobj, member, &_j)) { \ + char _new_path[strlen(path) + strlen(member) + 2]; \ + snprintfz(_new_path, sizeof(_new_path), "%s%s%s", path, *path?".":"", member); \ + if (!callback(_j, _new_path, dst, error)) { \ + return false; \ + } \ + } else { \ + buffer_sprintf(error, "missing '%s.%s' object", path, member); \ + return false; \ + } \ +} while(0) + +#endif //NETDATA_JSON_C_PARSER_INLINE_H |