-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_service.ts
314 lines (271 loc) · 13.2 KB
/
monitor_service.ts
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import database_service, { SearchDomain, UpdateMonitorDevice } from "./database_service";
import notification_service from "./notification_service";
import { MonitorDevice, MonitorTrigger, Proto } from "@prisma/client";
import { z } from "zod";
import { Ok, Err, Result } from 'ts-results-es';
import { isIP, Socket } from 'net';
import { lookup } from "dns";
import * as ping from 'net-ping';
import dgram from 'dgram';
export {SearchDomain} from "./database_service";
const MonitorDeviceCreateSchema = z.object({
name: z.string().optional(),
identifier: z.string(),
port: z.number().optional(),
proto: z.nativeEnum(Proto).default(Proto.ICMP),
persist: z.boolean().default(false),
monitor_trigger: z.nativeEnum(MonitorTrigger).default("OFFLINE"),
monitor_start_utc: z.number().gte(0, {message: "Notification time range must be between 0000 and 2400"}).lte(2400, {message: "Notification time range must be between 0000 and 2400"}).default(0),
monitor_end_utc: z.number().gte(0, {message: "Notification time range must be between 0000 and 2400"}).lte(2400, {message: "Notification time range must be between 0000 and 2400"}).default(2400),
requested_by: z.string(),
notify: z.string().optional(),
comments: z.string().optional(),
email_subject: z.string().optional(),
email_body: z.string().optional()
})
const MonitorDeviceUpdateSchema = z.object({
id: z.number(),
name: z.string().optional(),
identifier: z.string().optional(),
port: z.number().optional(),
proto: z.nativeEnum(Proto).optional(),
persist: z.boolean().optional(),
monitor_trigger: z.nativeEnum(MonitorTrigger).optional(),
monitor_start_utc: z.number().gte(0, {message: "Notification time range must be between 0000 and 2400"}).lte(2400, {message: "Notification time range must be between 0000 and 2400"}).optional(),
monitor_end_utc: z.number().gte(0, {message: "Notification time range must be between 0000 and 2400"}).lte(2400, {message: "Notification time range must be between 0000 and 2400"}).optional(),
notify: z.string().optional(),
comments: z.string().optional(),
email_subject: z.string().optional(),
email_body: z.string().optional(),
been_notified: z.boolean().optional()
})
export { MonitorDevice, MonitorDeviceCreateSchema, MonitorDeviceUpdateSchema };
interface DeviceState {
device: MonitorDevice,
current_address: string,
reachable: boolean | null
persistent_alarm_ended: boolean
}
class MonitorService {
private DEFAULT_FREQUENCY_MIN = "1";
constructor() {
const frequency_minutes = parseInt(process.env.MONITOR_FREQUENCY_MIN ?? this.DEFAULT_FREQUENCY_MIN);
setInterval(() => this.processMonitorQueue(), frequency_minutes * 60 * 1000)
}
addDevices(devices: MonitorDevice[]): Promise<Result<MonitorDevice[], string>> {
return database_service.createMonitorDevices(devices)
.then(results => {
return Ok(results);
}).catch(error => {
return Err(error as string)
});
}
removeDevices(deviceIds: number[], user: string): Promise<Result<null, string>> {
return database_service.deleteUserMonitorDevices(deviceIds, user)
.then(_ => {
return Ok(null)
})
.catch(error => {
return Err(error as string)
});
}
updateDevices(devices: UpdateMonitorDevice[]): Promise<Result<MonitorDevice[], string>> {
return database_service.updateMonitorDevices(devices)
.then(results => {
return Ok(results)
})
.catch(error => {
return Err(error as string)
});
}
getDevices(deviceIds: number[] | undefined): Promise<Result<MonitorDevice[], string>> {
return database_service.getMonitorDevices(deviceIds)
.then(results => {
return Ok(results)
})
.catch(error => {
return Err(error as string)
})
}
searchDevices(searchDomain: SearchDomain, searchText: string): Promise<Result<MonitorDevice[], string>> {
return database_service.searchMonitorDevices(searchDomain, searchText)
.then(results => {
return Ok(results)
})
.catch (error => {
return Err(error as string)
})
}
async processMonitorQueue() {
// Get devices within their monitoring window
console.log("Starting monitor run")
const date = new Date();
const time_utc = (date.getUTCHours() * 100) + date.getUTCMinutes(); // Get the current UTC in 2400 format, minus the colon
const deviceStates: DeviceState[] = (await database_service.getActiveMonitorDevicesForTime(time_utc)).map(device => {
return {
device: device,
current_address: device.identifier,
reachable: null,
persistent_alarm_ended: false
} as DeviceState
});
console.log(`Retrieved ${deviceStates.length} devices from monitor queue`);
if (!deviceStates.length) {
console.log('No devices in queue, exiting monitor run')
return
}
// Test connectivity based upon monitor trigger and protocol
await Promise.allSettled(deviceStates.map(async state => {
await new Promise<DeviceState>((resolve, reject) => {
if (!isIP(state.device.identifier)) {
console.log(`Resolving IP address for device ${state.device.id}, identifier ${state.device.identifier}`)
lookup(state.device.identifier, (error, address, _) => {
if (error) {
reject(error);
} else {
state.current_address = address;
console.log(`${state.device.identifier} resolved to ${address}`)
resolve(state);
}
})
} else {
resolve(state);
}
})
.then(async state => {
console.log(`Testing connectivity of device ${state.device.id}, address ${state.current_address}`)
switch (state.device.proto) {
case Proto.ICMP:
state.reachable = await this.checkIcmp(state.current_address);
break;
case Proto.TCP:
if (state.device.port) {
state.reachable = await this.checkTcpPort(state.current_address, state.device.port)
} else {
Promise.reject(`Device ${state.device.id} missing port but proto set to TCP`);
}
break;
}
})
.catch(error => {
console.error(error)
});
}));
// Determine which devices need to generate notifications
console.log('Connectivity checks finished, checking need to send notifications');
const toBeNotified: DeviceState[] = []
for (const state of deviceStates) {
if (state.reachable == null) {
//TODO: Improve error handling such that users know their record is throwing an error
// Skip records that have thrown an error.
console.error(`Skipping device ${state.device.id} as it has encountered an error and has no 'reachable' property.`);
console.error(state);
continue;
}
let shouldBeNotified: boolean;
const reachableAndOnlineTriggered = (state.reachable && state.device.monitor_trigger == MonitorTrigger.ONLINE);
const notReachableAndOfflineTriggered = (!state.reachable && state.device.monitor_trigger == MonitorTrigger.OFFLINE);
const triggerCriteriaMet = reachableAndOnlineTriggered || notReachableAndOfflineTriggered
if (!state.device.persist) {
shouldBeNotified = triggerCriteriaMet;
} else {
/*
Send notification for persistent records when:
- When notification hasn't already been sent AND trigger criteria is met
- When notification HAS been sent AND trigger criteria is NO LONGER met
- Set persistent_alarm_ended to true
*/
const triggeredAndHasntBeNotified = !state.device.been_notified && triggerCriteriaMet;
const noLongerTriggeredAndHasBeenNotified = state.device.been_notified && !triggerCriteriaMet;
shouldBeNotified = triggeredAndHasntBeNotified || noLongerTriggeredAndHasBeenNotified;
if (noLongerTriggeredAndHasBeenNotified) state.persistent_alarm_ended = true;
}
console.log(`Device ${state.device.id} reachable: ${state.reachable}, trigger: ${state.device.monitor_trigger}, notification trigged: ${shouldBeNotified}, persistent: ${state.device.persist}`)
if (shouldBeNotified) toBeNotified.push(state);
}
if (!toBeNotified.length) {
console.log(`No devices triggered for notification. Monitor run finished.`);
return;
}
console.log(`${toBeNotified.length} devices triggered for notification`);
await notification_service.processMonitorNotifications(toBeNotified.map(state => state.device));
// Remove non-persistent devices from queue
const devicesToBeRemoved = toBeNotified.filter(state => !state.device.persist).map(state => state.device.id);
if (devicesToBeRemoved.length) {
console.log(`${devicesToBeRemoved.length} devices marked for removal`);
console.log(`Deleting devices from queue: [${devicesToBeRemoved}]`)
await database_service.deleteMonitorDevices(devicesToBeRemoved);
} else {
console.log('No devices marked for removal');
}
// Update been_notified flag for persistent records
const pendingUpdate: MonitorDevice[] = [];
for (const state of toBeNotified.filter(state => state.device.persist)) {
if (state.persistent_alarm_ended) {
state.device.been_notified = false;
} else {
state.device.been_notified = true;
}
pendingUpdate.push(state.device);
}
await database_service.updateMonitorDevices(pendingUpdate);
console.log('Monitor run finished.');
}
private async checkIcmp(address: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
const session = ping.createSession();
session.pingHost(address, (error: unknown, target: string) => {
if (error) {
if (error instanceof ping.RequestTimedOutError) {
resolve(false)
} else {
reject(`Error pinging ${address}: ${error}`);
}
} else {
resolve(true);
}
})
})}
private async checkTcpPort(address: string, port: number): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
console.log(`Testing TCP connectivity to ${address}:${port}`);
const socket = new Socket();
socket.setTimeout(2000);
socket.connect(port, address, () => {
console.log(`TCP connectivity to ${address}:${port}: ACTIVE`);
socket.destroy();
resolve(true);
})
socket.once('timeout', () => {
console.log(`TCP connectivity to ${address}:${port}: INACTIVE`);
socket.destroy();
resolve(false);
})
socket.once('error', error => {
socket.destroy();
if (error['code'] && error['code'] == 'ECONNREFUSED') {
console.log(`TCP connectivity to ${address}:${port}: INACTIVE`);
resolve(false);
} else {
reject(`Error occured while opening TCP socket to ${address}:${port}: ${error}`)
}
})
})
}
/*NOTE: Checking UDP connectivity is unreliable due to the protocol being connectionless.
Any approach to remedy this would be inconcistent at best, resulting in false alarms.
As such, this feature has been axed but the code shall remain as a reminder.*/
// private async checkUdpPort(address: string, port: number): Promise<boolean> {
// return new Promise<boolean>((resolve, reject) => {
// console.log(`Testing UDP connectivity to ${address}:${port}`)
// const socket = dgram.createSocket('udp4');
// socket.send('ping', port, address, (error) => {
// console.log(`UDP connectivity to ${address}:${port}: ${!error ? "ACTIVE" : "INACTIVE"}`)
// socket.close();
// resolve(!error); // No error means port is likely open
// });
// })
// }
}
const monitor_service = new MonitorService();
export default monitor_service;