@@ -101,11 +101,6 @@ methodMap.reload = function() {
101
101
process . emit ( 'reload' ) ;
102
102
} ;
103
103
104
- // heapdump
105
- methodMap . heapdump = function ( message ) {
106
- process . emit ( 'heapdump' , message . GET ) ;
107
- } ;
108
-
109
104
// profiler
110
105
methodMap . profiler = function ( message ) {
111
106
process . emit ( 'profiler' , message . GET ) ;
@@ -130,24 +125,6 @@ process.on('top100', function(e) {
130
125
global . top100 = [ ] ;
131
126
} ) ;
132
127
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
-
151
128
process . on ( 'profiler' , function ( data = { } ) {
152
129
logger . info ( 'profiler time: ${time}' , data ) ;
153
130
if ( isWin32Like ) {
@@ -390,61 +367,7 @@ function heartBeat() {
390
367
391
368
// 高负载告警
392
369
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, '>' ) ;
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 ) ;
448
371
}
449
372
450
373
const currMemory = process . memoryUsage ( ) ;
@@ -453,3 +376,63 @@ function heartBeat() {
453
376
tnm2 . Attr_API_Set ( 'AVG_TSW_MEMORY_HEAP' , currMemory . heapTotal ) ;
454
377
tnm2 . Attr_API_Set ( 'AVG_TSW_MEMORY_EXTERNAL' , currMemory . external ) ;
455
378
}
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, '>' ) ;
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
+ }
0 commit comments