-
Notifications
You must be signed in to change notification settings - Fork 1
/
WindowsInformation.php
143 lines (132 loc) · 4.32 KB
/
WindowsInformation.php
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
<?php
class WindowsInformation {
public $com;
protected $localeCache;
public function __construct() {
if (extension_loaded('com') || extension_loaded('com_dotnet'))
$this->com = new COM('winmgmts:{impersonationLevel=impersonate}');
else
$this->com = false;
}
public function processorsNumber() {
if ($this->com)
{
$cpus = $this->com->execquery('SELECT NumberOfLogicalProcessors FROM Win32_Processor');
foreach ($cpus as $cpu) return $cpu->NumberOfLogicalProcessors;
}
else
{
exec('wmic cpu get NumberOfLogicalProcessors', $p);
return intval($p[1]);
}
}
public function processorLoad() {
if ($this->com)
{
// echo 'inner start: '.($st = microtime(true)).PHP_EOL;
$cpus = $this->com->execquery('SELECT LoadPercentage FROM Win32_Processor');
foreach ($cpus as $cpu) { $load = $cpu->LoadPercentage; break; }
// echo 'inner end: '.(microtime(true)).PHP_EOL;
return $load;
}
else
{
exec('wmic cpu get LoadPercentage', $p);
array_shift($p);
return array_sum($p);
}
// exec('typeperf -sc 1 "'.$_ENV['typeperfCounter'].'"', $p);
// $line = explode(',', $p[2]);
// $load = trim($line[1], '"');
// return $load;
}
public function totalMemory() {
if ($this->com)
{
$cs = $this->com->execquery('SELECT TotalPhysicalMemory FROM Win32_ComputerSystem');
foreach ($cs as $cs) return $cs->TotalPhysicalMemory;
}
else
{
exec('wmic computersystem get TotalPhysicalMemory', $p);
return floatval($p[1]);
}
}
public function freeMemory() {
if ($this->com)
{
$os = $this->com->execquery('SELECT FreeVirtualMemory FROM Win32_OperatingSystem');
foreach ($os as $os) return $os->FreeVirtualMemory;
}
else
{
exec('wmic os get FreeVirtualMemory', $p);
return floatval($p[1]);
}
}
public function swapSize() {
if ($this->com)
{
$os = $this->com->execquery('SELECT TotalSwapSpaceSize FROM Win32_OperatingSystem');
foreach ($os as $os) return ($os->TotalSwapSpaceSize === NULL ? 0 : $os->TotalSwapSpaceSize);
}
else
{
exec('wmic os get TotalSwapSpaceSize', $p);
array_shift($p);
return array_sum($p);
}
}
public function freeSwapSize() {
if ($this->com)
{
$os = $this->com->execquery('SELECT FreeSpaceInPagingFiles FROM Win32_OperatingSystem');
foreach ($os as $os) return $os->FreeSpaceInPagingFiles;
}
else
{
exec('wmic os get FreeSpaceInPagingFiles', $p);
return floatval($p[1]);
}
}
public function tasksNumber() {
if ($this->com)
{
$p = $this->com->execquery('SELECT LastBootUpTime FROM Win32_Process');
return count($p);
}
else
{
exec('tasklist /FO csv', $p);
return count($p) - 2;
}
}
public function runningTasksNumber() {
if ($this->com)
{
$p = $this->com->execquery('SELECT LastBootUpTime FROM Win32_Process WHERE `status` = "RUNNING"');
return count($p);
}
else
{
exec('tasklist /FI "STATUS eq RUNNING" /FO csv', $p);
return count($p) - 2;
}
}
public function uptime() {
if ($this->com)
{
$os = $this->com->execquery('SELECT LastBootUpTime FROM Win32_OperatingSystem');
foreach ($os as $os) { $v = $os->LastBootUpTime; break; };
$time = mktime(substr($v, 8, 2), substr($v, 10, 2), substr($v, 12, 2), substr($v, 4, 2), substr($v, 6, 2), substr($v, 0, 4));
return time() - $time;
}
else
{
exec('wmic os get LastBootUpTime', $p);
$v = trim($p[1]);
$time = mktime(substr($v, 8, 2), substr($v, 10, 2), substr($v, 12, 2), substr($v, 4, 2), substr($v, 6, 2), substr($v, 0, 4));
return time() - $time;
}
}
}