Просмотр исходного кода

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 5 лет назад
Родитель
Сommit
57055c9ac1
6 измененных файлов с 152 добавлено и 17 удалено
  1. 1 0
      health/health.h
  2. 44 13
      health/health_json.c
  3. 50 1
      web/api/netdata-swagger.json
  4. 38 0
      web/api/netdata-swagger.yaml
  5. 18 3
      web/api/web_api_v1.c
  6. 1 0
      web/api/web_api_v1.h

+ 1 - 0
health/health.h

@@ -62,6 +62,7 @@ extern void health_reload(void);
 extern int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result);
 extern void health_aggregate_alarms(RRDHOST *host, BUFFER *wb, BUFFER* context, RRDCALC_STATUS status);
 extern void health_alarms2json(RRDHOST *host, BUFFER *wb, int all);
+extern void health_alarms_values2json(RRDHOST *host, BUFFER *wb, int all);
 extern void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after);
 
 void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf);

+ 44 - 13
health/health_json.c

@@ -113,6 +113,25 @@ void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after) {
     netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
 }
 
+static inline void health_rrdcalc_values2json_nolock(RRDHOST *host, BUFFER *wb, RRDCALC *rc) {
+    (void)host;
+    buffer_sprintf(wb,
+                   "\t\t\"%s.%s\": {\n"
+                   "\t\t\t\"id\": %lu,\n"
+                   , rc->chart, rc->name
+                   , (unsigned long)rc->id);
+
+    buffer_strcat(wb, "\t\t\t\"value\":");
+    buffer_rrd_value(wb, rc->value);
+    buffer_strcat(wb, ",\n");
+
+    buffer_sprintf(wb,
+                   "\t\t\t\"status\": \"%s\"\n"
+                   , rrdcalc_status2string(rc->status));
+
+    buffer_strcat(wb, "\t\t}");
+}
+
 static inline void health_rrdcalc2json_nolock(RRDHOST *host, BUFFER *wb, RRDCALC *rc) {
     char value_string[100 + 1];
     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
     rrdhost_unlock(host);
 }
 
-void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
+void health_alarms2json_fill_alarms(RRDHOST *host, BUFFER *wb, int all, void (*fp)(RRDHOST *, BUFFER *, RRDCALC *)) {
+    RRDCALC *rc;
     int i;
+    for(i = 0, rc = host->alarms; rc ; rc = rc->next) {
+        if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
+            continue;
+
+        if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
+            continue;
+
+        if(likely(i)) buffer_strcat(wb, ",\n");
+        fp(host, wb, rc);
+        i++;
+    }
+}
 
+void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
     rrdhost_rdlock(host);
     buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
                     "\n\t\"latest_alarm_log_unique_id\": %u,"
@@ -286,18 +319,7 @@ void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
             host->health_enabled?"true":"false",
             (unsigned long)now_realtime_sec());
 
-    RRDCALC *rc;
-    for(i = 0, rc = host->alarms; rc ; rc = rc->next) {
-        if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
-            continue;
-
-        if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
-            continue;
-
-        if(likely(i)) buffer_strcat(wb, ",\n");
-        health_rrdcalc2json_nolock(host, wb, rc);
-        i++;
-    }
+    health_alarms2json_fill_alarms(host, wb, all,  health_rrdcalc2json_nolock);
 
 //    buffer_strcat(wb, "\n\t},\n\t\"templates\": {");
 //    RRDCALCTEMPLATE *rt;
@@ -308,5 +330,14 @@ void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
     rrdhost_unlock(host);
 }
 
+void health_alarms_values2json(RRDHOST *host, BUFFER *wb, int all) {
+    rrdhost_rdlock(host);
+    buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
+                       "\n\t\"alarms\": {\n",
+                   host->hostname);
 
+    health_alarms2json_fill_alarms(host, wb, all,  health_rrdcalc_values2json_nolock);
 
+    buffer_strcat(wb, "\n\t}\n}\n");
+    rrdhost_unlock(host);
+}

+ 50 - 1
web/api/netdata-swagger.json

@@ -812,7 +812,7 @@
     "/alarms": {
       "get": {
         "summary": "Get a list of active or raised alarms on the server",
-        "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.",
+        "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.",
         "parameters": [
           {
             "name": "all",
@@ -823,6 +823,16 @@
             "schema": {
               "type": "boolean"
             }
+          },
+          {
+            "name": "active",
+            "in": "query",
+            "description": "If passed, the raised alarms in state WARNING or CRITICAL are returned.",
+            "required": false,
+            "allowEmptyValue": true,
+            "schema": {
+              "type": "boolean"
+            }
           }
         ],
         "responses": {
@@ -839,6 +849,45 @@
         }
       }
     },
+    "/alarms_values": {
+      "get": {
+        "summary": "Get a list of active or raised alarms on the server",
+        "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.",
+        "parameters": [
+          {
+            "name": "all",
+            "in": "query",
+            "description": "If passed, all enabled alarms are returned.",
+            "required": false,
+            "allowEmptyValue": true,
+            "schema": {
+              "type": "boolean"
+            }
+          },
+          {
+            "name": "active",
+            "in": "query",
+            "description": "If passed, the raised alarms in state WARNING or CRITICAL are returned.",
+            "required": false,
+            "allowEmptyValue": true,
+            "schema": {
+              "type": "boolean"
+            }
+        ],
+        "responses": {
+          "200": {
+            "description": "An object containing general info and a linked list of alarms.",
+            "content": {
+              "application/json": {
+                "schema": {
+                  "$ref": "#/components/schemas/alarms_values"
+                }
+              }
+            }
+          }
+        }
+      }
+    },
     "/alarm_log": {
       "get": {
         "summary": "Retrieves the entries of the alarm log",

+ 38 - 0
web/api/netdata-swagger.yaml

@@ -679,6 +679,13 @@ paths:
           allowEmptyValue: true
           schema:
             type: boolean
+        - name: active
+          in: query
+          description: If passed, the raised alarms in state WARNING or CRITICAL are returned.
+          required: false
+          allowEmptyValue: true
+          schema:
+            type: boolean
       responses:
         "200":
           description: An object containing general info and a linked list of alarms.
@@ -686,6 +693,37 @@ paths:
             application/json:
               schema:
                 $ref: "#/components/schemas/alarms"
+  /alarms_values:
+    get:
+      summary: Get a list of active or raised alarms on the server
+      description: The alarms_values 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.
+        This option output differs from `/alarms` in the number of variables delivered. This endpoint gives
+        to user `id`, `value` and alarm `status`.
+      parameters:
+        - name: all
+          in: query
+          description: If passed, all enabled alarms are returned.
+          required: false
+          allowEmptyValue: true
+          schema:
+            type: boolean
+        - name: active
+          in: query
+          description: If passed, the raised alarms in state WARNING or CRITICAL are returned.
+          required: false
+          allowEmptyValue: true
+          schema:
+            type: boolean
+      responses:
+        "200":
+          description: An object containing general info and a linked list of alarms.
+          content:
+            application/json:
+              schema:
+                $ref: "#/components/schemas/alarms_values"
   /alarm_log:
     get:
       summary: Retrieves the entries of the alarm log

+ 18 - 3
web/api/web_api_v1.c

@@ -196,10 +196,8 @@ inline uint32_t web_client_api_request_v1_data_google_format(char *name) {
     return DATASOURCE_JSON;
 }
 
-
-inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url) {
+int web_client_api_request_v1_alarms_select (char *url) {
     int all = 0;
-
     while(url) {
         char *value = mystrsep(&url, "&");
         if (!value || !*value) continue;
@@ -208,6 +206,12 @@ inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w,
         else if(!strcmp(value, "active")) all = 0;
     }
 
+    return all;
+}
+
+inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url) {
+    int all = web_client_api_request_v1_alarms_select(url);
+
     buffer_flush(w->response.data);
     w->response.data->contenttype = CT_APPLICATION_JSON;
     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,
     return HTTP_RESP_OK;
 }
 
+inline int web_client_api_request_v1_alarms_values(RRDHOST *host, struct web_client *w, char *url) {
+    int all = web_client_api_request_v1_alarms_select(url);
+
+    buffer_flush(w->response.data);
+    w->response.data->contenttype = CT_APPLICATION_JSON;
+    health_alarms_values2json(host, w->response.data, all);
+    buffer_no_cacheable(w->response.data);
+    return HTTP_RESP_OK;
+}
+
 inline int web_client_api_request_v1_alarm_count(RRDHOST *host, struct web_client *w, char *url) {
     RRDCALC_STATUS status = RRDCALC_STATUS_RAISED;
     BUFFER *contexts = NULL;
@@ -878,6 +892,7 @@ static struct api_command {
         { "badge.svg",       0, WEB_CLIENT_ACL_DASHBOARD|WEB_CLIENT_ACL_BADGE, web_client_api_request_v1_badge },
 
         { "alarms",          0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms          },
+        { "alarms_values",   0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarms_values   },
         { "alarm_log",       0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_log       },
         { "alarm_variables", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_variables },
         { "alarm_count",     0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_alarm_count     },

+ 1 - 0
web/api/web_api_v1.h

@@ -13,6 +13,7 @@ extern uint32_t web_client_api_request_v1_data_format(char *name);
 extern uint32_t web_client_api_request_v1_data_google_format(char *name);
 
 extern int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url);
+extern int web_client_api_request_v1_alarms_values(RRDHOST *host, struct web_client *w, char *url);
 extern int web_client_api_request_v1_alarm_log(RRDHOST *host, struct web_client *w, char *url);
 extern int web_client_api_request_single_chart(RRDHOST *host, struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf));
 extern int web_client_api_request_v1_alarm_variables(RRDHOST *host, struct web_client *w, char *url);