Skip to content

Commit 893d768

Browse files
authored
Merge pull request #18 from aliyun-node/get_process_env
add get_process_env command
2 parents 0f88865 + 48ef354 commit 893d768

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.debug.js
2+
*.min.js
3+
node_modules/*

.eslintrc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"rules": {
3+
"indent": [
4+
2,
5+
2
6+
],
7+
"quotes": [
8+
2,
9+
"single"
10+
],
11+
"linebreak-style": [
12+
2,
13+
"unix"
14+
],
15+
"semi": [2, "always"],
16+
"strict": [2, "global"],
17+
"curly": 2,
18+
"eqeqeq": 2,
19+
"no-eval": 2,
20+
"guard-for-in": 2,
21+
"no-caller": 2,
22+
"no-else-return": 2,
23+
"no-eq-null": 2,
24+
"no-extend-native": 2,
25+
"no-extra-bind": 2,
26+
"no-floating-decimal": 2,
27+
"no-implied-eval": 2,
28+
"no-labels": 2,
29+
"no-with": 2,
30+
"no-loop-func": 1,
31+
"no-native-reassign": 2,
32+
"no-redeclare": [2, {"builtinGlobals": true}],
33+
"no-delete-var": 2,
34+
"no-shadow-restricted-names": 2,
35+
"no-undef-init": 2,
36+
"no-use-before-define": 2,
37+
"no-unused-vars": [2, {"args": "none"}],
38+
"no-undef": 2,
39+
"callback-return": [2, ["callback", "cb", "next"]],
40+
"global-require": 0,
41+
"no-console": 0,
42+
"require-yield": 0
43+
},
44+
"env": {
45+
"es6": true,
46+
"node": true,
47+
"browser": true
48+
},
49+
"globals": {
50+
"describe": true,
51+
"it": true,
52+
"before": true,
53+
"after": true
54+
},
55+
"parserOptions": {
56+
"ecmaVersion": 8,
57+
"sourceType": "script",
58+
"ecmaFeatures": {
59+
"jsx": true
60+
}
61+
},
62+
"extends": "eslint:recommended"
63+
}

get_process_env

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
export ENABLE_NODE_LOG=NO
4+
5+
DIR=$(cd "$(dirname "$0")"; pwd)
6+
GET_NODE_EXE=$DIR/get_node_exe
7+
NODE_EXE=`$GET_NODE_EXE`
8+
9+
SCRIPT=$DIR/get_process_env.js
10+
11+
#get_process_env pid
12+
$NODE_EXE "$SCRIPT" $1

get_process_env.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
3+
const { execSync } = require('child_process');
4+
5+
const argv = process.argv.slice(2);
6+
7+
const [pid] = argv;
8+
9+
if (!pid) {
10+
console.log('Please pass <pid>');
11+
console.log('Usage:');
12+
console.log(' get_process_env <pid>');
13+
process.exit(1);
14+
}
15+
16+
if (require('os').type() !== 'Linux') {
17+
console.log('Not support non-linux');
18+
process.exit(1);
19+
}
20+
21+
function getAlinodeVersion(pid) {
22+
const output = execSync(`ls -l /proc/${pid}/exe`, {
23+
encoding: 'utf8'
24+
});
25+
// get execPath from PID
26+
// $ ls -l /proc/10532/exe
27+
// lrwxrwxrwx 1 puling.tyq users 0 Mar 8 16:54 /proc/10532/exe -> /home/puling.tyq/.tnvm/versions/node/v8.6.0/bin/node
28+
let [, execPath] = output.split('->');
29+
execPath = execPath.trim();
30+
if (!execPath.endsWith('bin/node')) {
31+
return '';
32+
}
33+
34+
const versionOutput = execSync(`${execPath} -p "process.alinode"`, {
35+
encoding: 'utf8'
36+
});
37+
38+
return versionOutput.trim();
39+
}
40+
41+
function getProcessEnv(pid) {
42+
const output = execSync(`cat /proc/${pid}/environ`, {
43+
encoding: 'utf8'
44+
});
45+
const lines = output.split('\u0000');
46+
47+
var env = {ENABLE_NODE_LOG: '', NODE_LOG_DIR: '/tmp'};
48+
49+
for (var i = 0; i < lines.length; i++) {
50+
const line = lines[i];
51+
if (line.startsWith('ENABLE_NODE_LOG')) {
52+
env.ENABLE_NODE_LOG = line.split('=')[1];
53+
}
54+
55+
if (line.startsWith('NODE_LOG_DIR')) {
56+
env.NODE_LOG_DIR = line.split('=')[1];
57+
}
58+
}
59+
60+
if (!env.NODE_LOG_DIR.endsWith('/')) {
61+
env.NODE_LOG_DIR += '/';
62+
}
63+
64+
return env;
65+
}
66+
67+
var env = getProcessEnv(pid);
68+
69+
const logdir = process.env.logdir;
70+
71+
var data = {
72+
alinode_version: getAlinodeVersion(pid),
73+
ENABLE_NODE_LOG: env.ENABLE_NODE_LOG,
74+
NODE_LOG_DIR: env.NODE_LOG_DIR,
75+
logdir: logdir
76+
};
77+
78+
console.log(JSON.stringify(data));

0 commit comments

Comments
 (0)