monit.js 2.94 KB
'use strict';

const Rp = require('request-promise');
const _ = require('lodash');
const ssh = require('ssh2');
const models = require('../models');

const Server = models.Server;

const ws = require('../../lib/ws');

const monit = {
    node: function (project, hosts) {
        hosts.forEach((host) => {
            var obj = {
                'total': 0,
                'status': {},
            };

            Rp({
                uri: `http://${host}:9615`,
                json: true
            }).then(function (data) {
                var processes = data.processes || [];
                processes.forEach(function (p) {
                    if (p.name === 'pm2-server-monit') {
                        obj.cpuUsg = p.pm2_env.axm_monitor['CPU usage'].value;
                        obj.freeMen = p.pm2_env.axm_monitor['Free memory'].value;
                    }

                    if (p.name === project.name) {
                        obj.total++;

                        if (!obj.status[p.pm2_env.status]) {
                            obj.status[p.pm2_env.status] = 1;
                        } else {
                            obj.status[p.pm2_env.status]++;
                        }
                    }
                });
            }).catch(function (err) {
                obj.errmsg = '获取监控状态失败'
            }).finally(function () {
                ws.broadcast(`/monit/${project._id}`, {
                    host: host,
                    monitdata: obj
                });
            });
        });
    },
    php: function(project, hosts) {
        if (!_.isArray(hosts)) {
            return;
        }
        _.forEach(hosts, host => {
            var obj = {
                'total': 0,
                'status': {},
            };

            Server.findByHost(host).then(server => {
                let conn = new ssh.Client();

                conn.on('ready', async() => {
                    conn.exec('ps aux|grep httpd|wc -l ', (err, stream) => {
                        if (err) {
                            console.log(err);
                        } else {
                            stream.on('close', () => {
                                ws.broadcast(`/monit/${project._id}`, {
                                    host: host,
                                    monitdata: obj
                                });

                                conn.end();
                            }).on('data', (data) => {
                                let total = _.trim(data.toString('utf-8'));
                                
                                obj.total = total;
                                obj.status.online = total;
                            });
                        }
                    });
                }).on('error', (err) => {
                    console.log(err);
                    conn.end();
                }).connect(server);
            }).catch(e => {}); 
        });
    }
};

module.exports = monit;