-
Notifications
You must be signed in to change notification settings - Fork 1
/
InformationCollector.php
180 lines (164 loc) · 5.02 KB
/
InformationCollector.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
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
<?php
include_once "WindowsInformation.php";
class InformationCollector {
protected $windows = false;
protected $linux = false;
protected $memoryTotalCache = false;
public function __construct() {
if (strncasecmp(PHP_OS, 'win', 3) === 0) {
$this->windows = new WindowsInformation();
} else {
$this->linux = true;
}
}
public function collect() {
$information = array
(
'processors' => $this->collectProccessorsNumber(),
'processor_load' => $this->collectProcessorLoad(),
'memory' => array('total' => $this->collectMemoryTotal(), 'free' => $this->collectMemoryFree()),
'swap' => array('total' => $this->collectSwapTotal(), 'free' => $this->collectSwapFree()),
'tasks' => $this->collectTasksNumber(),
'uptime' => $this->collectUptime(),
);
$information['memory']['busy'] = $information['memory']['total'] - $information['memory']['free'];
$information['swap']['busy'] = $information['swap']['total'] - $information['swap']['free'];
return $information;
}
protected function collectProccessorsNumber() {
if ($this->windows)
{
//echo microtime(true).PHP_EOL;
return $this->windows->processorsNumber();
}
else
{
//echo microtime(true).PHP_EOL;
return substr_count(file_get_contents('/proc/cpuinfo'), 'processor');
}
}
protected function collectProcessorLoad() {
if ($this->windows)
{
//echo ($pl = microtime(true)).PHP_EOL;
$load = $this->windows->processorLoad();
//echo 'processor load: '.(microtime(true) - $pl).PHP_EOL;
return $load;
}
else
{
//echo microtime(true).PHP_EOL;
exec('top -bn1 | head -n3', $p);
$line = $p[2];
preg_match('~([0-9.,]+) id~', $line, $load);
return 100 - $load[1];
}
}
protected function collectMemoryTotal() {
if ($this->windows)
{
//echo microtime(true).PHP_EOL;
if ($this->memoryTotalCache === false)
return ($this->memoryTotalCache = $this->windows->totalMemory());
else
return $this->memoryTotalCache;
}
else
{
//echo microtime(true).PHP_EOL;
$meminfo = file('/proc/meminfo');
$line = explode(' ', $meminfo[0]);
return $line[count($line) - 2];
}
}
protected function collectMemoryFree() {
if ($this->windows)
{
//echo microtime(true).PHP_EOL;
return $this->windows->freeMemory();
}
else
{
//echo microtime(true).PHP_EOL;
$meminfo = file('/proc/meminfo');
$line = explode(' ', $meminfo[1]);
return $line[count($line) - 2];
}
}
protected function collectSwapTotal() {
if ($this->windows)
{
//echo microtime(true).PHP_EOL;
return $this->windows->swapSize();
}
else
{
//echo microtime(true).PHP_EOL;
$meminfo = file('/proc/meminfo');
foreach ($meminfo as $l)
{
$line = explode(' ', $l);
if ($line[0] == 'SwapTotal:')
return $line[count($line) - 2];
}
}
}
protected function collectSwapFree() {
if ($this->windows)
{
//echo microtime(true).PHP_EOL;
return $this->windows->freeSwapSize();
}
else
{
//echo microtime(true).PHP_EOL;
$meminfo = file('/proc/meminfo');
foreach ($meminfo as $l)
{
$line = explode(' ', $l);
if ($line[0] == 'SwapFree:')
return $line[count($line) - 2];
}
}
}
protected function collectTasksNumber() {
if ($this->windows)
{
//echo microtime(true).PHP_EOL;
return $this->windows->tasksNumber();
}
else
{
//echo microtime(true).PHP_EOL;
exec('ps -aux', $p);
return count($p) - 1;
}
}
protected function collectTasksRunningNumber() {
if ($this->windows)
{
//echo microtime(true).PHP_EOL;
return $this->windows->runningTasksNumber();
}
else
{
//echo microtime(true).PHP_EOL;
exec('ps -auxr', $p);
return count($p) - 1;
}
}
protected function collectUptime() {
if ($this->windows)
{
//echo microtime(true).PHP_EOL;
return $this->windows->uptime();
}
else
{
//echo microtime(true).PHP_EOL;
exec('uptime --since', $o);
$time = strtotime($o[0]);
return time() - $time;
}
}
}