-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
63 lines (47 loc) · 1.38 KB
/
index.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
var EventEmitter = require('events')
var parser=require("./parser.js")
var spawn = require('child_process').spawn
var proc=null
var startTime=0
var new_parser=require("./new_parser.js")
class TopEmitter extends EventEmitter {}
var topEmitter = new TopEmitter()
topEmitter.start=(options={})=>{
startTime=new Date().getTime()
proc= spawn('top', ['-b',"-d","1"])// linux top command parameter: ['-b',"-d","1"]
var top_data=""
proc.stdout.on('data',data=>{
top_data+=data.toString()
processData(top_data)
})
proc.on('close', code=>{
topEmitter.emit("close",{code})// then precess was stopped
})
proc.stderr.on("data",data=>{
topEmitter.emit("error",{error:data.toString()})
})
proc.on("error",error=>{
topEmitter.emit("error",{error})
})
var processData=(_data)=>{
try{
var start=_data.indexOf("top - ")
var end=_data.indexOf("top - ",start+1)
if(end==-1||end==start){return}
var data=_data.slice(start,end)
var result={}
try{
result=new_parser(data,options,error=>{topEmitter.emit("error",{error})})
}catch(error){
topEmitter.emit("error",{error:error})
}
topEmitter.emit("data",result)
top_data=_data.replace(data,"")
return true
}catch(error){topEmitter.emit("error",{error})}
}//start
topEmitter.stop=()=>{
if(proc){proc.kill('SIGINT')}// SIGHUP -linux ,SIGINT -windows
}//stop
}//emiter
module.exports=topEmitter