feat: add some audit log for monitor

This commit is contained in:
moonrailgun 2024-01-22 13:23:30 +08:00
parent 7243b991ae
commit a176bbcfb4
3 changed files with 43 additions and 8 deletions

View File

@ -10,6 +10,7 @@ export async function createAuditLog(info: {
relatedType?: WorkspaceAuditLogType; relatedType?: WorkspaceAuditLogType;
content: string; content: string;
}) { }) {
try {
const log = await prisma.workspaceAuditLog.create({ const log = await prisma.workspaceAuditLog.create({
data: { data: {
...info, ...info,
@ -17,4 +18,7 @@ export async function createAuditLog(info: {
}); });
return log; return log;
} catch (err) {
console.error('[AuditLog] create log error', String(err));
}
} }

View File

@ -7,6 +7,7 @@ import dayjs from 'dayjs';
import { logger } from '../../utils/logger'; import { logger } from '../../utils/logger';
import { token } from '../notification/token'; import { token } from '../notification/token';
import { ContentToken } from '../notification/token/type'; import { ContentToken } from '../notification/token/type';
import { createAuditLog } from '../auditLog';
/** /**
* Class which actually run monitor data collect * Class which actually run monitor data collect
@ -48,6 +49,12 @@ export class MonitorRunner {
value = await provider.run(monitor); value = await provider.run(monitor);
} catch (err) { } catch (err) {
logger.error(`[Monitor] (id: ${monitor.id}) run error:`, String(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; value = -1;
} }
@ -90,7 +97,13 @@ export class MonitorRunner {
// Run next loop // Run next loop
nextAction(); nextAction();
} catch (err) { } 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)}`,
});
} }
}; };

View File

@ -28,6 +28,7 @@ import { OPENAPI_TAG } from '../../utils/const';
import { OpenApiMeta } from 'trpc-openapi'; import { OpenApiMeta } from 'trpc-openapi';
import { MonitorStatusPageModelSchema } from '../../prisma/zod'; import { MonitorStatusPageModelSchema } from '../../prisma/zod';
import { runCodeInVM } from '../../model/monitor/provider/custom'; import { runCodeInVM } from '../../model/monitor/provider/custom';
import { createAuditLog } from '../../model/auditLog';
export const monitorRouter = router({ export const monitorRouter = router({
all: workspaceProcedure all: workspaceProcedure
@ -239,8 +240,9 @@ export const monitorRouter = router({
}) })
) )
.output(monitorInfoSchema) .output(monitorInfoSchema)
.mutation(async ({ input }) => { .mutation(async ({ input, ctx }) => {
const { workspaceId, monitorId, active } = input; const { workspaceId, monitorId, active } = input;
const user = ctx.user;
const monitor = await prisma.monitor.update({ const monitor = await prisma.monitor.update({
where: { where: {
@ -265,12 +267,28 @@ export const monitorRouter = router({
'UP', 'UP',
`Monitor [${monitor.name}] has been manual start` `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 { } else {
runner.stopMonitor(); runner.stopMonitor();
runner.createEvent( runner.createEvent(
'DOWN', 'DOWN',
`Monitor [${monitor.name}] has been manual stop` `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; return monitor;