Add a new parameter 'chart' to the /api/v1/alarm_log. (#10788)
* add a chart parameter to api alarm_log * Use hash_chart instead * also do the strcmp * cleaner? * save an if * move simple_hash out of the loop * Changed if * formatting changes * fix formating
Emmanuel Vasilakis committed
Mar 26, 2021 at 11:34 UTC
5510b429a642eb2abd8f9831ac98116c4f473325
3 files changed
+13
-7
health/health.h
+1
-1
@@ -64,7 +64,7 @@ extern int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *
64
extern void health_aggregate_alarms(RRDHOST *host, BUFFER *wb, BUFFER* context, RRDCALC_STATUS status);
65
extern void health_alarms2json(RRDHOST *host, BUFFER *wb, int all);
66
extern void health_alarms_values2json(RRDHOST *host, BUFFER *wb, int all);
67
-extern void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after);
67
+extern void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *chart);
68
69
void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf);
70
void health_api_v1_chart_custom_variables2json(RRDSET *st, BUFFER *buf);
health/health_json.c
+8
-4
@@ -93,18 +93,22 @@ void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host)
93
buffer_strcat(wb, "\t}");
94
}
95
96
-void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after) {
96
+void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *chart) {
97
netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
98
99
buffer_strcat(wb, "[");
100
101
unsigned int max = host->health_log.max;
102
unsigned int count = 0;
103
+ uint32_t hash_chart = 0;
104
+ if (chart) hash_chart = simple_hash(chart);
105
ALARM_ENTRY *ae;
104
- for(ae = host->health_log.alarms; ae && count < max ; count++, ae = ae->next) {
105
- if(ae->unique_id > after) {
106
- if(likely(count)) buffer_strcat(wb, ",");
106
+ for (ae = host->health_log.alarms; ae && count < max; ae = ae->next) {
107
+ if ((ae->unique_id > after) && (!chart || (ae->hash_chart == hash_chart && !strcmp(ae->chart, chart)))) {
108
+ if (likely(count))
109
+ buffer_strcat(wb, ",");
110
health_alarm_entry2json_nolock(wb, ae, host);
111
+ count++;
112
}
113
}
114
web/api/web_api_v1.c
+4
-2
@@ -276,6 +276,7 @@ inline int web_client_api_request_v1_alarm_count(RRDHOST *host, struct web_clien
276
277
inline int web_client_api_request_v1_alarm_log(RRDHOST *host, struct web_client *w, char *url) {
278
uint32_t after = 0;
279
+ char *chart = NULL;
280
281
while(url) {
282
char *value = mystrsep(&url, "&");
@@ -285,12 +286,13 @@ inline int web_client_api_request_v1_alarm_log(RRDHOST *host, struct web_client
286
if(!name || !*name) continue;
287
if(!value || !*value) continue;
288
288
- if(!strcmp(name, "after")) after = (uint32_t)strtoul(value, NULL, 0);
289
+ if (!strcmp(name, "after")) after = (uint32_t)strtoul(value, NULL, 0);
290
+ else if (!strcmp(name, "chart")) chart = value;
291
}
292
293
buffer_flush(w->response.data);
294
w->response.data->contenttype = CT_APPLICATION_JSON;
293
- health_alarm_log2json(host, w->response.data, after);
295
+ health_alarm_log2json(host, w->response.data, after, chart);
296
return HTTP_RESP_OK;
297
}
298