goto-host-from-alarm.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <!DOCTYPE html>
  2. <!-- SPDX-License-Identifier: GPL-3.0-or-later -->
  3. <html lang="en">
  4. <head>
  5. <title>Goto a host you know...</title>
  6. <meta name="application-name" content="netdata">
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <meta charset="utf-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  10. <meta name="viewport" content="width=device-width, initial-scale=1">
  11. <meta name="apple-mobile-web-app-capable" content="yes">
  12. <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  13. </head>
  14. <script>
  15. var netdataRegistry = true;
  16. var netdataRegistryAfterMs = 0;
  17. var netdataTheme = 'slate';
  18. var netdataShowHelp = true;
  19. </script>
  20. <script type="text/javascript" src="dashboard.js?v20190902-0"></script>
  21. <script>
  22. function escapeUserInputHTML(s) {
  23. return s.toString()
  24. .replace(/&/g, '&amp;')
  25. .replace(/</g, '&lt;')
  26. .replace(/>/g, '&gt;')
  27. .replace(/"/g, '&quot;')
  28. .replace(/#/g, '&#35;')
  29. .replace(/'/g, '&#39;')
  30. .replace(/\(/g,'&#40;')
  31. .replace(/\)/g,'&#41;')
  32. .replace(/\//g,'&#47;');
  33. }
  34. // if string.startsWith is not defined, define it
  35. if(typeof String.prototype.startsWith !== 'function') {
  36. String.prototype.startsWith = function(s) {
  37. if(s.length > this.length) return false;
  38. return this.slice(s.length) === s;
  39. };
  40. }
  41. function verifyURL(s) {
  42. if(typeof(s) === 'string' && (s.startsWith('http://') || s.startsWith('https://')))
  43. return s
  44. .replace(/'/g, '%22')
  45. .replace(/"/g, '%27')
  46. .replace(/\)/g, '%28')
  47. .replace(/\(/g, '%29');
  48. console.log('invalid URL detected:');
  49. console.log(s);
  50. return 'javascript:alert("invalid url");';
  51. }
  52. var urlOptions = {
  53. host: null,
  54. chart: null,
  55. family: null,
  56. alarm: null,
  57. alarm_unique_id: 0,
  58. alarm_id: 0,
  59. alarm_event_id: 0,
  60. alarm_when: 0,
  61. alarm_status: null,
  62. alarm_chart: null,
  63. alarm_value: null,
  64. hasProperty: function(property) {
  65. return typeof this[property] !== 'undefined';
  66. }
  67. };
  68. function netdataQueryParse() {
  69. var query = document.location.search.split('?');
  70. var variables = query[1].split('&');
  71. var len = variables.length;
  72. while(len--) {
  73. var p = variables[len].split('=');
  74. if(urlOptions.hasProperty(p[0]) && typeof p[1] !== 'undefined')
  75. urlOptions[p[0]] = decodeURIComponent(p[1]);
  76. }
  77. if(typeof urlOptions.family !== 'string')
  78. urlOptions.family = '';
  79. if(typeof urlOptions.chart !== 'string')
  80. urlOptions.chart = '';
  81. }
  82. function netdataURL(url) {
  83. return url + '#top'
  84. + ';nowelcome=1'
  85. // + ';show_alarms=1'
  86. + ';chart=' + encodeURIComponent(urlOptions.chart)
  87. + ';family=' + encodeURIComponent(urlOptions.family)
  88. + ';alarm=' + encodeURIComponent(urlOptions.alarm)
  89. + ';alarm_unique_id=' + urlOptions.alarm_unique_id.toString()
  90. + ';alarm_id=' + urlOptions.alarm_id.toString()
  91. + ';alarm_event_id=' + urlOptions.alarm_event_id.toString()
  92. + ';alarm_when=' + urlOptions.alarm_when.toString()
  93. + ';alarm_status=' + urlOptions.alarm_status
  94. + ';alarm_value=' + encodeURIComponent(urlOptions.alarm_value)
  95. + ';alarm_chart=' + urlOptions.alarm_chart
  96. ;
  97. }
  98. var gotoServerValidateRemaining = 0;
  99. var gotoServerMiddleClick = false;
  100. var gotoServerStop = false;
  101. var thisIsHttps = false;
  102. var urlsInHttp = 0;
  103. function gotoServerValidateUrl(id, guid, url) {
  104. var penaldy = 0;
  105. var error = 'failed';
  106. if(thisIsHttps === false && url.toString().startsWith('https://'))
  107. // we penalize https only if the current url is http
  108. // to allow the user walk through all its servers.
  109. penaldy = 500;
  110. else if(thisIsHttps === true && url.toString().startsWith('http://')) {
  111. error = 'can\'t check';
  112. urlsInHttp++;
  113. }
  114. var finalURL = netdataURL(url);
  115. setTimeout(function() {
  116. document.getElementById('gotoServerList').innerHTML += '<tr><td style="padding-left: 20px;"><a href="' + verifyURL(finalURL) + '" target="_blank">' + escapeUserInputHTML(url) + '</a></td><td style="padding-left: 30px;"><code id="' + guid + '-' + id + '-status">checking...</code></td></tr>';
  117. NETDATA.registry.hello(url, function(data) {
  118. if(typeof data !== 'undefined' && data !== null && typeof data.machine_guid === 'string' && data.machine_guid === guid) {
  119. // console.log('OK ' + id + ' URL: ' + url);
  120. document.getElementById(guid + '-' + id + '-status').innerHTML = "OK";
  121. if(!gotoServerStop) {
  122. gotoServerStop = true;
  123. if(gotoServerMiddleClick) {
  124. window.open(finalURL);
  125. gotoServerMiddleClick = false;
  126. document.getElementById('gotoServerResponse').innerHTML = '<b>Opening new window to ' + NETDATA.registry.machines[guid].name + '<br/><a href="' + verifyURL(finalURL) + '">' + escapeUserInputHTML(url) + '</a></b><br/>(check your pop-up blocker if it fails)';
  127. }
  128. else {
  129. document.getElementById('gotoServerResponse').innerHTML += 'found it! It is at:<br/><small>' + escapeUserInputHTML(url) + '</small>';
  130. document.location = verifyURL(finalURL);
  131. }
  132. }
  133. }
  134. else {
  135. if(typeof data !== 'undefined' && data !== null && typeof data.machine_guid === 'string' && data.machine_guid !== guid)
  136. error = 'wrong machine';
  137. document.getElementById(guid + '-' + id + '-status').innerHTML = error;
  138. gotoServerValidateRemaining--;
  139. if(gotoServerValidateRemaining <= 0) {
  140. gotoServerMiddleClick = false;
  141. document.getElementById('gotoServerResponse').innerHTML = '<b>Sorry! I cannot find any operational URL for this server</b>';
  142. if(thisIsHttps === true && urlsInHttp > 0) {
  143. document.getElementById('gotoServerResponse').innerHTML += '<br/>redirecting myself to HTTP to allow checking';
  144. document.location = verifyURL(document.location.toString().replace('https://', 'http://'));
  145. }
  146. }
  147. }
  148. });
  149. }, (id * 50) + penaldy);
  150. }
  151. var netdataRegistryCallback = function(machines_array) {
  152. if(typeof urlOptions.host !== 'string') {
  153. document.getElementById('bodylog').innerHTML = "Sorry... bad request.";
  154. return;
  155. }
  156. document.getElementById('message').innerHTML = 'These are the URLs this machine is known:';
  157. if(document.location.toString().startsWith('https://'))
  158. thisIsHttps = true;
  159. if(machines_array) {
  160. var guids = {};
  161. var checked = {};
  162. var len = machines_array.length;
  163. var count = 0;
  164. while(len--) {
  165. if(machines_array[len].name === urlOptions.host) {
  166. var ulen = machines_array[len].alternate_urls.length;
  167. var guid = machines_array[len].guid;
  168. guids[guid] = true;
  169. gotoServerValidateRemaining = ulen;
  170. while(ulen--) {
  171. var url = machines_array[len].alternate_urls[ulen];
  172. checked[url] = true;
  173. gotoServerValidateUrl(count++, guid, url);
  174. }
  175. setTimeout(function() {
  176. if(gotoServerStop === false) {
  177. document.getElementById('gotoServerResponse').innerHTML = '<b>Added all the known URLs for this machine.</b>';
  178. var guid;
  179. for(guid in guids) {
  180. NETDATA.registry.search(guid, function(data) {
  181. // console.log(data);
  182. len = data.urls.length;
  183. while(len--) {
  184. var url = data.urls[len][1];
  185. // console.log(url);
  186. if(typeof checked[url] === 'undefined') {
  187. gotoServerValidateRemaining++;
  188. checked[url] = true;
  189. gotoServerValidateUrl(count++, guid, url);
  190. }
  191. }
  192. });
  193. }
  194. }
  195. }, 2000);
  196. return false;
  197. }
  198. }
  199. }
  200. document.getElementById('bodylog').innerHTML = "Sorry... your account is not linked to a netdata server named: <b>" + escapeUserInputHTML(urlOptions.host) + '</b>';
  201. };
  202. netdataQueryParse();
  203. </script>
  204. <body>
  205. <div class="container">
  206. <div id="bodylog" style="padding-top: 8vmax; font-size: 2.0vmax;">
  207. <span id="message">Please wait...</span>
  208. <div style="padding-top: 20px;">
  209. <table id="gotoServerList" class="table">
  210. </table>
  211. </div>
  212. <p style="padding-top: 10px;"><small>
  213. This page can only find netdata URLs you have already visited and are linked to your account on this netdata registry.
  214. </small></p>
  215. <div id="gotoServerResponse" style="display: block; width: 100%; text-align: center; padding-top: 20px;"></div>
  216. </div>
  217. </div>
  218. </body>
  219. </html>