123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /**
- * Created by bin.xu on 2018/8/31.
- * 本JS为后台服务接口
- * 1.获取服务器状态信息CPU 内存 等信息
- * 2.定期进行考核报告生成
- * 3.定期发送数据问题报告
- * ┌─┐ ┌─┐
- * ┌──┘ ┴───────┘ ┴──┐
- * │ │
- * │ ─── │
- * │ ─┬┘ └┬─ │
- * │ │
- * │ ─┴─ │
- * │ │
- * └───┐ ┌───┘
- * │ │
- * │ │
- * │ │
- * │ └──────────────┐
- * │ │
- * │ ├─┐
- * │ ┌─┘
- * │ │
- * └─┐ ┐ ┌───────┬──┐ ┌──┘
- * │ ─┤ ─┤ │ ─┤ ─┤
- * └──┴──┘ └──┴──┘
- * May the force be with you!
- */
- var moment = require("moment");
- var os = require("os");//操作系统
- var storage = require ("storage-device-info");//磁盘信息
- var monitor=require("../models/t_loadMonitor");//压力监控表
- var corp=require("../models/t_corporation");//企业表
- var schedule = require('node-schedule');//定时任务
- //OS系统状态获取规则
- var rule = new schedule.RecurrenceRule();
- var times = [1,6,11,16,21,26,31,36,41,46,51,56];
- rule.minute = times;//每5分钟查询
- //应用服务器
- var appName="应用服务器";
- var appSysPath = "/";
- var appLogPath = "/opt/apps";
- //数据库服务器
- var dbName="数据库服务器";
- var dbSysPath = "/";
- var dbLogPath = "/opt/mongoDB";
- //定期获取操作系统信息
- var osInfo = schedule.scheduleJob(rule, function(fireDate){
- // // CPU 的字节序
- // console.log('endianness : ' + os.endianness());
- // // 操作系统名
- // console.log('type : ' + os.type());
- // CPU使用率
- // const cpus = os.cpus();
- // console.log('*****cpu信息*******');
- // cpus.forEach((cpu,idx,arr)=>{
- // var times = cpu.times;
- // console.log(`cpu${idx}:`);
- // console.log(`型号:${cpu.model}`);
- // console.log(`频率:${cpu.speed}MHz`);
- // console.log(`使用率:${((1-times.idle/(times.idle+times.user+times.nice+times.sys+times.irq))*100).toFixed(2)}%`);
- // });
- //
- console.log('服务器名称:'+ appName);
- //每五分钟的负载
- var loadavg=os.loadavg();
- console.log('每五分钟的负载:'+loadavg[0]);
- var ipAddr= "";
- var network = os.networkInterfaces()
- for(var i = 0; i < network.eth0.length; i++) {
- var json = network.eth0[i];
- if(json.family == 'IPv4') {
- console.log('IP地址: ' +json.address);
- ipAddr = json.address;
- }
- }
- var memPercent = (os.totalmem() - os.freemem())/os.totalmem();
- console.log('操作系统内存使用百分比: ' + os.freemem()/os.totalmem() + " %");
- storage.getPartitionSpace(appSysPath, function(error, space) {
- if (error) {
- console.log(error);
- }
- else {
- var sysDiskPercent = (space.totalMegaBytes -space.freeMegaBytes)/space.totalMegaBytes;
- console.log(sysDiskPercent);
- storage.getPartitionSpace(appLogPath, function(error, space)
- {
- if (error) {
- console.log(error);
- }
- else {
- var logDiskPercent = (space.totalMegaBytes -space.freeMegaBytes)/space.totalMegaBytes;
- // console.log(logDiskPercent)
- //todo 存入数据库
- var monitorAdd = new monitor({
- serverName:appName,
- serverAddr:ipAddr,//服务器地址
- CPULoad:loadavg[0]/loadavg.length,//CPU使用百分比
- memory:memPercent,//内存使用百分比
- diskSpace:sysDiskPercent,//系统磁盘空间使用百分比
- LogSpace:logDiskPercent,//日志空间使用百分比
- time:moment()
- });
- monitorAdd.save(function (err) {
- if (err) {
- console.log(err);
- } else {
- console.log("monitor 存入成功");
- }
- });
- }
- })
- }
- });
- });
- //每年
- schedule.scheduleJob('1 0 0 1 1 *', function(){
- corp.updateMany({},{$set:{year:0}},function (err) {
- if(err)
- {
- console.log(err);
- }
- else
- {
- console.log("每年的企业追溯总数信息重置");
- }
- })
- });
- //每个月
- schedule.scheduleJob('1 0 0 1 * *', function(){
- corp.updateMany({},{$set:{month:0}},function (err) {
- if(err)
- {
- console.log(err);
- }
- else
- {
- console.log("每月的企业追溯总数信息重置");
- }
- })
- });
- //每天执行
- schedule.scheduleJob('1 0 0 * * *', function(){
- corp.updateMany({},{$set:{day:0}},function (err) {
- if(err)
- {
- console.log(err);
- }
- else
- {
- console.log("每天的企业追溯总数信息重置");
- }
- })
- });
|