feat: add some audit log for monitor
This commit is contained in:
parent
7243b991ae
commit
a176bbcfb4
@ -10,6 +10,7 @@ export async function createAuditLog(info: {
|
||||
relatedType?: WorkspaceAuditLogType;
|
||||
content: string;
|
||||
}) {
|
||||
try {
|
||||
const log = await prisma.workspaceAuditLog.create({
|
||||
data: {
|
||||
...info,
|
||||
@ -17,4 +18,7 @@ export async function createAuditLog(info: {
|
||||
});
|
||||
|
||||
return log;
|
||||
} catch (err) {
|
||||
console.error('[AuditLog] create log error', String(err));
|
||||
}
|
||||
}
|
||||
|
@ -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)}`,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user