@cryptotaxi247 / netdata-1 / commits / 57055c9ac

alarms_values: New endpoint (#7836)

* alarms_values: New endpoint This commit brings the new endpoint to Netdata * alarms_values: Documentation This commit brings the missing documentation for the PR * alarms_values: New function This commit brings a new code that removes dupplication * alarms_values: Fix typo * alarms_values: Fix missing word This commit fixes the missing word inside the documentation * alarms_values: Fix order This commit fixes the order of the alarm answer * alarms_values: Fixes typo and remmove unecessary variable * alarms_values: Fixes doc Describe all paramenters present in the endpoint * alarms_values: Same options This commit brings the same input pattern for alams and alams_values * alarms_values: Update swagger This commit brings the missing information to swagger json * alarms_values: Update swagger This commit brings the missing information to swagger yaml

thiagoftsm committed Feb 7, 2020 at 20:13 UTC 57055c9ac18e70a812aec4230219e928405cbb26
6 files changed +152 -17
health/health.h
+1
@@ -62,6 +62,7 @@ extern void health_reload(void);
62 extern int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result);
63 extern void health_aggregate_alarms(RRDHOST *host, BUFFER *wb, BUFFER* context, RRDCALC_STATUS status);
64 extern void health_alarms2json(RRDHOST *host, BUFFER *wb, int all);
65 +extern void health_alarms_values2json(RRDHOST *host, BUFFER *wb, int all);
66 extern void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after);
67
68 void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf);
health/health_json.c
+44 -13
@@ -113,6 +113,25 @@ void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after) {
113 netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
114 }
115
116 +static inline void health_rrdcalc_values2json_nolock(RRDHOST *host, BUFFER *wb, RRDCALC *rc) {
117 + (void)host;
118 + buffer_sprintf(wb,
119 + "\t\t\"%s.%s\": {\n"
120 + "\t\t\t\"id\": %lu,\n"
121 + , rc->chart, rc->name
122 + , (unsigned long)rc->id);
123 +
124 + buffer_strcat(wb, "\t\t\t\"value\":");
125 + buffer_rrd_value(wb, rc->value);
126 + buffer_strcat(wb, ",\n");
127 +
128 + buffer_sprintf(wb,
129 + "\t\t\t\"status\": \"%s\"\n"
130 + , rrdcalc_status2string(rc->status));
131 +
132 + buffer_strcat(wb, "\t\t}");
133 +}
134 +
135 static inline void health_rrdcalc2json_nolock(RRDHOST *host, BUFFER *wb, RRDCALC *rc) {
136 char value_string[100 + 1];
137 format_value_and_unit(value_string, 100, rc->value, rc->units, -1);
@@ -272,9 +291,23 @@ void health_aggregate_alarms(RRDHOST *host, BUFFER *wb, BUFFER* contexts, RRDCAL
291 rrdhost_unlock(host);
292 }
293
275 -void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
294 +void health_alarms2json_fill_alarms(RRDHOST *host, BUFFER *wb, int all, void (*fp)(RRDHOST *, BUFFER *, RRDCALC *)) {
295 + RRDCALC *rc;
296 int i;
297 + for(i = 0, rc = host->alarms; rc ; rc = rc->next) {
298 + if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
299 + continue;
300 +
301 + if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
302 + continue;
303 +
304 + if(likely(i)) buffer_strcat(wb, ",\n");
305 + fp(host, wb, rc);
306 + i++;
307 + }
308 +}
309
310 +void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
311 rrdhost_rdlock(host);
312 buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
313 "\n\t\"latest_alarm_log_unique_id\": %u,"
@@ -286,18 +319,7 @@ void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
319 host->health_enabled?"true":"false",
320 (unsigned long)now_realtime_sec());
321
289 - RRDCALC *rc;
290 - for(i = 0, rc = host->alarms; rc ; rc = rc->next) {
291 - if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
292 - continue;
293 -
294 - if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
295 - continue;
296 -
297 - if(likely(i)) buffer_strcat(wb, ",\n");
298 - health_rrdcalc2json_nolock(host, wb, rc);
299 - i++;
300 - }
322 + health_alarms2json_fill_alarms(host, wb, all, health_rrdcalc2json_nolock);
323
324 // buffer_strcat(wb, "\n\t},\n\t\"templates\": {");
325 // RRDCALCTEMPLATE *rt;
@@ -308,5 +330,14 @@ void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
330 rrdhost_unlock(host);
331 }
332
333 +void health_alarms_values2json(RRDHOST *host, BUFFER *wb, int all) {
334 + rrdhost_rdlock(host);
335 + buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
336 + "\n\t\"alarms\": {\n",
337 + host->hostname);
338
339 + health_alarms2json_fill_alarms(host, wb, all, health_rrdcalc_values2json_nolock);
340
341 + buffer_strcat(wb, "\n\t}\n}\n");
342 + rrdhost_unlock(host);
343 +}
web/api/netdata-swagger.json
+50 -1
@@ -812,7 +812,7 @@
812 "/alarms": {
813 "get": {
814 "summary": "Get a list of active or raised alarms on the server",
815 - "description": "The alarms endpoint returns the list of all raised or enabled alarms on the netdata server. Called without any parameters, the raised alarms in state WARNING or CRITICAL are returned. By passing \"?all\", all the enabled alarms are returned.",
815 + "description": "The alarms endpoint returns the list of all raised or enabled alarms on the Netdata agent. Called without any parameters, the raised alarms in state WARNING or CRITICAL are returned, the same response is delivered when \"?active\" is passed in the URL. By passing \"?all\", all the enabled alarms are returned.",
816 "parameters": [
817 {
818 "name": "all",
@@ -823,6 +823,16 @@
823 "schema": {
824 "type": "boolean"
825 }
826 + },
827 + {
828 + "name": "active",
829 + "in": "query",
830 + "description": "If passed, the raised alarms in state WARNING or CRITICAL are returned.",
831 + "required": false,
832 + "allowEmptyValue": true,
833 + "schema": {
834 + "type": "boolean"
835 + }
836 }
837 ],
838 "responses": {
@@ -839,6 +849,45 @@
849 }
850 }
851 },
852 + "/alarms_values": {
853 + "get": {
854 + "summary": "Get a list of active or raised alarms on the server",
855 + "description": "The alarms endpoint returns the list of all raised or enabled alarms on the Netdata agent. Called without any parameters, the raised alarms in state WARNING or CRITICAL are returned, the same response is delivered when \"?active\" is passed in the URL. By passing \"?all\", all the enabled alarms are returned.",
856 + "parameters": [
857 + {
858 + "name": "all",
859 + "in": "query",
860 + "description": "If passed, all enabled alarms are returned.",
861 + "required": false,
862 + "allowEmptyValue": true,
863 + "schema": {
864 + "type": "boolean"
865 + }
866 + },
867 + {
868 + "name": "active",
869 + "in": "query",
870 + "description": "If passed, the raised alarms in state WARNING or CRITICAL are returned.",
871 + "required": false,
872 + "allowEmptyValue": true,
873 + "schema": {
874 + "type": "boolean"
875 + }
876 + ],
877 + "responses": {
878 + "200": {
879 + "description": "An object containing general info and a linked list of alarms.",
880 + "content": {
881 + "application/json": {
882 + "schema": {
883 + "$ref": "#/components/schemas/alarms_values"
884 + }
885 + }
886 + }
887 + }
888 + }
889 + }
890 + },
891 "/alarm_log": {
892 "get": {
893 "summary": "Retrieves the entries of the alarm log",
web/api/netdata-swagger.yaml
+38
@@ -679,6 +679,13 @@ paths:
679 allowEmptyValue: true
680 schema:
681 type: boolean
682 + - name: active
683 + in: query
684 + description: If passed, the raised alarms in state WARNING or CRITICAL are returned.
685 + required: false
686 + allowEmptyValue: true
687 + schema:
688 + type: boolean
689 responses:
690 "200":
691 description: An object containing general info and a linked list of alarms.
@@ -686,6 +693,37 @@ paths:
693 application/json:
694 schema:
695 $ref: "#/components/schemas/alarms"
696 + /alarms_values:
697 + get:
698 + summary: Get a list of active or raised alarms on the server
699 + description: The alarms_values endpoint returns the list of all raised or enabled alarms on
700 + the netdata server. Called without any parameters, the raised alarms in
701 + state WARNING or CRITICAL are returned. By passing "?all", all the
702 + enabled alarms are returned.
703 + This option output differs from `/alarms` in the number of variables delivered. This endpoint gives
704 + to user `id`, `value` and alarm `status`.
705 + parameters:
706 + - name: all
707 + in: query
708 + description: If passed, all enabled alarms are returned.
709 + required: false
710 + allowEmptyValue: true
711 + schema:
712 + type: boolean
713 + - name: active
714 + in: query
715 + description: If passed, the raised alarms in state WARNING or CRITICAL are returned.
716 + required: false
717 + allowEmptyValue: true
718 + schema:
719 + type: boolean
720 + responses:
721 + "200":
722 + description: An object containing general info and a linked list of alarms.
723 + content:
724 + application/json:
725 + schema:
726 + $ref: "#/components/schemas/alarms_values"
727 /alarm_log:
728 get:
729 summary: Retrieves the entries of the alarm log
web/api/web_api_v1.c
+18 -3
@@ -196,10 +196,8 @@ inline uint32_t web_client_api_request_v1_data_google_format(char *name) {
196 return DATASOURCE_JSON;
197 }
198
199 -
200 -inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url) {
199 +int web_client_api_request_v1_alarms_select (char *url) {
200 int all = 0;
202 -
201 while(url) {
202 char *value = mystrsep(&url, "&");
203 if (!value || !*value) continue;
@@ -208,6 +206,12 @@ inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w,
206 else if(!strcmp(value, "active")) all = 0;
207 }
208
209 + return all;
210 +}
211 +
212 +inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url) {
213 + int all = web_client_api_request_v1_alarms_select(url);
214 +
215 buffer_flush(w->response.data);
216 w->response.data->contenttype = CT_APPLICATION_JSON;
217 health_alarms2json(host, w->response.data, all);
@@ -215,6 +219,16 @@ inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w,
219 return HTTP_RESP_OK;
220 }
221
222 +inline int web_client_api_request_v1_alarms_values(RRDHOST *host, struct web_client *w, char *url) {
223 + int all = web_client_api_request_v1_alarms_select(url);
224 +
225 + buffer_flush(w->response.data);
226 + w->response.data->contenttype = CT_APPLICATION_JSON;
227 + health_alarms_values2json(host, w->response.data, all);
228 + buffer_no_cacheable(w->response.data);
229 + return HTTP_RESP_OK;
230 +}
231 +
232 inline int web_client_api_request_v1_alarm_count(RRDHOST *host, struct web_client *w, char *url) {
233 RRDCALC_STATUS status = RRDCALC_STATUS_RAISED;
234 BUFFER *contexts = NULL;
@@ -878,6 +892,7 @@ static struct api_command {
892 { "badge.svg", 0, WEB_CLIENT_ACL_DASHBOARD|WEB_CLIENT_ACL_BADGE, web_client_api_request_v1_badge },
893
894 { "alarms", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms },
895 + { "alarms_values", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms_values },
896 { "alarm_log", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_log },
897 { "alarm_variables", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_variables },
898 { "alarm_count", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_count },
web/api/web_api_v1.h
+1
@@ -13,6 +13,7 @@ extern uint32_t web_client_api_request_v1_data_format(char *name);
13 extern uint32_t web_client_api_request_v1_data_google_format(char *name);
14
15 extern int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url);
16 +extern int web_client_api_request_v1_alarms_values(RRDHOST *host, struct web_client *w, char *url);
17 extern int web_client_api_request_v1_alarm_log(RRDHOST *host, struct web_client *w, char *url);
18 extern int web_client_api_request_single_chart(RRDHOST *host, struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf));
19 extern int web_client_api_request_v1_alarm_variables(RRDHOST *host, struct web_client *w, char *url);