Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezsysinfo.php
Go to the documentation of this file.
1 <?php
31 class eZSysInfo
32 {
41 function attributes()
42 {
43 return array( 'is_valid',
44 'cpu_type',
45 'cpu_unit',
46 'cpu_speed',
47 'memory_size' );
48 }
49
54 function hasAttribute( $name )
55 {
56 return in_array( $name, $this->attributes() );
57 }
58
63 function attribute( $name )
64 {
65 if ( $name == 'is_valid' )
66 return $this->IsValid;
67 else if ( $name == 'cpu_type' )
68 return $this->CPUType;
69 else if ( $name == 'cpu_unit' )
70 return $this->CPUUnit;
71 else if ( $name == 'cpu_speed' )
72 return $this->CPUSpeed;
73 else if ( $name == 'memory_size' )
74 return $this->MemorySize;
75 else
76 {
77 eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
78 return null;
79 }
80 }
81
85 function isValid()
86 {
87 return $this->IsValid;
88 }
89
95 function cpuType()
96 {
97 return $this->CPUType;
98 }
99
106 function cpuSpeed()
107 {
108 return $this->CPUSpeed;
109 }
110
116 function memorySize()
117 {
118 return $this->MemorySize;
119 }
120
125 function scan()
126 {
127 $this->IsValid = false;
128 $this->CPUSpeed = false;
129 $this->CPUType = false;
130 $this->CPUUnit = false;
131 $this->MemorySize = false;
132
133 $sys = eZSys::instance();
134 $osType = $sys->osType();
135
136 if ( $osType == 'win32' )
137 {
138 // Windows (win32) is not supported yet
139 // eZDebug::writeWarning( "System scan for Windows (win32) machines not supported yet" );
140 }
141 else if ( $osType == 'mac' )
142 {
143 // Mac means FreeBSD type of structure?
144 $this->IsValid = $this->scanDMesg();
145 return $this->IsValid;
146 }
147 else if ( $osType == 'unix' )
148 {
149 // Now determine specific 'Unix' type
150 $osName = $sys->osName();
151 if ( $osName == 'linux' )
152 {
153 $this->IsValid = $this->scanProc();
154 return $this->IsValid;
155 }
156 else if ( $osName == 'freebsd' )
157 {
158 $this->IsValid = $this->scanDMesg();
159 return $this->IsValid;
160 }
161 else
162 {
163 // Not known so we just try the various scanners
164 //
165 // /proc for Linux systems
166 if ( $this->scanProc() )
167 {
168 $this->IsValid = true;
169 return true;
170 }
171 // dmesg.boot for FreeBSD systems
172 if ( $this->scanDMesg() )
173 {
174 $this->IsValid = true;
175 return true;
176 }
177 }
178 }
179 return false;
180 }
181
190 function scanProc( $cpuinfoPath = false, $meminfoPath = false )
191 {
192 if ( !$cpuinfoPath )
193 $cpuinfoPath = '/proc/cpuinfo';
194 if ( !$meminfoPath )
195 $meminfoPath = '/proc/meminfo';
196
197 if ( !file_exists( $cpuinfoPath ) )
198 return false;
199 if ( !file_exists( $meminfoPath ) )
200 return false;
201
202 $fileLines = file( $cpuinfoPath );
203 foreach ( $fileLines as $line )
204 {
205 if ( substr( $line, 0, 7 ) == 'cpu MHz' )
206 {
207 $cpu = trim( substr( $line, 11, strlen( $line ) - 11 ) );
208 $this->CPUSpeed = $cpu;
209 $this->CPUUnit = 'MHz';
210 }
211 if ( substr( $line, 0, 10 ) == 'model name' )
212 {
213 $system = trim( substr( $line, 13, strlen( $line ) - 13 ) );
214 $this->CPUType = $system;
215 }
216 if ( $this->CPUSpeed !== false and
217 $this->CPUType !== false and
218 $this->CPUUnit !== false )
219 break;
220 }
221
222 $fileLines = file( $meminfoPath );
223 foreach ( $fileLines as $line )
224 {
225 if ( substr( $line, 0, 8 ) == 'MemTotal' )
226 {
227 $mem = trim( substr( $line, 11, strlen( $line ) - 11 ) );
228 $memBytes = $mem;
229 if ( preg_match( "#^([0-9]+) *([a-zA-Z]+)#", $mem, $matches ) )
230 {
231 $memBytes = (int)$matches[1];
232 $unit = strtolower( $matches[2] );
233 if ( $unit == 'kb' )
234 {
235 $memBytes *= 1024;
236 }
237 else if ( $unit == 'mb' )
238 {
239 $memBytes *= 1024*1024;
240 }
241 else if ( $unit == 'gb' )
242 {
243 $memBytes *= 1024*1024*1024;
244 }
245 }
246 else
247 {
248 $memBytes = (int)$memBytes;
249 }
250 $this->MemorySize = $memBytes;
251 }
252 if ( $this->MemorySize !== false )
253 break;
254 }
255
256 return true;
257 }
258
265 function scanDMesg( $dmesgPath = false )
266 {
267 if ( !$dmesgPath )
268 $dmesgPath = '/var/run/dmesg.boot';
269 if ( !file_exists( $dmesgPath ) )
270 return false;
271 $fileLines = file( $dmesgPath );
272 foreach ( $fileLines as $line )
273 {
274 if ( substr( $line, 0, 3 ) == 'CPU' )
275 {
276 $system = trim( substr( $line, 4, strlen( $line ) - 4 ) );
277 $cpu = false;
278 $cpuunit = false;
279 if ( preg_match( "#^(.+)\\((.+)(MHz) +([^)]+)\\)#", $system, $matches ) )
280 {
281 $system = trim( $matches[1] ) . ' (' . trim( $matches[4] ) . ')';
282 $cpu = $matches[2];
283 $cpuunit = $matches[3];
284 }
285 $this->CPUSpeed = $cpu;
286 $this->CPUType = $system;
287 $this->CPUUnit = $cpuunit;
288 }
289 if ( substr( $line, 0, 11 ) == 'real memory' )
290 {
291 $mem = trim( substr( $line, 12, strlen( $line ) - 12 ) );
292 $memBytes = $mem;
293 if ( preg_match( "#^= *([0-9]+)#", $mem, $matches ) )
294 {
295 $memBytes = $matches[1];
296 }
297 $memBytes = (int)$memBytes;
298 $this->MemorySize = $memBytes;
299 }
300 if ( $this->CPUSpeed !== false and
301 $this->CPUType !== false and
302 $this->CPUUnit !== false and
303 $this->MemorySize !== false )
304 break;
305
306 }
307 return true;
308 }
309
311 public $IsValid = false;
312 public $CPUSpeed = false;
313 public $CPUType = false;
314 public $CPUUnit = false;
315 public $MemorySize = false;
316 }
317
318 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZSysInfo
Provides common information on the running system.
Definition: ezsysinfo.php:32
eZSysInfo\scan
scan()
Definition: ezsysinfo.php:125
eZSysInfo\$IsValid
$IsValid
Definition: ezsysinfo.php:311
eZSysInfo\hasAttribute
hasAttribute( $name)
Definition: ezsysinfo.php:54
eZSysInfo\$CPUType
$CPUType
Definition: ezsysinfo.php:313
eZSysInfo\scanProc
scanProc( $cpuinfoPath=false, $meminfoPath=false)
Definition: ezsysinfo.php:190
eZSysInfo\cpuSpeed
cpuSpeed()
Definition: ezsysinfo.php:106
eZSysInfo\isValid
isValid()
Definition: ezsysinfo.php:85
eZSysInfo\attribute
attribute( $name)
Definition: ezsysinfo.php:63
eZSysInfo\attributes
attributes()
Definition: ezsysinfo.php:41
eZSysInfo\$MemorySize
$MemorySize
Definition: ezsysinfo.php:315
eZSysInfo\scanDMesg
scanDMesg( $dmesgPath=false)
Definition: ezsysinfo.php:265
eZSysInfo\cpuType
cpuType()
Definition: ezsysinfo.php:95
eZSysInfo\$CPUSpeed
$CPUSpeed
Definition: ezsysinfo.php:312
eZSysInfo\memorySize
memorySize()
Definition: ezsysinfo.php:116
eZSysInfo\$CPUUnit
$CPUUnit
Definition: ezsysinfo.php:314
eZSys\instance
static instance()
Definition: ezsys.php:1272
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.