Skip to content

Commit a6b5ed0

Browse files
Wing924ktsaou
authored andcommitted
add api/v1/info endpoint (netdata#4770)
* add api/v1/info endpoint fix netdata#3540 * implement alarms info * add list of hosts mirrored * add lock * fix mirrored hosts
1 parent 7cbb47b commit a6b5ed0

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

web/api/web_api_v1.c

+60
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,72 @@ inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *
615615
}
616616
}
617617

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+
618677
static struct api_command {
619678
const char *command;
620679
uint32_t hash;
621680
WEB_CLIENT_ACL acl;
622681
int (*callback)(RRDHOST *host, struct web_client *w, char *url);
623682
} api_commands[] = {
683+
{ "info", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_info },
624684
{ "data", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_data },
625685
{ "chart", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_chart },
626686
{ "charts", 0, WEB_CLIENT_ACL_DASHBOARD, web_client_api_request_v1_charts },

web/api/web_api_v1.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w,
1919
extern int web_client_api_request_v1_chart(RRDHOST *host, struct web_client *w, char *url);
2020
extern int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url);
2121
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);
2223
extern int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url);
2324

2425
extern void web_client_api_v1_init(void);

0 commit comments

Comments
 (0)