2020-04-10 00:43:15 +02:00
|
|
|
import _ from "lodash";
|
|
|
|
import request from "request";
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
import { config } from "../config";
|
|
|
|
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";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { RestParams } from "../utils/resources";
|
|
|
|
import { normalizeMac, parseInteger } from "../shared/utils/strings";
|
|
|
|
import { monitoringDisableUrl } from "../utils/urlBuilder";
|
2022-08-04 15:31:01 +02:00
|
|
|
import CONSTRAINTS from "../shared/validation/constraints";
|
2022-08-23 20:08:53 +02:00
|
|
|
import { forConstraint } from "../shared/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-08-23 21:01:58 +02:00
|
|
|
filterUndefinedFromJSON,
|
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-08-24 18:20:49 +02:00
|
|
|
isMailType,
|
2022-06-23 14:26:15 +02:00
|
|
|
isMonitoringSortField,
|
2022-08-24 18:20:49 +02:00
|
|
|
isMonitoringState,
|
2022-07-18 17:49:42 +02:00
|
|
|
isOnlineState,
|
2022-08-23 21:38:37 +02:00
|
|
|
isPlainObject,
|
2022-07-28 12:24:07 +02:00
|
|
|
isSite,
|
2022-07-28 13:16:13 +02:00
|
|
|
isString,
|
|
|
|
isUndefined,
|
2022-08-23 21:38:37 +02:00
|
|
|
JSONValue,
|
2022-06-23 14:26:15 +02:00
|
|
|
MAC,
|
2022-08-24 18:20:49 +02:00
|
|
|
MailId,
|
2022-06-23 14:26:15 +02:00
|
|
|
MailType,
|
2022-08-24 18:20:49 +02:00
|
|
|
MapId,
|
|
|
|
mapIdFromMAC,
|
2022-06-23 14:26:15 +02:00
|
|
|
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,
|
2022-08-24 18:20:49 +02:00
|
|
|
NodeMonitoringStateResponse,
|
2022-06-23 14:26:15 +02:00
|
|
|
NodeStateData,
|
2022-08-24 18:20:49 +02:00
|
|
|
NodeStateId,
|
2022-06-23 14:26:15 +02:00
|
|
|
OnlineState,
|
2022-08-23 21:38:37 +02:00
|
|
|
parseJSON,
|
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-08-23 20:08:53 +02:00
|
|
|
UnixTimestampSeconds,
|
2022-06-23 14:26:15 +02:00
|
|
|
} from "../types";
|
2022-08-23 20:08:53 +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 = {
|
2022-08-24 18:20:49 +02:00
|
|
|
id: NodeStateId;
|
2022-08-23 20:08:53 +02:00
|
|
|
created_at: UnixTimestampSeconds;
|
|
|
|
domain: Domain | null;
|
|
|
|
hostname: Hostname | null;
|
|
|
|
import_timestamp: UnixTimestampSeconds;
|
|
|
|
last_seen: UnixTimestampSeconds;
|
2022-08-24 18:20:49 +02:00
|
|
|
last_status_mail_sent: UnixTimestampSeconds | null;
|
2022-08-23 20:08:53 +02:00
|
|
|
last_status_mail_type: string | null;
|
|
|
|
mac: MAC;
|
|
|
|
modified_at: UnixTimestampSeconds;
|
|
|
|
monitoring_state: string | null;
|
|
|
|
site: Site | null;
|
|
|
|
state: string;
|
2022-07-18 17:49:42 +02:00
|
|
|
};
|
|
|
|
|
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-08-23 20:08:53 +02:00
|
|
|
mac: MAC;
|
|
|
|
importTimestamp: UnixTimestampSeconds;
|
|
|
|
state: OnlineState;
|
|
|
|
lastSeen: UnixTimestampSeconds;
|
|
|
|
site?: Site;
|
|
|
|
domain?: Domain;
|
2020-04-10 00:43:15 +02:00
|
|
|
};
|
|
|
|
|
2020-06-30 01:10:18 +02:00
|
|
|
export type NodesParsingResult = {
|
2022-08-23 20:08:53 +02:00
|
|
|
importTimestamp: UnixTimestampSeconds;
|
|
|
|
nodes: ParsedNode[];
|
|
|
|
failedNodesCount: number;
|
|
|
|
totalNodesCount: number;
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2020-06-30 17:08:24 +02:00
|
|
|
export type RetrieveNodeInformationResult = {
|
2022-08-23 20:08:53 +02:00
|
|
|
failedParsingNodesCount: number;
|
|
|
|
totalNodesCount: number;
|
2020-06-30 17:08:24 +02:00
|
|
|
};
|
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
let previousImportTimestamp: UnixTimestampSeconds | null = null;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function insertNodeInformation(
|
|
|
|
nodeData: ParsedNode,
|
|
|
|
node: StoredNode
|
|
|
|
): Promise<void> {
|
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"Node is new in monitoring, creating data: %s",
|
|
|
|
nodeData.mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
await db.run(
|
2022-08-23 20:08:53 +02:00
|
|
|
"INSERT INTO node_state " +
|
|
|
|
"(hostname, mac, site, domain, monitoring_state, state, last_seen, import_timestamp, last_status_mail_sent, last_status_mail_type) " +
|
|
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
2020-04-10 00:43:15 +02:00
|
|
|
[
|
|
|
|
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
|
2022-08-23 20:08:53 +02:00
|
|
|
null, // new node so we haven't send a mail yet
|
2020-04-10 00:43:15 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function updateNodeInformation(
|
|
|
|
nodeData: ParsedNode,
|
|
|
|
node: StoredNode,
|
|
|
|
row: any
|
|
|
|
): Promise<void> {
|
|
|
|
Logger.tag("monitoring", "informacallbacktion-retrieval").debug(
|
|
|
|
"Node is known in monitoring: %s",
|
|
|
|
nodeData.mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
if (row.import_timestamp >= nodeData.importTimestamp) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"No new data for node, skipping: %s",
|
|
|
|
nodeData.mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"New data for node, updating: %s",
|
|
|
|
nodeData.mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
await db.run(
|
2022-08-23 20:08:53 +02:00
|
|
|
"UPDATE node_state " +
|
|
|
|
"SET " +
|
|
|
|
"hostname = ?, " +
|
|
|
|
"site = ?, " +
|
|
|
|
"domain = ?, " +
|
|
|
|
"monitoring_state = ?, " +
|
|
|
|
"state = ?, " +
|
|
|
|
"last_seen = ?, " +
|
|
|
|
"import_timestamp = ?, " +
|
|
|
|
"modified_at = ? " +
|
|
|
|
"WHERE id = ? AND mac = ?",
|
2020-04-10 00:43:15 +02:00
|
|
|
[
|
|
|
|
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,
|
2022-08-23 20:08:53 +02:00
|
|
|
node.mac,
|
2020-04-10 00:43:15 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function storeNodeInformation(
|
|
|
|
nodeData: ParsedNode,
|
|
|
|
node: StoredNode
|
|
|
|
): Promise<void> {
|
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"Storing status for node: %s",
|
|
|
|
nodeData.mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const row = await db.get("SELECT * FROM node_state WHERE mac = ?", [
|
|
|
|
node.mac,
|
|
|
|
]);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
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-08-23 20:08:53 +02:00
|
|
|
export function parseNode(
|
|
|
|
importTimestamp: UnixTimestampSeconds,
|
2022-08-23 21:38:37 +02:00
|
|
|
nodeData: JSONValue
|
2022-08-23 20:08:53 +02:00
|
|
|
): ParsedNode {
|
2022-08-23 21:38:37 +02:00
|
|
|
if (!isPlainObject(nodeData)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw new Error("Unexpected node type: " + typeof nodeData);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
if (!isPlainObject(nodeData.nodeinfo)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw new Error(
|
2022-08-23 20:08:53 +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}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
if (!isPlainObject(nodeData.nodeinfo.network)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw new Error(
|
2022-08-23 20:08:53 +02:00
|
|
|
"Node " +
|
|
|
|
nodeId +
|
|
|
|
": Unexpected nodeinfo.network type: " +
|
|
|
|
typeof nodeData.nodeinfo.network
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidMac(nodeData.nodeinfo.network.mac)) {
|
|
|
|
throw new Error(
|
2022-08-23 20:08:53 +02:00
|
|
|
"Node " + nodeId + ": Invalid MAC: " + nodeData.nodeinfo.network.mac
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
2022-08-23 21:38:37 +02:00
|
|
|
const mac = normalizeMac(nodeData.nodeinfo.network.mac as MAC);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
if (!isPlainObject(nodeData.flags)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw new Error(
|
2022-08-23 20:08:53 +02:00
|
|
|
"Node " +
|
|
|
|
nodeId +
|
|
|
|
": Unexpected flags type: " +
|
|
|
|
typeof nodeData.flags
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
2022-07-28 13:16:13 +02:00
|
|
|
if (!isBoolean(nodeData.flags.online)) {
|
2020-04-10 00:43:15 +02:00
|
|
|
throw new Error(
|
2022-08-23 20:08:53 +02:00
|
|
|
"Node " +
|
|
|
|
nodeId +
|
|
|
|
": Unexpected flags.online type: " +
|
|
|
|
typeof nodeData.flags.online
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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(
|
2022-08-23 20:08:53 +02:00
|
|
|
"Node " +
|
|
|
|
nodeId +
|
|
|
|
": Invalid lastseen timestamp: " +
|
|
|
|
nodeData.lastseen
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-28 12:24:07 +02:00
|
|
|
let site: Site | undefined;
|
2022-08-23 20:08:53 +02:00
|
|
|
if (
|
2022-08-23 21:38:37 +02:00
|
|
|
isPlainObject(nodeData.nodeinfo.system) &&
|
2022-08-23 20:08:53 +02:00
|
|
|
isSite(nodeData.nodeinfo.system.site_code)
|
|
|
|
) {
|
2022-07-28 12:24:07 +02:00
|
|
|
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;
|
2022-08-23 20:08:53 +02:00
|
|
|
if (
|
2022-08-23 21:38:37 +02:00
|
|
|
isPlainObject(nodeData.nodeinfo.system) &&
|
2022-08-23 20:08:53 +02:00
|
|
|
isDomain(nodeData.nodeinfo.system.domain_code)
|
|
|
|
) {
|
2022-07-28 12:24:07 +02:00
|
|
|
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 {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"Parsing nodes.json..."
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
const json = parseJSON(body);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 21:38:37 +02:00
|
|
|
if (!isPlainObject(json)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw new Error(
|
|
|
|
`Expecting a JSON object as the nodes.json root, but got: ${typeof json}`
|
|
|
|
);
|
2020-06-30 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const expectedVersion = 2;
|
|
|
|
if (json.version !== expectedVersion) {
|
2022-08-23 20:08:53 +02:00
|
|
|
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) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw new Error("Invalid timestamp: " + json.timestamp);
|
2022-07-21 18:39:33 +02:00
|
|
|
}
|
|
|
|
|
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)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw new Error("Invalid nodes array type: " + typeof json.nodes);
|
2020-06-30 01:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "parsing-nodes-json").debug(
|
|
|
|
`Parsing node successful: ${parsedNode.mac}`
|
|
|
|
);
|
2020-06-30 01:10:18 +02:00
|
|
|
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;
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "parsing-nodes-json").error(
|
|
|
|
"Could not parse node.",
|
|
|
|
error,
|
|
|
|
nodeData
|
|
|
|
);
|
2020-06-30 01:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
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-08-23 20:08:53 +02:00
|
|
|
async function updateSkippedNode(
|
2022-08-24 18:20:49 +02:00
|
|
|
id: NodeStateId,
|
2022-08-23 20:08:53 +02:00
|
|
|
node?: StoredNode
|
|
|
|
): Promise<RunResult> {
|
2020-04-10 00:43:15 +02:00
|
|
|
return await db.run(
|
2022-08-23 20:08:53 +02:00
|
|
|
"UPDATE node_state " +
|
|
|
|
"SET hostname = ?, monitoring_state = ?, modified_at = ?" +
|
|
|
|
"WHERE id = ?",
|
|
|
|
[node ? node.hostname : "", node ? node.monitoringState : "", now(), id]
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendMonitoringMailsBatched(
|
|
|
|
name: string,
|
|
|
|
mailType: MailType,
|
2022-08-24 18:20:49 +02:00
|
|
|
findBatchFun: () => Promise<NodeStateRow[]>
|
2020-04-10 00:43:15 +02:00
|
|
|
): Promise<void> {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").debug(
|
|
|
|
'Sending "%s" mails...',
|
|
|
|
name
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
while (true) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").debug("Sending next batch...");
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
const nodeStates = await findBatchFun();
|
|
|
|
if (_.isEmpty(nodeStates)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").debug(
|
|
|
|
'Done sending "%s" mails.',
|
|
|
|
name
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const nodeState of nodeStates) {
|
|
|
|
const mac = nodeState.mac;
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").debug(
|
|
|
|
"Loading node data for: %s",
|
|
|
|
mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const result = await NodeService.findNodeDataWithSecretsByMac(mac);
|
2020-04-10 00:43:15 +02:00
|
|
|
if (!result) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").debug(
|
|
|
|
'Node not found. Skipping sending of "' +
|
|
|
|
name +
|
|
|
|
'" mail: ' +
|
|
|
|
mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
await updateSkippedNode(nodeState.id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const { node, nodeSecrets } = result;
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
if (node.monitoringState !== MonitoringState.ACTIVE) {
|
2022-08-23 20:08:53 +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) {
|
2022-08-23 20:08:53 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").info(
|
|
|
|
'Sending "%s" mail for: %s',
|
|
|
|
name,
|
|
|
|
mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
await MailService.enqueue(
|
|
|
|
config.server.email.from,
|
2022-08-23 20:08:53 +02:00
|
|
|
node.nickname + " <" + node.email + ">",
|
2020-04-10 00:43:15 +02:00
|
|
|
mailType,
|
|
|
|
{
|
2022-08-23 21:01:58 +02:00
|
|
|
node: filterUndefinedFromJSON(node),
|
2020-04-10 00:43:15 +02:00
|
|
|
lastSeen: nodeState.last_seen,
|
2022-07-18 17:49:42 +02:00
|
|
|
disableUrl: monitoringDisableUrl(monitoringToken),
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").debug(
|
|
|
|
"Updating node state: ",
|
|
|
|
mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-21 18:39:33 +02:00
|
|
|
const timestamp = now();
|
2020-04-10 00:43:15 +02:00
|
|
|
await db.run(
|
2022-08-23 20:08:53 +02:00
|
|
|
"UPDATE node_state " +
|
|
|
|
"SET hostname = ?, monitoring_state = ?, modified_at = ?, last_status_mail_sent = ?, last_status_mail_type = ?" +
|
|
|
|
"WHERE id = ?",
|
2020-04-10 00:43:15 +02:00
|
|
|
[
|
2022-08-23 20:08:53 +02:00
|
|
|
node.hostname,
|
|
|
|
node.monitoringState,
|
|
|
|
timestamp,
|
|
|
|
timestamp,
|
|
|
|
mailType,
|
|
|
|
nodeState.id,
|
2020-04-10 00:43:15 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function sendOnlineAgainMails(
|
|
|
|
startTime: UnixTimestampSeconds
|
|
|
|
): Promise<void> {
|
2020-04-10 00:43:15 +02:00
|
|
|
await sendMonitoringMailsBatched(
|
2022-08-23 20:08:53 +02:00
|
|
|
"online again",
|
2022-07-18 17:49:42 +02:00
|
|
|
MailType.MONITORING_ONLINE_AGAIN,
|
2022-08-24 18:20:49 +02:00
|
|
|
async (): Promise<NodeStateRow[]> =>
|
|
|
|
await db.all<NodeStateRow>(
|
2022-08-23 20:08:53 +02:00
|
|
|
"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 ?",
|
|
|
|
[startTime, "ONLINE", MONITORING_MAILS_DB_BATCH_SIZE]
|
|
|
|
)
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +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(
|
2022-08-23 20:08:53 +02:00
|
|
|
"offline " + mailNumber,
|
2022-07-18 17:49:42 +02:00
|
|
|
mailType,
|
2022-08-24 18:20:49 +02:00
|
|
|
async (): Promise<NodeStateRow[]> => {
|
2020-04-10 00:43:15 +02:00
|
|
|
const previousType =
|
2022-08-23 20:08:53 +02:00
|
|
|
mailNumber === 1
|
|
|
|
? "monitoring-online-again"
|
|
|
|
: "monitoring-offline-" + (mailNumber - 1);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
// the first time the first offline mail is send, there was no mail before
|
2022-08-23 20:08:53 +02:00
|
|
|
const allowNull =
|
|
|
|
mailNumber === 1 ? " OR last_status_mail_type IS NULL" : "";
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-08-24 18:20:49 +02:00
|
|
|
return await db.all<NodeStateRow>(
|
2022-08-23 20:08:53 +02:00
|
|
|
"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 ?",
|
2020-04-10 00:43:15 +02:00
|
|
|
[
|
2022-07-21 18:39:33 +02:00
|
|
|
startTime,
|
2022-08-23 20:08:53 +02:00
|
|
|
"OFFLINE",
|
2020-04-10 00:43:15 +02:00
|
|
|
previousType,
|
2022-07-21 18:39:33 +02:00
|
|
|
scheduledTimeBefore,
|
|
|
|
scheduledTimeBefore,
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
MONITORING_MAILS_DB_BATCH_SIZE,
|
|
|
|
]
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
2022-08-23 20:08:53 +02:00
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
function doRequest(
|
|
|
|
url: string
|
|
|
|
): Promise<{ response: request.Response; body: string }> {
|
|
|
|
return new Promise<{ response: request.Response; body: string }>(
|
|
|
|
(resolve, reject) => {
|
|
|
|
request(url, function (err, response, body) {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
resolve({ response, body });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function withUrlsData(urls: string[]): Promise<NodesParsingResult[]> {
|
|
|
|
const results: NodesParsingResult[] = [];
|
|
|
|
|
|
|
|
for (const url of urls) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"Retrieving nodes.json: %s",
|
|
|
|
url
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const { response, body } = await doRequest(url);
|
2020-04-10 00:43:15 +02:00
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
throw new Error(
|
2022-08-23 20:08:53 +02:00
|
|
|
"Could not download nodes.json from " +
|
|
|
|
url +
|
|
|
|
": " +
|
|
|
|
response.statusCode +
|
|
|
|
" - " +
|
|
|
|
response.statusMessage
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push(await parseNodesJson(body));
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +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-08-23 20:08:53 +02:00
|
|
|
if (
|
|
|
|
previousImportTimestamp !== null &&
|
|
|
|
maxTimestamp >= previousImportTimestamp
|
|
|
|
) {
|
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"No new data, skipping. Current timestamp: %s, previous timestamp: %s",
|
|
|
|
formatTimestamp(maxTimestamp),
|
|
|
|
formatTimestamp(previousImportTimestamp)
|
|
|
|
);
|
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.
|
2022-08-23 20:08:53 +02:00
|
|
|
const allNodes = _.flatMap(datas, (data) => data.nodes);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
// Get rid of duplicates from different nodes.json files. Always use the one with the newest
|
2022-08-23 20:08:53 +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) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"Importing: %s",
|
|
|
|
nodeData.mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
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) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"Unknown node, skipping: %s",
|
|
|
|
nodeData.mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-25 22:40:41 +01:00
|
|
|
await storeNodeInformation(nodeData, result);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"Updating / deleting node data done: %s",
|
|
|
|
nodeData.mac
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "information-retrieval").debug(
|
|
|
|
"Marking missing nodes as offline."
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
|
|
|
// Mark nodes as offline that haven't been imported in this run.
|
|
|
|
await db.run(
|
2022-08-23 20:08:53 +02:00
|
|
|
"UPDATE node_state " +
|
|
|
|
"SET state = ?, modified_at = ?" +
|
|
|
|
"WHERE import_timestamp < ?",
|
|
|
|
[OnlineState.OFFLINE, now(), minTimestamp]
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
2020-06-30 17:08:24 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
failedParsingNodesCount,
|
|
|
|
totalNodesCount,
|
2022-08-23 20:08:53 +02:00
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-24 18:20:49 +02:00
|
|
|
function toResponse(row: NodeStateRow): NodeMonitoringStateResponse {
|
|
|
|
// TODO: Handle conversion errors.
|
|
|
|
return {
|
|
|
|
id: row.id,
|
|
|
|
created_at: row.created_at,
|
|
|
|
domain: row.domain || undefined,
|
|
|
|
hostname: row.hostname || undefined,
|
|
|
|
import_timestamp: row.import_timestamp,
|
|
|
|
last_seen: row.last_seen,
|
|
|
|
last_status_mail_sent: row.last_status_mail_sent || undefined,
|
|
|
|
last_status_mail_type: isMailType(row.last_status_mail_type)
|
|
|
|
? row.last_status_mail_type
|
|
|
|
: undefined,
|
|
|
|
mac: row.mac,
|
|
|
|
modified_at: row.modified_at,
|
|
|
|
monitoring_state: isMonitoringState(row.monitoring_state)
|
|
|
|
? row.monitoring_state
|
|
|
|
: undefined,
|
|
|
|
site: row.site || undefined,
|
|
|
|
state: isOnlineState(row.state) ? row.state : OnlineState.OFFLINE,
|
|
|
|
mapId: mapIdFromMAC(row.mac),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
export async function getAll(
|
|
|
|
restParams: RestParams
|
2022-08-24 18:20:49 +02:00
|
|
|
): Promise<{ total: number; monitoringStates: NodeMonitoringStateResponse[] }> {
|
2020-04-10 00:43:15 +02:00
|
|
|
const filterFields = [
|
2022-08-23 20:08:53 +02:00
|
|
|
"hostname",
|
|
|
|
"mac",
|
|
|
|
"monitoring_state",
|
|
|
|
"state",
|
|
|
|
"last_status_mail_type",
|
2020-04-10 00:43:15 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
const where = Resources.whereCondition(restParams, filterFields);
|
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
const row = await db.get<{ total: number }>(
|
2022-08-23 20:08:53 +02:00
|
|
|
"SELECT count(*) AS total FROM node_state WHERE " + where.query,
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2022-08-24 18:20:49 +02:00
|
|
|
const monitoringStates = await db.all<NodeStateRow>(
|
2022-08-23 20:08:53 +02:00
|
|
|
"SELECT * FROM node_state WHERE " + filter.query,
|
|
|
|
filter.params
|
2020-04-10 00:43:15 +02:00
|
|
|
);
|
|
|
|
|
2022-08-24 18:20:49 +02:00
|
|
|
return {
|
|
|
|
monitoringStates: monitoringStates.map(toResponse),
|
|
|
|
total,
|
|
|
|
};
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +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)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
const inCondition = DatabaseUtil.inCondition("mac", subMacs);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-07-18 17:49:42 +02:00
|
|
|
const rows = await db.all<NodeStateRow>(
|
2022-08-23 20:08:53 +02:00
|
|
|
"SELECT * FROM node_state WHERE " + inCondition.query,
|
|
|
|
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)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw new Error(
|
|
|
|
`Invalid online state in database: "${onlineState}"`
|
|
|
|
);
|
2022-07-18 17:49:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const { node, nodeSecrets } =
|
|
|
|
await NodeService.getNodeDataWithSecretsByMonitoringToken(token);
|
|
|
|
if (
|
|
|
|
node.monitoringState === MonitoringState.DISABLED ||
|
|
|
|
!nodeSecrets.monitoringToken ||
|
|
|
|
nodeSecrets.monitoringToken !== token
|
|
|
|
) {
|
|
|
|
throw { data: "Invalid token.", type: ErrorTypes.badRequest };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
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> {
|
2022-08-23 20:08:53 +02:00
|
|
|
const { node, nodeSecrets } =
|
|
|
|
await NodeService.getNodeDataWithSecretsByMonitoringToken(token);
|
|
|
|
if (
|
|
|
|
node.monitoringState === MonitoringState.DISABLED ||
|
|
|
|
!nodeSecrets.monitoringToken ||
|
|
|
|
nodeSecrets.monitoringToken !== token
|
|
|
|
) {
|
|
|
|
throw { data: "Invalid token.", type: ErrorTypes.badRequest };
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
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)) {
|
2022-08-23 20:08:53 +02:00
|
|
|
throw new Error(
|
|
|
|
"No nodes.json-URLs set. Please adjust config.json: server.map.nodesJsonUrl"
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return await retrieveNodeInformationForUrls(urls);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendMonitoringMails(): Promise<void> {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").debug(
|
|
|
|
"Sending monitoring mails..."
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
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
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").error(
|
|
|
|
'Error sending "online again" mails.',
|
|
|
|
error
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
|
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
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("monitoring", "mail-sending").error(
|
|
|
|
'Error sending "' + mailType + '" mails.',
|
|
|
|
error
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteOfflineNodes(): Promise<void> {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes", "delete-offline").info(
|
|
|
|
`Deleting offline nodes older than ${DELETE_OFFLINE_NODES_AFTER_DURATION} seconds.`
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const deleteBefore = subtract(now(), DELETE_OFFLINE_NODES_AFTER_DURATION);
|
2021-07-26 21:40:17 +02:00
|
|
|
|
|
|
|
await deleteNeverOnlineNodesBefore(deleteBefore);
|
|
|
|
await deleteNodesOfflineSinceBefore(deleteBefore);
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function deleteNeverOnlineNodesBefore(
|
|
|
|
deleteBefore: UnixTimestampSeconds
|
|
|
|
): Promise<void> {
|
|
|
|
Logger.tag("nodes", "delete-never-online").info(
|
|
|
|
"Deleting nodes that were never online created before " + deleteBefore
|
|
|
|
);
|
2021-08-09 21:36:41 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const deletionCandidates: StoredNode[] =
|
|
|
|
await NodeService.findNodesModifiedBefore(deleteBefore);
|
2021-08-09 21:36:41 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes", "delete-never-online").info(
|
|
|
|
"Number of nodes created before " +
|
2021-08-09 21:36:41 +02:00
|
|
|
deleteBefore +
|
2022-08-23 20:08:53 +02:00
|
|
|
": " +
|
2021-08-09 21:36:41 +02:00
|
|
|
deletionCandidates.length
|
2022-08-23 20:08:53 +02:00
|
|
|
);
|
2021-08-09 21:36:41 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const deletionCandidateMacs: MAC[] = deletionCandidates.map(
|
|
|
|
(node) => node.mac
|
|
|
|
);
|
|
|
|
const chunks: MAC[][] = _.chunk(
|
|
|
|
deletionCandidateMacs,
|
|
|
|
NEVER_ONLINE_NODES_DELETION_CHUNK_SIZE
|
|
|
|
);
|
2021-07-26 21:40:17 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes", "delete-never-online").info(
|
|
|
|
"Number of chunks to check for deletion: " + chunks.length
|
|
|
|
);
|
2021-08-09 21:36:41 +02:00
|
|
|
|
2021-07-26 21:40:17 +02:00
|
|
|
for (const macs of chunks) {
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes", "delete-never-online").info(
|
|
|
|
"Checking chunk of " + macs.length + " MACs for deletion."
|
|
|
|
);
|
2021-08-09 21:36:41 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const placeholders = macs.map(() => "?").join(",");
|
2021-07-26 21:40:17 +02:00
|
|
|
|
2022-08-24 18:20:49 +02:00
|
|
|
const rows = await db.all<NodeStateRow>(
|
2021-07-26 21:40:17 +02:00
|
|
|
`SELECT * FROM node_state WHERE mac IN (${placeholders})`,
|
|
|
|
macs
|
|
|
|
);
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes", "delete-never-online").info(
|
|
|
|
"Of the chunk of " +
|
2021-08-09 21:36:41 +02:00
|
|
|
macs.length +
|
2022-08-23 20:08:53 +02:00
|
|
|
" MACs there were " +
|
2021-08-09 21:36:41 +02:00
|
|
|
rows.length +
|
2022-08-23 20:08:53 +02:00
|
|
|
" nodes found in monitoring database. Those should be skipped."
|
|
|
|
);
|
2021-08-09 21:36:41 +02:00
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
const seenMacs: MAC[] = rows.map((row) => row.mac);
|
2021-07-26 21:40:17 +02:00
|
|
|
const neverSeenMacs = _.difference(macs, seenMacs);
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes", "delete-never-online").info(
|
|
|
|
"Of the chunk of " +
|
2021-08-09 21:36:41 +02:00
|
|
|
macs.length +
|
2022-08-23 20:08:53 +02:00
|
|
|
" MACs there are " +
|
2021-08-09 22:03:27 +02:00
|
|
|
neverSeenMacs.length +
|
2022-08-23 20:08:53 +02:00
|
|
|
" nodes that were never online. Those will be deleted."
|
|
|
|
);
|
2021-08-09 21:36:41 +02:00
|
|
|
|
2021-07-26 21:40:17 +02:00
|
|
|
for (const neverSeenMac of neverSeenMacs) {
|
|
|
|
await deleteNodeByMac(neverSeenMac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 20:08:53 +02:00
|
|
|
async function deleteNodesOfflineSinceBefore(
|
|
|
|
deleteBefore: UnixTimestampSeconds
|
|
|
|
): Promise<void> {
|
2022-07-18 17:49:42 +02:00
|
|
|
const rows = await db.all<NodeStateRow>(
|
2022-08-23 20:08:53 +02:00
|
|
|
"SELECT * FROM node_state WHERE state = ? AND last_seen < ?",
|
|
|
|
["OFFLINE", 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> {
|
2022-08-23 20:08:53 +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.
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes", "delete-offline").error(
|
|
|
|
"Could not find node to delete: " + mac,
|
|
|
|
error
|
|
|
|
);
|
2021-07-26 21:40:17 +02:00
|
|
|
}
|
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 {
|
2022-08-23 20:08:53 +02:00
|
|
|
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.
|
2022-08-23 20:08:53 +02:00
|
|
|
Logger.tag("nodes", "delete-offline").error(
|
|
|
|
"Could not delete node state: " + mac,
|
|
|
|
error
|
|
|
|
);
|
2020-04-10 00:43:15 +02:00
|
|
|
}
|
|
|
|
}
|