| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "alarm_config.h" |
| 4 | |
| 5 | #include "alarm/v1/config.pb.h" |
| 6 | |
| 7 | #include "libnetdata/libnetdata.h" |
| 8 | |
| 9 | #include "schema_wrapper_utils.h" |
| 10 | |
| 11 | using namespace alarms::v1; |
| 12 | |
| 13 | void destroy_aclk_alarm_configuration(struct aclk_alarm_configuration *cfg) |
| 14 | { |
| 15 | freez(cfg->alarm); |
| 16 | freez(cfg->tmpl); |
| 17 | freez(cfg->on_chart); |
| 18 | freez(cfg->classification); |
| 19 | freez(cfg->type); |
| 20 | freez(cfg->component); |
| 21 | freez(cfg->os); |
| 22 | freez(cfg->hosts); |
| 23 | freez(cfg->plugin); |
| 24 | freez(cfg->module); |
| 25 | freez(cfg->charts); |
| 26 | freez(cfg->lookup); |
| 27 | freez(cfg->every); |
| 28 | freez(cfg->units); |
| 29 | freez(cfg->green); |
| 30 | freez(cfg->red); |
| 31 | freez(cfg->calculation_expr); |
| 32 | freez(cfg->warning_expr); |
| 33 | freez(cfg->critical_expr); |
| 34 | freez(cfg->recipient); |
| 35 | freez(cfg->exec); |
| 36 | freez(cfg->delay); |
| 37 | freez(cfg->repeat); |
| 38 | freez(cfg->info); |
| 39 | freez(cfg->options); |
| 40 | freez(cfg->host_labels); |
| 41 | freez(cfg->p_db_lookup_dimensions); |
| 42 | freez(cfg->p_db_lookup_method); |
| 43 | freez(cfg->p_db_lookup_options); |
| 44 | freez(cfg->chart_labels); |
| 45 | freez(cfg->summary); |
| 46 | } |
| 47 | |
| 48 | char *generate_provide_alarm_configuration(size_t *len, struct provide_alarm_configuration *data) |
| 49 | { |
| 50 | ProvideAlarmConfiguration msg; |
| 51 | AlarmConfiguration *cfg = msg.mutable_config(); |
| 52 | |
| 53 | msg.set_config_hash(data->cfg_hash); |
| 54 | |
| 55 | if (data->cfg.alarm) |
| 56 | cfg->set_alarm(data->cfg.alarm); |
| 57 | if (data->cfg.tmpl) |
| 58 | cfg->set_template_(data->cfg.tmpl); |
| 59 | if(data->cfg.on_chart) |
| 60 | cfg->set_on_chart(data->cfg.on_chart); |
| 61 | if (data->cfg.classification) |
| 62 | cfg->set_classification(data->cfg.classification); |
| 63 | if (data->cfg.type) |
| 64 | cfg->set_type(data->cfg.type); |
| 65 | if (data->cfg.component) |
| 66 | cfg->set_component(data->cfg.component); |
| 67 | if (data->cfg.os) |
| 68 | cfg->set_os(data->cfg.os); |
| 69 | if (data->cfg.hosts) |
| 70 | cfg->set_hosts(data->cfg.hosts); |
| 71 | if (data->cfg.plugin) |
| 72 | cfg->set_plugin(data->cfg.plugin); |
| 73 | if(data->cfg.module) |
| 74 | cfg->set_module(data->cfg.module); |
| 75 | if(data->cfg.charts) |
| 76 | cfg->set_charts(data->cfg.charts); |
| 77 | if(data->cfg.lookup) |
| 78 | cfg->set_lookup(data->cfg.lookup); |
| 79 | if(data->cfg.every) |
| 80 | cfg->set_every(data->cfg.every); |
| 81 | if(data->cfg.units) |
| 82 | cfg->set_units(data->cfg.units); |
| 83 | if (data->cfg.green) |
| 84 | cfg->set_green(data->cfg.green); |
| 85 | if (data->cfg.red) |
| 86 | cfg->set_red(data->cfg.red); |
| 87 | if (data->cfg.calculation_expr) |
| 88 | cfg->set_calculation_expr(data->cfg.calculation_expr); |
| 89 | if (data->cfg.warning_expr) |
| 90 | cfg->set_warning_expr(data->cfg.warning_expr); |
| 91 | if (data->cfg.critical_expr) |
| 92 | cfg->set_critical_expr(data->cfg.critical_expr); |
| 93 | if (data->cfg.recipient) |
| 94 | cfg->set_recipient(data->cfg.recipient); |
| 95 | if (data->cfg.exec) |
| 96 | cfg->set_exec(data->cfg.exec); |
| 97 | if (data->cfg.delay) |
| 98 | cfg->set_delay(data->cfg.delay); |
| 99 | if (data->cfg.repeat) |
| 100 | cfg->set_repeat(data->cfg.repeat); |
| 101 | if (data->cfg.info) |
| 102 | cfg->set_info(data->cfg.info); |
| 103 | if (data->cfg.options) |
| 104 | cfg->set_options(data->cfg.options); |
| 105 | if (data->cfg.host_labels) |
| 106 | cfg->set_host_labels(data->cfg.host_labels); |
| 107 | |
| 108 | cfg->set_p_db_lookup_after(data->cfg.p_db_lookup_after); |
| 109 | cfg->set_p_db_lookup_before(data->cfg.p_db_lookup_before); |
| 110 | if (data->cfg.p_db_lookup_dimensions) |
| 111 | cfg->set_p_db_lookup_dimensions(data->cfg.p_db_lookup_dimensions); |
| 112 | if (data->cfg.p_db_lookup_method) |
| 113 | cfg->set_p_db_lookup_method(data->cfg.p_db_lookup_method); |
| 114 | if (data->cfg.p_db_lookup_options) |
| 115 | cfg->set_p_db_lookup_options(data->cfg.p_db_lookup_options); |
| 116 | cfg->set_p_update_every(data->cfg.p_update_every); |
| 117 | |
| 118 | if (data->cfg.chart_labels) |
| 119 | cfg->set_chart_labels(data->cfg.chart_labels); |
| 120 | if (data->cfg.summary) |
| 121 | cfg->set_summary(data->cfg.summary); |
| 122 | |
| 123 | *len = PROTO_COMPAT_MSG_SIZE(msg); |
| 124 | char *bin = (char*)mallocz(*len); |
| 125 | if (!msg.SerializeToArray(bin, *len)) |
| 126 | return NULL; |
| 127 | |
| 128 | return bin; |
| 129 | } |
| 130 | |
| 131 | char *parse_send_alarm_configuration(const char *data, size_t len) |
| 132 | { |
| 133 | SendAlarmConfiguration msg; |
| 134 | if (!msg.ParseFromArray(data, len)) |
| 135 | return NULL; |
| 136 | if (!msg.config_hash().c_str()) |
| 137 | return NULL; |
| 138 | return strdupz(msg.config_hash().c_str()); |
| 139 | } |
| 140 |