2020-04-10 00:43:15 +02:00
|
|
|
import _ from "lodash";
|
|
|
|
import request from "request";
|
|
|
|
|
|
|
|
import {config} from "../config";
|
2022-07-21 12:07:18 +02:00
|
|
|
import {db} from "../db/database";
|
2020-04-10 00:43:15 +02:00
|
|
|
import * as DatabaseUtil from "../utils/databaseUtil";
|
|
|
|
import ErrorTypes from "../utils/errorTypes";
|
|
|
|
import Logger from "../logger";
|
|
|
|
|
|
|
|
import * as MailService from "../services/mailService";
|
|
|
|
import * as NodeService from "../services/nodeService";
|
|
|
|
import * as Resources from "../utils/resources";
|
|
|
|
import {RestParams} from "../utils/resources";
|
2022-07-18 17:49:42 +02:00
|
|
|
import {normalizeMac, parseInteger} from "../utils/strings";
|
2020-04-10 00:43:15 +02:00
|
|
|
import {monitoringDisableUrl} from "../utils/urlBuilder";
|
2022-08-04 15:31:01 +02:00
|
|
|
import CONSTRAINTS from "../shared/validation/constraints";
|
2020-04-10 00:43:15 +02:00
|
|
|
import {forConstraint} from "../validation/validator";
|
2022-06-23 14:26:15 +02:00
|
|
|
import {
|
2022-07-14 20:06:05 +02:00
|
|
|
Domain,
|
2022-07-21 18:39:33 +02:00
|
|
|
DurationSeconds,
|
2022-07-18 17:49:42 +02:00
|
|
|
Hostname,
|
2022-07-28 13:16:13 +02:00
|
|
|
isBoolean,
|
2022-07-28 12:24:07 +02:00
|
|
|
isDomain,
|
2022-06-23 14:26:15 +02:00
|
|
|
isMonitoringSortField,
|
2022-07-18 17:49:42 +02:00
|
|
|
isOnlineState,
|
2022-07-28 12:24:07 +02:00
|
|
|
isSite,
|
2022-07-28 13:16:13 +02:00
|
|
|
isString,
|
|
|
|
isUndefined,
|
2022-06-23 14:26:15 +02:00
|
|
|
MAC,
|
|
|
|
MailType,
|
|
|
|
MonitoringSortField,
|
2022-07-21 18:39:33 +02:00
|
|
|
MonitoringState,
|
2022-07-14 20:06:05 +02:00
|
|
|
MonitoringToken,
|
2022-06-23 14:26:15 +02:00
|
|
|
NodeId,
|
|
|
|
NodeStateData,
|
|
|
|
OnlineState,
|
2022-07-21 12:07:18 +02:00
|
|
|
RunResult,
|
2022-07-14 20:06:05 +02:00
|
|
|
Site,
|
2022-07-21 18:39:33 +02:00
|
|
|
StoredNode,
|
|
|
|
toCreateOrUpdateNode,
|
2022-06-23 14:26:15 +02:00
|
|
|
UnixTimestampSeconds
|
|
|
|
} from "../types";
|
2022-07-21 18:39:33 +02:00
|
|
|
import {days, formatTimestamp, hours, now, parseTimestamp, subtract, weeks} from "../utils/time";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
type NodeStateRow = {
|
|
|
|
id: number,
|
|
|
|
created_at: UnixTimestampSeconds,
|
|
|
|
domain: Domain | null,
|
|
|
|
hostname: Hostname | null,
|
|
|
|
import_timestamp: UnixTimestampSeconds,
|
|
|
|
last_seen: UnixTimestampSeconds,
|
|
|
|
last_status_mail_sent: string | null,
|
|
|
|
last_status_mail_type: string | null,
|
|
|
|
mac: MAC,
|
|
|
|
modified_at: UnixTimestampSeconds,
|
|
|
|
monitoring_state: string | null,
|
|
|
|
site: Site | null,
|
|
|
|
state: string,
|
|
|
|
};
|
|
|
|
|
2020-04-10 00:43:15 +02:00
|
|
|
const MONITORING_STATE_MACS_CHUNK_SIZE = 100;
|
2021-07-26 21:40:17 +02:00
|
|
|
const NEVER_ONLINE_NODES_DELETION_CHUNK_SIZE = 20;
|
2020-04-10 00:43:15 +02:00
|
|
|
const MONITORING_MAILS_DB_BATCH_SIZE = 50;
|
|
|
|
/**
|
|
|
|
* Defines the intervals emails are sent if a node is offline
|
|
|
|
*/
|
2022-07-21 18:39:33 +02:00
|
|
|
const MONITORING_OFFLINE_MAILS_SCHEDULE: Record<number, DurationSeconds> = {
|
|
|
|
1: hours(3),
|
|
|
|
2: days(1),
|
|
|
|
3: weeks(1),
|
2020-04-10 00:43:15 +02:00
|
|
|
};
|
2022-07-21 18:39:33 +02:00
|
|
|
const DELETE_OFFLINE_NODES_AFTER_DURATION: DurationSeconds = days(100);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
export type ParsedNode = {
|
2022-07-14 20:06:05 +02:00
|
|
|
mac: MAC,
|
2022-07-21 18:39:33 +02:00
|
|
|
importTimestamp: UnixTimestampSeconds,
|
2022-05-26 13:58:01 +02:00
|
|
|
state: OnlineState,
|
2022-07-21 18:39:33 +02:00
|
|
|
lastSeen: UnixTimestampSeconds,
|
2022-07-28 12:24:07 +02:00
|
|
|
site?: Site,
|
|
|
|
domain?: Domain,
|
2020-04-10 00:43:15 +02:00
|
|
|
};
|
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
export type NodesParsingResult = {
|
2022-07-21 18:39:33 +02:00
|
|
|
importTimestamp: UnixTimestampSeconds,
|
2020-04-10 00:43:15 +02:00
|
|
|
nodes: ParsedNode[],
|
2020-06-30 17:08:24 +02:00
|
|
|
failedNodesCount: number,
|
|
|
|
totalNodesCount: number,
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:08:24 +02:00
|
|
|
export type RetrieveNodeInformationResult = {
|
|
|
|
failedParsingNodesCount: number,
|
|
|
|
totalNodesCount: number,
|
|
|
|
};
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
let previousImportTimestamp: UnixTimestampSeconds | null = null;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function insertNodeInformation(nodeData: ParsedNode, node: StoredNode): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'information-retrieval')
|
|
|
|
.debug('Node is new in monitoring, creating data: %s', nodeData.mac);
|
|
|
|
|
|
|
|
await db.run(
|
|
|
|
'INSERT INTO node_state ' +
|
|
|
|
'(hostname, mac, site, domain, monitoring_state, state, last_seen, import_timestamp, last_status_mail_sent, last_status_mail_type) ' +
|
|
|
|
'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
|
|
[
|
|
|
|
node.hostname,
|
|
|
|
node.mac,
|
|
|
|
nodeData.site,
|
|
|
|
nodeData.domain,
|
|
|
|
node.monitoringState,
|
|
|
|
nodeData.state,
|
2022-07-21 18:39:33 +02:00
|
|
|
nodeData.lastSeen,
|
|
|
|
nodeData.importTimestamp,
|
2020-04-10 00:43:15 +02:00
|
|
|
null, // new node so we haven't send a mail yet
|
|
|
|
null // new node so we haven't send a mail yet
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function updateNodeInformation(nodeData: ParsedNode, node: StoredNode, row: any): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'informacallbacktion-retrieval')
|
|
|
|
.debug('Node is known in monitoring: %s', nodeData.mac);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
if (row.import_timestamp >= nodeData.importTimestamp) {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'information-retrieval')
|
|
|
|
.debug('No new data for node, skipping: %s', nodeData.mac);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'information-retrieval')
|
|
|
|
.debug('New data for node, updating: %s', nodeData.mac);
|
|
|
|
|
|
|
|
await db.run(
|
|
|
|
'UPDATE node_state ' +
|
|
|
|
'SET ' +
|
|
|
|
'hostname = ?, ' +
|
|
|
|
'site = ?, ' +
|
|
|
|
'domain = ?, ' +
|
|
|
|
'monitoring_state = ?, ' +
|
|
|
|
'state = ?, ' +
|
|
|
|
'last_seen = ?, ' +
|
|
|
|
'import_timestamp = ?, ' +
|
|
|
|
'modified_at = ? ' +
|
|
|
|
'WHERE id = ? AND mac = ?',
|
|
|
|
[
|
|
|
|
node.hostname,
|
|
|
|
nodeData.site || row.site,
|
|
|
|
nodeData.domain || row.domain,
|
|
|
|
node.monitoringState,
|
|
|
|
nodeData.state,
|
2022-07-21 18:39:33 +02:00
|
|
|
nodeData.lastSeen,
|
|
|
|
nodeData.importTimestamp,
|
|
|
|
now(),
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
row.id,
|
|
|
|
node.mac
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function storeNodeInformation(nodeData: ParsedNode, node: StoredNode): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger.tag('monitoring', 'information-retrieval').debug('Storing status for node: %s', nodeData.mac);
|
|
|
|
|
|
|
|
const row = await db.get('SELECT * FROM node_state WHERE mac = ?', [node.mac]);
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
if (isUndefined(row)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
return await insertNodeInformation(nodeData, node);
|
|
|
|
} else {
|
|
|
|
return await updateNodeInformation(nodeData, node, row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const isValidMac = forConstraint(CONSTRAINTS.node.mac, false);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export function parseNode(importTimestamp: UnixTimestampSeconds, nodeData: any): ParsedNode {
|
2020-04-10 00:43:15 +02:00
|
|
|
if (!_.isPlainObject(nodeData)) {
|
|
|
|
throw new Error(
|
2020-06-30 01:10:18 +02:00
|
|
|
'Unexpected node type: ' + (typeof nodeData)
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isPlainObject(nodeData.nodeinfo)) {
|
|
|
|
throw new Error(
|
2020-06-30 01:10:18 +02:00
|
|
|
'Unexpected nodeinfo type: ' + (typeof nodeData.nodeinfo)
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
2020-06-30 01:10:18 +02:00
|
|
|
|
|
|
|
const nodeId = nodeData.nodeinfo.node_id;
|
2022-07-28 13:16:13 +02:00
|
|
|
if (!nodeId || !isString(nodeId)) {
|
2020-06-30 01:10:18 +02:00
|
|
|
throw new Error(
|
|
|
|
`Invalid node id of type "${typeof nodeId}": ${nodeId}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-10 00:43:15 +02:00
|
|
|
if (!_.isPlainObject(nodeData.nodeinfo.network)) {
|
|
|
|
throw new Error(
|
|
|
|
'Node ' + nodeId + ': Unexpected nodeinfo.network type: ' + (typeof nodeData.nodeinfo.network)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidMac(nodeData.nodeinfo.network.mac)) {
|
|
|
|
throw new Error(
|
|
|
|
'Node ' + nodeId + ': Invalid MAC: ' + nodeData.nodeinfo.network.mac
|
|
|
|
);
|
|
|
|
}
|
2022-07-18 17:49:42 +02:00
|
|
|
const mac = normalizeMac(nodeData.nodeinfo.network.mac) as MAC;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
if (!_.isPlainObject(nodeData.flags)) {
|
|
|
|
throw new Error(
|
|
|
|
'Node ' + nodeId + ': Unexpected flags type: ' + (typeof nodeData.flags)
|
|
|
|
);
|
|
|
|
}
|
2022-07-28 13:16:13 +02:00
|
|
|
if (!isBoolean(nodeData.flags.online)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw new Error(
|
|
|
|
'Node ' + nodeId + ': Unexpected flags.online type: ' + (typeof nodeData.flags.online)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const isOnline = nodeData.flags.online;
|
|
|
|
|
|
|
|
const lastSeen = parseTimestamp(nodeData.lastseen);
|
2022-07-21 18:39:33 +02:00
|
|
|
if (lastSeen === null) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw new Error(
|
|
|
|
'Node ' + nodeId + ': Invalid lastseen timestamp: ' + nodeData.lastseen
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:24:07 +02:00
|
|
|
let site: Site | undefined;
|
|
|
|
if (_.isPlainObject(nodeData.nodeinfo.system) && isSite(nodeData.nodeinfo.system.site_code)) {
|
|
|
|
site = nodeData.nodeinfo.system.site_code;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 12:24:07 +02:00
|
|
|
let domain: Domain | undefined;
|
|
|
|
if (_.isPlainObject(nodeData.nodeinfo.system) && isDomain(nodeData.nodeinfo.system.domain_code)) {
|
|
|
|
domain = nodeData.nodeinfo.system.domain_code;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-07-18 17:49:42 +02:00
|
|
|
mac,
|
2020-04-10 00:43:15 +02:00
|
|
|
importTimestamp: importTimestamp,
|
2022-05-26 13:58:01 +02:00
|
|
|
state: isOnline ? OnlineState.ONLINE : OnlineState.OFFLINE,
|
2022-07-21 18:39:33 +02:00
|
|
|
lastSeen,
|
2022-07-18 17:49:42 +02:00
|
|
|
site,
|
|
|
|
domain,
|
2020-04-10 00:43:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
export function parseNodesJson(body: string): NodesParsingResult {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger.tag('monitoring', 'information-retrieval').debug('Parsing nodes.json...');
|
|
|
|
|
|
|
|
const json = JSON.parse(body);
|
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
if (!_.isPlainObject(json)) {
|
|
|
|
throw new Error(`Expecting a JSON object as the nodes.json root, but got: ${typeof json}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const expectedVersion = 2;
|
|
|
|
if (json.version !== expectedVersion) {
|
|
|
|
throw new Error(`Unexpected nodes.json version "${json.version}". Expected: "${expectedVersion}"`);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const importTimestamp = parseTimestamp(json.timestamp);
|
|
|
|
if (importTimestamp === null) {
|
|
|
|
throw new Error('Invalid timestamp: ' + json.timestamp);
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
const result: NodesParsingResult = {
|
2022-07-21 18:39:33 +02:00
|
|
|
importTimestamp,
|
2020-06-30 17:08:24 +02:00
|
|
|
nodes: [],
|
|
|
|
failedNodesCount: 0,
|
|
|
|
totalNodesCount: 0,
|
2020-06-30 01:10:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!_.isArray(json.nodes)) {
|
|
|
|
throw new Error('Invalid nodes array type: ' + (typeof json.nodes));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const nodeData of json.nodes) {
|
2020-06-30 17:08:24 +02:00
|
|
|
result.totalNodesCount += 1;
|
2020-06-30 01:10:18 +02:00
|
|
|
try {
|
|
|
|
const parsedNode = parseNode(result.importTimestamp, nodeData);
|
|
|
|
Logger.tag('monitoring', 'parsing-nodes-json').debug(`Parsing node successful: ${parsedNode.mac}`);
|
|
|
|
result.nodes.push(parsedNode);
|
2022-06-23 14:26:15 +02:00
|
|
|
} catch (error) {
|
2020-06-30 17:08:24 +02:00
|
|
|
result.failedNodesCount += 1;
|
2020-06-30 01:10:18 +02:00
|
|
|
Logger.tag('monitoring', 'parsing-nodes-json').error("Could not parse node.", error, nodeData);
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
return result;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function updateSkippedNode(id: NodeId, node?: StoredNode): Promise<RunResult> {
|
2020-04-10 00:43:15 +02:00
|
|
|
return await db.run(
|
|
|
|
'UPDATE node_state ' +
|
|
|
|
'SET hostname = ?, monitoring_state = ?, modified_at = ?' +
|
|
|
|
'WHERE id = ?',
|
|
|
|
[
|
2022-07-21 18:39:33 +02:00
|
|
|
node ? node.hostname : '', node ? node.monitoringState : '', now(),
|
2020-04-10 00:43:15 +02:00
|
|
|
id
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendMonitoringMailsBatched(
|
|
|
|
name: string,
|
|
|
|
mailType: MailType,
|
|
|
|
findBatchFun: () => Promise<any[]>,
|
|
|
|
): Promise<void> {
|
|
|
|
Logger.tag('monitoring', 'mail-sending').debug('Sending "%s" mails...', name);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
Logger.tag('monitoring', 'mail-sending').debug('Sending next batch...');
|
|
|
|
|
|
|
|
const nodeStates = await findBatchFun();
|
|
|
|
if (_.isEmpty(nodeStates)) {
|
|
|
|
Logger.tag('monitoring', 'mail-sending').debug('Done sending "%s" mails.', name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const nodeState of nodeStates) {
|
|
|
|
const mac = nodeState.mac;
|
|
|
|
Logger.tag('monitoring', 'mail-sending').debug('Loading node data for: %s', mac);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const result = await NodeService.findNodeDataWithSecretsByMac(mac);
|
2020-04-10 00:43:15 +02:00
|
|
|
if (!result) {
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'mail-sending')
|
|
|
|
.debug(
|
|
|
|
'Node not found. Skipping sending of "' + name + '" mail: ' + mac
|
|
|
|
);
|
|
|
|
await updateSkippedNode(nodeState.id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {node, nodeSecrets} = result;
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
if (node.monitoringState !== MonitoringState.ACTIVE) {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'mail-sending')
|
|
|
|
.debug('Monitoring disabled, skipping "%s" mail for: %s', name, mac);
|
2022-07-21 18:39:33 +02:00
|
|
|
await updateSkippedNode(nodeState.id, node);
|
2020-04-10 00:43:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const monitoringToken = nodeSecrets.monitoringToken;
|
|
|
|
if (!monitoringToken) {
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'mail-sending')
|
|
|
|
.error('Node has no monitoring token. Cannot send mail "%s" for: %s', name, mac);
|
2022-07-21 18:39:33 +02:00
|
|
|
await updateSkippedNode(nodeState.id, node);
|
2020-04-10 00:43:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'mail-sending')
|
|
|
|
.info('Sending "%s" mail for: %s', name, mac);
|
|
|
|
|
|
|
|
await MailService.enqueue(
|
|
|
|
config.server.email.from,
|
|
|
|
node.nickname + ' <' + node.email + '>',
|
|
|
|
mailType,
|
|
|
|
{
|
|
|
|
node: node,
|
|
|
|
lastSeen: nodeState.last_seen,
|
2022-07-18 17:49:42 +02:00
|
|
|
disableUrl: monitoringDisableUrl(monitoringToken),
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'mail-sending')
|
|
|
|
.debug('Updating node state: ', mac);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const timestamp = now();
|
2020-04-10 00:43:15 +02:00
|
|
|
await db.run(
|
|
|
|
'UPDATE node_state ' +
|
|
|
|
'SET hostname = ?, monitoring_state = ?, modified_at = ?, last_status_mail_sent = ?, last_status_mail_type = ?' +
|
|
|
|
'WHERE id = ?',
|
|
|
|
[
|
2022-07-21 18:39:33 +02:00
|
|
|
node.hostname, node.monitoringState, timestamp, timestamp, mailType,
|
2020-04-10 00:43:15 +02:00
|
|
|
nodeState.id
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function sendOnlineAgainMails(startTime: UnixTimestampSeconds): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
await sendMonitoringMailsBatched(
|
|
|
|
'online again',
|
2022-07-18 17:49:42 +02:00
|
|
|
MailType.MONITORING_ONLINE_AGAIN,
|
2020-04-10 00:43:15 +02:00
|
|
|
async (): Promise<any[]> => await db.all(
|
|
|
|
'SELECT * FROM node_state ' +
|
|
|
|
'WHERE modified_at < ? AND state = ? AND last_status_mail_type IN (' +
|
|
|
|
'\'monitoring-offline-1\', \'monitoring-offline-2\', \'monitoring-offline-3\'' +
|
|
|
|
')' +
|
|
|
|
'ORDER BY id ASC LIMIT ?',
|
|
|
|
[
|
2022-07-21 18:39:33 +02:00
|
|
|
startTime,
|
2020-04-10 00:43:15 +02:00
|
|
|
'ONLINE',
|
|
|
|
|
|
|
|
MONITORING_MAILS_DB_BATCH_SIZE
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
async function sendOfflineMails(startTime: UnixTimestampSeconds, mailType: MailType): Promise<void> {
|
2022-07-18 17:49:42 +02:00
|
|
|
const mailNumber = parseInteger(mailType.split("-")[2]);
|
2020-04-10 00:43:15 +02:00
|
|
|
await sendMonitoringMailsBatched(
|
|
|
|
'offline ' + mailNumber,
|
2022-07-18 17:49:42 +02:00
|
|
|
mailType,
|
2020-04-10 00:43:15 +02:00
|
|
|
async (): Promise<any[]> => {
|
|
|
|
const previousType =
|
|
|
|
mailNumber === 1 ? 'monitoring-online-again' : ('monitoring-offline-' + (mailNumber - 1));
|
|
|
|
|
|
|
|
// the first time the first offline mail is send, there was no mail before
|
|
|
|
const allowNull = mailNumber === 1 ? ' OR last_status_mail_type IS NULL' : '';
|
|
|
|
|
|
|
|
const schedule = MONITORING_OFFLINE_MAILS_SCHEDULE[mailNumber];
|
2022-07-21 18:39:33 +02:00
|
|
|
const scheduledTimeBefore = subtract(now(), schedule);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
return await db.all(
|
|
|
|
'SELECT * FROM node_state ' +
|
|
|
|
'WHERE modified_at < ? AND state = ? AND (last_status_mail_type = ?' + allowNull + ') AND ' +
|
|
|
|
'last_seen <= ? AND (last_status_mail_sent <= ? OR last_status_mail_sent IS NULL) ' +
|
|
|
|
'ORDER BY id ASC LIMIT ?',
|
|
|
|
[
|
2022-07-21 18:39:33 +02:00
|
|
|
startTime,
|
2020-04-10 00:43:15 +02:00
|
|
|
'OFFLINE',
|
|
|
|
previousType,
|
2022-07-21 18:39:33 +02:00
|
|
|
scheduledTimeBefore,
|
|
|
|
scheduledTimeBefore,
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
MONITORING_MAILS_DB_BATCH_SIZE
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
function doRequest(url: string): Promise<{ response: request.Response, body: string }> {
|
|
|
|
return new Promise<{ response: request.Response, body: string }>((resolve, reject) => {
|
2020-04-10 00:43:15 +02:00
|
|
|
request(url, function (err, response, body) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve({response, body});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function withUrlsData(urls: string[]): Promise<NodesParsingResult[]> {
|
|
|
|
const results: NodesParsingResult[] = [];
|
|
|
|
|
|
|
|
for (const url of urls) {
|
|
|
|
Logger.tag('monitoring', 'information-retrieval').debug('Retrieving nodes.json: %s', url);
|
|
|
|
|
|
|
|
const {response, body} = await doRequest(url);
|
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
throw new Error(
|
|
|
|
'Could not download nodes.json from ' + url + ': ' +
|
|
|
|
response.statusCode + ' - ' + response.statusMessage
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push(await parseNodesJson(body));
|
|
|
|
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-06-30 17:08:24 +02:00
|
|
|
async function retrieveNodeInformationForUrls(urls: string[]): Promise<RetrieveNodeInformationResult> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const datas = await withUrlsData(urls);
|
|
|
|
|
|
|
|
let maxTimestamp = datas[0].importTimestamp;
|
|
|
|
let minTimestamp = maxTimestamp;
|
2020-06-30 17:08:24 +02:00
|
|
|
|
|
|
|
let failedParsingNodesCount = 0;
|
|
|
|
let totalNodesCount = 0;
|
|
|
|
|
2020-04-10 00:43:15 +02:00
|
|
|
for (const data of datas) {
|
2022-07-21 18:39:33 +02:00
|
|
|
if (data.importTimestamp >= maxTimestamp) {
|
2020-04-10 00:43:15 +02:00
|
|
|
maxTimestamp = data.importTimestamp;
|
|
|
|
}
|
2022-07-21 18:39:33 +02:00
|
|
|
if (data.importTimestamp <= minTimestamp) {
|
2020-04-10 00:43:15 +02:00
|
|
|
minTimestamp = data.importTimestamp;
|
|
|
|
}
|
2020-06-30 17:08:24 +02:00
|
|
|
|
|
|
|
failedParsingNodesCount += data.failedNodesCount;
|
|
|
|
totalNodesCount += data.totalNodesCount;
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
if (previousImportTimestamp !== null && maxTimestamp >= previousImportTimestamp) {
|
2020-04-10 00:43:15 +02:00
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'information-retrieval')
|
|
|
|
.debug(
|
|
|
|
'No new data, skipping. Current timestamp: %s, previous timestamp: %s',
|
2022-07-21 18:39:33 +02:00
|
|
|
formatTimestamp(maxTimestamp),
|
|
|
|
formatTimestamp(previousImportTimestamp)
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
2020-06-30 17:08:24 +02:00
|
|
|
return {
|
|
|
|
failedParsingNodesCount,
|
|
|
|
totalNodesCount,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
previousImportTimestamp = maxTimestamp;
|
|
|
|
|
|
|
|
// We do not parallelize here as the sqlite will start slowing down and blocking with too many
|
|
|
|
// parallel queries. This has resulted in blocking other requests too and thus in a major slowdown.
|
|
|
|
const allNodes = _.flatMap(datas, data => data.nodes);
|
|
|
|
|
|
|
|
// Get rid of duplicates from different nodes.json files. Always use the one with the newest
|
2022-07-21 18:39:33 +02:00
|
|
|
const sortedNodes = _.orderBy(allNodes, [node => node.lastSeen], ['desc']);
|
2020-04-10 00:43:15 +02:00
|
|
|
const uniqueNodes = _.uniqBy(sortedNodes, function (node) {
|
|
|
|
return node.mac;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const nodeData of uniqueNodes) {
|
|
|
|
Logger.tag('monitoring', 'information-retrieval').debug('Importing: %s', nodeData.mac);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const result = await NodeService.findNodeDataByMac(nodeData.mac);
|
2020-04-10 00:43:15 +02:00
|
|
|
if (!result) {
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'information-retrieval')
|
|
|
|
.debug('Unknown node, skipping: %s', nodeData.mac);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-25 22:40:41 +01:00
|
|
|
await storeNodeInformation(nodeData, result);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'information-retrieval')
|
|
|
|
.debug('Updating / deleting node data done: %s', nodeData.mac);
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'information-retrieval')
|
|
|
|
.debug('Marking missing nodes as offline.');
|
|
|
|
|
|
|
|
// Mark nodes as offline that haven't been imported in this run.
|
|
|
|
await db.run(
|
|
|
|
'UPDATE node_state ' +
|
|
|
|
'SET state = ?, modified_at = ?' +
|
|
|
|
'WHERE import_timestamp < ?',
|
|
|
|
[
|
2022-07-21 18:39:33 +02:00
|
|
|
OnlineState.OFFLINE, now(),
|
|
|
|
minTimestamp
|
2020-04-10 00:43:15 +02:00
|
|
|
]
|
|
|
|
);
|
2020-06-30 17:08:24 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
failedParsingNodesCount,
|
|
|
|
totalNodesCount,
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
// FIXME: Replace any[] by type.
|
2022-06-23 14:26:15 +02:00
|
|
|
export async function getAll(restParams: RestParams): Promise<{ total: number, monitoringStates: any[] }> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const filterFields = [
|
|
|
|
'hostname',
|
|
|
|
'mac',
|
|
|
|
'monitoring_state',
|
|
|
|
'state',
|
|
|
|
'last_status_mail_type'
|
|
|
|
];
|
|
|
|
|
|
|
|
const where = Resources.whereCondition(restParams, filterFields);
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
const row = await db.get<{ total: number }>(
|
2020-04-10 00:43:15 +02:00
|
|
|
'SELECT count(*) AS total FROM node_state WHERE ' + where.query,
|
2022-07-28 13:16:13 +02:00
|
|
|
where.params,
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
const total = row?.total || 0;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const filter = Resources.filterClause(
|
|
|
|
restParams,
|
2022-06-23 14:26:15 +02:00
|
|
|
MonitoringSortField.ID,
|
|
|
|
isMonitoringSortField,
|
2020-04-10 00:43:15 +02:00
|
|
|
filterFields
|
|
|
|
);
|
|
|
|
|
|
|
|
const monitoringStates = await db.all(
|
|
|
|
'SELECT * FROM node_state WHERE ' + filter.query,
|
2022-07-28 13:16:13 +02:00
|
|
|
filter.params,
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return {monitoringStates, total};
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
export async function getByMacs(macs: MAC[]): Promise<Record<MAC, NodeStateData>> {
|
2020-04-10 00:43:15 +02:00
|
|
|
if (_.isEmpty(macs)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
const nodeStateByMac: { [key: string]: NodeStateData } = {};
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
for (const subMacs of _.chunk(macs, MONITORING_STATE_MACS_CHUNK_SIZE)) {
|
|
|
|
const inCondition = DatabaseUtil.inCondition('mac', subMacs);
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
const rows = await db.all<NodeStateRow>(
|
2020-04-10 00:43:15 +02:00
|
|
|
'SELECT * FROM node_state WHERE ' + inCondition.query,
|
2022-07-28 13:16:13 +02:00
|
|
|
inCondition.params,
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
for (const row of rows) {
|
2022-07-18 17:49:42 +02:00
|
|
|
const onlineState = row.state;
|
|
|
|
if (!isOnlineState(onlineState)) {
|
|
|
|
throw new Error(`Invalid online state in database: "${onlineState}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeStateByMac[row.mac] = {
|
2022-07-28 12:24:07 +02:00
|
|
|
site: row.site || undefined,
|
|
|
|
domain: row.domain || undefined,
|
2022-07-18 17:49:42 +02:00
|
|
|
state: onlineState,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeStateByMac;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export async function confirm(token: MonitoringToken): Promise<StoredNode> {
|
2021-01-25 22:40:41 +01:00
|
|
|
const {node, nodeSecrets} = await NodeService.getNodeDataWithSecretsByMonitoringToken(token);
|
2022-07-21 18:39:33 +02:00
|
|
|
if (node.monitoringState === MonitoringState.DISABLED || !nodeSecrets.monitoringToken || nodeSecrets.monitoringToken !== token) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw {data: 'Invalid token.', type: ErrorTypes.badRequest};
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
if (node.monitoringState === MonitoringState.ACTIVE) {
|
2020-04-10 00:43:15 +02:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
node.monitoringState = MonitoringState.ACTIVE;
|
|
|
|
return await NodeService.internalUpdateNode(
|
|
|
|
node.token,
|
|
|
|
toCreateOrUpdateNode(node),
|
|
|
|
node.monitoringState,
|
|
|
|
nodeSecrets
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
export async function disable(token: MonitoringToken): Promise<StoredNode> {
|
2021-01-25 22:40:41 +01:00
|
|
|
const {node, nodeSecrets} = await NodeService.getNodeDataWithSecretsByMonitoringToken(token);
|
2022-07-21 18:39:33 +02:00
|
|
|
if (node.monitoringState === MonitoringState.DISABLED || !nodeSecrets.monitoringToken || nodeSecrets.monitoringToken !== token) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw {data: 'Invalid token.', type: ErrorTypes.badRequest};
|
|
|
|
}
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
node.monitoringState = MonitoringState.DISABLED;
|
2022-07-14 20:06:05 +02:00
|
|
|
nodeSecrets.monitoringToken = undefined;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
return await NodeService.internalUpdateNode(
|
|
|
|
node.token,
|
|
|
|
toCreateOrUpdateNode(node),
|
|
|
|
node.monitoringState,
|
|
|
|
nodeSecrets
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2020-06-30 17:08:24 +02:00
|
|
|
export async function retrieveNodeInformation(): Promise<RetrieveNodeInformationResult> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const urls = config.server.map.nodesJsonUrl;
|
|
|
|
if (_.isEmpty(urls)) {
|
|
|
|
throw new Error('No nodes.json-URLs set. Please adjust config.json: server.map.nodesJsonUrl')
|
|
|
|
}
|
|
|
|
|
|
|
|
return await retrieveNodeInformationForUrls(urls);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendMonitoringMails(): Promise<void> {
|
|
|
|
Logger.tag('monitoring', 'mail-sending').debug('Sending monitoring mails...');
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const startTime = now();
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
await sendOnlineAgainMails(startTime);
|
2022-06-23 14:26:15 +02:00
|
|
|
} catch (error) {
|
2020-04-10 00:43:15 +02:00
|
|
|
// only logging an continuing with next type
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'mail-sending')
|
|
|
|
.error('Error sending "online again" mails.', error);
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
for (const mailType of [
|
|
|
|
MailType.MONITORING_OFFLINE_1,
|
|
|
|
MailType.MONITORING_OFFLINE_2,
|
|
|
|
MailType.MONITORING_OFFLINE_3,
|
|
|
|
]) {
|
2020-04-10 00:43:15 +02:00
|
|
|
try {
|
2022-07-18 17:49:42 +02:00
|
|
|
await sendOfflineMails(startTime, mailType);
|
2022-06-23 14:26:15 +02:00
|
|
|
} catch (error) {
|
2020-04-10 00:43:15 +02:00
|
|
|
// only logging an continuing with next type
|
|
|
|
Logger
|
|
|
|
.tag('monitoring', 'mail-sending')
|
2022-07-18 17:49:42 +02:00
|
|
|
.error('Error sending "' + mailType + '" mails.', error);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteOfflineNodes(): Promise<void> {
|
|
|
|
Logger
|
|
|
|
.tag('nodes', 'delete-offline')
|
|
|
|
.info(
|
2022-07-21 18:39:33 +02:00
|
|
|
`Deleting offline nodes older than ${DELETE_OFFLINE_NODES_AFTER_DURATION} seconds.`
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
2021-07-26 21:40:17 +02:00
|
|
|
const deleteBefore =
|
2022-07-21 18:39:33 +02:00
|
|
|
subtract(
|
|
|
|
now(),
|
|
|
|
DELETE_OFFLINE_NODES_AFTER_DURATION,
|
|
|
|
);
|
2021-07-26 21:40:17 +02:00
|
|
|
|
|
|
|
await deleteNeverOnlineNodesBefore(deleteBefore);
|
|
|
|
await deleteNodesOfflineSinceBefore(deleteBefore);
|
|
|
|
}
|
|
|
|
|
2021-08-09 21:54:13 +02:00
|
|
|
async function deleteNeverOnlineNodesBefore(deleteBefore: UnixTimestampSeconds): Promise<void> {
|
2021-08-09 21:36:41 +02:00
|
|
|
Logger
|
|
|
|
.tag('nodes', 'delete-never-online')
|
|
|
|
.info(
|
2022-07-18 12:55:19 +02:00
|
|
|
'Deleting nodes that were never online created before ' +
|
2021-08-09 21:36:41 +02:00
|
|
|
deleteBefore
|
|
|
|
);
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const deletionCandidates: StoredNode[] = await NodeService.findNodesModifiedBefore(deleteBefore);
|
2021-08-09 21:36:41 +02:00
|
|
|
|
|
|
|
Logger
|
|
|
|
.tag('nodes', 'delete-never-online')
|
|
|
|
.info(
|
|
|
|
'Number of nodes created before ' +
|
|
|
|
deleteBefore +
|
|
|
|
': ' +
|
|
|
|
deletionCandidates.length
|
|
|
|
);
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
const deletionCandidateMacs: MAC[] = deletionCandidates.map(node => node.mac);
|
2021-07-26 21:40:17 +02:00
|
|
|
const chunks: MAC[][] = _.chunk(deletionCandidateMacs, NEVER_ONLINE_NODES_DELETION_CHUNK_SIZE);
|
|
|
|
|
2021-08-09 21:36:41 +02:00
|
|
|
Logger
|
|
|
|
.tag('nodes', 'delete-never-online')
|
|
|
|
.info(
|
|
|
|
'Number of chunks to check for deletion: ' +
|
|
|
|
chunks.length
|
|
|
|
);
|
|
|
|
|
2021-07-26 21:40:17 +02:00
|
|
|
for (const macs of chunks) {
|
2021-08-09 21:36:41 +02:00
|
|
|
Logger
|
|
|
|
.tag('nodes', 'delete-never-online')
|
|
|
|
.info(
|
|
|
|
'Checking chunk of ' +
|
|
|
|
macs.length +
|
|
|
|
' MACs for deletion.'
|
|
|
|
);
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
const placeholders = macs.map(() => '?').join(',');
|
2021-07-26 21:40:17 +02:00
|
|
|
|
2022-06-23 14:26:15 +02:00
|
|
|
const rows: { mac: MAC }[] = await db.all(
|
2021-07-26 21:40:17 +02:00
|
|
|
`SELECT * FROM node_state WHERE mac IN (${placeholders})`,
|
|
|
|
macs
|
|
|
|
);
|
|
|
|
|
2021-08-09 21:36:41 +02:00
|
|
|
Logger
|
|
|
|
.tag('nodes', 'delete-never-online')
|
|
|
|
.info(
|
|
|
|
'Of the chunk of ' +
|
|
|
|
macs.length +
|
|
|
|
' MACs there were ' +
|
|
|
|
rows.length +
|
|
|
|
' nodes found in monitoring database. Those should be skipped.'
|
|
|
|
);
|
|
|
|
|
2022-07-28 13:16:13 +02:00
|
|
|
const seenMacs: MAC[] = rows.map(row => row.mac);
|
2021-07-26 21:40:17 +02:00
|
|
|
const neverSeenMacs = _.difference(macs, seenMacs);
|
|
|
|
|
2021-08-09 21:36:41 +02:00
|
|
|
Logger
|
|
|
|
.tag('nodes', 'delete-never-online')
|
|
|
|
.info(
|
|
|
|
'Of the chunk of ' +
|
|
|
|
macs.length +
|
|
|
|
' MACs there are ' +
|
2021-08-09 22:03:27 +02:00
|
|
|
neverSeenMacs.length +
|
2021-08-09 21:36:41 +02:00
|
|
|
' nodes that were never online. Those will be deleted.'
|
|
|
|
);
|
|
|
|
|
2021-07-26 21:40:17 +02:00
|
|
|
for (const neverSeenMac of neverSeenMacs) {
|
|
|
|
await deleteNodeByMac(neverSeenMac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 21:54:13 +02:00
|
|
|
async function deleteNodesOfflineSinceBefore(deleteBefore: UnixTimestampSeconds): Promise<void> {
|
2022-07-18 17:49:42 +02:00
|
|
|
const rows = await db.all<NodeStateRow>(
|
2020-04-10 00:43:15 +02:00
|
|
|
'SELECT * FROM node_state WHERE state = ? AND last_seen < ?',
|
|
|
|
[
|
|
|
|
'OFFLINE',
|
2021-07-26 21:40:17 +02:00
|
|
|
deleteBefore
|
2020-04-10 00:43:15 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const row of rows) {
|
2021-07-26 21:40:17 +02:00
|
|
|
await deleteNodeByMac(row.mac);
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2021-07-26 21:40:17 +02:00
|
|
|
async function deleteNodeByMac(mac: MAC): Promise<void> {
|
2021-08-09 21:36:41 +02:00
|
|
|
Logger.tag('nodes', 'delete-offline').debug('Deleting node ' + mac);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2021-07-26 21:40:17 +02:00
|
|
|
let node;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2021-07-26 21:40:17 +02:00
|
|
|
try {
|
2022-07-21 18:39:33 +02:00
|
|
|
node = await NodeService.findNodeDataByMac(mac);
|
2022-06-23 14:26:15 +02:00
|
|
|
} catch (error) {
|
2021-07-26 21:40:17 +02:00
|
|
|
// Only log error. We try to delete the nodes state anyways.
|
|
|
|
Logger.tag('nodes', 'delete-offline').error('Could not find node to delete: ' + mac, error);
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2021-07-26 21:40:17 +02:00
|
|
|
if (node && node.token) {
|
|
|
|
await NodeService.deleteNode(node.token);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await db.run(
|
|
|
|
'DELETE FROM node_state WHERE mac = ? AND state = ?',
|
|
|
|
[mac, 'OFFLINE'],
|
|
|
|
);
|
2022-06-23 14:26:15 +02:00
|
|
|
} catch (error) {
|
2021-07-26 21:40:17 +02:00
|
|
|
// Only log error and continue with next node.
|
|
|
|
Logger.tag('nodes', 'delete-offline').error('Could not delete node state: ' + mac, error);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-26 21:40:17 +02:00
|
|
|
|