simple registry access from the dashboard - in debug mode
Costa Tsaousis committed
May 11, 2016 at 01:59 UTC
6a3cfec42bfa977c3d3118ead715e33171fa40ac
4 files changed
+92
-3
src/registry.c
+1
-1
@@ -1014,7 +1014,7 @@ static inline void registry_json_footer(struct web_client *w) {
1014
buffer_strcat(w->response.data, "\n}\n");
1015
}
1016
1017
-static inline int registry_json_redirect(struct web_client *w) {
1017
+int registry_json_redirect(struct web_client *w) {
1018
registry_json_header(w, 0);
1019
1020
buffer_sprintf(w->response.data, ",\n\t\"registry\": \"%s\"",
src/registry.h
+2
@@ -8,6 +8,7 @@
8
extern int registry_request_access_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *name, time_t when);
9
extern int registry_request_delete_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *delete_url, time_t when);
10
extern int registry_request_search_json(struct web_client *w, char *person_guid, char *machine_guid, char *url, char *request_machine, time_t when);
11
+extern int registry_json_redirect(struct web_client *w);
12
13
extern int registry_init(void);
14
extern void registry_free(void);
@@ -17,4 +18,5 @@ extern char *registry_get_this_machine_guid(void);
18
19
extern void registry_statistics(void);
20
21
+
22
#endif /* NETDATA_REGISTRY_H */
src/web_client.c
+33
-2
@@ -817,6 +817,7 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
817
818
if(!strcmp(name, "action")) {
819
if(!strcmp(value, "access")) action = 'A';
820
+ else if(!strcmp(value, "hello")) action = 'H';
821
else if(!strcmp(value, "delete")) action = 'D';
822
else if(!strcmp(value, "search")) action = 'S';
823
}
@@ -840,12 +841,34 @@ int web_client_api_request_v1_registry(struct web_client *w, char *url)
841
}
842
}
843
843
- if((!action || !machine_guid || !machine_url) || (action == 'A' && !url_name) || (action == 'D' && !delete_url) || (action == 'S' && !search_machine_guid)) {
844
+ if(action == 'A' && (!machine_guid || !machine_url || !url_name)) {
845
buffer_flush(w->response.data);
845
- buffer_sprintf(w->response.data, "Invalid registry request - required parameters missing.");
846
+ buffer_sprintf(w->response.data, "Invalid registry request - access requires these parameters: machine ('%s'), url ('%s'), name ('%s')",
847
+ machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", url_name?url_name:"UNSET");
848
+ return 400;
849
+ }
850
+ else if(action == 'D' && (!machine_guid || !machine_url || !delete_url)) {
851
+ buffer_flush(w->response.data);
852
+ buffer_sprintf(w->response.data, "Invalid registry request - delete requires these parameters: machine ('%s'), url ('%s'), delete_url ('%s')",
853
+ machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", delete_url?delete_url:"UNSET");
854
+ return 400;
855
+ }
856
+ else if(action == 'S' && (!machine_guid || !machine_url || !search_machine_guid)) {
857
+ buffer_flush(w->response.data);
858
+ buffer_sprintf(w->response.data, "Invalid registry request - search requires these parameters: machine ('%s'), url ('%s'), for ('%s')",
859
+ machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", search_machine_guid?search_machine_guid:"UNSET");
860
return 400;
861
}
862
863
+ /*
864
+ * No, this is not right
865
+ if(action != 'H' && !person_guid[0]) {
866
+ buffer_flush(w->response.data);
867
+ buffer_sprintf(w->response.data, "Invalid registry request - you need to send your cookie for this action.");
868
+ return 400;
869
+ }
870
+ */
871
+
872
switch(action) {
873
case 'A':
874
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)
878
879
case 'S':
880
return registry_request_search_json(w, person_guid, machine_guid, machine_url, search_machine_guid, time(NULL));
881
+
882
+ case 'H':
883
+ return registry_json_redirect(w);
884
+
885
+ default:
886
+ buffer_flush(w->response.data);
887
+ buffer_sprintf(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
888
+ return 400;
889
}
890
891
buffer_flush(w->response.data);
web/dashboard.js
+56
@@ -5472,6 +5472,60 @@
5472
NETDATA.loadRequiredCSS(++index);
5473
};
5474
5475
+ NETDATA.registry = {
5476
+ host: null,
5477
+ machine_guid: null,
5478
+ urls: null,
5479
+
5480
+ init: function() {
5481
+ NETDATA.registry.hello(function() {
5482
+ console.log('hello completed');
5483
+ NETDATA.registry.access(function() {
5484
+ console.log('access completed');
5485
+ console.log(NETDATA.registry);
5486
+ })
5487
+ });
5488
+ },
5489
+
5490
+ hello: function(callback) {
5491
+ $.ajax({
5492
+ url: NETDATA.serverDefault + '/api/v1/registry?action=hello',
5493
+ async: true,
5494
+ cache: false,
5495
+ xhrFields: { withCredentials: true }
5496
+ })
5497
+ .done(function(data) {
5498
+ NETDATA.registry.host = data.registry;
5499
+ NETDATA.registry.machine_guid = data.machine_guid;
5500
+
5501
+ if(typeof callback === 'function')
5502
+ callback();
5503
+ })
5504
+ .fail(function() {
5505
+ console.log('failed to hello (registry) netdata server: ' + NETDATA.serverDefault);
5506
+ });
5507
+ },
5508
+
5509
+ access: function(callback) {
5510
+ $.ajax({
5511
+ url: NETDATA.registry.host + '/api/v1/registry?action=access&machine=' + NETDATA.registry.machine_guid + '&name=test&url=' + document.location,
5512
+ async: true,
5513
+ cache: false,
5514
+ xhrFields: { withCredentials: true }
5515
+ })
5516
+ .done(function(data) {
5517
+ NETDATA.registry.urls = data.urls;
5518
+
5519
+ if(typeof callback === 'function')
5520
+ callback();
5521
+ })
5522
+ .fail(function() {
5523
+ console.log('failed to access (registry) netdata server: ' + NETDATA.registry.host);
5524
+ });
5525
+ }
5526
+
5527
+ };
5528
+
5529
NETDATA.errorReset();
5530
NETDATA.loadRequiredCSS(0);
5531
@@ -5486,6 +5540,8 @@
5540
if(NETDATA.options.debug.main_loop === true)
5541
console.log('starting chart refresh thread');
5542
5543
+ setTimeout(NETDATA.registry.init, 500);
5544
+
5545
NETDATA.start();
5546
}
5547
});