-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcessList.phpclass
executable file
·381 lines (286 loc) · 11.5 KB
/
ProcessList.phpclass
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**************************************************************************************************************
NAME
ProcessList.phpclass
DESCRIPTION
A platform-independent class for process list retrieval.
AUTHOR
Christian Vigh, 11/2015.
HISTORY
[Version : 1.0] [Date : 2015/11/30] [Author : CV]
Initial version.
**************************************************************************************************************/
/*==============================================================================================================
ProcessList class -
Retrieves the running processes.
==============================================================================================================*/
class ProcessList implements \ArrayAccess, \Countable, \IteratorAggregate
{
// Process list (array of Process objects)
protected $Processes ;
// True if we are running on a Windows platform
protected $IsWindows ;
/*--------------------------------------------------------------------------------------------------------------
Constructor -
Instanciate a ProcessList object and optionally get the running process list if the $load parameter is
true.
*-------------------------------------------------------------------------------------------------------------*/
public function __construct ( $load = true )
{
if ( ! strncasecmp ( php_uname ( 's' ), 'windows', 7 ) )
$this -> IsWindows = true ;
else
$this -> IsWindows = false ;
if ( $load )
$this -> Refresh ( ) ;
}
/*--------------------------------------------------------------------------------------------------------------
GetProcess -
Gets a process entry by its process id.
*-------------------------------------------------------------------------------------------------------------*/
public function GetProcess ( $id )
{
foreach ( $this -> Processes as $process )
{
if ( $process -> ProcessId == $id )
return ( $process ) ;
}
return ( false ) ;
}
/*--------------------------------------------------------------------------------------------------------------
GetProcessByName -
Gets a process entry by its name.
Returns an array since it can match several running processes.
*-------------------------------------------------------------------------------------------------------------*/
public function GetProcessByName ( $name )
{
$result = [] ;
foreach ( $this -> Processes as $process )
{
if ( $process -> Command == $name )
$result [] = $process ;
}
return ( $result ) ;
}
/*--------------------------------------------------------------------------------------------------------------
GetChildren -
Gets process children.
*-------------------------------------------------------------------------------------------------------------*/
public function GetChildren ( $id )
{
$result = [] ;
foreach ( $this -> Processes as $process )
{
if ( $process -> ParentProcessId == $id )
$result [] = $process ;
}
return ( $result ) ;
}
/*--------------------------------------------------------------------------------------------------------------
Refresh -
Refreshes the process list.
*-------------------------------------------------------------------------------------------------------------*/
public function Refresh ( )
{
$this -> Processes = [] ;
if ( $this -> IsWindows )
$this -> WindowsPs ( ) ;
else
$this -> UnixPs ( ) ;
}
/*--------------------------------------------------------------------------------------------------------------
Interfaces implementations.
*-------------------------------------------------------------------------------------------------------------*/
// Countable interface
public function Count ( )
{ return ( count ( $this -> Processes ) ) ; }
// IteratorAggregate interface
public function getIterator ( )
{ return ( new \ArrayIterator ( $this -> Processes ) ) ; }
// ArrayAccess interface
public function offsetExists ( $offset )
{ return ( $offset >= 0 && $offset < count ( $this -> Processes ) ) ; }
public function offsetGet ( $offset )
{ return ( $this -> Processes [ $offset ] ) ; }
public function offsetSet ( $offset, $member )
{ throw ( new \Exception ( "Unsupported operation.") ) ; }
public function offsetUnset ( $offset )
{ throw ( new \Exception ( "Unsupported operation.") ) ; }
/*--------------------------------------------------------------------------------------------------------------
Protected functions.
*-------------------------------------------------------------------------------------------------------------*/
// WindowsPs -
// Retrieves the process list on Windows platforms.
protected function WindowsPs ( )
{
$wmi = new Wmi ( ) ;
$processes = $wmi -> QueryInstances ( 'Win32_Process' ) ;
foreach ( $processes as $winprocess )
{
$winprocess -> GetOwner ( $user, $domain ) ;
$user = "$domain/$user" ;
$pid = $winprocess -> ProcessId ;
$ppid = $winprocess -> ParentProcessId ;
$tty = '?' ;
$ctime = $winprocess -> CreationDate ;
$start_time = substr ( $ctime, 0, 4 ) . '-' .
substr ( $ctime, 4, 2 ) . '-' .
substr ( $ctime, 6, 2 ) . ' ' .
substr ( $ctime, 8, 2 ) . ':' .
substr ( $ctime, 10, 2 ) . ':' .
substr ( $ctime, 12, 2 ) ;
$seconds = $winprocess -> KernelModeTime + $winprocess -> UserModeTime ;
$seconds = ( integer ) ( $seconds / ( 10 * 1000 * 1000 ) ) ;
$hours = ( integer ) ( $seconds / 3600 ) ;
$seconds -= $hours * 3600 ;
$minutes = ( integer ) ( $seconds / 60 ) ;
$seconds -= $minutes * 60 ;
$process = new Process ( $winprocess -> CommandLine, $winprocess -> Caption, $this -> IsWindows ) ;
$process -> User = $user ;
$process -> ProcessId = $pid ;
$process -> ParentProcessId = $ppid ;
$process -> StartTime = $start_time ;
$process -> CpuTime = sprintf ( '%02d', $hours ) . ':' .
sprintf ( '%02d', $minutes ) . ':' .
sprintf ( '%02d', $seconds ) ;
$process -> Tty = $tty ;
$this -> Processes [] = $process ;
}
}
// UnixPs -
// Retrieves the process list on Unix platforms.
protected function UnixPs ( )
{
exec ( "ps -aefwwww", $output, $status ) ;
$count = count ( $output ) ;
for ( $i = 1 ; $i < $count ; $i ++ )
{
$line = trim ( $output [$i] ) ;
$columns = String::SplitFields ( $line, 6 ) ;
$process = new Process ( $columns [7], null, $this -> IsWindows ) ;
if ( preg_match ( '/\d+:\d+/', $columns [4] ) )
$start_time = date ( 'Y-m-d H:i:s', strtotime ( $columns [4] ) ) ;
else
$start_time = date ( 'Y-m-d', strtotime ( $columns [4] ) ) . ' ??:??:??' ;
$process -> User = $columns [0] ;
$process -> ProcessId = $columns [1] ;
$process -> ParentProcessId = $columns [2] ;
$process -> StartTime = $start_time ;
$process -> CpuTime = $columns [5] ;
$process -> Tty = $columns [6] ;
$this -> Processes [] = $process ;
}
}
}
/*==============================================================================================================
Process -
Holds information about a given process.
==============================================================================================================*/
class Process // extends Object
{
// Process-related data
public $User ;
public $ProcessId ;
public $ParentProcessId ;
public $StartTime ;
public $CpuTime ;
public $Tty ;
// Command-line related properties
public $Command = '' ; // Command name, without its path
public $CommandLine ; // Full command line
public $Title ; // Caption on Windows, process name on Unix
public $Argv ; // An argv array, with argv[0] being the command path
public function __construct ( $command, $process_name = false, $is_windows = false )
{
$this -> Argv = $this -> ToArgv ( $command, false, $is_windows ) ;
if ( count ( $this -> Argv ) )
$this -> Command = pathinfo ( $this -> Argv [0], PATHINFO_FILENAME ) ;
$this -> CommandLine = $command ;
$this -> Title = ( $process_name ) ? $process_name : $this -> Command ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
ToArgv - Converts a command-line string to an argv array.
PROTOTYPE
$argv = Convert::ToArgv ( $str, $argv0 = false ) ;
DESCRIPTION
Converts the specified string, which represents a command line, to an argv array.
Quotes can be used to protect individual arguments from being split and are removed from the argument.
PARAMETERS
$str (string) -
Command-line string to be parsed.
$argv0 (string) -
Normally, the first element of a $argv array is the program name. $argv0 allows to specify a
program name if the supplied command-line contains only arguments.
RETURN VALUE
Returns an array containing the arguments.
*-------------------------------------------------------------------------------------------------------------*/
protected function ToArgv ( $str, $argv0 = false, $is_windows = false )
{
$argv = [] ;
if ( $argv0 )
$argv [] = $argv0 ;
$length = strlen ( $str ) ;
$in_quote = false ;
$param = '' ;
// Loop through input string characters
for ( $i = 0 ; $i < $length ; $i ++ )
{
$ch = $str [$i] ;
switch ( $ch )
{
// Backslash : escape sequence - only interpret a few special characters
case '\\' :
if ( ! $is_windows && $i + 1 < $length )
{
$ch2 = $str [++$i] ;
switch ( $ch2 )
{
case 'n' : $param .= "\n" ; break ;
case 't' : $param .= "\t" ; break ;
case 'r' : $param .= "\r" ; break ;
case 'v' : $param .= "\v" ; break ;
default : $param .= $ch2 ;
}
}
else
$param .= '\\' ;
break ;
// Space - this terminates the current parameter, if we are not in a quoted string
case ' ' :
case "\t" :
case "\n" :
case "\r" :
if ( $in_quote )
$param .= $ch ;
else if ( $param )
{
$argv [] = $param ;
$param = '' ;
}
break ;
// A quote - Either the start or the end of a quoted value
case '"' :
case "'" :
if ( $in_quote ) // We started a quoted string
{
if ( $in_quote == $ch ) // This quoted string started with the same character as the current one
$in_quote = false ;
else // This quoted string started with a different character
$param .= $ch ;
}
else // We are not in a quoted string, so say that one quoted string has started
$in_quote = $ch ;
break ;
// Other : just append the current character to the current parameter
default :
$param .= $ch ;
}
}
// Check for last parameter
if ( $param )
$argv [] = $param ;
// All done, return
return ( $argv ) ;
}
}