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