2024-06-14 11:09:53 +00:00
|
|
|
import RunCron from "../../Utils/Cron";
|
|
|
|
import LIMIT_MAX from "Common/Types/Database/LimitMax";
|
|
|
|
import OneUptimeDate from "Common/Types/Date";
|
|
|
|
import { CheckOn } from "Common/Types/Monitor/CriteriaFilter";
|
|
|
|
import MonitorType from "Common/Types/Monitor/MonitorType";
|
2024-08-05 01:56:45 +00:00
|
|
|
import ServerMonitorResponse from "Common/Types/Monitor/ServerMonitor/ServerMonitorResponse";
|
2024-06-14 11:09:53 +00:00
|
|
|
import { EVERY_MINUTE } from "Common/Utils/CronTime";
|
2024-08-07 21:50:32 +00:00
|
|
|
import MonitorService from "Common/Server/Services/MonitorService";
|
|
|
|
import QueryHelper from "Common/Server/Types/Database/QueryHelper";
|
|
|
|
import logger from "Common/Server/Utils/Logger";
|
|
|
|
import MonitorResourceUtil from "Common/Server/Utils/Monitor/MonitorResource";
|
2024-08-05 19:00:31 +00:00
|
|
|
import Monitor from "Common/Models/DatabaseModels/Monitor";
|
2024-03-09 18:52:45 +00:00
|
|
|
|
|
|
|
RunCron(
|
2024-06-14 11:09:53 +00:00
|
|
|
"ServerMonitor:CheckOnlineStatus",
|
|
|
|
{ schedule: EVERY_MINUTE, runOnStartup: false },
|
|
|
|
async () => {
|
2024-09-20 10:07:35 +00:00
|
|
|
try {
|
|
|
|
const twoMinsAgo: Date = OneUptimeDate.getSomeMinutesAgo(2);
|
2024-03-09 18:52:45 +00:00
|
|
|
|
2024-09-20 10:07:35 +00:00
|
|
|
const serverMonitors: Array<Monitor> = await MonitorService.findBy({
|
|
|
|
query: {
|
|
|
|
monitorType: MonitorType.Server,
|
|
|
|
serverMonitorRequestReceivedAt:
|
|
|
|
QueryHelper.lessThanEqualToOrNull(twoMinsAgo),
|
|
|
|
},
|
|
|
|
props: {
|
|
|
|
isRoot: true,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
_id: true,
|
|
|
|
monitorSteps: true,
|
|
|
|
serverMonitorRequestReceivedAt: true,
|
|
|
|
createdAt: true,
|
|
|
|
},
|
|
|
|
limit: LIMIT_MAX,
|
|
|
|
skip: 0,
|
|
|
|
});
|
2024-03-09 18:52:45 +00:00
|
|
|
|
2024-09-20 10:07:35 +00:00
|
|
|
for (const monitor of serverMonitors) {
|
|
|
|
try {
|
|
|
|
if (!monitor.monitorSteps) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-09 18:52:45 +00:00
|
|
|
|
2024-09-20 10:07:35 +00:00
|
|
|
const processRequest: boolean = shouldProcessRequest(monitor);
|
2024-03-09 18:52:45 +00:00
|
|
|
|
2024-09-20 10:07:35 +00:00
|
|
|
if (!processRequest) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-09 18:52:45 +00:00
|
|
|
|
2024-09-20 10:07:35 +00:00
|
|
|
const serverMonitorResponse: ServerMonitorResponse = {
|
|
|
|
monitorId: monitor.id!,
|
|
|
|
onlyCheckRequestReceivedAt: true,
|
|
|
|
requestReceivedAt:
|
|
|
|
monitor.serverMonitorRequestReceivedAt || monitor.createdAt!,
|
|
|
|
hostname: "",
|
|
|
|
};
|
2024-03-09 18:52:45 +00:00
|
|
|
|
2024-09-20 10:07:35 +00:00
|
|
|
await MonitorResourceUtil.monitorResource(serverMonitorResponse);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
`Error in ServerMonitor:CheckOnlineStatus for monitorId: ${monitor.id}`,
|
|
|
|
);
|
|
|
|
logger.error(error);
|
|
|
|
}
|
2024-06-27 12:40:07 +00:00
|
|
|
}
|
2024-09-20 10:07:35 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.error("Error in ServerMonitor:CheckOnlineStatus");
|
|
|
|
logger.error(error);
|
2024-03-09 18:52:45 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
},
|
2024-03-09 18:52:45 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
type ShouldProcessRequestFunction = (monitor: Monitor) => boolean;
|
|
|
|
|
|
|
|
const shouldProcessRequest: ShouldProcessRequestFunction = (
|
2024-06-14 11:09:53 +00:00
|
|
|
monitor: Monitor,
|
2024-03-09 18:52:45 +00:00
|
|
|
): boolean => {
|
2024-06-14 11:09:53 +00:00
|
|
|
// check if any criteria has Is Online step. If yes, then process the request. If no then skip the request.
|
2024-03-09 18:52:45 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
let shouldWeProcessRequest: boolean = false;
|
2024-03-09 18:52:45 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
for (const steps of monitor.monitorSteps?.data?.monitorStepsInstanceArray ||
|
|
|
|
[]) {
|
|
|
|
if (steps.data?.monitorCriteria.data?.monitorCriteriaInstanceArray) {
|
|
|
|
for (const criteria of steps.data?.monitorCriteria.data
|
|
|
|
?.monitorCriteriaInstanceArray || []) {
|
|
|
|
for (const filters of criteria.data?.filters || []) {
|
|
|
|
if (filters.checkOn === CheckOn.IsOnline) {
|
|
|
|
shouldWeProcessRequest = true;
|
|
|
|
break;
|
|
|
|
}
|
2024-03-09 18:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldWeProcessRequest) {
|
2024-06-14 11:09:53 +00:00
|
|
|
break;
|
2024-03-09 18:52:45 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldWeProcessRequest) {
|
|
|
|
break;
|
2024-03-09 18:52:45 +00:00
|
|
|
}
|
2024-06-14 11:09:53 +00:00
|
|
|
}
|
2024-03-09 18:52:45 +00:00
|
|
|
|
2024-06-14 11:09:53 +00:00
|
|
|
return shouldWeProcessRequest;
|
2024-03-09 18:52:45 +00:00
|
|
|
};
|