@cryptotaxi247 / netdata-1 / commits / 490088222

removed static references to localhost from the API

Costa Tsaousis (ktsaou) committed Feb 19, 2017 at 03:48 UTC 490088222ea06444aca9cf3fc66c0f4418103c77
3 files changed +77 -58
src/health_json.c
+2 -2
@@ -219,7 +219,7 @@ static inline void health_rrdcalc2json_nolock(RRDHOST *host, BUFFER *wb, RRDCALC
219 void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
220 int i;
221
222 - rrdhost_rdlock(localhost);
222 + rrdhost_rdlock(host);
223 buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
224 "\n\t\"latest_alarm_log_unique_id\": %u,"
225 "\n\t\"status\": %s,"
@@ -249,7 +249,7 @@ void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
249 // health_rrdcalctemplate2json_nolock(wb, rt);
250
251 buffer_strcat(wb, "\n\t}\n}\n");
252 - rrdhost_unlock(localhost);
252 + rrdhost_unlock(host);
253 }
254
255
src/rrdhost.c
+26 -7
@@ -49,15 +49,21 @@ static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_
49 // ----------------------------------------------------------------------------
50 // RRDHOST - add a host
51
52 -RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
52 +RRDHOST *rrdhost_create(const char *hostname,
53 + const char *guid,
54 + int update_every,
55 + int entries,
56 + RRD_MEMORY_MODE memory_mode,
57 + int health_enabled) {
58 +
59 debug(D_RRDHOST, "Adding host '%s' with guid '%s'", hostname, guid);
60
61 RRDHOST *host = callocz(1, sizeof(RRDHOST));
62
57 - host->rrd_update_every = default_localhost_rrd_update_every;
58 - host->rrd_history_entries = default_localhost_rrd_history_entries;
59 - host->rrd_memory_mode = default_localhost_rrd_memory_mode;
60 - host->health_enabled = default_localhost_health_enabled;
63 + host->rrd_update_every = update_every;
64 + host->rrd_history_entries = entries;
65 + host->rrd_memory_mode = memory_mode;
66 + host->health_enabled = health_enabled;
67
68 pthread_rwlock_init(&(host->rrdset_root_rwlock), NULL);
69
@@ -158,7 +164,13 @@ RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
164
165 RRDHOST *host = rrdhost_find(guid, 0);
166 if(!host)
161 - host = rrdhost_create(hostname, guid);
167 + host = rrdhost_create(hostname,
168 + guid,
169 + default_localhost_rrd_update_every,
170 + default_localhost_rrd_history_entries,
171 + default_localhost_rrd_memory_mode,
172 + default_localhost_health_enabled
173 + );
174
175 return host;
176 }
@@ -168,7 +180,14 @@ RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid) {
180
181 void rrd_init(char *hostname) {
182 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
171 - localhost = rrdhost_create(hostname, registry_get_this_machine_guid());
183 +
184 + localhost = rrdhost_create(hostname,
185 + registry_get_this_machine_guid(),
186 + default_localhost_rrd_update_every,
187 + default_localhost_rrd_history_entries,
188 + default_localhost_rrd_memory_mode,
189 + default_localhost_health_enabled
190 + );
191 }
192
193 // ----------------------------------------------------------------------------
src/web_client.c
+49 -49
@@ -669,7 +669,7 @@ int web_client_api_request_v1_data_group(char *name, int def)
669 return def;
670 }
671
672 -int web_client_api_request_v1_alarms(struct web_client *w, char *url)
672 +int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w, char *url)
673 {
674 int all = 0;
675
@@ -683,11 +683,11 @@ int web_client_api_request_v1_alarms(struct web_client *w, char *url)
683
684 buffer_flush(w->response.data);
685 w->response.data->contenttype = CT_APPLICATION_JSON;
686 - health_alarms2json(localhost, w->response.data, all);
686 + health_alarms2json(host, w->response.data, all);
687 return 200;
688 }
689
690 -int web_client_api_request_v1_alarm_log(struct web_client *w, char *url)
690 +int web_client_api_request_v1_alarm_log(RRDHOST *host, struct web_client *w, char *url)
691 {
692 uint32_t after = 0;
693
@@ -699,16 +699,16 @@ int web_client_api_request_v1_alarm_log(struct web_client *w, char *url)
699 if(!name || !*name) continue;
700 if(!value || !*value) continue;
701
702 - if(!strcmp(name, "after")) after = strtoul(value, NULL, 0);
702 + if(!strcmp(name, "after")) after = (uint32_t)strtoul(value, NULL, 0);
703 }
704
705 buffer_flush(w->response.data);
706 w->response.data->contenttype = CT_APPLICATION_JSON;
707 - health_alarm_log2json(localhost, w->response.data, after);
707 + health_alarm_log2json(host, w->response.data, after);
708 return 200;
709 }
710
711 -int web_client_api_request_single_chart(struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf))
711 +int web_client_api_request_single_chart(RRDHOST *host, struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf))
712 {
713 int ret = 400;
714 char *chart = NULL;
@@ -738,8 +738,8 @@ int web_client_api_request_single_chart(struct web_client *w, char *url, void ca
738 goto cleanup;
739 }
740
741 - RRDSET *st = rrdset_find_localhost(chart);
742 - if(!st) st = rrdset_find_byname_localhost(chart);
741 + RRDSET *st = rrdset_find(host, chart);
742 + if(!st) st = rrdset_find_byname(host, chart);
743 if(!st) {
744 buffer_strcat(w->response.data, "Chart is not found: ");
745 buffer_strcat_htmlescape(w->response.data, chart);
@@ -755,9 +755,9 @@ int web_client_api_request_single_chart(struct web_client *w, char *url, void ca
755 return ret;
756 }
757
758 -int web_client_api_request_v1_alarm_variables(struct web_client *w, char *url)
758 +int web_client_api_request_v1_alarm_variables(RRDHOST *host, struct web_client *w, char *url)
759 {
760 - return web_client_api_request_single_chart(w, url, health_api_v1_chart_variables2json);
760 + return web_client_api_request_single_chart(host, w, url, health_api_v1_chart_variables2json);
761 }
762
763 int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w, char *url)
@@ -770,7 +770,7 @@ int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w, char *
770 return 200;
771 }
772
773 -int web_client_api_request_v1_allmetrics(struct web_client *w, char *url)
773 +int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client *w, char *url)
774 {
775 int format = ALLMETRICS_SHELL;
776
@@ -798,12 +798,12 @@ int web_client_api_request_v1_allmetrics(struct web_client *w, char *url)
798 switch(format) {
799 case ALLMETRICS_SHELL:
800 w->response.data->contenttype = CT_TEXT_PLAIN;
801 - rrd_stats_api_v1_charts_allmetrics_shell(localhost, w->response.data);
801 + rrd_stats_api_v1_charts_allmetrics_shell(host, w->response.data);
802 return 200;
803
804 case ALLMETRICS_PROMETHEUS:
805 w->response.data->contenttype = CT_PROMETHEUS;
806 - rrd_stats_api_v1_charts_allmetrics_prometheus(localhost, w->response.data);
806 + rrd_stats_api_v1_charts_allmetrics_prometheus(host, w->response.data);
807 return 200;
808
809 default:
@@ -813,12 +813,12 @@ int web_client_api_request_v1_allmetrics(struct web_client *w, char *url)
813 }
814 }
815
816 -int web_client_api_request_v1_chart(struct web_client *w, char *url)
816 +int web_client_api_request_v1_chart(RRDHOST *host, struct web_client *w, char *url)
817 {
818 - return web_client_api_request_single_chart(w, url, rrd_stats_api_v1_chart);
818 + return web_client_api_request_single_chart(host, w, url, rrd_stats_api_v1_chart);
819 }
820
821 -int web_client_api_request_v1_badge(struct web_client *w, char *url) {
821 +int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *url) {
822 int ret = 400;
823 buffer_flush(w->response.data);
824
@@ -888,8 +888,8 @@ int web_client_api_request_v1_badge(struct web_client *w, char *url) {
888 goto cleanup;
889 }
890
891 - RRDSET *st = rrdset_find_localhost(chart);
892 - if(!st) st = rrdset_find_byname_localhost(chart);
891 + RRDSET *st = rrdset_find(host, chart);
892 + if(!st) st = rrdset_find_byname(host, chart);
893 if(!st) {
894 buffer_no_cacheable(w->response.data);
895 buffer_svg(w->response.data, "chart not found", NAN, "", NULL, NULL, -1);
@@ -1072,7 +1072,7 @@ cleanup:
1072 }
1073
1074 // returns the HTTP code
1075 -int web_client_api_request_v1_data(struct web_client *w, char *url)
1075 +int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url)
1076 {
1077 debug(D_WEB_CLIENT, "%llu: API v1 data with URL '%s'", w->id, url);
1078
@@ -1174,8 +1174,8 @@ int web_client_api_request_v1_data(struct web_client *w, char *url)
1174 goto cleanup;
1175 }
1176
1177 - RRDSET *st = rrdset_find_localhost(chart);
1178 - if(!st) st = rrdset_find_byname_localhost(chart);
1177 + RRDSET *st = rrdset_find(host, chart);
1178 + if(!st) st = rrdset_find_byname(host, chart);
1179 if(!st) {
1180 buffer_strcat(w->response.data, "Chart is not found: ");
1181 buffer_strcat_htmlescape(w->response.data, chart);
@@ -1247,7 +1247,7 @@ cleanup:
1247 }
1248
1249
1250 -int web_client_api_request_v1_registry(struct web_client *w, char *url)
1250 +int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *w, char *url)
1251 {
1252 static uint32_t hash_action = 0, hash_access = 0, hash_hello = 0, hash_delete = 0, hash_search = 0,
1253 hash_switch = 0, hash_machine = 0, hash_url = 0, hash_name = 0, hash_delete_url = 0, hash_for = 0,
@@ -1386,22 +1386,22 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
1386 switch(action) {
1387 case 'A':
1388 w->tracking_required = 1;
1389 - return registry_request_access_json(localhost, w, person_guid, machine_guid, machine_url, url_name, now_realtime_sec());
1389 + return registry_request_access_json(host, w, person_guid, machine_guid, machine_url, url_name, now_realtime_sec());
1390
1391 case 'D':
1392 w->tracking_required = 1;
1393 - return registry_request_delete_json(localhost, w, person_guid, machine_guid, machine_url, delete_url, now_realtime_sec());
1393 + return registry_request_delete_json(host, w, person_guid, machine_guid, machine_url, delete_url, now_realtime_sec());
1394
1395 case 'S':
1396 w->tracking_required = 1;
1397 - return registry_request_search_json(localhost, w, person_guid, machine_guid, machine_url, search_machine_guid, now_realtime_sec());
1397 + return registry_request_search_json(host, w, person_guid, machine_guid, machine_url, search_machine_guid, now_realtime_sec());
1398
1399 case 'W':
1400 w->tracking_required = 1;
1401 - return registry_request_switch_json(localhost, w, person_guid, machine_guid, machine_url, to_person_guid, now_realtime_sec());
1401 + return registry_request_switch_json(host, w, person_guid, machine_guid, machine_url, to_person_guid, now_realtime_sec());
1402
1403 case 'H':
1404 - return registry_request_hello_json(localhost, w);
1404 + return registry_request_hello_json(host, w);
1405
1406 default:
1407 buffer_flush(w->response.data);
@@ -1410,7 +1410,7 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
1410 }
1411 }
1412
1413 -int web_client_api_request_v1(struct web_client *w, char *url) {
1413 +int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *url) {
1414 static uint32_t hash_data = 0, hash_chart = 0, hash_charts = 0, hash_registry = 0, hash_badge = 0, hash_alarms = 0, hash_alarm_log = 0, hash_alarm_variables = 0, hash_raw = 0;
1415
1416 if(unlikely(hash_data == 0)) {
@@ -1432,31 +1432,31 @@ int web_client_api_request_v1(struct web_client *w, char *url) {
1432 uint32_t hash = simple_hash(tok);
1433
1434 if(hash == hash_data && !strcmp(tok, "data"))
1435 - return web_client_api_request_v1_data(w, url);
1435 + return web_client_api_request_v1_data(host, w, url);
1436
1437 else if(hash == hash_chart && !strcmp(tok, "chart"))
1438 - return web_client_api_request_v1_chart(w, url);
1438 + return web_client_api_request_v1_chart(host, w, url);
1439
1440 else if(hash == hash_charts && !strcmp(tok, "charts"))
1441 - return web_client_api_request_v1_charts(localhost, w, url);
1441 + return web_client_api_request_v1_charts(host, w, url);
1442
1443 else if(hash == hash_registry && !strcmp(tok, "registry"))
1444 - return web_client_api_request_v1_registry(w, url);
1444 + return web_client_api_request_v1_registry(host, w, url);
1445
1446 else if(hash == hash_badge && !strcmp(tok, "badge.svg"))
1447 - return web_client_api_request_v1_badge(w, url);
1447 + return web_client_api_request_v1_badge(host, w, url);
1448
1449 else if(hash == hash_alarms && !strcmp(tok, "alarms"))
1450 - return web_client_api_request_v1_alarms(w, url);
1450 + return web_client_api_request_v1_alarms(host, w, url);
1451
1452 else if(hash == hash_alarm_log && !strcmp(tok, "alarm_log"))
1453 - return web_client_api_request_v1_alarm_log(w, url);
1453 + return web_client_api_request_v1_alarm_log(host, w, url);
1454
1455 else if(hash == hash_alarm_variables && !strcmp(tok, "alarm_variables"))
1456 - return web_client_api_request_v1_alarm_variables(w, url);
1456 + return web_client_api_request_v1_alarm_variables(host, w, url);
1457
1458 else if(hash == hash_raw && !strcmp(tok, "allmetrics"))
1459 - return web_client_api_request_v1_allmetrics(w, url);
1459 + return web_client_api_request_v1_allmetrics(host, w, url);
1460
1461 else {
1462 buffer_flush(w->response.data);
@@ -1472,14 +1472,14 @@ int web_client_api_request_v1(struct web_client *w, char *url) {
1472 }
1473 }
1474
1475 -int web_client_api_request(struct web_client *w, char *url)
1475 +int web_client_api_request(RRDHOST *host, struct web_client *w, char *url)
1476 {
1477 // get the api version
1478 char *tok = mystrsep(&url, "/?&");
1479 if(tok && *tok) {
1480 debug(D_WEB_CLIENT, "%llu: Searching for API version '%s'.", w->id, tok);
1481 if(strcmp(tok, "v1") == 0)
1482 - return web_client_api_request_v1(w, url);
1482 + return web_client_api_request_v1(host, w, url);
1483 else {
1484 buffer_flush(w->response.data);
1485 buffer_strcat(w->response.data, "Unsupported API version: ");
@@ -1494,7 +1494,7 @@ int web_client_api_request(struct web_client *w, char *url)
1494 }
1495 }
1496
1497 -int web_client_api_old_data_request(struct web_client *w, char *url, int datasource_type)
1497 +int web_client_api_old_data_request(RRDHOST *host, struct web_client *w, char *url, int datasource_type)
1498 {
1499 if(!url || !*url) {
1500 buffer_flush(w->response.data);
@@ -1517,8 +1517,8 @@ int web_client_api_old_data_request(struct web_client *w, char *url, int datasou
1517 // do we have such a data set?
1518 if(*tok) {
1519 debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
1520 - st = rrdset_find_byname_localhost(tok);
1521 - if(!st) st = rrdset_find_localhost(tok);
1520 + st = rrdset_find_byname(host, tok);
1521 + if(!st) st = rrdset_find(host, tok);
1522 }
1523
1524 if(!st) {
@@ -2146,7 +2146,7 @@ void web_client_process(struct web_client *w) {
2146
2147 if(hash == hash_api && strcmp(tok, "api") == 0) {
2148 // the client is requesting api access
2149 - code = web_client_api_request(w, url);
2149 + code = web_client_api_request(localhost, w, url);
2150 }
2151 else if(hash == hash_netdata_conf && strcmp(tok, "netdata.conf") == 0) {
2152 code = 200;
@@ -2158,11 +2158,11 @@ void web_client_process(struct web_client *w) {
2158 }
2159 else if(hash == hash_data && strcmp(tok, WEB_PATH_DATA) == 0) { // "data"
2160 // the client is requesting rrd data -- OLD API
2161 - code = web_client_api_old_data_request(w, url, DATASOURCE_JSON);
2161 + code = web_client_api_old_data_request(localhost, w, url, DATASOURCE_JSON);
2162 }
2163 else if(hash == hash_datasource && strcmp(tok, WEB_PATH_DATASOURCE) == 0) { // "datasource"
2164 // the client is requesting google datasource -- OLD API
2165 - code = web_client_api_old_data_request(w, url, DATASOURCE_DATATABLE_JSONP);
2165 + code = web_client_api_old_data_request(localhost, w, url, DATASOURCE_DATATABLE_JSONP);
2166 }
2167 else if(hash == hash_graph && strcmp(tok, WEB_PATH_GRAPH) == 0) { // "graph"
2168 // the client is requesting an rrd graph -- OLD API
@@ -2173,8 +2173,8 @@ void web_client_process(struct web_client *w) {
2173 debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
2174
2175 // do we have such a data set?
2176 - RRDSET *st = rrdset_find_byname_localhost(tok);
2177 - if(!st) st = rrdset_find_localhost(tok);
2176 + RRDSET *st = rrdset_find_byname(localhost, tok);
2177 + if(!st) st = rrdset_find(localhost, tok);
2178 if(!st) {
2179 // we don't have it
2180 // try to send a file with that name
@@ -2239,8 +2239,8 @@ void web_client_process(struct web_client *w) {
2239 debug(D_WEB_CLIENT, "%llu: Searching for RRD data with name '%s'.", w->id, tok);
2240
2241 // do we have such a data set?
2242 - RRDSET *st = rrdset_find_byname_localhost(tok);
2243 - if(!st) st = rrdset_find_localhost(tok);
2242 + RRDSET *st = rrdset_find_byname(localhost, tok);
2243 + if(!st) st = rrdset_find(localhost, tok);
2244 if(!st) {
2245 code = 404;
2246 buffer_strcat(w->response.data, "Chart is not found: ");