-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm2-logs.js
executable file
·115 lines (106 loc) · 3.63 KB
/
pm2-logs.js
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
#!/usr/bin/env node
var readline = require('readline');
var fs = require('fs');
var asy = require("async");
var path = require('path');
var cl = console.log;
var his, __his = __dirname+'/history.json';
try{his = require(__his)}
catch(err){his = {};hisput('init',true)}
function strpad(str,len,chr){
chr = chr || ' ';
if(str.length > len) return str.substr(0,len-3)+'...';
return str += new Array(len-str.length+1).join(chr);
}
function hisget(id,uid){
var idu = id+'_'+uid;
if(!(idu in his)) his[idu]=null;
return his[idu];
}
function hisput(id,val,uid){
var idu = id+'_'+uid;
if(!(idu in his)) his[idu]=null;
if(his[idu] != val) his[idu]=val;
return fs.writeFileSync(__his,JSON.stringify(his,null,2));
}
function hislast(id,uid){
var last = hisget(id,uid);
if(last) return last;
return '';
}
function walkSync(dir, filelist) {
files = fs.readdirSync(dir);
files.forEach(function(file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
filelist = walkSync(path.join(dir, file), filelist);
}
else {
var rel = dir.replace(lpath,'')+'/';
if((/net-out-.*\.log$/).test(file)){
filelist.push(path.join(lpath, file));
}
}
});
return filelist;
};
var lpath = '/root/.pm2/logs/';
var data = [];
const rl = readline.createInterface({input: process.stdin,output: process.stdout});
rl.question(strpad('Enter 3 Letter User ID:',25), function(uid){
uid = uid.toLowerCase();
rl.question(strpad('Start Time:',25), function(start){
if(start.length < 14) {cl('Invalid Date/Time:'+start); return rl.close();}
hisput('log_start',start,uid);
rl.question(strpad('End Time: ',25), function(end){
hisput('log_end',end,uid)
rl.question(strpad('Site ID:',25), function(sid){
if(sid && typeof(sid)=='string') {
sid = sid.toUpperCase().replace(/ /g,'');
var sids = new RegExp(sid.split(',').join('|'));
}
hisput('log_sid',sid,uid)
rl.question(strpad('Keyword:',25), function(key){
rl.close();
hisput('log_key',key,uid)
var ltot=0, files = walkSync(lpath,[]);
asy.eachSeries(files,function(file,next){
var logs = fs.readFileSync(file,'utf-8');
if(!logs) return next();
var lines = logs.split('\n');
var site = file.replace(lpath,'').split('.')[0].toUpperCase();
if(sids && !sids.test(site)) return next();
lines.map(function(line,i){
ltot ++;
line = line.trim().replace(/\u001b\[.*?m/g, '');
var bits = line.split(' ');
var ts = bits.slice(0,2).join(' ').replace(/:$/,'');
if(key && line.indexOf(key)<0) return;
if(ts >= start && ts <= end){
var lb = bits.slice(2).join(' ').match(/.{1,80}/g);
var log = lb[0].trim();
if(lb.length > 1) {
lb.slice(1).map(function(e){log+="\n"+Array(29).join(' ')+e})
log+="\n";
}
data.push([strpad(ts,16),strpad(site,10),log]);
}
});
next();
},function(){
cl(strpad('=',110,'='))
data.sort().map(function(x){
cl(x[0],x[1],x[2]);
})
cl(strpad('=',110,'='))
cl('Found '+data.length+' rows in '+files.length+' log files out of '+ltot+' total lines.');
cl();
}) // asy.each()
})
rl.write(hislast('log_key',uid));
})
rl.write(hislast('log_sid',uid));
})
rl.write(hislast('log_end',uid));
})
rl.write(hislast('log_start',uid));
});