-
Notifications
You must be signed in to change notification settings - Fork 1
/
hsm-display-server.js
167 lines (164 loc) · 6.13 KB
/
hsm-display-server.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// HSM display server - displays logs collected by the Host Spot Monitor
// Setup all required packages
'use strict';
var express = require("express");
var bodyParser = require("body-parser");
var fs = require("fs");
var jsdom = require("jsdom");
var readLine = require("readline");
var WebSocketServer = require("ws").Server;
var spawn = require("child_process").spawn;
var execSync = require("child_process").execSync;
// Read main html page - this will be parsed later
let mainPageContents = fs.readFileSync("./index.html");
// Read graphing html page - this will be parsed later
let graphPageContents = fs.readFileSync("./graph.html");
// Read realtime graphing html page - this will be parsed later
let realtimePageContents = fs.readFileSync("./realtime.html");
// Read log entry page - this will be reissued later
// create an express server to make a static file server
var app = express();
var statTime = [];
var relay = [];
var pipe_data = [];
var spawn = require('child_process').spawn;
var pipe = spawn('./flow_meter.py', []);
pipe.stdout.on('data', function(data) {
pipe_data.push(data.toString());
});
pipe.stderr.on('data', function(data) {
console.log("got an error");
console.log(data.toString());
});
pipe.on('exit', function(error) {
console.log("flow_meter terminated!");
console.log(error);
});
// initialize global information
// check for changes to the test database every second
setInterval(function(){
let changed = false;
for (let entry of statTime) {
changed |= fs.statSync(entry.Path).ctime.valueOf() != entry.Time.valueOf();
entry.Time = fs.statSync(entry.Path).ctime;
}
if (changed) {
console.log("need to do something");
}
let dataSent = false;
for (let entry of relay) {
for (let point of pipe_data) {
entry.send("refresh " + point);
dataSent = true;
}
}
if (dataSent || ! relay.length) {
pipe_data = [];
}
},5000);
// if the express server is contacted, look at the request and build a response or
// forward the request to the standard server behavior.
app.get("/", function(request, response, next) {
// this is the main page so build replacement DOM
// that has the sections available to edit
let files = fs.readdirSync("./");
let dom = new jsdom.JSDOM(mainPageContents);
let document = dom.window.document;
let insertionPoint = document.querySelector("#list");
for (let file of files) {
if (file.indexOf("plot_") == 0) {
let element = document.createElement("a");
let graphFile = file.replace("plot_", "graph_").replace(".js", ".html");
element.setAttribute("href",graphFile);
element.innerHTML = graphFile;
let listElement = document.createElement("li");
listElement.appendChild(element);
insertionPoint.appendChild(listElement);
}
}
insertionPoint = document.querySelector("#JSONlist");
for (let file of files) {
if (file.indexOf("plot_") == 0) {
let element = document.createElement("a");
element.setAttribute("href",file);
element.innerHTML = file;
let listElement = document.createElement("li");
listElement.appendChild(element);
insertionPoint.appendChild(listElement);
}
}
response.send(dom.serialize());
});
app.get("/graph_*", function(request, response, next) {
console.log("process a graph route");
let postScript = request.url.replace("/graph_","").replace(".html","") + ".js";
let plotDataName = "plot_" + postScript;
let dom = new jsdom.JSDOM(graphPageContents);
let document = dom.window.document;
let modificationPoint = document.querySelector("#title");
modificationPoint.innerHTML = request.url;
modificationPoint = document.querySelector("#dataTarget");
modificationPoint.setAttribute("src", plotDataName);
response.send(dom.serialize());
});
app.get("/realtime.html", function(request, response, next) {
console.log("process a realtime request route");
let plotDataName = "realtime.js";
let dom = new jsdom.JSDOM(realtimePageContents);
let document = dom.window.document;
let modificationPoint = document.querySelector("#dataTarget");
modificationPoint.setAttribute("src", plotDataName);
response.send(dom.serialize());
});
app.get("*", function(request, response, next) {
console.log("fell into default get");
console.log(request.url);
console.log(request.method);
next();
});
app.use(bodyParser.urlencoded({extended: true}));
app.post("/process_new_logs.html", function(request, response, next) {
console.log("fell into process new logs");
console.log(request.body);
if (typeof request.body.yes === "undefined") {
console.log("not doing post processing");
response.redirect("/");
return;
}
console.log("processing kernel logs");
let date = new Date();
let filePrefix = "plot_" + (date.getMonth()+1) + "-" + date.getDate() + "-" + (1900 + date.getYear());
execSync("./process_iptable_logs.py /var/log/kern.log " + filePrefix);
if (! (typeof request.body.box === "undefined")) {
console.log("update the annotation database");
execSync("./build_url_ip_db");
}
execSync("./annotate_plot_files")
response.redirect("/");
next();
});
app.post("*", function(request, response, next) {
console.log("fell into default post");
console.log(request.url);
console.log(request.method);
next();
});
app.use(express.static("./"));
var ws = new WebSocketServer({server: app.listen(process.env.PORT || 3000)});
ws.on("connection", function(connection) {
relay.push(connection); // store for communication
console.log("web socket connection made at server from HTML client page");
connection.send("connected");
connection.on("message", function (message) {
if (message === "exit") {
relay.splice(relay.indexOf(connection), 1);
connection.close();
}
});
connection.on("close", function(message) {
relay.splice(relay.indexOf(connection), 1);
connection.close();
console.log("closing a connection");
});
});
console.log("HSM display server is listening");