summaryrefslogtreecommitdiffstats
path: root/health
diff options
context:
space:
mode:
authorStelios Fragkakis <52996999+stelfrag@users.noreply.github.com>2023-06-22 01:16:57 +0300
committerGitHub <noreply@github.com>2023-06-22 01:16:57 +0300
commitf3efdba1a0cff9b8d5a2df7149824dba2b59b653 (patch)
tree79315d2a3037931627a831737b0d5acb55e6e08b /health
parent8e8531d402b4038c12dfb73431559082787ebaa8 (diff)
New alerts endpoint (#15232)
* alerts / alerts_log v2 * Add global_id to ae Populate entries with global id * Remove transition id from template Change history to instances * Link ae to rc in all cases Code cleanup
Diffstat (limited to 'health')
-rw-r--r--health/health.c3
-rw-r--r--health/health.h2
-rw-r--r--health/health_json.c89
-rw-r--r--health/health_log.c1
4 files changed, 95 insertions, 0 deletions
diff --git a/health/health.c b/health/health.c
index 3dd3f96162..97df34fcf8 100644
--- a/health/health.c
+++ b/health/health.c
@@ -1128,6 +1128,7 @@ void *health_main(void *ptr) {
rc->last_status_change = now;
rc->last_updated = now;
rc->value = NAN;
+ rc->ae = ae;
#ifdef ENABLE_ACLK
if (netdata_cloud_enabled)
@@ -1396,6 +1397,7 @@ void *health_main(void *ptr) {
rc->last_status_change = now;
rc->old_status = rc->status;
rc->status = status;
+ rc->ae = ae;
}
rc->last_updated = now;
@@ -1473,6 +1475,7 @@ void *health_main(void *ptr) {
ae->flags |= HEALTH_ENTRY_RUN_ONCE;
}
rc->run_flags |= RRDCALC_FLAG_RUN_ONCE;
+ rc->ae = ae;
health_process_notifications(host, ae);
debug(D_HEALTH, "Notification sent for the repeating alarm %u.", ae->alarm_id);
health_alarm_wait_for_execution(ae);
diff --git a/health/health.h b/health/health.h
index c36aabac7e..8b967c197b 100644
--- a/health/health.h
+++ b/health/health.h
@@ -40,6 +40,8 @@ void health_reload(void);
void health_aggregate_alarms(RRDHOST *host, BUFFER *wb, BUFFER* context, RRDCALC_STATUS status);
void health_alarms2json(RRDHOST *host, BUFFER *wb, int all);
+void health_alert2json(RRDHOST *host, BUFFER *wb, ALERT_OPTIONS all, Pvoid_t JudyHS, time_t after, time_t before, uint32_t top);
+void health_alert2json_conf(RRDHOST *host, BUFFER *wb, ALERT_OPTIONS all);
void health_alarms_values2json(RRDHOST *host, BUFFER *wb, int all);
void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf);
diff --git a/health/health_json.c b/health/health_json.c
index 4f81998f07..6961b1dcb1 100644
--- a/health/health_json.c
+++ b/health/health_json.c
@@ -167,6 +167,64 @@ static inline void health_rrdcalc2json_nolock(RRDHOST *host, BUFFER *wb, RRDCALC
buffer_strcat(wb, "\t\t}");
}
+
+static inline void health_alerts_rrdcalc2json_nolock(RRDHOST *host __maybe_unused, BUFFER *wb,
+ RRDCALC *rc, ALERT_OPTIONS options __maybe_unused,
+ Pvoid_t JudyHS, time_t after, time_t before, uint32_t top)
+{
+ ssize_t idx= get_alert_index(JudyHS, &rc->config_hash_id);
+ // If not in index then skip it
+ if (idx < 0)
+ return;
+
+ char value_string[100 + 1];
+ format_value_and_unit(value_string, 100, rc->value, rrdcalc_units(rc), -1);
+
+
+ char hash_id[UUID_STR_LEN];
+ uuid_unparse_lower(rc->config_hash_id, hash_id);
+
+ buffer_json_add_array_item_object(wb);
+ if ((!after || after <= rc->last_updated) && (!before || before >= rc->last_updated)) {
+
+ buffer_json_member_add_uint64(wb, "li", (size_t) idx);
+
+ char trans_uuid_str[UUID_STR_LEN];
+ if (rc->ae) {
+ uuid_unparse_lower(rc->ae->transition_id, trans_uuid_str);
+ buffer_json_member_add_string(wb, "transition_id", trans_uuid_str);
+ buffer_json_member_add_uint64(wb, "gi", rc->ae->global_id);
+ }
+ else {
+ buffer_json_member_add_quoted_string(wb, "transition_id", "NULL");
+ buffer_json_member_add_quoted_string(wb, "gi", "NULL");
+ }
+
+ buffer_json_member_add_string(wb, "status", rrdcalc_status2string(rc->status));
+ buffer_json_member_add_uint64(wb, "last_status_change", (unsigned long)rc->last_status_change);
+ buffer_json_member_add_uint64(wb, "last_updated", (unsigned long)rc->last_updated);
+ buffer_json_member_add_uint64(wb, "next_update", (unsigned long)rc->next_update);
+ buffer_json_member_add_uint64(wb, "delay_up_to_timestamp", (unsigned long)rc->delay_up_to_timestamp);
+
+ buffer_json_member_add_string(wb, "value_string", value_string);
+ buffer_json_member_add_uint64(wb, "last_repeat", (unsigned long)rc->last_repeat);
+ buffer_json_member_add_uint64(wb, "times_repeat", (unsigned long)rc->times_repeat);
+ buffer_json_member_add_uint64(wb, "db_after", (unsigned long)rc->db_after);
+ buffer_json_member_add_uint64(wb, "db_before", (unsigned long)rc->db_before);
+
+ buffer_json_member_add_double(wb, "green", rc->green);
+ buffer_json_member_add_double(wb, "red", rc->red);
+ buffer_json_member_add_double(wb, "value", rc->value);
+
+ if (options & ALERT_OPTION_INSTANCES) {
+ buffer_json_member_add_array(wb, "transitions");
+ sql_health_alarm_log2json_v2(host, wb, rc->id, NULL, after, before, top);
+ buffer_json_array_close(wb);
+ }
+ }
+ buffer_json_object_close(wb); // array entry
+}
+
//void health_rrdcalctemplate2json_nolock(BUFFER *wb, RRDCALCTEMPLATE *rt) {
//
//}
@@ -234,6 +292,37 @@ static void health_alarms2json_fill_alarms(RRDHOST *host, BUFFER *wb, int all, v
foreach_rrdcalc_in_rrdhost_done(rc);
}
+static void health_alerts2json_fill_alarms(
+ RRDHOST *host,
+ BUFFER *wb,
+ ALERT_OPTIONS all,
+ Pvoid_t JudyHS,
+ time_t after,
+ time_t before,
+ uint32_t top,
+ void (*fp)(RRDHOST *, BUFFER *, RRDCALC *, ALERT_OPTIONS, Pvoid_t , time_t, time_t, uint32_t))
+{
+ RRDCALC *rc;
+ foreach_rrdcalc_in_rrdhost_read(host, rc) {
+ if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
+ continue;
+
+ if (unlikely(!rrdset_is_available_for_exporting_and_alarms(rc->rrdset)))
+ continue;
+
+ if(likely((all & ALERT_OPTION_ACTIVE) && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
+ continue;
+
+ fp(host, wb, rc, all, JudyHS, after, before, top);
+ }
+ foreach_rrdcalc_in_rrdhost_done(rc);
+}
+
+void health_alert2json(RRDHOST *host, BUFFER *wb, ALERT_OPTIONS options, Pvoid_t JudyHS, time_t after, time_t before, uint32_t top)
+{
+ health_alerts2json_fill_alarms(host, wb, options, JudyHS, after, before, top, health_alerts_rrdcalc2json_nolock);
+}
+
void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
"\n\t\"latest_alarm_log_unique_id\": %u,"
diff --git a/health/health_log.c b/health/health_log.c
index b62e0ace41..788fa57ac3 100644
--- a/health/health_log.c
+++ b/health/health_log.c
@@ -47,6 +47,7 @@ inline ALARM_ENTRY* health_create_alarm_entry(
uuid_copy(ae->config_hash_id, *((uuid_t *) config_hash_id));
uuid_generate_random(ae->transition_id);
+ ae->global_id = now_realtime_usec();
ae->family = string_dup(family);
ae->classification = string_dup(class);