log.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const filePath = require('../../config/filespath');
  4. class LogController extends Controller {
  5. // ssl日志查询
  6. async logquery() {
  7. const { ctx } = this;
  8. try {
  9. const type = ctx.request.query.type;
  10. let res;
  11. if (type === 'ssl') res = await this.service.shell.shell(`tail -n 100 ${filePath.ssllog}`);
  12. if (type === 'sec') res = await this.service.shell.shell(`tail -n 100 ${filePath.swanlog}`);
  13. if (type === 'systemct') res = await this.service.shell.shell(`tail -n 100 ${filePath.systemlog}`);
  14. if (res.errcode === 0) {
  15. ctx.body = { errcode: 0, errmsg: '', data: res.data };
  16. } else {
  17. this.ctx.body = { errcode: -1006, errmsg: '获取日志失败' };
  18. }
  19. } catch (error) {
  20. const body = { errcode: -1006, errmsg: '日志查询失败失败', error };
  21. throw new Error(JSON.stringify(body));
  22. }
  23. }
  24. // ssl日志下载
  25. async logdownload() {
  26. try {
  27. const type = this.ctx.request.query.type;
  28. let res;
  29. if (type === 'ssl') res = await this.secservice.fileshandler.download({ filePath: filePath.ssllog });
  30. if (type === 'sec') res = await this.secservice.fileshandler.download({ filePath: filePath.ssllog });
  31. if (type === 'systemct') res = await this.secservice.fileshandler.download({ filePath: filePath.ssllog });
  32. if (res) {
  33. this.ctx.body = res;
  34. } else {
  35. this.ctx.body = { errcode: -1007, errmsg: '下载日志失败' };
  36. }
  37. } catch (error) {
  38. const body = { errcode: -1007, errmsg: '下载日志失败', error };
  39. throw new Error(JSON.stringify(body));
  40. }
  41. }
  42. }
  43. module.exports = LogController;