public function __construct($enc)
{
parent::__construct(__CLASS__, $enc);
switch (strtolower(PSI_PLUGIN_PS_ACCESS)) {
case 'command':
if (PSI_OS == 'WINNT') {
try {
$objLocator = new COM('WbemScripting.SWbemLocator');
$wmi = $objLocator->ConnectServer('', 'root\\CIMv2');
$os_wmi = CommonFunctions::getWMI($wmi, 'Win32_OperatingSystem', array('TotalVisibleMemorySize'));
foreach ($os_wmi as $os) {
$memtotal = $os['TotalVisibleMemorySize'] * 1024;
}
$process_wmi = CommonFunctions::getWMI($wmi, 'Win32_Process', array('Caption', 'CommandLine', 'ProcessId', 'ParentProcessId', 'WorkingSetSize'));
foreach ($process_wmi as $process) {
if (strlen(trim($process['CommandLine'])) > 0) {
$ps = trim($process['CommandLine']);
} else {
$ps = trim($process['Caption']);
}
if (trim($process['ProcessId']) != 0) {
$memusage = round(trim($process['WorkingSetSize']) * 100 / $memtotal, 1);
//ParentProcessId
//Unique identifier of the process that creates a process. Process identifier numbers are reused, so they
//only identify a process for the lifetime of that process. It is possible that the process identified by
//ParentProcessId is terminated, so ParentProcessId may not refer to a running process. It is also
//possible that ParentProcessId incorrectly refers to a process that reuses a process identifier. You can
//use the CreationDate property to determine whether the specified parent was created after the process
//represented by this Win32_Process instance was created.
//=> subtrees of processes may be missing (WHAT TODO?!?)
$this->_filecontent[] = trim($process['ProcessId']) . " " . trim($process['ParentProcessId']) . " " . $memusage . " " . $ps;
}
}
} catch (Exception $e) {
}
} else {
CommonFunctions::executeProgram("ps", "axo pid,ppid,pmem,args", $buffer, PSI_DEBUG);
if ((PSI_OS == 'Linux' || PSI_OS == 'Android') && !preg_match("/^[^\n]+\n\\s*\\d+\\s+\\d+\\s+[\\d\\.]+\\s+.+/", $buffer)) {
//alternative method if no data
if (CommonFunctions::rfts('/proc/meminfo', $mbuf)) {
$bufe = preg_split("/\n/", $mbuf, -1, PREG_SPLIT_NO_EMPTY);
$totalmem = 0;
foreach ($bufe as $buf) {
if (preg_match('/^MemTotal:\\s+(.*)\\s*kB/i', $buf, $ar_buf)) {
$totalmem = $ar_buf[1];
break;
}
}
$buffer = " PID PPID %MEM COMMAND\n";
$processlist = glob('/proc/*/status', GLOB_NOSORT);
if (($total = count($processlist)) > 0) {
natsort($processlist);
//first sort
$prosess = array();
foreach ($processlist as $processitem) {
//second sort
$process[] = $processitem;
}
$buf = "";
for ($i = 0; $i < $total; $i++) {
if (CommonFunctions::rfts($process[$i], $buf, 0, 4096, false)) {
if ($totalmem != 0 && preg_match('/^VmRSS:\\s+(\\d+)\\s+kB/m', $buf, $tmppmem)) {
$pmem = round(100 * $tmppmem[1] / $totalmem, 1);
} else {
$pmem = 0;
}
$name = null;
if (CommonFunctions::rfts(substr($process[$i], 0, strlen($process[$i]) - 6) . "cmdline", $namebuf, 0, 4096, false)) {
$name = str_replace(chr(0), ' ', trim($namebuf));
}
if (preg_match('/^Pid:\\s+(\\d+)/m', $buf, $tmppid) && preg_match('/^PPid:\\s+(\\d+)/m', $buf, $tmpppid) && preg_match('/^Name:\\s+(.+)/m', $buf, $tmpargs)) {
$pid = $tmppid[1];
$ppid = $tmpppid[1];
$args = $tmpargs[1];
if ($name !== null) {
if ($name !== "") {
$args = $name;
} else {
$args = "[" . $args . "]";
}
}
$buffer .= $pid . " " . $ppid . " " . $pmem . " " . $args . "\n";
}
}
}
}
}
}
}
break;
case 'data':
CommonFunctions::rfts(APP_ROOT . "/data/ps.txt", $buffer);
break;
default:
$this->global_error->addConfigError("__construct()", "PSI_PLUGIN_PS_ACCESS");
break;
}
if (PSI_OS != 'WINNT') {
if (trim($buffer) != "") {
$this->_filecontent = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY);
unset($this->_filecontent[0]);
} else {
$this->_filecontent = array();
}
}
}