master
c 153 lines 5.11 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "api_v1_calls.h"
4
5 static int web_client_api_request_v1_alarms_select(char *url) {
6 int all = 0;
7 while(url) {
8 char *value = strsep_skip_consecutive_separators(&url, "&");
9 if (!value || !*value) continue;
10
11 if(!strcmp(value, "all") || !strcmp(value, "all=true")) all = 1;
12 else if(!strcmp(value, "active") || !strcmp(value, "active=true")) all = 0;
13 }
14
15 return all;
16 }
17
18 int api_v1_alarms(RRDHOST *host, struct web_client *w, char *url) {
19 int all = web_client_api_request_v1_alarms_select(url);
20
21 buffer_flush(w->response.data);
22 w->response.data->content_type = CT_APPLICATION_JSON;
23 health_alarms2json(host, w->response.data, all);
24 buffer_no_cacheable(w->response.data);
25 return HTTP_RESP_OK;
26 }
27
28 int api_v1_alarms_values(RRDHOST *host, struct web_client *w, char *url) {
29 int all = web_client_api_request_v1_alarms_select(url);
30
31 buffer_flush(w->response.data);
32 w->response.data->content_type = CT_APPLICATION_JSON;
33 health_alarms_values2json(host, w->response.data, all);
34 buffer_no_cacheable(w->response.data);
35 return HTTP_RESP_OK;
36 }
37
38 int api_v1_alarm_count(RRDHOST *host, struct web_client *w, char *url) {
39 RRDCALC_STATUS status = RRDCALC_STATUS_RAISED;
40 BUFFER *contexts = NULL;
41
42 buffer_flush(w->response.data);
43 buffer_sprintf(w->response.data, "[");
44
45 while(url) {
46 char *value = strsep_skip_consecutive_separators(&url, "&");
47 if(!value || !*value) continue;
48
49 char *name = strsep_skip_consecutive_separators(&value, "=");
50 if(!name || !*name) continue;
51 if(!value || !*value) continue;
52
53 netdata_log_debug(D_WEB_CLIENT, "%llu: API v1 alarm_count query param '%s' with value '%s'", w->id, name, value);
54
55 char* p = value;
56 if(!strcmp(name, "status")) {
57 while ((*p = toupper(*p))) p++;
58 if (!strcmp("CRITICAL", value)) status = RRDCALC_STATUS_CRITICAL;
59 else if (!strcmp("WARNING", value)) status = RRDCALC_STATUS_WARNING;
60 else if (!strcmp("UNINITIALIZED", value)) status = RRDCALC_STATUS_UNINITIALIZED;
61 else if (!strcmp("UNDEFINED", value)) status = RRDCALC_STATUS_UNDEFINED;
62 else if (!strcmp("REMOVED", value)) status = RRDCALC_STATUS_REMOVED;
63 else if (!strcmp("CLEAR", value)) status = RRDCALC_STATUS_CLEAR;
64 }
65 else if(!strcmp(name, "context") || !strcmp(name, "ctx")) {
66 if(!contexts) contexts = buffer_create(255, &netdata_buffers_statistics.buffers_api);
67 buffer_strcat(contexts, "|");
68 buffer_strcat(contexts, value);
69 }
70 }
71
72 health_aggregate_alarms(host, w->response.data, contexts, status);
73
74 buffer_sprintf(w->response.data, "]\n");
75 w->response.data->content_type = CT_APPLICATION_JSON;
76 buffer_no_cacheable(w->response.data);
77
78 buffer_free(contexts);
79 return 200;
80 }
81
82 int api_v1_alarm_log(RRDHOST *host, struct web_client *w, char *url) {
83 time_t after = 0;
84 char *chart = NULL;
85
86 while(url) {
87 char *value = strsep_skip_consecutive_separators(&url, "&");
88 if (!value || !*value) continue;
89
90 char *name = strsep_skip_consecutive_separators(&value, "=");
91 if(!name || !*name) continue;
92 if(!value || !*value) continue;
93
94 if (!strcmp(name, "after")) after = (time_t) strtoul(value, NULL, 0);
95 else if (!strcmp(name, "chart")) chart = value;
96 }
97
98 buffer_flush(w->response.data);
99 w->response.data->content_type = CT_APPLICATION_JSON;
100 sql_health_alarm_log2json(host, w->response.data, after, chart);
101 return HTTP_RESP_OK;
102 }
103
104 int api_v1_variable(RRDHOST *host, struct web_client *w, char *url) {
105 int ret = HTTP_RESP_BAD_REQUEST;
106 char *chart = NULL;
107 char *variable = NULL;
108
109 buffer_flush(w->response.data);
110
111 while(url) {
112 char *value = strsep_skip_consecutive_separators(&url, "&");
113 if(!value || !*value) continue;
114
115 char *name = strsep_skip_consecutive_separators(&value, "=");
116 if(!name || !*name) continue;
117 if(!value || !*value) continue;
118
119 // name and value are now the parameters
120 // they are not null and not empty
121
122 if(!strcmp(name, "chart")) chart = value;
123 else if(!strcmp(name, "variable")) variable = value;
124 }
125
126 if(!chart || !*chart || !variable || !*variable) {
127 buffer_sprintf(w->response.data, "A chart= and a variable= are required.");
128 goto cleanup;
129 }
130
131 RRDSET *st = rrdset_find(host, chart, false);
132 if(!st) st = rrdset_find_byname(host, chart);
133 if(!st) {
134 buffer_strcat(w->response.data, "Chart is not found: ");
135 buffer_strcat_htmlescape(w->response.data, chart);
136 ret = HTTP_RESP_NOT_FOUND;
137 goto cleanup;
138 }
139
140 w->response.data->content_type = CT_APPLICATION_JSON;
141 st->last_accessed_time_s = now_realtime_sec();
142 alert_variable_lookup_trace(host, st, variable, w->response.data);
143
144 return HTTP_RESP_OK;
145
146 cleanup:
147 return ret;
148 }
149
150 int api_v1_alarm_variables(RRDHOST *host, struct web_client *w, char *url) {
151 return api_v1_single_chart_helper(host, w, url, health_api_v1_chart_variables2json);
152 }
153