summaryrefslogtreecommitdiffstats
path: root/web/gui/v2/registry-alert-redirect.html
blob: 0ef68dce08d87de5673591e1055e18d8c9a3b399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!doctype html><html><head><title>Netdata Registry</title><meta name="application-name" content="netdata"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta charset="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/><meta name="viewport" content="width=device-width,height=device-height,initial-scale=1,minimum-scale=1"/><meta name="apple-mobile-web-app-capable" content="yes"/><meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"/><script>function loadStyle(url, { media, insertAfter: aref, insertBefore: bref, rel, type } = {}) {
        rel = rel || 'stylesheet'
        type = type || 'text/css'
        return new Promise(function(resolve, reject) {
          let link = document.createElement('link');
          link.type = type;
          link.rel = rel;
          link.href = url;
          link.media = media || 'all';

          link.onerror = function(err) {
            reject(new URIError(`loadStyle: the stylesheet ${err.target.src} is not accessible.`));
          };

          link.onload = function() {
            resolve();
          };

          if (aref) {
            aref.parentNode.insertBefore(link, aref.nextSibling);
            return;
          }

          if (bref) {
            bref.parentNode.insertBefore(link, bref);
            return;
          }

          document.head.appendChild(link);
        });
      }

      loadStyle("/v2/static/splash.css")
      loadStyle("/v2/favicon.ico", {rel: "icon", type: "image/x-icon"})</script></head><body><div id="agent-splash-screen" class="loading"><div class="hero"><div class="logo-container"><svg width="133" height="105" viewBox="0 0 133 105" fill="none" xmlns="http://www.w3.org/2000/svg" class="logo-blur"><path fill-rule="evenodd" clip-rule="evenodd" d="M81.697 105H55.0693L0.5 0.5H77.9598C108.079 0.554913 132.484 24.7711 132.5 54.6451C132.452 82.485 109.73 105 81.697 105Z" fill="rgba(0,171,68,0.1)"/></svg> <svg width="133" height="105" viewBox="0 0 133 105" fill="none" xmlns="http://www.w3.org/2000/svg" class="logo"><path fill-rule="evenodd" clip-rule="evenodd" d="M81.697 105H55.0693L0.5 0.5H77.9598C108.079 0.554913 132.484 24.7711 132.5 54.6451C132.452 82.485 109.73 105 81.697 105Z" fill="#DDFFEB"/></svg></div><div class="headings"><h1 class="title">Netdata Alert Notifications</h1><div class="flex-center flex-column" id="main-message">Trying to find a Netdata Agent for this alert...</div><table id="mynodes"></table></div></div></div><script>let searchParams = new URLSearchParams(location.search)
        let mg = searchParams.get("agent_machine_guid")
        let tr_i = searchParams.get("transition_id")
        let token = location.hash.substring(1)

        function buildIframe(url) {
          let iframe = document.createElement('iframe');
          iframe.src = url + "/registry-hello.html" + location.search + "&url=" + url;
          iframe.style = { position: "absolute", left: "-99999999px" };
          iframe.width = 0 ;
          iframe.height = 0;
          iframe.tabindex = -1;
          iframe.title = "empty";
          iframe.classList.add("hidden");
          document.body.appendChild(iframe);
          return iframe
        }

        function urlToId(s) {
          s = s.trim();
          s = s.replace(/^https?:\/\//i, '');
          s = s.replace(/\W/g, '_');
          return s;
        }

        fetch("/api/v1/registry?action=search&for=" + mg, { cache: "no-cache", credentials: "include", headers: { Authorization: 'Bearer ' + (token || localStorage.getItem("registry-netdata-token")) } })
        .then(function(response) { return response.json() })
        .then(function(data) {
          let table = document.getElementById("mynodes");

          if (data.status === "ok") {
            if (location.protocol === "https:" && data.urls.some(function(u) {
              return (/http:/).test(u)
            })) {
              return location.replace('http://' + location.href.substring(8) + "#" + localStorage.getItem("registry-netdata-token"))
            }

            data.urls.sort(function(a, b) {
              return b[2] - a[2];
            }).forEach(function(urlData) {
              let urlMg = urlData[0]
              let url = urlData[1]
              let urlHostname = urlData[4]

              let rowIframe = buildIframe(url)

              let row = document.createElement("tr");
              let cell1 = document.createElement("td");
              let link = document.createElement("a");

              link.href = urlHostname ? (url + "/spaces/" + urlHostname + "/rooms/local/alerts/" + (tr_i || "") + location.search) : url + location.search;
              link.textContent = url;
              link.target = "_blank";
              link.classList.add("button", "ghost");

              cell1.appendChild(link);
              row.appendChild(cell1);

              let cell2 = document.createElement("td");
              cell2.id = urlToId("_" + url + urlMg);
              cell2.textContent = "checking...";
              row.appendChild(cell2);

              table.appendChild(row);

              window.addEventListener('message', function(event) {
                if (event.source !== rowIframe.contentWindow) return;
                if (event.data.url !== url || event.data.mg !== urlMg) return;
                link.href = event.data.hostname ? (url + "/spaces/" + event.data.hostname + "/rooms/local/alerts/" + (tr_i || "") + location.search) : url + location.search;

                let cellStatus = document.getElementById(urlToId("_" + url + urlMg));
                if (event.data.isSame) {
                  cell2.textContent = "OK";
                } else if (event.data.hasError) {
                  cell2.textContent = "can't connect";
                } else {
                  cell2.textContent = "wrong node";
                }
              });

              setTimeout(function() {
                let cellStatus = document.getElementById(urlToId("_" + url + urlMg));
                document.getElementById('agent-splash-screen').classList.remove("loading");
                if (cell2.textContent !== "checking...") {
                  return;
                }
                cell2.textContent = "can't connect";
              }, 5000)
            })
            let el = document.getElementById('agent-splash-screen');
            el.classList.add("table");
            document.getElementById('main-message').textContent = "Select a URL to see details about this alert:"
          } else {
            let el = document.getElementById('main-message');
            el.innerHTML = "<p>Can't find any Netdata Agent for this alert.</p><small>Netdata learns Agent URLs when you view them and associates them with web browsers.<br/>Probably, you have never viewed the dashboard of the Netdata Agent that sent this notification, with the browser you use now.</small>"
          }
        }).catch(function(e) {
          let el = document.getElementById('main-message');
          el.textContent = "Oops! Something went wrong."
        })</script></body></html>