monitor.js
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 16/8/4
*/
'use strict';
import Router from 'koa-router';
import {
Project
} from '../../models';
import InfluxDB from '../../logger/influxdb';
const r = new Router();
const monitor = {
async log(ctx) {
let id = ctx.query.pid;
let ip = ctx.query.ip;
let env = ctx.query.env;
let projects = await Project.findAll();
let data = {
projects: projects, ip: ip, env: [{
name: '线上环境',
value: 'production',
checked: env === 'production'
},{
name: '灰度环境',
value: 'preview',
checked: env === 'preview'
}, {
name: '测试环境',
value: 'test',
checked: env === 'test'
}]
};
if (id) {
projects.forEach((p) => {
p.checked = p._id === id;
if (env) {
data.hosts = p.deploy[env].target;
}
});
}
await ctx.render('action/log_view', data);
},
async query(ctx) {
let influxName = ctx.query.influxName;
let ip = ctx.query.ip;
let limit = ctx.query.limit || 30;
let page = ctx.query.page || 1;
let oldTime = ctx.query.oldTime;
let newTime = ctx.query.newTime;
ip = ip.replace(/\./g, '-');
let where = 'where host =~ /./ ';
if (ip) {
where += ` and host='ip-${ip}'`;
}
if (oldTime) {
where += ` and time<'${oldTime}' order by time desc`;
}
if (newTime) {
where += ` and time>'${newTime}'`;
}
if (!(oldTime || newTime)) {
where += ' order by time desc';
}
let sql = `select * from ${influxName} ${where} limit ${limit} OFFSET ${(page -1) * limit}`;
let logs = await InfluxDB.query(sql);
ctx.body = logs[0];
}
};
r.get('/log', monitor.log);
r.get('/log/query', monitor.query);
export default r;