creeperxtsread.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* eslint-disable strict */
  2. const _ = require('lodash');
  3. const path = require('path');
  4. const moment = require('moment');
  5. const QueryLinesReader = require('query-lines-reader');
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const fs = require('fs');
  8. class CreeperxtsreadService extends CrudService {
  9. async creeper() {
  10. // 链接linux服务器
  11. console.log('进入读取取回的文本文件');
  12. // await this.sshcon();
  13. await this.top();
  14. await this.qnodes();
  15. }
  16. async top() {
  17. const nowdate = moment().locale('zh-cn').format('YYYY-MM-DD');
  18. const pathDir = this.app.config.dataDir;
  19. const pathread = `${pathDir}/top${nowdate}.txt`;
  20. const res = await this.readFileToArr(pathread);
  21. if (res) {
  22. for (const rl of res.lineList) {
  23. const strsplit = rl.split(' ');
  24. const strs = _.pull(strsplit, '');
  25. const newdata = {};
  26. newdata.pid = strs[0];
  27. newdata.user = strs[1];
  28. newdata.pr = strs[2];
  29. newdata.ni = strs[3];
  30. newdata.virt = strs[4];
  31. newdata.res = strs[5];
  32. newdata.shr = strs[6];
  33. newdata.s = strs[7];
  34. newdata.cpu = strs[8];
  35. newdata.mem = strs[9];
  36. newdata.time = strs[10];
  37. newdata.command = strs[11];
  38. if (newdata.pid && newdata.pid !== '0' && newdata.user && newdata.user !== '0') {
  39. await this.ctx.model.Top.create(newdata);
  40. }
  41. }
  42. }
  43. }
  44. async qnodes() {
  45. const nowdate = moment().locale('zh-cn').format('YYYY-MM-DD HH:mm:ss');
  46. const pathDir = this.app.config.dataDir;
  47. const pathread = `${pathDir}/qnodes.txt`;
  48. let { lineList } = await this.readFileToArr(pathread);
  49. // res,去掉前两行和最后一行
  50. lineList = _.drop(lineList, 2);
  51. lineList = _.dropRight(lineList, 1);
  52. const arr = [];
  53. if (_.isArray(lineList)) {
  54. const match = /^(gpu|cu)(\d+)$/i;
  55. // 规律,当line符合 cuxx(数字)或gpuxx(数字) 时,之后的行数就是集群属性,直到 line === ''时截止,再扫到条件1的时候又是开始
  56. let obj = {};
  57. for (let line of lineList) {
  58. if (line.match(match)) {
  59. // 找到开始
  60. obj.node = _.trim(line);
  61. } else if (line === '') {
  62. // 找到结束,推进去,重置
  63. obj.time = nowdate;
  64. arr.push(_.cloneDeep(obj));
  65. obj = {};
  66. } else {
  67. line = _.trim(line);
  68. const arr = line.split(',');
  69. for (const i of arr) {
  70. const iarr = i.split('=');
  71. if (iarr && iarr.length >= 2) {
  72. obj[_.trim(iarr[0])] = _.trim(iarr[1]);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. this.toMq(arr);
  79. fs.unlinkSync(`${pathread}`);
  80. }
  81. async readFileToArr(path_) {
  82. const options = { start: 0, end: 1, reverse: false };
  83. const toGetTotal = new QueryLinesReader(path.resolve(__dirname, path_), options);
  84. const total = (await toGetTotal.getTotal()) || 0;
  85. options.end = total;
  86. const queryLinesReader = new QueryLinesReader(path.resolve(__dirname, path_), options);
  87. const lineObj = await queryLinesReader.queryLines();
  88. return lineObj;
  89. }
  90. async toMq(data) {
  91. if (this.ctx.mq) {
  92. const exchange = 'service-count';
  93. const routerKey = 'nodes';
  94. const parm = { durable: true };
  95. await this.mq.fanout(exchange, routerKey, JSON.stringify(data), parm);
  96. }
  97. }
  98. }
  99. module.exports = CreeperxtsreadService;