1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var {execSync} = require('child_process');
|
8 | var fs = require('fs');
|
9 | var util = require('./util')
|
10 | var agentSetting = require('./agent-setting')
|
11 |
|
12 | function captureProcessCoreDump(){}
|
13 |
|
14 | captureProcessCoreDump.parseCommand = function(commPath,fileName,clientSocket){
|
15 |
|
16 | try{
|
17 | if(!fileName)
|
18 | fileName = 'coreDump_'+agentSetting.getTierName()+'_'+agentSetting.getServerName()+'_'+agentSetting.getInstance()+'_'+new Date().getTime()
|
19 | if(!commPath)
|
20 | commPath = '/tmp'
|
21 | validateCommPath(commPath,function (err){
|
22 | if(!err){
|
23 | executeCoreDump(commPath,fileName,clientSocket)
|
24 | }else if(typeof (err) == 'string'){
|
25 | util.logger.error(agentSetting.currentTestRun , ' | Error : ',err)
|
26 | clientSocket.write('nd_control_rep:action=captureProcessCoreDump;result=Error:'+err+';\n')
|
27 | }
|
28 | })
|
29 | }catch(e){
|
30 | clientSocket.write('nd_control_rep:action=captureProcessCoreDump;result=Error:Error in Parsing Command;\n')
|
31 | util.logger.error(agentSetting.currentTestRun , ' | Error in ParseCommand : ',e)
|
32 | }
|
33 | }
|
34 |
|
35 | function executeCoreDump(commPath,fileName,clientSocket){
|
36 |
|
37 | try{
|
38 |
|
39 | var output = execSync('sudo -l gcore',{cwd:commPath})
|
40 | console.log('Output',output.toString())
|
41 | if(output.toString().indexOf('gcore') > -1 ){
|
42 | var command = 'sudo gcore -o '+fileName+' '+process.pid
|
43 | if(agentSetting.enableBciDebug > 2)
|
44 | util.logger.info(agentSetting.currentTestRun ,' | Command Available ,Going to Run command : ',command)
|
45 |
|
46 | var result = execSync(command,{cwd:commPath}).toString();
|
47 |
|
48 | util.logger.info(agentSetting.currentTestRun + ' | Output of Caturing Core Dump :',result.toString())
|
49 | if(result.indexOf('Saved') > -1){
|
50 | clientSocket.write('nd_control_rep:action=captureProcessCoreDump;result=Success:core Dump file Created;\n')
|
51 | util.logger.info(agentSetting.currentTestRun , 'Core File Saved on path : '+commPath,' ,Named :',fileName+'.'+process.pid)
|
52 | }
|
53 | }else{
|
54 | clientSocket.write('nd_control_rep:action=captureProcessCoreDump;result=Error:Execution Command is failed;\n')
|
55 | util.logger.error(agentSetting.currentTestRun + ' | Execution Command is failed.')
|
56 | }
|
57 | }catch (err){
|
58 |
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | if(agentSetting.enableBciDebug > 2){
|
72 | util.logger.error(agentSetting.currentTestRun + ' | err.stdout :',err.stdout.toString())
|
73 | util.logger.error(agentSetting.currentTestRun + ' | err.stderr :',err.stderr.toString())
|
74 | util.logger.error(agentSetting.currentTestRun + ' | err.signal :',err.signal)
|
75 | util.logger.error(agentSetting.currentTestRun + ' | err.status :',err.status)
|
76 | }
|
77 |
|
78 | clientSocket.write('nd_control_rep:action=captureProcessCoreDump;result=Error:Execution Command is failed;\n')
|
79 | util.logger.error(agentSetting.currentTestRun + ' | Execution Command is failed Due to permission Issue.')
|
80 |
|
81 | }
|
82 | }
|
83 |
|
84 | function validateCommPath(commPath,callback){
|
85 |
|
86 | try{
|
87 | if(!commPath)
|
88 | callback('Command path Invalid');
|
89 | else{
|
90 | if(fs.existsSync(commPath)){
|
91 | try{
|
92 | fs.accessSync(commPath, fs.constants.X_OK| fs.constants.W_OK | fs.constants.R_OK)
|
93 | callback(false);
|
94 | }catch(e){
|
95 | util.logger.error(agentSetting.currentTestRun + ' | EACCES: permission denied, access :',e)
|
96 | callback('Path does not have a Sufficient Permissons.')
|
97 | }
|
98 | }else{
|
99 | callback('Command Path Does not Exist.')
|
100 | }
|
101 | }
|
102 | }catch(e){
|
103 | util.logger.error(agentSetting.currentTestRun + ' | Error in validateCommandPath :',e)
|
104 | callback('Error in validateCommandPath')
|
105 | }
|
106 | }
|
107 |
|
108 | module.exports = captureProcessCoreDump;
|