From a176bbcfb47d1b1fa2d3e8afb49785338df27580 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 22 Jan 2024 13:23:30 +0800 Subject: [PATCH] feat: add some audit log for monitor --- src/server/model/auditLog.ts | 16 ++++++++++------ src/server/model/monitor/runner.ts | 15 ++++++++++++++- src/server/trpc/routers/monitor.ts | 20 +++++++++++++++++++- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/server/model/auditLog.ts b/src/server/model/auditLog.ts index 4e2fb23..febcdae 100644 --- a/src/server/model/auditLog.ts +++ b/src/server/model/auditLog.ts @@ -10,11 +10,15 @@ export async function createAuditLog(info: { relatedType?: WorkspaceAuditLogType; content: string; }) { - const log = await prisma.workspaceAuditLog.create({ - data: { - ...info, - }, - }); + try { + const log = await prisma.workspaceAuditLog.create({ + data: { + ...info, + }, + }); - return log; + return log; + } catch (err) { + console.error('[AuditLog] create log error', String(err)); + } } diff --git a/src/server/model/monitor/runner.ts b/src/server/model/monitor/runner.ts index dc95bed..d1b4b4e 100644 --- a/src/server/model/monitor/runner.ts +++ b/src/server/model/monitor/runner.ts @@ -7,6 +7,7 @@ import dayjs from 'dayjs'; import { logger } from '../../utils/logger'; import { token } from '../notification/token'; import { ContentToken } from '../notification/token/type'; +import { createAuditLog } from '../auditLog'; /** * Class which actually run monitor data collect @@ -48,6 +49,12 @@ export class MonitorRunner { value = await provider.run(monitor); } catch (err) { logger.error(`[Monitor] (id: ${monitor.id}) run error:`, String(err)); + createAuditLog({ + workspaceId: this.monitor.workspaceId, + relatedId: this.monitor.id, + relatedType: 'Monitor', + content: `Monitor(id: ${monitor.id}) exec error: ${String(err)}`, + }); value = -1; } @@ -90,7 +97,13 @@ export class MonitorRunner { // Run next loop nextAction(); } catch (err) { - logger.error('Run monitor error,', monitor.id, String(err)); + logger.error('[Monitor] Run monitor error,', monitor.id, String(err)); + createAuditLog({ + workspaceId: this.monitor.workspaceId, + relatedId: this.monitor.id, + relatedType: 'Monitor', + content: `Run monitor(id: ${monitor.id}) error: ${String(err)}`, + }); } }; diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index dc4433b..3189316 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -28,6 +28,7 @@ import { OPENAPI_TAG } from '../../utils/const'; import { OpenApiMeta } from 'trpc-openapi'; import { MonitorStatusPageModelSchema } from '../../prisma/zod'; import { runCodeInVM } from '../../model/monitor/provider/custom'; +import { createAuditLog } from '../../model/auditLog'; export const monitorRouter = router({ all: workspaceProcedure @@ -239,8 +240,9 @@ export const monitorRouter = router({ }) ) .output(monitorInfoSchema) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { workspaceId, monitorId, active } = input; + const user = ctx.user; const monitor = await prisma.monitor.update({ where: { @@ -265,12 +267,28 @@ export const monitorRouter = router({ 'UP', `Monitor [${monitor.name}] has been manual start` ); + createAuditLog({ + workspaceId: workspaceId, + relatedId: monitorId, + relatedType: 'Monitor', + content: `Monitor(id: ${monitor.id}) manual start by ${String( + user.username + )}(${String(user.id)})`, + }); } else { runner.stopMonitor(); runner.createEvent( 'DOWN', `Monitor [${monitor.name}] has been manual stop` ); + createAuditLog({ + workspaceId: workspaceId, + relatedId: monitorId, + relatedType: 'Monitor', + content: `Monitor(id: ${monitor.id}) manual stop by ${String( + user.username + )}(${String(user.id)})`, + }); } return monitor;