diff options
author | Wei He <weihe924stephen@gmail.com> | 2018-11-30 20:23:37 +0900 |
---|---|---|
committer | Costa Tsaousis <costa@tsaousis.gr> | 2018-11-30 13:23:37 +0200 |
commit | a6b5ed0fc7627f303a10b84ff668c7af78eb24a6 (patch) | |
tree | a76b6d890d36516adcc8d9ab75cf904cc24be6a6 /web | |
parent | 7cbb47be0c26b5bd91e95ac010129668e4b49eb0 (diff) |
add api/v1/info endpoint (#4770)
* add api/v1/info endpoint
fix #3540
* implement alarms info
* add list of hosts mirrored
* add lock
* fix mirrored hosts
Diffstat (limited to 'web')
-rw-r--r-- | web/api/web_api_v1.c | 60 | ||||
-rw-r--r-- | web/api/web_api_v1.h | 1 |
2 files changed, 61 insertions, 0 deletions
diff --git a/web/api/web_api_v1.c b/web/api/web_api_v1.c index 44e78716b0..e21616462c 100644 --- a/web/api/web_api_v1.c +++ b/web/api/web_api_v1.c @@ -615,12 +615,72 @@ inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client * } } +inline void web_client_api_request_v1_info_summary_alarm_statuses(const RRDHOST *host, BUFFER *wb) { + int alarm_normal = 0, alarm_warn = 0, alarm_crit = 0; + RRDCALC *rc; + rrdhost_rdlock(host); + for(rc = host->alarms; rc ; rc = rc->next) { + if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec)) + continue; + + switch(rc->status) { + case RRDCALC_STATUS_WARNING: + alarm_warn++; + break; + case RRDCALC_STATUS_CRITICAL: + alarm_crit++; + break; + default: + alarm_normal++; + } + } + rrdhost_unlock(host); + buffer_sprintf(wb, "\t\t\"normal\": %d,\n", alarm_normal); + buffer_sprintf(wb, "\t\t\"warning\": %d,\n", alarm_warn); + buffer_sprintf(wb, "\t\t\"critical\": %d\n", alarm_crit); +} + +inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) { + RRDHOST *rc; + int count = 0; + rrd_rdlock(); + rrdhost_foreach_read(rc) { + if(count > 0) buffer_strcat(wb, ",\n"); + buffer_sprintf(wb, "\t\t\"%s\"", rc->hostname); + count++; + } + buffer_strcat(wb, "\n"); + rrd_unlock(); +} + +inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url) { + BUFFER *wb = w->response.data; + buffer_flush(wb); + wb->contenttype = CT_APPLICATION_JSON; + + buffer_strcat(wb, "{\n"); + buffer_sprintf(wb, "\t\"version\": \"%s\",\n", host->program_version); + buffer_sprintf(wb, "\t\"uid\": \"%s\",\n", host->machine_guid); + + buffer_strcat(wb, "\t\"mirrored_hosts\": [\n"); + web_client_api_request_v1_info_mirrored_hosts(wb); + buffer_strcat(wb, "\t],\n"); + + buffer_strcat(wb, "\t\"alarms\": {\n"); + web_client_api_request_v1_info_summary_alarm_statuses(host, wb); + buffer_strcat(wb, "\t}\n"); + + buffer_strcat(wb, "}"); + return 200; +} + static struct api_command { const char *command; uint32_t hash; WEB_CLIENT_ACL acl; int (*callback)(RRDHOST *host, struct web_client *w, char *url); } api_commands[] = { + { "info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_info }, { "data", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_data }, { "chart", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_chart }, { "charts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_charts }, diff --git a/web/api/web_api_v1.h b/web/api/web_api_v1.h index b6f315dcab..179fab7cdc 100644 --- a/web/api/web_api_v1.h +++ b/web/api/web_api_v1.h @@ -19,6 +19,7 @@ extern int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w, extern int web_client_api_request_v1_chart(RRDHOST *host, struct web_client *w, char *url); extern int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url); extern int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *w, char *url); +extern int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url); extern int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url); extern void web_client_api_v1_init(void); |