Authored by 沈志敏

增加服务器状态

... ... @@ -2,10 +2,12 @@
import Router from 'koa-router';
import moment from 'moment';
import Rp from 'request-promise';
import Build from '../../ci/build';
import Deploy from '../../ci/deploy';
import Restart from '../../ci/restart';
import Operation from '../../logger/operation';
import ws from '../../../lib/ws';
import {
Building,
... ... @@ -316,6 +318,55 @@ const p = {
ctx.body = {
code: 200
};
},
project_monit: async(ctx) => {
const projectId = ctx.request.body.id;
const env = ctx.request.body.env;
const project = await Project.findById(projectId);
if (!project) {
ctx.body = {
code: 201,
msg: '该项目不存在'
};
return;
}
const hosts = project.deploy[env].target;
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 === 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/${projectId}`, {
host: host,
monitdata: obj
});
});
});
ctx.body = {
code: 200
};
}
};
... ... @@ -328,4 +379,5 @@ r.post('/save', p.save);
r.post('/build/:pid', p.project_build);
r.post('/deploy/:building', p.project_deploy);
r.post('/restart', p.project_restart);
r.post('/monit', p.project_monit);
export default r;
\ No newline at end of file
... ...
... ... @@ -10,6 +10,7 @@
<li>{{project.name}}</li>
</ul>
<h4>{{project.name}} ({{project.subname}})</h4>
<input id="info" type="hidden" data-id='{{project._id}}' data-env='{{deploy.env}}'>
</div>
</div>
<!-- media -->
... ... @@ -47,7 +48,7 @@
<div class="panel-heading">
<h4>服务器信息</h4>
<p>点击状态Label,可以查看实时日志</p>
<button class="btn btn-success btn-xs restart-btn" data-id="{{project._id}}" data-host='all' data-env='{{deploy.env}}'>全部重启</button>
<button class="btn btn-success btn-xs restart-btn" data-host='all'>全部重启</button>
</div>
<div class="panel-body">
<div class="row">
... ... @@ -62,7 +63,7 @@
<h2 class="nomargin">{{host}}</h2>
<h5 class="md-title mt5 version">当前运行版本:&nbsp;<code>{{#if info}}{{info.building}}{{^}}
未知部署{{/if}}</code></h5>
<button class="btn btn-success btn-xs restart-btn" data-id="{{../project._id}}" data-host='{{host}}' data-env='{{../deploy.env}}'>重启</button>
<button class="btn btn-success btn-xs restart-btn" data-host='{{host}}'>重启</button>
</div><!-- media-body -->
<hr class="mt10 mb10">
<div class="clearfix mt5">
... ... @@ -71,6 +72,7 @@
<span class="label label-success deploy-log-btn" data-host="{{host}}"><i
class="fa fa-spinner fa-spin fa-fw margin-bottom"></i> <b>{{#if info}}{{info.state}}{{^}}
未知部署{{/if}}</b></span>
<span class="label label-status"></span>
</div>
<div class="col-xs-6">
... ... @@ -180,9 +182,9 @@
});
$('.restart-btn').click(function(){
var id = $(this).data('id');
var id = $('#info').data('id');
var env = $('#info').data('env');
var host = $(this).data('host');
var env = $(this).data('env');
layer.confirm('确定重启吗?', {
btn: ['确定', '取消']
... ... @@ -254,6 +256,28 @@
$('#d-' + data.host.replace(/\./g, '-')).find('b').text(data.state);
});
ws.on('/monit/{{project._id}}', function(data) {
console.log(data);
var label = $('#d-' + data.host.replace(/\./g, '-')).find('.label-status');
data = data.monitdata;
if (data.errmsg) {
label.text(data.errmsg).addClass('label-danger');
} else {
var msg = "线程数:" + data.total
for(var s in data.status) {
msg += '; [' + s + ']状态数:' + data.status[s];
}
label.text(msg);
if (data.total != data.status.online) {
label.addClass('label-danger');
} else {
label.addClass('label-success');
}
}
});
// ws.on('/deploy/{{project._id}}/log', function(data){
// if(tag == data.host){
// cm.replaceRange("> " +data.msg+ "\n", {line: Infinity});
... ... @@ -263,5 +287,25 @@
ws.on('error', function() {
console.log('connect fail');
});
var projectid = $('#info').data('id');
var env = $('#info').data('env');
var monit = function() {
$.post('/projects/monit', {
id: projectid,
env: env
},function(ret) {
if (ret.code != 200) {
layer.msg(ret.msg, {icon: 5});
}
});
}
setInterval(function(){
// 监控服务状态(轮训)
monit();
}, 5000);
monit();
});
</script>
\ No newline at end of file
... ...
... ... @@ -56,6 +56,7 @@
"nedb-promise": "^2.0.0",
"qn": "^1.3.0",
"qs": "^6.2.0",
"request-promise": "^4.1.1",
"shelljs": "^0.7.0",
"socket.io": "^1.4.6",
"ssh2": "^0.5.0",
... ... @@ -71,4 +72,4 @@
"babel-register": "^6.9.0",
"nodemon": "^1.9.2"
}
}
\ No newline at end of file
}
... ...