| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "api_v2_contexts_alerts.h" |
| 4 | |
| 5 | void contexts_v2_alert_config_to_json_from_sql_alert_config_data(struct sql_alert_config_data *t, void *data) { |
| 6 | struct alert_transitions_callback_data *d = data; |
| 7 | BUFFER *wb = d->wb; |
| 8 | bool debug = d->debug; |
| 9 | d->configs_added++; |
| 10 | |
| 11 | bool is_mcp = d->ctl && (d->ctl->options & CONTEXTS_OPTION_MCP); |
| 12 | |
| 13 | if(d->only_one_config) |
| 14 | buffer_json_add_array_item_object(wb); // alert config |
| 15 | |
| 16 | { |
| 17 | if(!is_mcp) |
| 18 | buffer_json_member_add_string(wb, "name", t->name); |
| 19 | |
| 20 | buffer_json_member_add_uuid_ptr(wb, "config_hash_id", t->config_hash_id); |
| 21 | |
| 22 | buffer_json_member_add_object(wb, "selectors"); |
| 23 | { |
| 24 | bool is_template = t->selectors.on_template && *t->selectors.on_template ? true : false; |
| 25 | buffer_json_member_add_string(wb, "type", is_template ? "template" : "alarm"); |
| 26 | buffer_json_member_add_string(wb, "on", is_template ? t->selectors.on_template : t->selectors.on_key); |
| 27 | |
| 28 | buffer_json_member_add_string(wb, "families", t->selectors.families); |
| 29 | buffer_json_member_add_string(wb, "host_labels", t->selectors.host_labels); |
| 30 | buffer_json_member_add_string(wb, "chart_labels", t->selectors.chart_labels); |
| 31 | } |
| 32 | buffer_json_object_close(wb); // selectors |
| 33 | |
| 34 | buffer_json_member_add_object(wb, "value"); // value |
| 35 | { |
| 36 | // buffer_json_member_add_string(wb, "every", t->value.every); // does not exist in Netdata Cloud |
| 37 | buffer_json_member_add_string(wb, "units", t->value.units); |
| 38 | buffer_json_member_add_uint64(wb, "update_every", t->value.update_every); |
| 39 | |
| 40 | if (t->value.db.after || debug) { |
| 41 | buffer_json_member_add_object(wb, "db"); |
| 42 | { |
| 43 | // buffer_json_member_add_string(wb, "lookup", t->value.db.lookup); // does not exist in Netdata Cloud |
| 44 | |
| 45 | buffer_json_member_add_time_t_formatted(wb, "after", t->value.db.after, d->ctl && (d->ctl->options & CONTEXTS_OPTION_RFC3339)); |
| 46 | buffer_json_member_add_time_t_formatted(wb, "before", t->value.db.before, d->ctl && (d->ctl->options & CONTEXTS_OPTION_RFC3339)); |
| 47 | buffer_json_member_add_string(wb, "time_group_condition", alerts_group_conditions_id2txt(t->value.db.time_group_condition)); |
| 48 | buffer_json_member_add_double(wb, "time_group_value", t->value.db.time_group_value); |
| 49 | buffer_json_member_add_string(wb, "dims_group", alerts_dims_grouping_id2group(t->value.db.dims_group)); |
| 50 | buffer_json_member_add_string(wb, "data_source", alerts_data_source_id2source(t->value.db.data_source)); |
| 51 | buffer_json_member_add_string(wb, "method", t->value.db.method); |
| 52 | buffer_json_member_add_string(wb, "dimensions", t->value.db.dimensions); |
| 53 | rrdr_options_to_buffer_json_array(wb, "options", (RRDR_OPTIONS)t->value.db.options); |
| 54 | } |
| 55 | buffer_json_object_close(wb); // db |
| 56 | } |
| 57 | |
| 58 | if (t->value.calc || debug) |
| 59 | buffer_json_member_add_string(wb, "calc", t->value.calc); |
| 60 | } |
| 61 | buffer_json_object_close(wb); // value |
| 62 | |
| 63 | if (t->status.warn || t->status.crit || debug) { |
| 64 | buffer_json_member_add_object(wb, "status"); // status |
| 65 | { |
| 66 | NETDATA_DOUBLE green = t->status.green ? str2ndd(t->status.green, NULL) : NAN; |
| 67 | NETDATA_DOUBLE red = t->status.red ? str2ndd(t->status.red, NULL) : NAN; |
| 68 | |
| 69 | if (!is_mcp && (!isnan(green) || debug)) |
| 70 | buffer_json_member_add_double(wb, "green", green); |
| 71 | |
| 72 | if (!is_mcp && (!isnan(red) || debug)) |
| 73 | buffer_json_member_add_double(wb, "red", red); |
| 74 | |
| 75 | if (t->status.warn || debug) |
| 76 | buffer_json_member_add_string(wb, "warn", t->status.warn); |
| 77 | |
| 78 | if (t->status.crit || debug) |
| 79 | buffer_json_member_add_string(wb, "crit", t->status.crit); |
| 80 | } |
| 81 | buffer_json_object_close(wb); // status |
| 82 | } |
| 83 | |
| 84 | buffer_json_member_add_object(wb, "notification"); |
| 85 | { |
| 86 | buffer_json_member_add_string(wb, "type", "agent"); |
| 87 | buffer_json_member_add_string(wb, "exec", t->notification.exec ? t->notification.exec : NULL); |
| 88 | buffer_json_member_add_string(wb, "to", t->notification.to_key ? t->notification.to_key : string2str(localhost->health.default_recipient)); |
| 89 | buffer_json_member_add_string(wb, "delay", t->notification.delay); |
| 90 | buffer_json_member_add_string(wb, "repeat", t->notification.repeat); |
| 91 | buffer_json_member_add_string(wb, "options", t->notification.options); |
| 92 | } |
| 93 | buffer_json_object_close(wb); // notification |
| 94 | |
| 95 | buffer_json_member_add_string(wb, "class", t->classification); |
| 96 | buffer_json_member_add_string(wb, "component", t->component); |
| 97 | buffer_json_member_add_string(wb, "type", t->type); |
| 98 | buffer_json_member_add_string(wb, "info", t->info); |
| 99 | buffer_json_member_add_string(wb, "summary", t->summary); |
| 100 | // buffer_json_member_add_string(wb, "source", t->source); // moved to alert instance |
| 101 | } |
| 102 | |
| 103 | if(d->only_one_config) |
| 104 | buffer_json_object_close(wb); |
| 105 | } |
| 106 | |
| 107 | int contexts_v2_alert_config_to_json(struct web_client *w, const char *config_hash_id) { |
| 108 | struct alert_transitions_callback_data data = { |
| 109 | .wb = w->response.data, |
| 110 | .debug = false, |
| 111 | .only_one_config = false, |
| 112 | }; |
| 113 | DICTIONARY *configs = dictionary_create(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE); |
| 114 | dictionary_set(configs, config_hash_id, NULL, 0); |
| 115 | |
| 116 | buffer_flush(w->response.data); |
| 117 | |
| 118 | buffer_json_initialize(w->response.data, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT); |
| 119 | |
| 120 | int added = sql_get_alert_configuration(configs, contexts_v2_alert_config_to_json_from_sql_alert_config_data, &data, false); |
| 121 | buffer_json_finalize(w->response.data); |
| 122 | |
| 123 | int ret = HTTP_RESP_OK; |
| 124 | |
| 125 | if(added <= 0) { |
| 126 | buffer_flush(w->response.data); |
| 127 | w->response.data->content_type = CT_TEXT_PLAIN; |
| 128 | if(added < 0) { |
| 129 | buffer_strcat(w->response.data, "Failed to execute SQL query."); |
| 130 | ret = HTTP_RESP_INTERNAL_SERVER_ERROR; |
| 131 | } |
| 132 | else { |
| 133 | buffer_strcat(w->response.data, "Config is not found."); |
| 134 | ret = HTTP_RESP_NOT_FOUND; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | dictionary_destroy(configs); |
| 139 | return ret; |
| 140 | } |