-
Notifications
You must be signed in to change notification settings - Fork 374
/
monitorLog.ts
131 lines (117 loc) · 3.45 KB
/
monitorLog.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*!
* Pomelo -- consoleModule monitorLog
* Copyright(c) 2012 fantasyni <fantasyni@163.com>
* MIT Licensed
*/
import { getLogger } from 'pinus-logger'; let logger = getLogger('pinus-admin', __filename);
import { exec } from 'child_process';
import * as path from 'path';
import { IModule, MonitorCallback } from '../consoleService';
import { MonitorAgent } from '../monitor/monitorAgent';
import { MasterAgent } from '../master/masterAgent';
import { MasterCallback } from '../../index';
let DEFAULT_INTERVAL = 5 * 60; // in second
/**
* Initialize a new 'Module' with the given 'opts'
*
* @class Module
* @constructor
* @param {object} opts
* @api public
*/
export class MonitorLogModule implements IModule
{
root: string;
interval: number;
static moduleId = 'monitorLog';
constructor(opts ?: {path ?: string , interval ?: number})
{
opts = opts || {};
this.root = opts.path;
this.interval = opts.interval || DEFAULT_INTERVAL;
};
/**
* collect monitor data from monitor
*
* @param {Object} agent monitorAgent object
* @param {Object} msg client message
* @param {Function} cb callback function
* @api public
*/
monitorHandler(agent : MonitorAgent, msg : any, cb : MonitorCallback)
{
if (!msg.logfile)
{
cb(new Error('logfile should not be empty'));
return;
}
let serverId = agent.id;
fetchLogs(this.root, msg, function (data)
{
cb(null, { serverId: serverId, body: data });
});
};
/**
* Handle client request
*
* @param {Object} agent masterAgent object
* @param {Object} msg client message
* @param {Function} cb callback function
* @api public
*/
clientHandler(agent : MasterAgent, msg : any, cb : MasterCallback)
{
agent.request(msg.serverId, MonitorLogModule.moduleId, msg, function (err, res)
{
if (err)
{
logger.error('fail to run log for ' + err.stack);
return;
}
cb(null, res);
});
};
}
//get the latest logs
let fetchLogs = function (root : string, msg : any, callback : (data:{logfile:string , dataArray:any})=>void)
{
let number = msg.number;
let logfile = msg.logfile;
let serverId = msg.serverId;
let filePath = path.join(root, getLogFileName(logfile, serverId));
let endLogs : any[] = [];
exec('tail -n ' + number + ' ' + filePath, function (error, output)
{
let endOut = [];
let outputS = output.replace(/^\s+|\s+$/g, "").split(/\s+/);
for (let i = 5; i < outputS.length; i += 6)
{
endOut.push(outputS[i]);
}
let endLength = endOut.length;
for (let j = 0; j < endLength; j++)
{
let json;
try
{
json = JSON.parse(endOut[j]);
} catch (e)
{
logger.error('the log cannot parsed to json, ' + e);
continue;
}
endLogs.push({
time : json.time,
route : json.route || json.service,
serverId : serverId,
timeUsed : json.timeUsed,
params : endOut[j]
});
}
callback({ logfile: logfile, dataArray: endLogs });
});
};
let getLogFileName = function (logfile : string, serverId : string)
{
return logfile + '-' + serverId + '.log';
};