-
Notifications
You must be signed in to change notification settings - Fork 0
/
deviceWatch.js
240 lines (188 loc) · 7.13 KB
/
deviceWatch.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
var util = require('util');
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');
var csv = require('csv');
var moment = require('moment');
var schedule = require('node-schedule');
var NetworkDevice = require("./networkDevice.js");
var JSONConfigurationController = require("./JSONConfigurationController.js");
var configuration;
var networkDevices = new Array();
var dateformat = "YYYY/MM/DD HH:mm:ss";
var fingCommand;
var nonWhiteListedDeviceWarnIntervalID;
var configFileIncPath = path.join(__dirname + '/configuration.json');
var convert_min_to_ms = 60 * 1000;
function main() {
clearAlertDevices();
clearWhiteListDevices();
configuration = new JSONConfigurationController();
configuration.setConfiguration(configFileIncPath);
configuration.on("configComplete", postConfiguration);
configuration.on("reset", resetConfiguration);
}
function resetConfiguration() {
clearAlertDevices();
clearWhiteListDevices();
}
function postConfiguration() {
for (var recordNum in configuration.data.AlertDevices) {
newNetworkDevice = processDevice(configuration.data.AlertDevices[recordNum].mac,
undefined, undefined, undefined, undefined);
newNetworkDevice.setAlertDevice(
configuration.data.AlertDevices[recordNum].name,
configuration.data.AlertDevices[recordNum].ttl)
newNetworkDevice.setAlertMethods(
configuration.data.AlertDevices[recordNum].alertMethods)
newNetworkDevice.setWakeMethods(
configuration.data.AlertDevices[recordNum].wakeMethods)
}
for (var recordNum in configuration.data.WhiteListDevices) {
newNetworkDevice = processDevice(configuration.data.WhiteListDevices[recordNum].mac,
undefined, undefined, undefined, undefined);
newNetworkDevice.setWhiteListDevice(configuration.data.WhiteListDevices[recordNum].name)
}
console.log("** (" + getCurrentTime() + ") CONFIGURATION: Fing netmask being set to: " + configuration.data.FingConfiguration.netmask);
if (typeof fingCommand !== 'undefined') {
fingCommand.stdin.pause();
fingCommand.kill();
}
if (typeof nonWhiteListedDeviceWarnIntervalID !== 'undefined')
{
clearInterval(nonWhiteListedDeviceWarnIntervalID);
}
assignConfiguration();
runFing();
nonWhiteListedDeviceWarnIntervalID = setInterval(function() {
clearUnknownDevicesReportedFlag();
}, configuration.data.NonWhiteListedDeviceWarnInterval * convert_min_to_ms);
}
function clearAlertDevices() {
for (var deviceCounter=0; deviceCounter<networkDevices.length; deviceCounter++)
{
networkDevices[deviceCounter].clearAlertDevice();
}
}
function clearWhiteListDevices() {
for (var deviceCounter=0; deviceCounter<networkDevices.length; deviceCounter++)
{
networkDevices[deviceCounter].clearWhiteListDevice();
}
}
function clearUnknownDevicesReportedFlag() {
for (var deviceCounter=0; deviceCounter<networkDevices.length; deviceCounter++)
{
if (!networkDevices[deviceCounter].isWhiteListedDevice())
{
networkDevices[deviceCounter].clearUnknownDeviceReportedFlag();
}
}
}
function assignConfiguration() {
if (typeof deviceWatchConfiguration === 'undefined') return;
for (var deviceCounter=0; deviceCounter<networkDevices.length; deviceCounter++)
{
networkDevices[deviceCounter].setConfiguration(deviceWatchConfiguration);
}
}
/*
Function: runFing
Parameters:
None
Description:
This function spawns the Fing process, and then asssigns a action to take upon output from Fing.
As a critical function for the applicaiton, this function parses the output from Fing and decides what to do with it.
Critical to this, is the rules that review the raw Fing results, and determine if it's a log message from Fing, or a device action.
If a device action needs to be taken, parseDevice() is called.
Known issues:
If a device has the keywords "Discovery" "hosts up" or "round" in them, this may been seen as a line to ignore.
*/
function runFing()
{
fingCommand = spawn('sudo',['fing', '-n', configuration.data.FingConfiguration.netmask, '-o', 'log,csv,console']);
fingCommand.stdout.on('data', function (data) {
if (configuration.data.Debug) console.log("Raw Output from fing: " + data);
var str = data.toString(), lines = str.split(/(\r?\n)/g);
for (var i=0; i<lines.length; i++) {
// if the line contains some key characters, or is less than 5 characters in length, ignore it.
if (lines[i].length > 5 && lines[i].indexOf("Discovery") == -1 && lines[i].indexOf("hosts up") == -1 && lines[i].indexOf("round") == -1)
{
console.log("** (" + getCurrentTime() + ") Line from Fing being processed: " + lines[i]);
parseFingOutput(lines[i]);
}
else if (lines[i].length > 5)
{
if (configuration.data.Debug) console.log("** (" + getCurrentTime() + ") Line from Fing being ignored: " + lines[i]);
}
}
});
fingCommand.on('close', function (code) {
console.log("** (" + getCurrentTime() + ") Fing process was closed and gave response: " + code);
});
fingCommand.on('error', function (err) {
console.log("** (" + getCurrentTime() + ") Fing process gave error: " + err);
})
}
function parseFingOutput(data) {
//console.log(data.toString());
csv()
.from.string(data.toString(), {delimiter: ';'})
.to.array(function(device, count) {
var manufacturer = device[0][6];
var mac = device[0][5];
var state = device[0][1];
var ip_address = device[0][2];
var timestamp = device[0][0];
var fqdn = device[0][4];
processDevice(mac, state == "up", ip_address, fqdn, manufacturer);
});
}
function findDevice(mac)
{
for (var deviceCounter=0; deviceCounter<networkDevices.length; deviceCounter++)
{
if (networkDevices[deviceCounter].getMACAddress().toUpperCase() == mac.toUpperCase())
{
return networkDevices[deviceCounter];
}
}
return undefined;
}
function processDevice(mac, state, ip, fqdn, manufacturer)
{
var whiteListDeviceFlag = false;
var alertDeviceFlag = false;
var alertExpiration = getCurrentTime();
var indigoValue;
var newRecord = false;
var previouslyReported = false;
var newNetworkDevice = findDevice(mac);
if (typeof(newNetworkDevice) === 'undefined') {
newRecord = true;
newNetworkDevice = new NetworkDevice(mac, ip, fqdn, manufacturer);
newNetworkDevice.setConfiguration(configuration);
newNetworkDevice.setDeviceState(state);
}
if (!newRecord)
{
if (configuration.data.Debug) console.log ("\n***************** " + getCurrentTime() + " -- UPDATE DEVICE " + mac + " / " + newNetworkDevice.getMACAddress() + " -- **************");
newNetworkDevice.setConfiguration(configuration);
if (typeof(ip) !== 'undefined') newNetworkDevice.setIPAddress(ip);
if (typeof(state) !== 'undefined') newNetworkDevice.setDeviceState(state);
if (typeof(fqdn) !== 'undefined') newNetworkDevice.setFQDN(fqdn);
if (typeof(manufacturer) !== 'undefined') newNetworkDevice.setManufacturer(manufacturer);
}
else if (newRecord)
{
if (configuration.data.Debug) console.log ("\n***************** " + getCurrentTime() + " -- NEW DEVICE -- **************");
networkDevices[networkDevices.length] = newNetworkDevice;
}
newNetworkDevice.logToConsole();
return newNetworkDevice;
}
function getCurrentTime() {
return moment().format(dateformat);
}
main();