chiark / gitweb /
f16e346d90d9e737a1a28d6b6f61cd17fa2076dd
[elogind.git] / src / journal / browse.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4         <title>Journal</title>
5         <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6         <style type="text/css">
7                 div#divlogs, div#diventry {
8                         font-family: monospace;
9                         font-size: 8pt;
10                         background-color: #ffffff;
11                         padding: 1em;
12                         margin: 2em 0em;
13                         border-radius: 10px 10px 10px 10px;
14                         border: 1px solid threedshadow;
15                         white-space: nowrap;
16                         overflow-x: scroll;
17                 }
18                 div#diventry {
19                         display: none;
20                 }
21                 div#divlogs {
22                         display: block;
23                 }
24                 body {
25                         background-color: #ededed;
26                         color: #313739;
27                         font: message-box;
28                         margin: 5em;
29                 }
30                 td.timestamp {
31                         text-align: right;
32                         border-right: 1px dotted lightgrey;
33                         padding-right: 5px;
34                 }
35                 td.process {
36                         border-right: 1px dotted lightgrey;
37                         padding-left: 5px;
38                         padding-right: 5px;
39                 }
40                 td.message {
41                         padding-left: 5px;
42                 }
43                 td.message > a:link, td.message > a:visited {
44                         text-decoration: none;
45                         color: #313739;
46                 }
47                 td.message-error {
48                         padding-left: 5px;
49                         color: red;
50                         font-weight: bold;
51                 }
52                 td.message-error > a:link, td.message-error > a:visited {
53                         text-decoration: none;
54                         color: red;
55                 }
56                 td.message-highlight {
57                         padding-left: 5px;
58                         font-weight: bold;
59                 }
60                 td.message-highlight > a:link, td.message-highlight > a:visited {
61                         text-decoration: none;
62                         color: #313739;
63                 }
64                 td > a:hover, td > a:active {
65                         text-decoration: underline;
66                         color: #c13739;
67                 }
68                 table#tablelogs, table#tableentry {
69                         border-collapse: collapse;
70                 }
71                 td.field {
72                         text-align: right;
73                         border-right: 1px dotted lightgrey;
74                         padding-right: 5px;
75                 }
76                 td.data {
77                         padding-left: 5px;
78                 }
79         </style>
80 </head>
81
82 <body>
83         <!-- TODO:
84                 - live display
85                 - keyboard navigation
86                 - localstorage
87                 - show red lines for reboots -->
88
89         <h1 id="title"></h1>
90
91         <div id="os"></div>
92         <div id="virtualization"></div>
93         <div id="cutoff"></div>
94         <div id="machine"></div>
95         <div id="usage"></div>
96         <div id="showing"></div>
97
98         <div id="divlogs"><table id="tablelogs"></table></div>
99         <a name="entry"></a>
100         <div id="diventry"><table id="tableentry"></table></div>
101
102         <form>
103                 <input id="head" type="button" value="|&lt;" onclick="entriesLoadHead();"/>
104                 <input id="previous" type="button" value="&lt;&lt;" onclick="entriesLoadPrevious();"/>
105                 <input id="next" type="button" value="&gt;&gt;" onclick="entriesLoadNext();"/>
106                 <input id="tail" type="button" value="&gt;|" onclick="entriesLoadTail();"/>
107                 &nbsp;&nbsp;&nbsp;&nbsp;
108                 <input id="more" type="button" value="More" onclick="entriesMore();"/>
109                 <input id="less" type="button" value="Less" onclick="entriesLess();"/>
110         </form>
111
112         <script type="text/javascript">
113                 var first_cursor = null;
114                 var last_cursor = null;
115
116                 function setCookie(name, value, msec) {
117                         var d = new Date();
118                         d.setMilliseconds(d.getMilliseconds() + msec);
119                         var v = escape(value) + "; expires=" + d.toUTCString();
120                         document.cookie = name + "=" + value;
121                 }
122
123                 function getCookie(name) {
124                         var i, l;
125                         l = document.cookie.split(";");
126                         for (i in l) {
127                                 var x, y, j;
128                                 j = l[i].indexOf("=");
129                                 x = l[i].substr(0, j);
130                                 y = l[i].substr(j+1);
131                                 if (x == name)
132                                         return unescape(y);
133                         }
134                         return null;
135                 }
136
137                 function getNEntries() {
138                         var n;
139                         n = getCookie("n_entries");
140                         if (n == null)
141                                 return 50;
142                         return parseInt(n);
143                 }
144
145                 function showNEntries(n) {
146                         var showing = document.getElementById("showing");
147                         showing.innerHTML = "Showing <b>" + n.toString() + "</b> entries.";
148                 }
149
150                 function setNEntries(n) {
151                         if (n < 10)
152                                 n = 10;
153                         else if (n > 1000)
154                                 n = 1000;
155
156                         setCookie("n_entries", n.toString(), 30*24*60*60*1000);
157                         showNEntries(n);
158                 }
159
160                 function machineLoad() {
161                         var request = new XMLHttpRequest();
162                         request.open("GET", "/machine");
163                         request.onreadystatechange = machineOnResult;
164                         request.setRequestHeader("Accept", "application/json");
165                         request.send(null);
166                 }
167
168                 function formatBytes(u) {
169                         if (u >= 1024*1024*1024*1024)
170                                 return (u/1024/1024/1024/1024).toFixed(1) + " TiB";
171                         else if (u >= 1024*1024*1024)
172                                 return (u/1024/1024/1024).toFixed(1) + " GiB";
173                         else if (u >= 1024*1024)
174                                 return (u/1024/1024).toFixed(1) + " MiB";
175                         else if (u >= 1024)
176                                 return (u/1024).toFixed(1) + " KiB";
177                         else
178                                 return u.toString() + " B";
179                 }
180
181                 function escapeHTML(s) {
182                         return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
183                 }
184
185                 function machineOnResult(event) {
186                         if ((event.currentTarget.readyState != 4) ||
187                                 (event.currentTarget.status != 200 && event.currentTarget.status != 0))
188                                 return;
189
190                         var d = JSON.parse(event.currentTarget.responseText);
191
192                         var title = document.getElementById("title");
193                         title.innerHTML = 'Journal of ' + escapeHTML(d.hostname);
194                         document.title = 'Journal of ' + escapeHTML(d.hostname);
195
196                         var machine = document.getElementById("machine");
197                         machine.innerHTML = 'Machine ID is <b>' + d.machine_id + '</b>, current boot ID is <b>' + d.boot_id + '</b>.';
198
199                         var cutoff = document.getElementById("cutoff");
200                         var from = new Date(parseInt(d.cutoff_from_realtime) / 1000);
201                         var to = new Date(parseInt(d.cutoff_to_realtime) / 1000);
202                         cutoff.innerHTML = 'Journal begins at <b>' + from.toLocaleString() + '</b> and ends at <b>' + to.toLocaleString() + '</b>.';
203
204                         var usage = document.getElementById("usage");
205                         usage.innerHTML = 'Disk usage is <b>' + formatBytes(parseInt(d.usage)) + '</b>.';
206
207                         var os = document.getElementById("os");
208                         os.innerHTML = 'Operating system is <b>' + escapeHTML(d.os_pretty_name) + '</b>.';
209
210                         var virtualization = document.getElementById("virtualization");
211                         virtualization.innerHTML = d.virtualization == "bare" ? "Running on <b>bare metal</b>." : "Running on virtualization <b>" + escapeHTML(d.virtualization) + "</b>.";
212                 }
213
214                 function entriesLoad(range) {
215                         var request = new XMLHttpRequest();
216                         request.open("GET", "/entries");
217                         request.onreadystatechange = entriesOnResult;
218                         request.setRequestHeader("Accept", "application/json");
219                         request.setRequestHeader("Range", "entries=" + range + ":" + getNEntries().toString());
220                         request.send(null);
221                 }
222
223                 function entriesLoadNext() {
224                         if (last_cursor == null)
225                                 entriesLoad("");
226                         else
227                                 entriesLoad(last_cursor + ":1");
228                 }
229
230                 function entriesLoadPrevious() {
231                         if (first_cursor == null)
232                                 entriesLoad("");
233                         else
234                                 entriesLoad(first_cursor + ":-" + getNEntries().toString());
235                 }
236
237                 function entriesLoadHead() {
238                         entriesLoad("");
239                 }
240
241                 function entriesLoadTail() {
242                         entriesLoad(":-" + getNEntries().toString());
243                 }
244
245                 function entriesOnResult(event) {
246
247                         if ((event.currentTarget.readyState != 4) ||
248                                 (event.currentTarget.status != 200 && event.currentTarget.status != 0))
249                                 return;
250
251                         var logs = document.getElementById("tablelogs");
252
253                         var lc = null;
254                         var fc = null;
255
256                         var i;
257                         var l = event.currentTarget.responseText.split('\n');
258
259                         if (l.length <= 1) {
260                                 logs.innerHTML = '<tbody><tr><td colspan="3"><i>No further entries...</i></td></tr></tbody>';
261                                 return;
262                         }
263
264                         var buf = '';
265
266                         for (i in l) {
267
268                                 if (l[i] == '')
269                                         continue;
270
271                                 var d = JSON.parse(l[i]);
272                                 if (d.MESSAGE == undefined || d.__CURSOR == undefined)
273                                         continue;
274
275                                 if (fc == null)
276                                         fc = d.__CURSOR;
277                                 lc = d.__CURSOR;
278
279                                 var priority;
280                                 if (d.PRIORITY != undefined)
281                                         priority = parseInt(d.PRIORITY);
282                                 else
283                                         priority = 6;
284
285                                 if (priority <= 3)
286                                         clazz = "message-error";
287                                 else if (priority <= 5)
288                                         clazz = "message-highlight";
289                                 else
290                                         clazz = "message";
291
292                                 buf += '<tr><td class="timestamp">';
293
294                                 if (d.__REALTIME_TIMESTAMP != undefined) {
295                                         var timestamp = new Date(parseInt(d.__REALTIME_TIMESTAMP) / 1000);
296                                         buf += timestamp.toLocaleString();
297                                 }
298
299                                 buf += '</td><td class="process">';
300
301                                 if (d.SYSLOG_IDENTIFIER != undefined)
302                                         buf += escapeHTML(d.SYSLOG_IDENTIFIER);
303                                 else if (d._COMM != undefined)
304                                         buf += escapeHTML(d._COMM);
305
306                                 if (d._PID != undefined)
307                                         buf += "[" + escapeHTML(d._PID) + "]";
308                                 else if (d.SYSLOG_PID != undefined)
309                                         buf += "[" + escapeHTML(d.SYSLOG_PID) + "]";
310
311                                 buf += '</td><td class="' + clazz + '"><a href="#entry" onclick="onMessageClick(\'' + lc + '\');">';
312
313                                 if (d.MESSAGE == null)
314                                         buf += "[blob data]";
315                                 else if (d.MESSAGE instanceof Array)
316                                         buf += "[" + formatBytes(d.MESSAGE.length) + " blob data]";
317                                 else
318                                         buf += escapeHTML(d.MESSAGE);
319
320                                 buf += '</a></td></tr>';
321                         }
322
323                         logs.innerHTML = '<tbody>' + buf + '</tbody>';
324
325                         if (fc != null)
326                                 first_cursor = fc;
327                         if (lc != null)
328                                 last_cursor = lc;
329                 }
330
331                 function entriesMore() {
332                         setNEntries(getNEntries() + 10);
333                         entriesLoad(first_cursor);
334                 }
335
336                 function entriesLess() {
337                         setNEntries(getNEntries() - 10);
338                         entriesLoad(first_cursor);
339                 }
340
341                 function onResultMessageClick(event) {
342                         if ((event.currentTarget.readyState != 4) ||
343                                 (event.currentTarget.status != 200 && event.currentTarget.status != 0))
344                                 return;
345
346                         var d = JSON.parse(event.currentTarget.responseText);
347
348                         document.getElementById("diventry").style.display = "block";
349                         entry = document.getElementById("tableentry");
350
351                         var buf = "";
352                         for (var key in d){
353                                 var data = d[key];
354
355                                 if (data == null)
356                                         data = "[blob data]";
357                                 else if (data instanceof Array)
358                                         data = "[" + formatBytes(data.length) + " blob data]";
359                                 else
360                                         data = escapeHTML(data);
361
362                                 buf += '<tr><td class="field">' + key + '</td><td class="data">' + data + '</td></tr>';
363                         }
364                         entry.innerHTML = '<tbody>' + buf + '</tbody>';
365                 }
366
367                 function onMessageClick(t) {
368                         var request = new XMLHttpRequest();
369                         request.open("GET", "/entries?discrete");
370                         request.onreadystatechange = onResultMessageClick;
371                         request.setRequestHeader("Accept", "application/json");
372                         request.setRequestHeader("Range", "entries=" + t + ":0:1");
373                         request.send(null);
374                 }
375
376                 machineLoad();
377                 entriesLoad("");
378                 showNEntries(getNEntries());
379         </script>
380 </body>
381 </html>