diff --git a/Common/Types/Infrastrucutre/BasicMetrics.ts b/Common/Types/Infrastrucutre/BasicMetrics.ts new file mode 100644 index 0000000000..34fc37c45e --- /dev/null +++ b/Common/Types/Infrastrucutre/BasicMetrics.ts @@ -0,0 +1,24 @@ +export interface MemoryMetrics { + total: number; + free: number; + used: number; +} + +export interface CPUMetrics { + percentUsage: number; +} + +export interface BasicDiskMetrics { + total: number; + free: number; + used: number; + available: number; + diskPath: string; +} + + +export default interface BasicInfrastructureMetrics { + cpuMetrics: CPUMetrics; + memoryMetrics: MemoryMetrics; + diskMetrics: Array +} \ No newline at end of file diff --git a/InfrastructureAgent/Index.ts b/InfrastructureAgent/Index.ts index e69de29bb2..8dc5a68f4b 100644 --- a/InfrastructureAgent/Index.ts +++ b/InfrastructureAgent/Index.ts @@ -0,0 +1,12 @@ +import BasicCron from 'CommonServer/Utils/BasicCron'; + +BasicCron({ + jobName: 'MonitorInfrastructure', + options: { + schedule: '*/5 * * * *', + runOnStartup: true, + }, + runFunction: async () => { + + } +}) \ No newline at end of file diff --git a/InfrastructureAgent/Utils/BasicMetrics.ts b/InfrastructureAgent/Utils/BasicMetrics.ts new file mode 100644 index 0000000000..62d8b701eb --- /dev/null +++ b/InfrastructureAgent/Utils/BasicMetrics.ts @@ -0,0 +1,51 @@ +import os from 'node:os'; +import diskusage from 'diskusage'; +import BasicInfrastructureMetrics, { BasicDiskMetrics, CPUMetrics, MemoryMetrics } from 'Common/Types/Infrastrucutre/BasicMetrics'; + + +export class BasicMetircs { + public static async getBasicMetrics(data: { + diskPaths: string[] + }): Promise { + + return { + memoryMetrics: await this.getMemoryMetrics(), + cpuMetrics: await this.getCPUMetrics(), + diskMetrics: await Promise.all(data.diskPaths.map(async (diskPath: string) => { + return this.getDiskUsage(diskPath) + })); + } + } + + public static async getDiskUsage(diskPath: string): Promise { + let info = await diskusage.check(diskPath); + + return { + total: info.total, + free: info.free, + used: info.total - info.free, + available: info.available, + diskPath: diskPath + } + } + + public static async getMemoryMetrics(): Promise { + const totalMemory = os.totalmem(); + const freeMemory = os.freemem(); + const usedMemory = totalMemory - freeMemory; + + return { + total: totalMemory, + free: freeMemory, + used: usedMemory + } + } + + public static async getCPUMetrics(): Promise { + const cpuUsage = os.loadavg()[0]; + + return { + percentUsage: cpuUsage + } + } +} \ No newline at end of file diff --git a/InfrastructureAgent/package-lock.json b/InfrastructureAgent/package-lock.json new file mode 100644 index 0000000000..10a7388640 --- /dev/null +++ b/InfrastructureAgent/package-lock.json @@ -0,0 +1,135 @@ +{ + "name": "infrastructure-agent", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "infrastructure-agent", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "Common": "file:../Common", + "CommonServer": "file:../CommonServer", + "diskusage": "^1.2.0" + } + }, + "../Common": { + "name": "common", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "@types/crypto-js": "^4.2.2", + "@types/uuid": "^8.3.4", + "axios": "^1.6.7", + "crypto-js": "^4.1.1", + "json5": "^2.2.3", + "moment": "^2.30.1", + "moment-timezone": "^0.5.45", + "posthog-js": "^1.105.7", + "reflect-metadata": "^0.2.1", + "slugify": "^1.6.5", + "typeorm": "^0.3.20", + "uuid": "^8.3.2" + }, + "devDependencies": { + "@faker-js/faker": "^8.0.2", + "@types/jest": "^27.5.2", + "@types/node": "^17.0.22", + "jest": "^27.5.1", + "ts-jest": "^27.1.4" + } + }, + "../CommonServer": { + "name": "common-server", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "@clickhouse/client": "^0.2.9", + "@elastic/elasticsearch": "^8.11.0", + "@opentelemetry/api": "^1.7.0", + "@opentelemetry/api-logs": "^0.48.0", + "@opentelemetry/exporter-logs-otlp-http": "^0.48.0", + "@opentelemetry/exporter-metrics-otlp-proto": "^0.48.0", + "@opentelemetry/exporter-trace-otlp-proto": "^0.48.0", + "@opentelemetry/id-generator-aws-xray": "^1.2.1", + "@opentelemetry/instrumentation-express": "^0.35.0", + "@opentelemetry/instrumentation-http": "^0.48.0", + "@opentelemetry/sdk-logs": "^0.48.0", + "@opentelemetry/sdk-metrics": "^1.21.0", + "@opentelemetry/sdk-node": "^0.48.0", + "@opentelemetry/sdk-trace-node": "^1.21.0", + "@socket.io/redis-adapter": "^8.2.1", + "airtable": "^0.12.2", + "axios": "^1.6.7", + "bullmq": "^5.3.3", + "Common": "file:../Common", + "cookie-parser": "^1.4.6", + "cors": "^2.8.5", + "cron-parser": "^4.8.1", + "dotenv": "^16.4.1", + "ejs": "^3.1.8", + "express": "^4.17.3", + "ioredis": "^5.3.2", + "json2csv": "^5.0.7", + "jsonwebtoken": "^9.0.0", + "markdown-it": "^13.0.1", + "Model": "file:../Model", + "node-cron": "^3.0.3", + "nodemailer": "^6.9.9", + "pg": "^8.7.3", + "socket.io": "^4.7.4", + "stripe": "^10.17.0", + "twilio": "^4.22.0", + "typeorm": "^0.3.20", + "typeorm-extension": "^2.2.13", + "vm2": "^3.9.14" + }, + "devDependencies": { + "@faker-js/faker": "^6.3.1", + "@types/cookie-parser": "^1.4.4", + "@types/cors": "^2.8.12", + "@types/ejs": "^3.1.1", + "@types/express": "^4.17.13", + "@types/jest": "^27.4.1", + "@types/json2csv": "^5.0.3", + "@types/jsonwebtoken": "^8.5.9", + "@types/markdown-it": "^12.2.3", + "@types/node": "^17.0.22", + "@types/node-cron": "^3.0.7", + "@types/nodemailer": "^6.4.7", + "jest": "^27.5.1", + "jest-mock-extended": "^3.0.5", + "ts-jest": "^27.1.4" + } + }, + "node_modules/Common": { + "resolved": "../Common", + "link": true + }, + "node_modules/CommonServer": { + "resolved": "../CommonServer", + "link": true + }, + "node_modules/diskusage": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/diskusage/-/diskusage-1.2.0.tgz", + "integrity": "sha512-2u3OG3xuf5MFyzc4MctNRUKjjwK+UkovRYdD2ed/NZNZPrt0lqHnLKxGhlFVvAb4/oufIgQG3nWgwmeTbHOvXA==", + "hasInstallScript": true, + "dependencies": { + "es6-promise": "^4.2.8", + "nan": "^2.18.0" + } + }, + "node_modules/es6-promise": { + "version": "4.2.8", + "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.8.tgz", + "integrity": "sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==" + }, + "node_modules/nan": { + "version": "2.18.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.18.0.tgz", + "integrity": "sha512-W7tfG7vMOGtD30sHoZSSc/JVYiyDPEyQVso/Zz+/uQd0B0L46gtC+pHha5FFMRpil6fm/AoEcRWyOVi4+E/f8w==" + } + } +} diff --git a/InfrastructureAgent/package.json b/InfrastructureAgent/package.json index 1b55a4c57b..be0d229d27 100644 --- a/InfrastructureAgent/package.json +++ b/InfrastructureAgent/package.json @@ -4,8 +4,19 @@ "description": "", "main": "index.js", "scripts": { + "start": "node --require ts-node/register Index.ts", + "compile": "tsc", + "clear-modules": "rm -rf node_modules && rm package-lock.json && npm install", + "dev": "npx nodemon", + "audit": "npm audit --audit-level=low", + "dep-check": "npm install -g depcheck && depcheck ./ --skip-missing=true", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", - "license": "ISC" + "license": "ISC", + "dependencies": { + "diskusage": "^1.2.0", + "Common": "file:../Common", + "CommonServer": "file:../CommonServer" + } }