From: Costa Tsaousis Date: Tue, 10 May 2016 22:59:36 +0000 (+0300) Subject: simple registry access from the dashboard - in debug mode X-Git-Tag: v1.2.0~7^2~35 X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a3cfec42bfa977c3d3118ead715e33171fa40ac;p=netdata.git simple registry access from the dashboard - in debug mode --- diff --git a/src/registry.c b/src/registry.c index 4d34c1d2..2c2bcf80 100644 --- a/src/registry.c +++ b/src/registry.c @@ -1014,7 +1014,7 @@ static inline void registry_json_footer(struct web_client *w) { buffer_strcat(w->response.data, "\n}\n"); } -static inline int registry_json_redirect(struct web_client *w) { +int registry_json_redirect(struct web_client *w) { registry_json_header(w, 0); buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"", diff --git a/src/registry.h b/src/registry.h index aa89862f..097ab4d8 100644 --- a/src/registry.h +++ b/src/registry.h @@ -8,6 +8,7 @@ extern int registry_request_access_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *name, time_t when); extern int registry_request_delete_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when); extern int registry_request_search_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when); +extern int registry_json_redirect(struct web_client *w); extern int registry_init(void); extern void registry_free(void); @@ -17,4 +18,5 @@ extern char *registry_get_this_machine_guid(void); extern void registry_statistics(void); + #endif /* NETDATA_REGISTRY_H */ diff --git a/src/web_client.c b/src/web_client.c index c4199b56..868f3c9a 100644 --- a/src/web_client.c +++ b/src/web_client.c @@ -817,6 +817,7 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url) if(!strcmp(name, "action")) { if(!strcmp(value, "access")) action = 'A'; + else if(!strcmp(value, "hello")) action = 'H'; else if(!strcmp(value, "delete")) action = 'D'; else if(!strcmp(value, "search")) action = 'S'; } @@ -840,12 +841,34 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url) } } - if((!action || !machine_guid || !machine_url) || (action == 'A' && !url_name) || (action == 'D' && !delete_url) || (action == 'S' && !search_machine_guid)) { + if(action == 'A' && (!machine_guid || !machine_url || !url_name)) { buffer_flush(w->response.data); - buffer_sprintf(w->response.data, "Invalid registry request - required parameters missing."); + buffer_sprintf(w->response.data, "Invalid registry request - access requires these parameters: machine ('%s'), url ('%s'), name ('%s')", + machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", url_name?url_name:"UNSET"); + return 400; + } + else if(action == 'D' && (!machine_guid || !machine_url || !delete_url)) { + buffer_flush(w->response.data); + buffer_sprintf(w->response.data, "Invalid registry request - delete requires these parameters: machine ('%s'), url ('%s'), delete_url ('%s')", + machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", delete_url?delete_url:"UNSET"); + return 400; + } + else if(action == 'S' && (!machine_guid || !machine_url || !search_machine_guid)) { + buffer_flush(w->response.data); + buffer_sprintf(w->response.data, "Invalid registry request - search requires these parameters: machine ('%s'), url ('%s'), for ('%s')", + machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", search_machine_guid?search_machine_guid:"UNSET"); return 400; } + /* + * No, this is not right + if(action != 'H' && !person_guid[0]) { + buffer_flush(w->response.data); + buffer_sprintf(w->response.data, "Invalid registry request - you need to send your cookie for this action."); + return 400; + } + */ + switch(action) { case 'A': return registry_request_access_json(w, person_guid, machine_guid, machine_url, url_name, time(NULL)); @@ -855,6 +878,14 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url) case 'S': return registry_request_search_json(w, person_guid, machine_guid, machine_url, search_machine_guid, time(NULL)); + + case 'H': + return registry_json_redirect(w); + + default: + buffer_flush(w->response.data); + buffer_sprintf(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search"); + return 400; } buffer_flush(w->response.data); diff --git a/web/dashboard.js b/web/dashboard.js index c05d1546..de026a2d 100644 --- a/web/dashboard.js +++ b/web/dashboard.js @@ -5472,6 +5472,60 @@ NETDATA.loadRequiredCSS(++index); }; + NETDATA.registry = { + host: null, + machine_guid: null, + urls: null, + + init: function() { + NETDATA.registry.hello(function() { + console.log('hello completed'); + NETDATA.registry.access(function() { + console.log('access completed'); + console.log(NETDATA.registry); + }) + }); + }, + + hello: function(callback) { + $.ajax({ + url: NETDATA.serverDefault + '/api/v1/registry?action=hello', + async: true, + cache: false, + xhrFields: { withCredentials: true } + }) + .done(function(data) { + NETDATA.registry.host = data.registry; + NETDATA.registry.machine_guid = data.machine_guid; + + if(typeof callback === 'function') + callback(); + }) + .fail(function() { + console.log('failed to hello (registry) netdata server: ' + NETDATA.serverDefault); + }); + }, + + access: function(callback) { + $.ajax({ + url: NETDATA.registry.host + '/api/v1/registry?action=access&machine=' + NETDATA.registry.machine_guid + '&name=test&url=' + document.location, + async: true, + cache: false, + xhrFields: { withCredentials: true } + }) + .done(function(data) { + NETDATA.registry.urls = data.urls; + + if(typeof callback === 'function') + callback(); + }) + .fail(function() { + console.log('failed to access (registry) netdata server: ' + NETDATA.registry.host); + }); + } + + }; + NETDATA.errorReset(); NETDATA.loadRequiredCSS(0); @@ -5486,6 +5540,8 @@ if(NETDATA.options.debug.main_loop === true) console.log('starting chart refresh thread'); + setTimeout(NETDATA.registry.init, 500); + NETDATA.start(); } });