]> arthur.barton.de Git - netdata.git/commitdiff
simple registry access from the dashboard - in debug mode
authorCosta Tsaousis <costa@tsaousis.gr>
Tue, 10 May 2016 22:59:36 +0000 (01:59 +0300)
committerCosta Tsaousis <costa@tsaousis.gr>
Tue, 10 May 2016 22:59:36 +0000 (01:59 +0300)
src/registry.c
src/registry.h
src/web_client.c
web/dashboard.js

index 4d34c1d246c2748591ff2c267489e093981e780b..2c2bcf80ca81899c154c83569eb7b968c263936c 100644 (file)
@@ -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\"",
index aa89862f5114ece60470fad32ec037cfcc52797b..097ab4d834ee33bb89e8ae2bbbd1b31a3550285b 100644 (file)
@@ -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 */
index c4199b5604f81153fb084a76475bdb72b2c82b85..868f3c9a43c872e794e6d08393af8e18056a9375 100644 (file)
@@ -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);
index c05d1546027bf117f445795cbd1b9b594271f22b..de026a2d4c22f8ebe2ddedd56cfebf2f0d822d7b 100644 (file)
                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);
 
                                if(NETDATA.options.debug.main_loop === true)
                                        console.log('starting chart refresh thread');
 
+                               setTimeout(NETDATA.registry.init, 500);
+
                                NETDATA.start();
                        }
                });