-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnodius.js
152 lines (135 loc) · 4.96 KB
/
nodius.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
global.projectPath = __dirname;
var util = require('util');
var fs = require('fs');
var logger = util.log;
var echo = util.puts;
var path = require('path');
spawn = require('child_process').spawn;
var CircularBuffer = require('./lib/CircularBuffer').CircularBuffer;
//load config
try {
var devicesJSON = fs.readFileSync(__dirname + "/config/devices.json", encoding = 'utf8');
var appConfig = fs.readFileSync(__dirname + "/config/config.json", encoding = 'utf8');
} catch(e) {
logger("File not found.\nerror :" + e);
}
//init objects and NODIUS namespace
var NODIUS = {};
NODIUS.Collectors = require('./lib/Collectors').Collectors;
NODIUS.App = {};
NODIUS.Storage = {};
global.NODIUS = {};
global.NODIUS.Storage = NODIUS.Storage;
NODIUS.Config = {};
NODIUS.Config.devices = JSON.parse(devicesJSON);
NODIUS.Config.appConfig = JSON.parse(appConfig);
global.NODIUS.Config = NODIUS.Config;
NODIUS.App.dispatcher = function(host) {
//@todo add validation in dispatcher
NODIUS.App.getResources(host);
};
//
// get single resource
// @param host JSON object
NODIUS.App.getResources = function(host) {
// init buffers container if empty
NODIUS.Storage.buffers = NODIUS.Storage.buffers || {};
for (var i = 0; i < host.resources.length; i++) {
var resource = host.resources[i];
var group = (host.group == '') ? '' : host.group + '.';
var bufferName = group + host.name + '.' + resource.method;
// fetch buffer of init new
NODIUS.Storage.buffers[bufferName] = NODIUS.Storage.buffers[bufferName] || new CircularBuffer(resource.size, resource.params);
var buffer = NODIUS.Storage.buffers[bufferName];
global.NODIUS.Storage.buffers = NODIUS.Storage.buffers;
NODIUS.App.readValue(resource.method, resource.params, buffer, function(response) {
// additional response handling here
});
}
};
// take a snapshot of existing buffers to disk
NODIUS.Storage.persist = function() {
for (var i in NODIUS.Storage.buffers) {
var bufferName = i;
var buffer = NODIUS.Storage.buffers[i];
var meta = buffer.getMetaData();
var elements = [];
buffer.getEach(function(element) {
elements.push(element);
});
elements = elements.reverse(false);
var diskObject = {"len":buffer._len,"meta":meta, "values":elements };
fs.writeFile(__dirname + '/data/buffers/' + bufferName + '.json', JSON.stringify(diskObject), function(err) {
if (err) {
util.log("EROOR SAVING BUFFER " + bufferName);
} else {
}
});
}
};
// load existing queues from disk
NODIUS.Storage.load = function() {
NODIUS.Storage.buffers = NODIUS.Storage.buffers || {};
//read existing queues from disk
var files = fs.readdirSync(__dirname + '/data/buffers');
try {
for (var i = 0; i < files.length; i++) {
//read file
var file = files[i];
var extension = path.extname(file);
if (extension === '.json') {
var fileName = __dirname + '/data/buffers/' + file;
var stats = fs.statSync(fileName);
var fileContent = fs.readFileSync(fileName, encoding = 'utf8');
//parse JSON
var fileJsonString = JSON.parse(fileContent);
//generate buffer name
var bufferName = file.replace('.json', '');
NODIUS.Storage.buffers[bufferName] = new CircularBuffer(fileJsonString.len, fileJsonString.meta);
for (var j = 0; j < fileJsonString.values.length; j ++) {
var value = fileJsonString.values[j];
NODIUS.Storage.buffers[bufferName].loadPush(value);
}
util.log(" LOADING::: buffer :" + bufferName + " loaded");
}
}
} catch(e) {
util.log("ERROR OCCURED: " + e.stack);
}
};
/**
*
* @param method - collect method
* @param params - additional parms
* @param buffer - which buffer to use
* @param callback - callback function
*/
NODIUS.App.readValue = function(method, params, buffer, callback) {
var methods = method.split(".");
NODIUS.Collectors[methods[0]][methods[1]].get(params, buffer, callback);
};
/**
* collect params for given host
* @param host - host object from config
*/
NODIUS.App.collect = function(host) {
setInterval(function() {
NODIUS.App.dispatcher(host);
}, host.pollInterval * 1000)
};
// Load existing buffers from disk
NODIUS.Storage.load();
//start collecting for all defined hosts
for (var i = 0; i < NODIUS.Config.devices.hosts.length; i++) {
var host = NODIUS.Config.devices.hosts[i];
NODIUS.App.collect(host);
}
// start web interface
var server = require('./server/Server');
/**
* start job that takes snapshots of existing buffers and saves
* them to disk
*/
setInterval(function() {
NODIUS.Storage.persist();
}, (NODIUS.Config.appConfig.appSettings.snapshotInterval * 1000));