|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZSysInfo class 00004 // 00005 // Created on: <29-Sep-2004 12:43:57 jb> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! \file ezsysinfo.php 00032 */ 00033 00034 /*! 00035 \class eZSysInfo ezsysinfo.php 00036 \brief Provides common information on the running system 00037 00038 The following information can be queried: 00039 - CPU Type (e.g Pentium) - cpuType() 00040 - CPU Speed (e.g 1000) - cpuSpeed() 00041 - CPU Unit (e.g. MHz) - cpuUnit() 00042 - Memory Size in bytes (e.g. 528424960) - memorySize() 00043 00044 \code 00045 $info = new eZSysInfo(); 00046 $info->scan(); 00047 print( $info->cpuType() . "\n" ); 00048 \endcode 00049 00050 \note This class supports the 'attribute' system and be used directly as a template variable. 00051 \note It uses eZSys to figure out the OS type. 00052 */ 00053 00054 //include_once( 'lib/ezutils/classes/ezsys.php' ); 00055 00056 class eZSysInfo 00057 { 00058 /*! 00059 Constructor 00060 */ 00061 function eZSysInfo() 00062 { 00063 } 00064 00065 /*! 00066 \return An array with available attributes. 00067 The available attributes: 00068 - cpu_type - cpuType() 00069 - cpu_unit - cpuUnit() 00070 - cpu_speed - cpuSpeed() 00071 - memory_size - memorySize() 00072 */ 00073 function attributes() 00074 { 00075 return array( 'is_valid', 00076 'cpu_type', 00077 'cpu_unit', 00078 'cpu_speed', 00079 'memory_size' ); 00080 } 00081 00082 /*! 00083 \return \c true if the attribute named \a $name exists. 00084 See attributes() for a list of available attributes. 00085 */ 00086 function hasAttribute( $name ) 00087 { 00088 return in_array( $name, $this->attributes() ); 00089 } 00090 00091 /*! 00092 \return The value of the attribute named \a $name, or \c null if it does not exist. 00093 See attributes() for a list of available attributes. 00094 */ 00095 function attribute( $name ) 00096 { 00097 if ( $name == 'is_valid' ) 00098 return $this->IsValid; 00099 else if ( $name == 'cpu_type' ) 00100 return $this->CPUType; 00101 else if ( $name == 'cpu_unit' ) 00102 return $this->CPUUnit; 00103 else if ( $name == 'cpu_speed' ) 00104 return $this->CPUSpeed; 00105 else if ( $name == 'memory_size' ) 00106 return $this->MemorySize; 00107 else 00108 { 00109 eZDebug::writeError( "Attribute '$name' does not exist", 'eZSysInfo::attribute' ); 00110 return null; 00111 } 00112 } 00113 00114 /*! 00115 \return \c true if the system has been scanned correctly. 00116 */ 00117 function isValid() 00118 { 00119 return $this->IsValid; 00120 } 00121 00122 /*! 00123 Contains the type of CPU, the type is taken directly from the OS 00124 and can vary a lot. 00125 \return The type as a string or \c false if no type was found. 00126 */ 00127 function cpuType() 00128 { 00129 return $this->CPUType; 00130 } 00131 00132 /*! 00133 Contains the speed of CPU, the type is taken directly from the OS 00134 and can vary a lot. The speed is just a number so use cpuUnit() 00135 to get the proper unit (e.g MHz). 00136 \return The speed as a string or \c false if no type was found. 00137 */ 00138 function cpuSpeed() 00139 { 00140 return $this->CPUSpeed; 00141 } 00142 00143 /*! 00144 Contains the amount of system memory the OS has, the value is 00145 in bytes. 00146 \return The type as a number \c false if no type was found. 00147 */ 00148 function memorySize() 00149 { 00150 return $this->MemorySize; 00151 } 00152 00153 /*! 00154 Scans the system depending on the OS and fills in the information internally. 00155 \return \c true if it was able to scan the system or \c false if it failed. 00156 */ 00157 function scan() 00158 { 00159 $this->IsValid = false; 00160 $this->CPUSpeed = false; 00161 $this->CPUType = false; 00162 $this->CPUUnit = false; 00163 $this->MemorySize = false; 00164 00165 $sys = eZSys::instance(); 00166 $osType = $sys->osType(); 00167 00168 if ( $osType == 'win32' ) 00169 { 00170 // Windows (win32) is not supported yet 00171 // eZDebug::writeWarning( "System scan for Windows (win32) machines not supported yet" ); 00172 } 00173 else if ( $osType == 'mac' ) 00174 { 00175 // Mac means FreeBSD type of structure? 00176 $this->IsValid = $this->scanDMesg(); 00177 return $this->IsValid; 00178 } 00179 else if ( $osType == 'unix' ) 00180 { 00181 // Now determine specific 'Unix' type 00182 $osName = $sys->osName(); 00183 if ( $osName == 'linux' ) 00184 { 00185 $this->IsValid = $this->scanProc(); 00186 return $this->IsValid; 00187 } 00188 else if ( $osName == 'freebsd' ) 00189 { 00190 $this->IsValid = $this->scanDMesg(); 00191 return $this->IsValid; 00192 } 00193 else 00194 { 00195 // Not known so we just try the various scanners 00196 // 00197 // /proc for Linux systems 00198 if ( $this->scanProc() ) 00199 { 00200 $this->IsValid = true; 00201 return true; 00202 } 00203 // dmesg.boot for FreeBSD systems 00204 if ( $this->scanDMesg() ) 00205 { 00206 $this->IsValid = true; 00207 return true; 00208 } 00209 } 00210 } 00211 return false; 00212 } 00213 00214 /*! 00215 \private 00216 Scans the /proc/cpuinfo and /proc/meminfo files for CPU 00217 and memory information. 00218 If this files are unavailable or could not be read it will return \c false. 00219 \param $cpuinfoPath The path to the cpuinfo file, if \c false it uses '/proc/cpuinfo' which should be sufficient. 00220 \param $meminfoPath The path to the meminfo file, if \c false it uses '/proc/meminfo' which should be sufficient. 00221 */ 00222 function scanProc( $cpuinfoPath = false, $meminfoPath = false ) 00223 { 00224 if ( !$cpuinfoPath ) 00225 $cpuinfoPath = '/proc/cpuinfo'; 00226 if ( !$meminfoPath ) 00227 $meminfoPath = '/proc/meminfo'; 00228 00229 if ( !file_exists( $cpuinfoPath ) ) 00230 return false; 00231 if ( !file_exists( $meminfoPath ) ) 00232 return false; 00233 00234 $fileLines = file( $cpuinfoPath ); 00235 foreach ( $fileLines as $line ) 00236 { 00237 if ( substr( $line, 0, 7 ) == 'cpu MHz' ) 00238 { 00239 $cpu = trim( substr( $line, 11, strlen( $line ) - 11 ) ); 00240 $this->CPUSpeed = $cpu; 00241 $this->CPUUnit = 'MHz'; 00242 } 00243 if ( substr( $line, 0, 10 ) == 'model name' ) 00244 { 00245 $system = trim( substr( $line, 13, strlen( $line ) - 13 ) ); 00246 $this->CPUType = $system; 00247 } 00248 if ( $this->CPUSpeed !== false and 00249 $this->CPUType !== false and 00250 $this->CPUUnit !== false ) 00251 break; 00252 } 00253 00254 $fileLines = file( $meminfoPath ); 00255 foreach ( $fileLines as $line ) 00256 { 00257 if ( substr( $line, 0, 8 ) == 'MemTotal' ) 00258 { 00259 $mem = trim( substr( $line, 11, strlen( $line ) - 11 ) ); 00260 $memBytes = $mem; 00261 if ( preg_match( "#^([0-9]+) *([a-zA-Z]+)#", $mem, $matches ) ) 00262 { 00263 $memBytes = (int)$matches[1]; 00264 $unit = strtolower( $matches[2] ); 00265 if ( $unit == 'kb' ) 00266 { 00267 $memBytes *= 1024; 00268 } 00269 else if ( $unit == 'mb' ) 00270 { 00271 $memBytes *= 1024*1024; 00272 } 00273 else if ( $unit == 'gb' ) 00274 { 00275 $memBytes *= 1024*1024*1024; 00276 } 00277 } 00278 else 00279 { 00280 $memBytes = (int)$memBytes; 00281 } 00282 $this->MemorySize = $memBytes; 00283 } 00284 if ( $this->MemorySize !== false ) 00285 break; 00286 } 00287 00288 return true; 00289 } 00290 00291 /*! 00292 \private 00293 Scans the dmesg.boot file which is created by the kernel. 00294 If this files are unavailable or could not be read it will return \c false. 00295 \param $dmesgPath The path to the dmesg file, if \c false it uses '/var/run/dmesg.boot' which should be sufficient. 00296 */ 00297 function scanDMesg( $dmesgPath = false ) 00298 { 00299 if ( !$dmesgPath ) 00300 $dmesgPath = '/var/run/dmesg.boot'; 00301 if ( !file_exists( $dmesgPath ) ) 00302 return false; 00303 $fileLines = file( $dmesgPath ); 00304 foreach ( $fileLines as $line ) 00305 { 00306 if ( substr( $line, 0, 3 ) == 'CPU' ) 00307 { 00308 $system = trim( substr( $line, 4, strlen( $line ) - 4 ) ); 00309 $cpu = false; 00310 $cpuunit = false; 00311 if ( preg_match( "#^(.+)\\((.+)(MHz) +([^)]+)\\)#", $system, $matches ) ) 00312 { 00313 $system = trim( $matches[1] ) . ' (' . trim( $matches[4] ) . ')'; 00314 $cpu = $matches[2]; 00315 $cpuunit = $matches[3]; 00316 } 00317 $this->CPUSpeed = $cpu; 00318 $this->CPUType = $system; 00319 $this->CPUUnit = $cpuunit; 00320 } 00321 if ( substr( $line, 0, 11 ) == 'real memory' ) 00322 { 00323 $mem = trim( substr( $line, 12, strlen( $line ) - 12 ) ); 00324 $memBytes = $mem; 00325 if ( preg_match( "#^= *([0-9]+)#", $mem, $matches ) ) 00326 { 00327 $memBytes = $matches[1]; 00328 } 00329 $memBytes = (int)$memBytes; 00330 $this->MemorySize = $memBytes; 00331 } 00332 if ( $this->CPUSpeed !== false and 00333 $this->CPUType !== false and 00334 $this->CPUUnit !== false and 00335 $this->MemorySize !== false ) 00336 break; 00337 00338 } 00339 return true; 00340 } 00341 00342 /// \privatesection 00343 public $IsValid = false; 00344 public $CPUSpeed = false; 00345 public $CPUType = false; 00346 public $CPUUnit = false; 00347 public $MemorySize = false; 00348 } 00349 00350 ?>