mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
Delete Config.ts and update start script in package.json
This commit is contained in:
@@ -1,99 +1,25 @@
|
||||
import BasicCron from 'CommonServer/Utils/BasicCron';
|
||||
import { BasicMetircs } from './Utils/BasicMetrics';
|
||||
import { EVERY_MINUTE } from 'Common/Utils/CronTime';
|
||||
import logger from 'CommonServer/Utils/Logger';
|
||||
import API from 'Common/Utils/API';
|
||||
import yargs from 'yargs';
|
||||
import URL from 'Common/Types/API/URL';
|
||||
import BaseModel from 'Common/Models/BaseModel';
|
||||
import Monitor from 'Model/Models/Monitor';
|
||||
import ServerMonitorResponse from 'Common/Types/Monitor/ServerMonitor/ServerMonitorResponse';
|
||||
import OneUptimeDate from 'Common/Types/Date';
|
||||
import JSONFunctions from 'Common/Types/JSONFunctions';
|
||||
import HTTPErrorResponse from 'Common/Types/API/HTTPErrorResponse';
|
||||
import HTTPResponse from 'Common/Types/API/HTTPResponse';
|
||||
import { OneUptimeURL, SecretKey } from './Utils/Config';
|
||||
import BadDataException from 'Common/Types/Exception/BadDataException';
|
||||
import Dictionary from 'Common/Types/Dictionary';
|
||||
import MonitorInfrastructure from './Jobs/MonitorInfrastructure';
|
||||
|
||||
BasicCron({
|
||||
jobName: 'MonitorInfrastructure',
|
||||
options: {
|
||||
schedule: EVERY_MINUTE, // Every minute
|
||||
runOnStartup: true,
|
||||
},
|
||||
runFunction: async () => {
|
||||
try {
|
||||
const secretKey: string | undefined = SecretKey;
|
||||
const oneuptimeHost: URL = OneUptimeURL;
|
||||
const usage = "\nUsage: oneuptime-infrastructure-agent --secret-key <secret-key>";
|
||||
|
||||
if (!secretKey) {
|
||||
throw new BadDataException(
|
||||
'No SECRET_KEY environment variable found. You can find secret key for this monitor on OneUptime Dashboard'
|
||||
);
|
||||
}
|
||||
const argv: Dictionary<string> = yargs
|
||||
.usage(usage)
|
||||
.option("k", {alias:"secret-key", describe: "Secret Key for this agent. You will find this on OneUptime Dashboard", type: "string", demandOption
|
||||
: true })
|
||||
.option("h", {alias:"oneuptime-host", describe: "OneUptime Host. By default this is https://oneupime.com", type: "string", demandOption
|
||||
: false })
|
||||
.help(true)
|
||||
.argv as Dictionary<string>;
|
||||
|
||||
// get monitor steps to get disk paths.
|
||||
const monitorResult: HTTPErrorResponse | HTTPResponse<BaseModel> =
|
||||
await API.get(
|
||||
URL.fromString(
|
||||
`${oneuptimeHost}/server-monitor/${secretKey}`
|
||||
)
|
||||
);
|
||||
const secretKey: string | undefined = argv["secret-key"];
|
||||
const oneuptimeHost: URL = URL.fromString(argv["oneuptime-host"] || "https://oneuptime.com");
|
||||
|
||||
if (monitorResult instanceof HTTPErrorResponse) {
|
||||
throw monitorResult;
|
||||
}
|
||||
|
||||
const monitor: Monitor = BaseModel.fromJSON(
|
||||
monitorResult.data,
|
||||
Monitor
|
||||
) as Monitor;
|
||||
if(!secretKey) {
|
||||
throw new Error("No secret-key argument found. You can find secret key for this monitor on OneUptime Dashboard");
|
||||
}
|
||||
|
||||
// get disk paths to monitor.
|
||||
|
||||
const diskPaths: string[] = [];
|
||||
|
||||
for (const step of monitor.monitorSteps?.data
|
||||
?.monitorStepsInstanceArray || []) {
|
||||
for (const criteriaInstance of step.data?.monitorCriteria.data
|
||||
?.monitorCriteriaInstanceArray || []) {
|
||||
for (const filter of criteriaInstance.data?.filters || []) {
|
||||
if (filter.serverMonitorOptions?.diskPath) {
|
||||
diskPaths.push(
|
||||
filter.serverMonitorOptions?.diskPath
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const serverMonitorResponse: ServerMonitorResponse = {
|
||||
monitorId: monitor.id!,
|
||||
requestReceivedAt: OneUptimeDate.getCurrentDate(),
|
||||
basicInfrastructureMetrics: await BasicMetircs.getBasicMetrics({
|
||||
diskPaths: diskPaths,
|
||||
}),
|
||||
onlyCheckRequestReceivedAt: false,
|
||||
};
|
||||
|
||||
logger.info('Server Monitor Response');
|
||||
logger.info(serverMonitorResponse);
|
||||
|
||||
// now we send this data back to server.
|
||||
|
||||
await API.post(
|
||||
URL.fromString(
|
||||
`${oneuptimeHost}/server-monitor/response/ingest/${secretKey}`
|
||||
),
|
||||
{
|
||||
serverMonitorResponse: JSONFunctions.serialize(
|
||||
serverMonitorResponse as any
|
||||
),
|
||||
},
|
||||
{}
|
||||
);
|
||||
} catch (err) {
|
||||
logger.error('Error reporting metrics to OneUptime Server.');
|
||||
logger.error(err);
|
||||
}
|
||||
},
|
||||
});
|
||||
MonitorInfrastructure.initJob(secretKey, oneuptimeHost);
|
||||
|
||||
106
InfrastructureAgent/Jobs/MonitorInfrastructure.ts
Normal file
106
InfrastructureAgent/Jobs/MonitorInfrastructure.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import BasicCron from 'CommonServer/Utils/BasicCron';
|
||||
import { BasicMetircs } from '../Utils/BasicMetrics';
|
||||
import { EVERY_MINUTE } from 'Common/Utils/CronTime';
|
||||
import logger from 'CommonServer/Utils/Logger';
|
||||
import API from 'Common/Utils/API';
|
||||
import URL from 'Common/Types/API/URL';
|
||||
import BaseModel from 'Common/Models/BaseModel';
|
||||
import Monitor from 'Model/Models/Monitor';
|
||||
import ServerMonitorResponse from 'Common/Types/Monitor/ServerMonitor/ServerMonitorResponse';
|
||||
import OneUptimeDate from 'Common/Types/Date';
|
||||
import JSONFunctions from 'Common/Types/JSONFunctions';
|
||||
import HTTPErrorResponse from 'Common/Types/API/HTTPErrorResponse';
|
||||
import HTTPResponse from 'Common/Types/API/HTTPResponse';
|
||||
import BadDataException from 'Common/Types/Exception/BadDataException';
|
||||
|
||||
export default class MonitorInfrastructure {
|
||||
public static initJob(secretKey: string, oneuptimeHost: URL) {
|
||||
BasicCron({
|
||||
jobName: 'MonitorInfrastructure',
|
||||
options: {
|
||||
schedule: EVERY_MINUTE, // Every minute
|
||||
runOnStartup: true,
|
||||
},
|
||||
runFunction: async () => {
|
||||
try {
|
||||
|
||||
if (!secretKey) {
|
||||
throw new BadDataException(
|
||||
'No SECRET_KEY environment variable found. You can find secret key for this monitor on OneUptime Dashboard'
|
||||
);
|
||||
}
|
||||
|
||||
// get monitor steps to get disk paths.
|
||||
const monitorResult:
|
||||
| HTTPErrorResponse
|
||||
| HTTPResponse<BaseModel> = await API.get(
|
||||
URL.fromString(
|
||||
`${oneuptimeHost}/server-monitor/${secretKey}`
|
||||
)
|
||||
);
|
||||
|
||||
if (monitorResult instanceof HTTPErrorResponse) {
|
||||
throw monitorResult;
|
||||
}
|
||||
|
||||
const monitor: Monitor = BaseModel.fromJSON(
|
||||
monitorResult.data,
|
||||
Monitor
|
||||
) as Monitor;
|
||||
|
||||
// get disk paths to monitor.
|
||||
|
||||
const diskPaths: string[] = [];
|
||||
|
||||
for (const step of monitor.monitorSteps?.data
|
||||
?.monitorStepsInstanceArray || []) {
|
||||
for (const criteriaInstance of step.data
|
||||
?.monitorCriteria.data
|
||||
?.monitorCriteriaInstanceArray || []) {
|
||||
for (const filter of criteriaInstance.data
|
||||
?.filters || []) {
|
||||
if (filter.serverMonitorOptions?.diskPath) {
|
||||
diskPaths.push(
|
||||
filter.serverMonitorOptions?.diskPath
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const serverMonitorResponse: ServerMonitorResponse = {
|
||||
monitorId: monitor.id!,
|
||||
requestReceivedAt: OneUptimeDate.getCurrentDate(),
|
||||
basicInfrastructureMetrics:
|
||||
await BasicMetircs.getBasicMetrics({
|
||||
diskPaths: diskPaths,
|
||||
}),
|
||||
onlyCheckRequestReceivedAt: false,
|
||||
};
|
||||
|
||||
logger.info('Server Monitor Response');
|
||||
logger.info(serverMonitorResponse);
|
||||
|
||||
// now we send this data back to server.
|
||||
|
||||
await API.post(
|
||||
URL.fromString(
|
||||
`${oneuptimeHost}/server-monitor/response/ingest/${secretKey}`
|
||||
),
|
||||
{
|
||||
serverMonitorResponse: JSONFunctions.serialize(
|
||||
serverMonitorResponse as any
|
||||
),
|
||||
},
|
||||
{}
|
||||
);
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
'Error reporting metrics to OneUptime Server.'
|
||||
);
|
||||
logger.error(err);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import URL from 'Common/Types/API/URL';
|
||||
import logger from 'CommonServer/Utils/Logger';
|
||||
|
||||
export const SecretKey: string | undefined = process.env['SECRET_KEY'];
|
||||
export const OneUptimeURL: URL = process.env['ONEUPTIME_URL']
|
||||
? URL.fromString(process.env['ONEUPTIME_URL'])
|
||||
: URL.fromString('https://oneuptime.com');
|
||||
|
||||
if (!SecretKey) {
|
||||
logger.error(
|
||||
'No SECRET_KEY environment variable found. You can find secret key for this monitor on OneUptime Dashboard'
|
||||
);
|
||||
}
|
||||
|
||||
if (OneUptimeURL.toString() === 'https://oneuptime.com') {
|
||||
logger.error(
|
||||
'No ONEUPTIME_URL environment variable found. Using default OneUptime URL - https://oneuptime.com'
|
||||
);
|
||||
}
|
||||
805
InfrastructureAgent/package-lock.json
generated
805
InfrastructureAgent/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -3,7 +3,7 @@
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"start": "node --require ts-node/register Index.ts",
|
||||
"start": "node --require ts-node/register Index.ts --secret-key hello",
|
||||
"build": "esbuild Index.ts --bundle --platform=node --outfile=./Build/Index.js --loader:.node=file",
|
||||
"compile": "tsc",
|
||||
"clear-modules": "rm -rf node_modules && rm package-lock.json && npm install",
|
||||
@@ -17,13 +17,19 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"boxen": "^7.1.1",
|
||||
"chalk": "^5.3.0",
|
||||
"Common": "file:../Common",
|
||||
"CommonServer": "file:../CommonServer",
|
||||
"diskusage": "^1.2.0",
|
||||
"esbuild": "^0.20.1",
|
||||
"Model": "file:../Model",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^5.4.2"
|
||||
"typescript": "^5.4.2",
|
||||
"yargs": "^17.7.2"
|
||||
},
|
||||
"bin": {
|
||||
"oneuptime-infrastructure-agent": "./build/Index.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.11",
|
||||
|
||||
Reference in New Issue
Block a user