|
@@ -1,10 +1,7 @@
|
|
/* eslint-disable strict */
|
|
/* eslint-disable strict */
|
|
const _ = require('lodash');
|
|
const _ = require('lodash');
|
|
-const path = require('path');
|
|
|
|
const moment = require('moment');
|
|
const moment = require('moment');
|
|
-const QueryLinesReader = require('query-lines-reader');
|
|
|
|
const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
-const fs = require('fs');
|
|
|
|
class CreeperxtsreadService extends CrudService {
|
|
class CreeperxtsreadService extends CrudService {
|
|
|
|
|
|
async creeper() {
|
|
async creeper() {
|
|
@@ -13,42 +10,41 @@ class CreeperxtsreadService extends CrudService {
|
|
// await this.sshcon();
|
|
// await this.sshcon();
|
|
await this.top();
|
|
await this.top();
|
|
await this.qnodes();
|
|
await this.qnodes();
|
|
|
|
+ console.log('读取取回的文本文件结束');
|
|
}
|
|
}
|
|
|
|
|
|
async top() {
|
|
async top() {
|
|
- const nowdate = moment().locale('zh-cn').format('YYYY-MM-DD');
|
|
|
|
- const pathDir = this.app.config.dataDir;
|
|
|
|
- const pathread = `${pathDir}/top${nowdate}.txt`;
|
|
|
|
- const res = await this.readFileToArr(pathread);
|
|
|
|
- if (res) {
|
|
|
|
- for (const rl of res.lineList) {
|
|
|
|
- const strsplit = rl.split(' ');
|
|
|
|
- const strs = _.pull(strsplit, '');
|
|
|
|
- const newdata = {};
|
|
|
|
- newdata.pid = strs[0];
|
|
|
|
- newdata.user = strs[1];
|
|
|
|
- newdata.pr = strs[2];
|
|
|
|
- newdata.ni = strs[3];
|
|
|
|
- newdata.virt = strs[4];
|
|
|
|
- newdata.res = strs[5];
|
|
|
|
- newdata.shr = strs[6];
|
|
|
|
- newdata.s = strs[7];
|
|
|
|
- newdata.cpu = strs[8];
|
|
|
|
- newdata.mem = strs[9];
|
|
|
|
- newdata.time = strs[10];
|
|
|
|
- newdata.command = strs[11];
|
|
|
|
- if (newdata.pid && newdata.pid !== '0' && newdata.user && newdata.user !== '0') {
|
|
|
|
- await this.ctx.model.Top.create(newdata);
|
|
|
|
- }
|
|
|
|
|
|
+ const redis = this.app.redis;
|
|
|
|
+ const text = await redis.get('top');
|
|
|
|
+ const arr = text.split('\\r\\n');
|
|
|
|
+ for (const rl of arr) {
|
|
|
|
+ const strsplit = rl.split(' ');
|
|
|
|
+ const strs = _.pull(strsplit, '');
|
|
|
|
+ if (_.isNaN(parseInt(strs[0]))) continue;
|
|
|
|
+ const newdata = {};
|
|
|
|
+ newdata.pid = strs[0];
|
|
|
|
+ newdata.user = strs[1];
|
|
|
|
+ newdata.pr = strs[2];
|
|
|
|
+ newdata.ni = strs[3];
|
|
|
|
+ newdata.virt = strs[4];
|
|
|
|
+ newdata.res = strs[5];
|
|
|
|
+ newdata.shr = strs[6];
|
|
|
|
+ newdata.s = strs[7];
|
|
|
|
+ newdata.cpu = strs[8];
|
|
|
|
+ newdata.mem = strs[9];
|
|
|
|
+ newdata.time = strs[10];
|
|
|
|
+ newdata.command = strs[11];
|
|
|
|
+ if (newdata.pid && newdata.pid !== '0' && newdata.user && newdata.user !== '0') {
|
|
|
|
+ await this.ctx.model.Top.create(newdata);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
async qnodes() {
|
|
async qnodes() {
|
|
- const nowdate = moment().locale('zh-cn').format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
- const pathDir = this.app.config.dataDir;
|
|
|
|
- const pathread = `${pathDir}/qnodes.txt`;
|
|
|
|
- let { lineList } = await this.readFileToArr(pathread);
|
|
|
|
|
|
+ const nowdate = moment().format('YYYY-MM-DD');
|
|
|
|
+ const redis = this.app.redis;
|
|
|
|
+ const text = await redis.get('qnodes');
|
|
|
|
+ let lineList = text.split('\\r\\n');
|
|
// res,去掉前两行和最后一行
|
|
// res,去掉前两行和最后一行
|
|
lineList = _.drop(lineList, 2);
|
|
lineList = _.drop(lineList, 2);
|
|
lineList = _.dropRight(lineList, 1);
|
|
lineList = _.dropRight(lineList, 1);
|
|
@@ -78,20 +74,10 @@ class CreeperxtsreadService extends CrudService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- console.log(`data length=>${arr.length}`);
|
|
|
|
|
|
+ await this.ctx.model.Qnodes.insertMany(arr);
|
|
await this.toMq(arr);
|
|
await this.toMq(arr);
|
|
- // await fs.unlinkSync(`${pathread}`);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- async readFileToArr(path_) {
|
|
|
|
- const options = { start: 0, end: 1, reverse: false };
|
|
|
|
- const toGetTotal = new QueryLinesReader(path.resolve(__dirname, path_), options);
|
|
|
|
- const total = (await toGetTotal.getTotal()) || 0;
|
|
|
|
- options.end = total;
|
|
|
|
- const queryLinesReader = new QueryLinesReader(path.resolve(__dirname, path_), options);
|
|
|
|
- const lineObj = await queryLinesReader.queryLines();
|
|
|
|
- return lineObj;
|
|
|
|
- }
|
|
|
|
|
|
|
|
async toMq(data) {
|
|
async toMq(data) {
|
|
if (this.ctx.mq) {
|
|
if (this.ctx.mq) {
|