-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateMemory.js
executable file
·81 lines (71 loc) · 2.21 KB
/
updateMemory.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
var http = require('http');
var MongoClient = require('mongodb').MongoClient, format = require('util').format;
var hostIP = '107.170.193.27';
var portAdd = 4567;
var document = {};
setInterval(function(){
var checks;
var options = {
host: hostIP,
port: portAdd,
path: '/clients'
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
var jsonChunk = JSON.parse(chunk, function(k, v) {
if(k == "name"){
if(v != 'test' ){
getChecks(v);
}
}
return v; // return the unchanged property value.
});
});
});
req.end();
}, 100*1000);
function getChecks(client){
var options = {
host: hostIP,
port: portAdd,
path: '/results/'+client
};
var req = http.request(options, function(res){
res.setEncoding('utf8');
res.on('data', function(chunk){
var jsonChunk = JSON.parse(chunk, function(checkName,checkValue){
if(checkName == 'client') document.client = checkValue;
if(checkName == 'name') document.name = checkValue;
if(checkName == 'output'){
var arr = checkValue.split("fin");
var arr2 = arr[1];
document.value = arr2;
if(document.name == 'keepalive' || document.name == 'disk') document = {};
}
if(checkName == 'status' && document != {} && document.client != undefined){
var docEntry = {client: document.client, value:document.value, time:new Date().toString()};
var p1 = new Promise(
function(resolve, reject) {
resolve(storeResults(docEntry, document.name));
}
);
p1.then(function(){console.log('ran')});
}
});
});
});
req.end();
}
function storeResults(docEntry, collectionName){
MongoClient.connect('mongodb://127.0.0.1:27017/thesis', function(err, db) {
if (err) throw err;
console.log("Connected to Database");
db.collection(collectionName.toString()).insert(docEntry, function(err, records) {
if (err) throw err;
document = {};
MongoClient.close();
return;
});
});
}