Skip to content

Commit 1e4a91e

Browse files
committed
chore(cpu): refactor cpu notify and del heapdump
1 parent 5a33c13 commit 1e4a91e

File tree

4 files changed

+62
-94
lines changed

4 files changed

+62
-94
lines changed

bin/proxy/admin.actions.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ module.exports = {
2626
res.end('done!\r\n');
2727
},
2828

29-
'/heapdump': function (req, res) {
30-
process.emit('sendCmd2workerOnce', {
31-
CMD: 'heapdump',
32-
GET: req.GET
33-
});
34-
res.writeHead(200, { 'Content-Type': 'text/plain; charset=UTF-8' });
35-
res.end('done!\r\n');
36-
},
3729

3830
'/profiler': function (req, res) {
3931
process.emit('sendCmd2workerOnce', {

bin/proxy/dump.heap.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.

bin/proxy/http.proxy.js

Lines changed: 61 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ methodMap.reload = function() {
101101
process.emit('reload');
102102
};
103103

104-
// heapdump
105-
methodMap.heapdump = function(message) {
106-
process.emit('heapdump', message.GET);
107-
};
108-
109104
// profiler
110105
methodMap.profiler = function(message) {
111106
process.emit('profiler', message.GET);
@@ -130,24 +125,6 @@ process.on('top100', function(e) {
130125
global.top100 = [];
131126
});
132127

133-
134-
process.on('heapdump', function(e) {
135-
if (isWin32Like) {
136-
return;
137-
}
138-
139-
require('heapdump').writeSnapshot(__dirname + '/cpu' + serverInfo.cpu + '.' + Date.now() + '.heapsnapshot', function(err, filename) {
140-
if (err) {
141-
logger.error(`dump heap error ${err.message}`);
142-
return;
143-
}
144-
logger.info('dump written to ${filename}', {
145-
filename: filename
146-
});
147-
});
148-
});
149-
150-
151128
process.on('profiler', function(data = {}) {
152129
logger.info('profiler time: ${time}', data);
153130
if (isWin32Like) {
@@ -390,61 +367,7 @@ function heartBeat() {
390367

391368
// 高负载告警
392369
if (global.cpuUsed80 === 4 && !config.isTest && !isWin32Like) {
393-
// 取进程快照
394-
cp.exec('top -bcn1', {
395-
env: {
396-
COLUMNS: 200
397-
},
398-
encoding: 'utf8',
399-
timeout: 5000
400-
}, function(err, data, errData) { // eslint-disable-line handle-callback-err
401-
const key = `cpu80.v4:${serverInfo.intranetIp}`;
402-
let content = `<strong>单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%, 最近5秒钟CPU Profiler见附件</strong>`;
403-
let str = '';
404-
405-
if (data) {
406-
str = data;
407-
str = str.replace(/</g, '&gt;');
408-
str = str.replace(/\r\n|\r|\n/g, '<br>');
409-
410-
content += '<p><strong>进程快照:</strong></p><pre style="font-size:12px">' + str + '</pre>';
411-
}
412-
413-
414-
// 获取本机信息,用来分组
415-
require('api/cmdb').GetDeviceThisServer().done(function(data) {
416-
data = data || {};
417-
const business = data.business && data.business[0] || {};
418-
let owner = '';
419-
420-
if (data.ownerMain) {
421-
owner = [owner, data.ownerMain].join(';');
422-
}
423-
424-
if (data.ownerBack) {
425-
owner = [owner, data.ownerBack].join(';');
426-
}
427-
428-
// 再抓一份CPU Profiler
429-
require('util/v8-profiler.js').getProfiler({
430-
recordTime: 5000
431-
}, result => {
432-
mail.SendMail(key, 'js', 600, {
433-
'to': config.mailTo,
434-
'cc': owner,
435-
'msgInfo': business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%',
436-
'title': business.module + '[CPU]' + serverInfo.intranetIp + '单核CPU' + serverInfo.cpu + '使用率为:' + cpuUsed + ',超过80%',
437-
'content': content,
438-
'attachment': result ? {
439-
fileType: true,
440-
dispositionType: 'attachment',
441-
fileName: 'cpu-profiler.cpuprofile',
442-
content: result
443-
} : ''
444-
});
445-
});
446-
});
447-
});
370+
afterCpu80(cpuUsed);
448371
}
449372

450373
const currMemory = process.memoryUsage();
@@ -453,3 +376,63 @@ function heartBeat() {
453376
tnm2.Attr_API_Set('AVG_TSW_MEMORY_HEAP', currMemory.heapTotal);
454377
tnm2.Attr_API_Set('AVG_TSW_MEMORY_EXTERNAL', currMemory.external);
455378
}
379+
380+
381+
function afterCpu80(cpuUsed) {
382+
// 取进程快照
383+
cp.exec('top -bcn1', {
384+
env: {
385+
COLUMNS: 200
386+
},
387+
encoding: 'utf8',
388+
timeout: 5000
389+
}, function(err, data, errData) { // eslint-disable-line handle-callback-err
390+
const key = `cpu80.v4:${serverInfo.intranetIp}`;
391+
let content = `<strong>单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%, 最近5秒钟CPU Profiler见附件</strong>`;
392+
let str = '';
393+
394+
if (data) {
395+
str = data;
396+
str = str.replace(/</g, '&gt;');
397+
str = str.replace(/\r\n|\r|\n/g, '<br>');
398+
399+
content += '<p><strong>进程快照:</strong></p><pre style="font-size:12px">' + str + '</pre>';
400+
}
401+
402+
403+
// 获取本机信息,用来分组
404+
require('api/cmdb').GetDeviceThisServer().done(function(data) {
405+
data = data || {};
406+
const business = data.business && data.business[0] || {};
407+
const profCallback = (result) => {
408+
mail.SendMail(key, 'js', 600, {
409+
'to': config.mailTo,
410+
'cc': config.mailCC,
411+
'msgInfo': `${business.module}[CPU]${serverInfo.intranetIp}单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%`,
412+
'title': `${business.module}[CPU]${serverInfo.intranetIp}单核CPU${serverInfo.cpu}使用率为:${cpuUsed},超过80%`,
413+
'content': content,
414+
'attachment': result ? {
415+
fileType: true,
416+
dispositionType: 'attachment',
417+
fileName: 'cpu-profiler.cpuprofile',
418+
content: result
419+
} : ''
420+
});
421+
};
422+
423+
// 再抓一份CPU Profiler
424+
let profiler;
425+
try {
426+
profiler = require('util/v8-profiler.js');
427+
} catch (err) {
428+
logger.info(err.stack);
429+
}
430+
431+
if (profiler) {
432+
profiler.getProfiler({ recordTime: 5000 }, profCallback);
433+
} else {
434+
profCallback();
435+
}
436+
});
437+
});
438+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"commitmsg": "commitlint -e $GIT_PARAMS",
99
"test": "mocha --recursive test/bin",
1010
"coverage": "nyc --reporter=lcov --reporter=text npm test && codecov",
11-
"optional": "npm i heapdump && npm i v8-profiler"
11+
"optional": "npm i v8-profiler"
1212
},
1313
"repository": {
1414
"type": "git",

0 commit comments

Comments
 (0)