lrf402788946 4 rokov pred
rodič
commit
2fd8f6ab78

+ 36 - 0
app/controller/.top.js

@@ -0,0 +1,36 @@
+module.exports = {
+  create: {
+    requestBody: ["pid", "user", "pr", "ni", "virt", "res", "shr", "s", "cpu", "mem", "time", "command"],
+  },
+  destroy: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["pid", "user", "pr", "ni", "virt", "res", "shr", "s", "cpu", "mem", "time", "command"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        pid:"pid", 
+        user:"user", 
+        pr:"pr", 
+        command:"command",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 18 - 0
app/controller/top.js

@@ -0,0 +1,18 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.top.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 作业监控管理
+class TopController extends Controller {
+
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.department;
+  }
+
+}
+
+module.exports = CrudController(TopController, meta);

+ 29 - 0
app/model/top.js

@@ -0,0 +1,29 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+// top表
+const TopSchema = {
+  pid: { type: String, required: false, maxLength: 200 }, // 信息ID
+  user: { type: String, required: false, maxLength: 500 }, // 用户ID
+  pr: { type: String, required: false }, // 进程的调度优先级
+  ni: { type: String, required: false }, // 进程的nice值
+  virt: { type: String, required: false }, // 进程使用的虚拟内存
+  res: { type: String, required: false }, // 驻留内存大小
+  shr: { type: String, required: false }, // 共享内存
+  s: { type: String, required: false }, // 进程的状态
+  cpu: { type: String, required: false }, // CPU时间百分比
+  mem: { type: String, required: false }, // 可用物理内存百分比
+  time: { type: String, required: false, maxLength: 100 }, // 任务启动后到现在所使用的全部CPU时间
+  command: { type: String, required: false, maxLength: 100 }, // 运行进程所使用的命令
+};
+
+
+const schema = new Schema(TopSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Top', schema, 'top');
+};

+ 46 - 23
app/router.js

@@ -7,45 +7,68 @@ module.exports = app => {
   const { router, controller } = app;
 
   // 信息表设置路由
-  router.resources('news', '/api/count/news', controller.news); // index、create、show、destroy
-  router.post('news', '/api/count/news/update/:id', controller.news.update);
+  router.resources("news", "/api/count/news", controller.news); // index、create、show、destroy
+  router.post("news", "/api/count/news/update/:id", controller.news.update);
 
   // 权限表设置路由
-  router.resources('roles', '/api/count/roles', controller.roles); // index、create、show、destroy
-  router.post('roles', '/api/count/roles/update/:id', controller.roles.update);
+  router.resources("roles", "/api/count/roles", controller.roles); // index、create、show、destroy
+  router.post("roles", "/api/count/roles/update/:id", controller.roles.update);
 
   // 用户表设置路由
-  router.resources('user', '/api/count/user', controller.user); // index、create、show、destroy
-  router.post('user', '/api/count/user/update/:id', controller.user.update);
-  router.post('user', '/api/count/user/uppasswd', controller.user.uppasswd);
-  router.post('user', '/api/count/user/login', controller.user.login);
-  router.post('user', '/api/count/user/register', controller.user.register);
+  router.resources("user", "/api/count/user", controller.user); // index、create、show、destroy
+  router.post("user", "/api/count/user/update/:id", controller.user.update);
+  router.post("user", "/api/count/user/uppasswd", controller.user.uppasswd);
+  router.post("user", "/api/count/user/login", controller.user.login);
+  router.post("user", "/api/count/user/register", controller.user.register);
 
   // 职务表设置路由
-  router.resources('level', '/api/count/level', controller.level); // index、create、show、destroy
-  router.post('level', '/api/count/level/update/:id', controller.level.update);
+  router.resources("level", "/api/count/level", controller.level); // index、create、show、destroy
+  router.post("level", "/api/count/level/update/:id", controller.level.update);
 
   // 部门表设置路由
-  router.resources('department', '/api/count/department', controller.department); // index、create、show、destroy
-  router.post('department', '/api/count/department/update/:id', controller.department.update);
+  router.resources(
+    "department",
+    "/api/count/department",
+    controller.department
+  ); // index、create、show、destroy
+  router.post(
+    "department",
+    "/api/count/department/update/:id",
+    controller.department.update
+  );
 
   // 人员表设置路由
-  router.resources('staff', '/api/count/staff', controller.staff); // index、create、show、destroy
-  router.post('staff', '/api/count/staff/update/:id', controller.staff.update);
+  router.resources("staff", "/api/count/staff", controller.staff); // index、create、show、destroy
+  router.post("staff", "/api/count/staff/update/:id", controller.staff.update);
 
   // 评论表设置路由
-  router.resources('comment', '/api/count/comment', controller.comment); // index、create、show、destroy
-  router.post('comment', '/api/count/comment/update/:id', controller.comment.update);
+  router.resources("comment", "/api/count/comment", controller.comment); // index、create、show、destroy
+  router.post(
+    "comment",
+    "/api/count/comment/update/:id",
+    controller.comment.update
+  );
 
   // 专家表设置路由
-  router.resources('expert', '/api/count/expert', controller.expert); // index、create、show、destroy
-  router.post('expert', '/api/count/expert/update/:id', controller.expert.update);
+  router.resources("expert", "/api/count/expert", controller.expert); // index、create、show、destroy
+  router.post(
+    "expert",
+    "/api/count/expert/update/:id",
+    controller.expert.update
+  );
 
   // 需求表设置路由
-  router.resources('demand', '/api/count/demand', controller.demand); // index、create、show、destroy
-  router.post('demand', '/api/count/demand/update/:id', controller.demand.update);
+  router.resources("demand", "/api/count/demand", controller.demand); // index、create、show、destroy
+  router.post(
+    "demand",
+    "/api/count/demand/update/:id",
+    controller.demand.update
+  );
 
   // 需求表设置路由
-  router.resources('link', '/api/count/link', controller.link); // index、create、show、destroy
-  router.post('link', '/api/count/link/update/:id', controller.link.update);
+  router.resources("link", "/api/count/link", controller.link); // index、create、show、destroy
+  router.post("link", "/api/count/link/update/:id", controller.link.update);
+  // 信息表设置路由
+  router.resources("top", "/api/count/top", controller.top); // index、create、show、destroy
+  router.post("top", "/api/count/top/update/:id", controller.top.update);
 };

+ 23 - 0
app/schedule/creeperxts.js

@@ -0,0 +1,23 @@
+'use strict';
+
+const Subscription = require('egg').Subscription;
+
+class Creeper extends Subscription {
+  // 通过 schedule 属性来设置定时任务的执行间隔等配置
+  // 更改执行时间
+  static get schedule() {
+    return {
+      cron: '0 6 10 * * ?', // 每天晚上23点执行任务
+      // // cron: '0 20 12 * * ?', // 每天晚上23点执行任务
+      // interval: '60s', // 1分钟执行一次
+      type: 'worker', // 指定所有的 worker 都需要执行
+    };
+  }
+
+  // subscribe 是真正定时任务执行时被运行的函数
+  async subscribe() {
+    console.log('选调生');
+    await this.ctx.service.creeperxts.creeper();// 选调生
+  }
+}
+module.exports = Creeper;

+ 23 - 0
app/schedule/creeperxtsqnode.js

@@ -0,0 +1,23 @@
+'use strict';
+
+const Subscription = require('egg').Subscription;
+
+class Creeper extends Subscription {
+  // 通过 schedule 属性来设置定时任务的执行间隔等配置
+  // 更改执行时间
+  static get schedule() {
+    return {
+      // cron: '0 6 30 * * ?', // 每天晚上23点执行任务
+      // // cron: '0 20 12 * * ?', // 每天晚上23点执行任务
+      interval: '60s', // 1分钟执行一次
+      type: 'worker', // 指定所有的 worker 都需要执行
+    };
+  }
+
+  // subscribe 是真正定时任务执行时被运行的函数
+  async subscribe() {
+    console.log('节点查询');
+    await this.ctx.service.creeperxtsqnode.creeper();// qnodes
+  }
+}
+module.exports = Creeper;

+ 23 - 0
app/schedule/creeperxtsread.js

@@ -0,0 +1,23 @@
+'use strict';
+
+const Subscription = require('egg').Subscription;
+
+class Creeper extends Subscription {
+  // 通过 schedule 属性来设置定时任务的执行间隔等配置
+  // 更改执行时间
+  static get schedule() {
+    return {
+      cron: '0 7 10 * * ?', // 每天晚上23点执行任务
+      // // cron: '0 20 12 * * ?', // 每天晚上23点执行任务
+      // interval: '60s', // 1分钟执行一次
+      type: 'worker', // 指定所有的 worker 都需要执行
+    };
+  }
+
+  // subscribe 是真正定时任务执行时被运行的函数
+  async subscribe() {
+    console.log('读取文件');
+    await this.ctx.service.creeperxtsread.creeper();// 读取top文件
+  }
+}
+module.exports = Creeper;

+ 53 - 0
app/service/creeperxts.js

@@ -0,0 +1,53 @@
+/* eslint-disable strict */
+const _ = require('lodash');
+// const fs = require('fs');
+const Client = require('ssh2').Client;
+const fs = require('fs');
+const moment = require('moment');
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+class CreeperxtsService extends CrudService {
+
+  async creeper() {
+    // 链接linux服务器
+    console.log('进入连接linux服务器');
+    // await this.sshcon();
+    await this.sshcon();
+  }
+
+  async sshcon() {
+    const nowtime = moment().locale('zh-cn').format('YYYY-MM-DD');
+    const conn = new Client();
+    conn.on('ready', function() {
+      conn.exec('top -bcn 1', function(err, stream) {
+        if (err) throw err;
+        stream.on('close', function(code, signal) {
+          conn.end();
+        }).on('data', function(data) {
+          console.log('STDOUT: ' + data);
+          const path_ = 'app/public/data/top' + nowtime + '.txt';
+          fs.access(path_, err => {
+            if (err) {
+              fs.writeFile(path_, data.toString(), function(err) {
+                if (err) throw err;
+              });
+            } else {
+              fs.appendFile(path_, data.toString(), function(err) {
+                if (err) throw err;
+              });
+            }
+          });
+        }).stderr.on('data', function(data) {
+          console.log('STDERR: ' + data);
+        });
+      });
+    }).connect({
+      host: '192.168.10.102',
+      port: 42973,
+      username: 'free_ly',
+      password: '1qaz2wsx',
+      // privateKey: require('fs').readFileSync('/home/admin/.ssh/id_dsa')
+    });
+  }
+
+}
+module.exports = CreeperxtsService;

+ 53 - 0
app/service/creeperxtsqnode.js

@@ -0,0 +1,53 @@
+/* eslint-disable strict */
+const _ = require('lodash');
+// const fs = require('fs');
+const Client = require('ssh2').Client;
+const fs = require('fs');
+const moment = require('moment');
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+class CreeperxtsService extends CrudService {
+
+  async creeper() {
+    // 链接linux服务器
+    console.log('进入连接linux服务器');
+    // await this.sshcon();
+    await this.sshcon();
+  }
+
+  async sshcon() {
+    const nowtime = moment().locale('zh-cn').format('YYYY-MM-DD');
+    const conn = new Client();
+    conn.on('ready', function() {
+      conn.exec('cd /home; qnodes', function(err, stream) {
+        if (err) throw err;
+        stream.on('close', function(code, signal) {
+          conn.end();
+        }).on('data', function(data) {
+          console.log('STDOUT: ' + data);
+          const path_ = 'app/public/data/qnodes' + nowtime + '.txt';
+          fs.access(path_, err => {
+            if (err) {
+              fs.writeFile(path_, data.toString(), function(err) {
+                if (err) throw err;
+              });
+            } else {
+              fs.appendFile(path_, data.toString(), function(err) {
+                if (err) throw err;
+              });
+            }
+          });
+        }).stderr.on('data', function(data) {
+          console.log('STDERR: ' + data);
+        });
+      });
+    }).connect({
+      host: '192.168.10.102',
+      port: 42973,
+      username: 'free_ly',
+      password: '1qaz2wsx',
+      // privateKey: require('fs').readFileSync('/home/admin/.ssh/id_dsa')
+    });
+  }
+
+}
+module.exports = CreeperxtsService;

+ 86 - 0
app/service/creeperxtsread.js

@@ -0,0 +1,86 @@
+/* eslint-disable strict */
+const _ = require('lodash');
+const path = require('path');
+const moment = require('moment');
+const QueryLinesReader = require('query-lines-reader');
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+class CreeperxtsreadService extends CrudService {
+
+  async creeper() {
+    // 链接linux服务器
+    console.log('进入读取取回的文本文件');
+    // await this.sshcon();
+    await this.top();
+    await this.qnodes();
+  }
+
+  async top() {
+    const nowtime = moment().locale('zh-cn').format('YYYY-MM-DD');
+    const pathread = '../public/data/top' + nowtime + '.txt';
+    const res = await this.readFileToArr(pathread);
+    if (res) {
+      let index = 0;
+      for (const rl of res.lineList) {
+        if (index > 6) {
+          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];
+          await this.ctx.model.Top.create(newdata);
+        }
+        index = index + 1;
+      }
+    }
+  }
+
+  async qnodes() {
+    const nowtime = moment().locale('zh-cn').format('YYYY-MM-DD');
+    const pathread = '../public/data/qnodes' + nowtime + '.txt';
+    const res = await this.readFileToArr(pathread);
+    if (res) {
+      console.log(res.lineList);
+      // let index = 0;
+      // for (const rl of res.lineList) {
+      //   if (index > 6) {
+      //     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];
+      //     await this.ctx.model.Top.create(newdata);
+      //   }
+      //   index = index + 1;
+      // }
+    }
+  }
+
+  async readFileToArr(path_) {
+    const options = { start: 0, end: 50, reverse: false };
+    const queryLinesReader = new QueryLinesReader(path.resolve(__dirname, path_), options);
+    const lineObj = await queryLinesReader.queryLines();
+    console.log(lineObj);
+    return lineObj;
+  }
+}
+module.exports = CreeperxtsreadService;

+ 18 - 0
app/service/top.js

@@ -0,0 +1,18 @@
+'use strict';
+
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class TopService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'top');
+    this.model = this.ctx.model.Top;
+  }
+
+}
+
+module.exports = TopService;

+ 2 - 1
package.json

@@ -11,7 +11,8 @@
     "egg-scripts": "^2.11.0",
     "jsonwebtoken": "^8.5.1",
     "lodash": "^4.17.15",
-    "naf-framework-mongoose": "^0.6.12"
+    "naf-framework-mongoose": "^0.6.12",
+    "ssh2": "^0.8.9"
   },
   "devDependencies": {
     "autod": "^3.0.1",