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
Wei He committed
Nov 30, 2018 at 20:23 UTC
a6b5ed0fc7627f303a10b84ff668c7af78eb24a6
2 files changed
+61
web/api/web_api_v1.c
+60
@@ -615,12 +615,72 @@ inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *
615
}
616
}
617
618
+inline void web_client_api_request_v1_info_summary_alarm_statuses(const RRDHOST *host, BUFFER *wb) {
619
+ int alarm_normal = 0, alarm_warn = 0, alarm_crit = 0;
620
+ RRDCALC *rc;
621
+ rrdhost_rdlock(host);
622
+ for(rc = host->alarms; rc ; rc = rc->next) {
623
+ if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
624
+ continue;
625
+
626
+ switch(rc->status) {
627
+ case RRDCALC_STATUS_WARNING:
628
+ alarm_warn++;
629
+ break;
630
+ case RRDCALC_STATUS_CRITICAL:
631
+ alarm_crit++;
632
+ break;
633
+ default:
634
+ alarm_normal++;
635
+ }
636
+ }
637
+ rrdhost_unlock(host);
638
+ buffer_sprintf(wb, "\t\t\"normal\": %d,\n", alarm_normal);
639
+ buffer_sprintf(wb, "\t\t\"warning\": %d,\n", alarm_warn);
640
+ buffer_sprintf(wb, "\t\t\"critical\": %d\n", alarm_crit);
641
+}
642
+
643
+inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) {
644
+ RRDHOST *rc;
645
+ int count = 0;
646
+ rrd_rdlock();
647
+ rrdhost_foreach_read(rc) {
648
+ if(count > 0) buffer_strcat(wb, ",\n");
649
+ buffer_sprintf(wb, "\t\t\"%s\"", rc->hostname);
650
+ count++;
651
+ }
652
+ buffer_strcat(wb, "\n");
653
+ rrd_unlock();
654
+}
655
+
656
+inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url) {
657
+ BUFFER *wb = w->response.data;
658
+ buffer_flush(wb);
659
+ wb->contenttype = CT_APPLICATION_JSON;
660
+
661
+ buffer_strcat(wb, "{\n");
662
+ buffer_sprintf(wb, "\t\"version\": \"%s\",\n", host->program_version);
663
+ buffer_sprintf(wb, "\t\"uid\": \"%s\",\n", host->machine_guid);
664
+
665
+ buffer_strcat(wb, "\t\"mirrored_hosts\": [\n");
666
+ web_client_api_request_v1_info_mirrored_hosts(wb);
667
+ buffer_strcat(wb, "\t],\n");
668
+
669
+ buffer_strcat(wb, "\t\"alarms\": {\n");
670
+ web_client_api_request_v1_info_summary_alarm_statuses(host, wb);
671
+ buffer_strcat(wb, "\t}\n");
672
+
673
+ buffer_strcat(wb, "}");
674
+ return 200;
675
+}
676
+
677
static struct api_command {
678
const char *command;
679
uint32_t hash;
680
WEB_CLIENT_ACL acl;
681
int (*callback)(RRDHOST *host, struct web_client *w, char *url);
682
} api_commands[] = {
683
+ { "info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_info },
684
{ "data", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_data },
685
{ "chart", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_chart },
686
{ "charts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_charts },
web/api/web_api_v1.h
+1
@@ -19,6 +19,7 @@ extern int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w,
19
extern int web_client_api_request_v1_chart(RRDHOST *host, struct web_client *w, char *url);
20
extern int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url);
21
extern int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *w, char *url);
22
+extern int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url);
23
extern int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url);
24
25
extern void web_client_api_v1_init(void);