|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PS Plugin |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PSI_Plugin_PS |
|
9
|
|
|
* @author Michael Cramer <[email protected]> |
|
10
|
|
|
* @copyright 2009 phpSysInfo |
|
11
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
|
12
|
|
|
* @version SVN: $Id: class.ps.inc.php 692 2012-09-08 17:12:08Z namiltd $ |
|
13
|
|
|
* @link http://phpsysinfo.sourceforge.net |
|
14
|
|
|
*/ |
|
15
|
|
|
/** |
|
16
|
|
|
* process Plugin, which displays all running processes |
|
17
|
|
|
* a simple tree view which is filled with the running processes which are determined by |
|
18
|
|
|
* calling the "ps" command line utility, another way is to provide |
|
19
|
|
|
* a file with the output of the ps utility, so there is no need to run a execute by the |
|
20
|
|
|
* webserver, the format of the command is written down in the phpsysinfo.ini file, where also |
|
21
|
|
|
* the method of getting the information is configured |
|
22
|
|
|
* |
|
23
|
|
|
* @category PHP |
|
24
|
|
|
* @package PSI_Plugin_PS |
|
25
|
|
|
* @author Michael Cramer <[email protected]> |
|
26
|
|
|
* @copyright 2009 phpSysInfo |
|
27
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
|
28
|
|
|
* @version Release: 3.0 |
|
29
|
|
|
* @link http://phpsysinfo.sourceforge.net |
|
30
|
|
|
*/ |
|
31
|
|
|
class PS extends PSI_Plugin |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* variable, which holds the content of the command |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $_filecontent = array(); |
|
38
|
|
|
/** |
|
39
|
|
|
* variable, which holds the result before the xml is generated out of this array |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $_result = array(); |
|
43
|
|
|
/** |
|
44
|
|
|
* read the data into an internal array and also call the parent constructor |
|
45
|
|
|
* |
|
46
|
|
|
* @param String $enc encoding |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($enc) |
|
49
|
|
|
{ |
|
50
|
|
|
parent::__construct(__CLASS__, $enc); |
|
51
|
|
|
switch (strtolower(PSI_PLUGIN_PS_ACCESS)) { |
|
52
|
|
|
case 'command': |
|
53
|
|
|
if (PSI_OS == 'WINNT') { |
|
54
|
|
|
try { |
|
55
|
|
|
$objLocator = new COM("WbemScripting.SWbemLocator"); |
|
|
|
|
|
|
56
|
|
|
$wmi = $objLocator->ConnectServer(); |
|
57
|
|
|
$os_wmi = $wmi->InstancesOf('Win32_OperatingSystem'); |
|
58
|
|
|
foreach ($os_wmi as $os) { |
|
59
|
|
|
$memtotal = $os->TotalVisibleMemorySize * 1024; |
|
60
|
|
|
} |
|
61
|
|
|
$process_wmi = $wmi->InstancesOf('Win32_Process'); |
|
62
|
|
|
foreach ($process_wmi as $process) { |
|
63
|
|
|
if (strlen(trim($process->CommandLine)) > 0) { |
|
64
|
|
|
$ps = trim($process->CommandLine); |
|
65
|
|
|
} else { |
|
66
|
|
|
$ps = trim($process->Caption); |
|
67
|
|
|
} |
|
68
|
|
|
if (trim($process->ProcessId) != 0) { |
|
69
|
|
|
$memusage = round(trim($process->WorkingSetSize) * 100 / $memtotal, 1); |
|
|
|
|
|
|
70
|
|
|
//ParentProcessId |
|
71
|
|
|
//Unique identifier of the process that creates a process. Process identifier numbers are reused, so they |
|
72
|
|
|
//only identify a process for the lifetime of that process. It is possible that the process identified by |
|
73
|
|
|
//ParentProcessId is terminated, so ParentProcessId may not refer to a running process. It is also |
|
74
|
|
|
//possible that ParentProcessId incorrectly refers to a process that reuses a process identifier. You can |
|
75
|
|
|
//use the CreationDate property to determine whether the specified parent was created after the process |
|
76
|
|
|
//represented by this Win32_Process instance was created. |
|
77
|
|
|
//=> subtrees of processes may be missing (WHAT TODO?!?) |
|
78
|
|
|
$this->_filecontent[] = trim($process->ProcessId)." ".trim($process->ParentProcessId)." ".$memusage." ".$ps; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} catch (Exception $e) { |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
CommonFunctions::executeProgram("ps", "axo pid,ppid,pmem,args", $buffer, PSI_DEBUG); |
|
85
|
|
|
if (((PSI_OS == 'Linux') || (PSI_OS == 'Android')) && (!preg_match("/^[^\n]+\n\s*\d+\s+\d+\s+[\d\.]+\s+.+/", $buffer))) { //alternative method if no data |
|
86
|
|
|
if (CommonFunctions::rfts('/proc/meminfo', $mbuf)) { |
|
87
|
|
|
$bufe = preg_split("/\n/", $mbuf, -1, PREG_SPLIT_NO_EMPTY); |
|
88
|
|
|
$totalmem = 0; |
|
89
|
|
|
foreach ($bufe as $buf) { |
|
90
|
|
|
if (preg_match('/^MemTotal:\s+(.*)\s*kB/i', $buf, $ar_buf)) { |
|
91
|
|
|
$totalmem = $ar_buf[1]; |
|
92
|
|
|
break; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
$buffer = " PID PPID %MEM COMMAND\n"; |
|
96
|
|
|
|
|
97
|
|
|
$processlist = glob('/proc/*/status', GLOB_NOSORT); |
|
98
|
|
|
if (($total = count($processlist)) > 0) { |
|
99
|
|
|
natsort($processlist); //first sort |
|
100
|
|
|
$prosess = array(); |
|
|
|
|
|
|
101
|
|
|
foreach ($processlist as $processitem) { //second sort |
|
102
|
|
|
$process[] = $processitem; |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$buf = ""; |
|
106
|
|
|
for ($i = 0; $i < $total; $i++) { |
|
107
|
|
|
if (CommonFunctions::rfts($process[$i], $buf, 0, 4096, false)) { |
|
108
|
|
|
|
|
109
|
|
|
if (($totalmem != 0) && (preg_match('/^VmRSS:\s+(\d+)\s+kB/m', $buf, $tmppmem))) { |
|
110
|
|
|
$pmem = round(100 * $tmppmem[1] / $totalmem, 1); |
|
111
|
|
|
} else { |
|
112
|
|
|
$pmem = 0; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$name = null; |
|
116
|
|
|
if (CommonFunctions::rfts(substr($process[$i], 0, strlen($process[$i])-6)."cmdline", $namebuf, 0, 4096, false)) { |
|
|
|
|
|
|
117
|
|
|
$name = str_replace(chr(0), ' ', trim($namebuf)); |
|
118
|
|
|
} |
|
119
|
|
|
if (preg_match('/^Pid:\s+(\d+)/m', $buf, $tmppid) && |
|
120
|
|
|
preg_match('/^PPid:\s+(\d+)/m', $buf, $tmpppid) && |
|
121
|
|
|
preg_match('/^Name:\s+(.+)/m', $buf, $tmpargs)) { |
|
122
|
|
|
$pid = $tmppid[1]; |
|
123
|
|
|
$ppid = $tmpppid[1]; |
|
124
|
|
|
$args = $tmpargs[1]; |
|
125
|
|
|
if ($name !== null) { |
|
126
|
|
|
if ($name !== "") { |
|
127
|
|
|
$args = $name; |
|
128
|
|
|
} else { |
|
129
|
|
|
$args = "[".$args."]"; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
$buffer .= $pid." ".$ppid." ".$pmem." ".$args."\n"; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
break; |
|
142
|
|
|
case 'data': |
|
143
|
|
|
CommonFunctions::rfts(APP_ROOT."/data/ps.txt", $buffer); |
|
144
|
|
|
break; |
|
145
|
|
|
default: |
|
146
|
|
|
$this->global_error->addConfigError("__construct()", "PSI_PLUGIN_PS_ACCESS"); |
|
147
|
|
|
break; |
|
148
|
|
|
} |
|
149
|
|
|
if (PSI_OS != 'WINNT') { |
|
150
|
|
|
if (trim($buffer) != "") { |
|
151
|
|
|
$this->_filecontent = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY); |
|
|
|
|
|
|
152
|
|
|
unset($this->_filecontent[0]); |
|
153
|
|
|
} else { |
|
154
|
|
|
$this->_filecontent = array(); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
/** |
|
159
|
|
|
* doing all tasks to get the required informations that the plugin needs |
|
160
|
|
|
* result is stored in an internal array<br>the array is build like a tree, |
|
161
|
|
|
* so that it is possible to get only a specific process with the childs |
|
162
|
|
|
* |
|
163
|
|
|
* @return void |
|
164
|
|
|
*/ |
|
165
|
|
|
public function execute() |
|
166
|
|
|
{ |
|
167
|
|
|
if (empty($this->_filecontent)) { |
|
168
|
|
|
return; |
|
169
|
|
|
} |
|
170
|
|
|
$items = array(); |
|
171
|
|
|
foreach ($this->_filecontent as $roworig) { |
|
172
|
|
|
$row = preg_split("/[\s]+/", trim($roworig), 4); |
|
173
|
|
|
if (count($row) != 4) { |
|
174
|
|
|
break; |
|
175
|
|
|
} |
|
176
|
|
|
foreach ($row as $key=>$val) { |
|
177
|
|
|
$items[$row[0]][$key] = $val; |
|
178
|
|
|
} |
|
179
|
|
|
if ($row[1] !== $row[0]) { |
|
180
|
|
|
$items[$row[1]]['childs'][$row[0]] = &$items[$row[0]]; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
foreach ($items as $item) { //find zombie |
|
184
|
|
|
if (!isset($item[0])) { |
|
185
|
|
|
foreach ($item["childs"] as $subitem) { |
|
186
|
|
|
$zombie = $subitem[1]; |
|
187
|
|
|
if ($zombie != 0) { |
|
188
|
|
|
$items[$zombie]["0"] = $zombie; |
|
189
|
|
|
$items[$zombie]["1"] = "0"; |
|
190
|
|
|
$items[$zombie]["2"] = "0"; |
|
191
|
|
|
$items[$zombie]["3"] = "unknown"; |
|
192
|
|
|
$items[0]['childs'][$zombie] = &$items[$zombie]; |
|
193
|
|
|
} |
|
194
|
|
|
break; //first is sufficient |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
if (isset($items[0])) { |
|
199
|
|
|
$this->_result = $items[0]; |
|
200
|
|
|
} else { |
|
201
|
|
|
$_result = array(); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
/** |
|
205
|
|
|
* generates the XML content for the plugin |
|
206
|
|
|
* |
|
207
|
|
|
* @return SimpleXMLElement entire XML content for the plugin |
|
208
|
|
|
*/ |
|
209
|
|
|
public function xml() |
|
210
|
|
|
{ |
|
211
|
|
|
if ($this->_result) { |
|
|
|
|
|
|
212
|
|
|
$positions = array(0=>0); |
|
213
|
|
|
$this->_addchild($this->_result['childs'], $this->xml, $positions); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $this->xml->getSimpleXmlElement(); |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
/** |
|
219
|
|
|
* recursive function to allow appending child processes to a parent process |
|
220
|
|
|
* |
|
221
|
|
|
* @param Array $child part of the array which should be appended to the XML |
|
222
|
|
|
* @param SimpleXMLExtended $xml XML-Object to which the array content is appended |
|
223
|
|
|
* @param Array &$positions array with parent positions in xml structure |
|
224
|
|
|
* |
|
225
|
|
|
* @return SimpleXMLExtended Object with the appended array content |
|
226
|
|
|
*/ |
|
227
|
|
|
private function _addchild($child, SimpleXMLExtended $xml, &$positions) |
|
228
|
|
|
{ |
|
229
|
|
|
foreach ($child as $key=>$value) { |
|
230
|
|
|
$xmlnode = $xml->addChild("Process"); |
|
231
|
|
|
if (isset($value[0])) { |
|
232
|
|
|
array_push($positions, $value[0]); |
|
233
|
|
|
$xmlnode->addAttribute('PID', $value[0]); |
|
234
|
|
|
$parentid = array_search($value[1], $positions); |
|
235
|
|
|
$xmlnode->addAttribute('ParentID', $parentid); |
|
236
|
|
|
$xmlnode->addAttribute('PPID', $value[1]); |
|
237
|
|
|
$xmlnode->addAttribute('MemoryUsage', $value[2]); |
|
238
|
|
|
$xmlnode->addAttribute('Name', $value[3]); |
|
239
|
|
|
if (PSI_OS !== 'WINNT') { |
|
240
|
|
|
if ($parentid === 1) { |
|
241
|
|
|
$xmlnode->addAttribute('Expanded', 0); |
|
242
|
|
|
} |
|
243
|
|
|
if (defined('PSI_PLUGIN_PS_SHOW_KTHREADD_EXPANDED') && (PSI_PLUGIN_PS_SHOW_KTHREADD_EXPANDED === false) && ($value[3] === "[kthreadd]")) { |
|
244
|
|
|
$xmlnode->addAttribute('Expanded', 0); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
if (isset($value['childs'])) { |
|
249
|
|
|
$this->_addChild($value['childs'], $xml, $positions); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return $xml; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.