/lib/ezutils/classes/ezsysinfo.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 325 lines · 212 code · 21 blank · 92 comment · 59 complexity · 7c8d0d877d016cbcdef5a48f9ed35f52 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZSysInfo class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version 2012.8
  8. * @package lib
  9. */
  10. /*!
  11. \class eZSysInfo ezsysinfo.php
  12. \brief Provides common information on the running system
  13. The following information can be queried:
  14. - CPU Type (e.g Pentium) - cpuType()
  15. - CPU Speed (e.g 1000) - cpuSpeed()
  16. - CPU Unit (e.g. MHz) - cpuUnit()
  17. - Memory Size in bytes (e.g. 528424960) - memorySize()
  18. \code
  19. $info = new eZSysInfo();
  20. $info->scan();
  21. print( $info->cpuType() . "\n" );
  22. \endcode
  23. \note This class supports the 'attribute' system and be used directly as a template variable.
  24. \note It uses eZSys to figure out the OS type.
  25. */
  26. class eZSysInfo
  27. {
  28. /*!
  29. Constructor
  30. */
  31. function eZSysInfo()
  32. {
  33. }
  34. /*!
  35. \return An array with available attributes.
  36. The available attributes:
  37. - cpu_type - cpuType()
  38. - cpu_unit - cpuUnit()
  39. - cpu_speed - cpuSpeed()
  40. - memory_size - memorySize()
  41. */
  42. function attributes()
  43. {
  44. return array( 'is_valid',
  45. 'cpu_type',
  46. 'cpu_unit',
  47. 'cpu_speed',
  48. 'memory_size' );
  49. }
  50. /*!
  51. \return \c true if the attribute named \a $name exists.
  52. See attributes() for a list of available attributes.
  53. */
  54. function hasAttribute( $name )
  55. {
  56. return in_array( $name, $this->attributes() );
  57. }
  58. /*!
  59. \return The value of the attribute named \a $name, or \c null if it does not exist.
  60. See attributes() for a list of available attributes.
  61. */
  62. function attribute( $name )
  63. {
  64. if ( $name == 'is_valid' )
  65. return $this->IsValid;
  66. else if ( $name == 'cpu_type' )
  67. return $this->CPUType;
  68. else if ( $name == 'cpu_unit' )
  69. return $this->CPUUnit;
  70. else if ( $name == 'cpu_speed' )
  71. return $this->CPUSpeed;
  72. else if ( $name == 'memory_size' )
  73. return $this->MemorySize;
  74. else
  75. {
  76. eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
  77. return null;
  78. }
  79. }
  80. /*!
  81. \return \c true if the system has been scanned correctly.
  82. */
  83. function isValid()
  84. {
  85. return $this->IsValid;
  86. }
  87. /*!
  88. Contains the type of CPU, the type is taken directly from the OS
  89. and can vary a lot.
  90. \return The type as a string or \c false if no type was found.
  91. */
  92. function cpuType()
  93. {
  94. return $this->CPUType;
  95. }
  96. /*!
  97. Contains the speed of CPU, the type is taken directly from the OS
  98. and can vary a lot. The speed is just a number so use cpuUnit()
  99. to get the proper unit (e.g MHz).
  100. \return The speed as a string or \c false if no type was found.
  101. */
  102. function cpuSpeed()
  103. {
  104. return $this->CPUSpeed;
  105. }
  106. /*!
  107. Contains the amount of system memory the OS has, the value is
  108. in bytes.
  109. \return The type as a number \c false if no type was found.
  110. */
  111. function memorySize()
  112. {
  113. return $this->MemorySize;
  114. }
  115. /*!
  116. Scans the system depending on the OS and fills in the information internally.
  117. \return \c true if it was able to scan the system or \c false if it failed.
  118. */
  119. function scan()
  120. {
  121. $this->IsValid = false;
  122. $this->CPUSpeed = false;
  123. $this->CPUType = false;
  124. $this->CPUUnit = false;
  125. $this->MemorySize = false;
  126. $sys = eZSys::instance();
  127. $osType = $sys->osType();
  128. if ( $osType == 'win32' )
  129. {
  130. // Windows (win32) is not supported yet
  131. // eZDebug::writeWarning( "System scan for Windows (win32) machines not supported yet" );
  132. }
  133. else if ( $osType == 'mac' )
  134. {
  135. // Mac means FreeBSD type of structure?
  136. $this->IsValid = $this->scanDMesg();
  137. return $this->IsValid;
  138. }
  139. else if ( $osType == 'unix' )
  140. {
  141. // Now determine specific 'Unix' type
  142. $osName = $sys->osName();
  143. if ( $osName == 'linux' )
  144. {
  145. $this->IsValid = $this->scanProc();
  146. return $this->IsValid;
  147. }
  148. else if ( $osName == 'freebsd' )
  149. {
  150. $this->IsValid = $this->scanDMesg();
  151. return $this->IsValid;
  152. }
  153. else
  154. {
  155. // Not known so we just try the various scanners
  156. //
  157. // /proc for Linux systems
  158. if ( $this->scanProc() )
  159. {
  160. $this->IsValid = true;
  161. return true;
  162. }
  163. // dmesg.boot for FreeBSD systems
  164. if ( $this->scanDMesg() )
  165. {
  166. $this->IsValid = true;
  167. return true;
  168. }
  169. }
  170. }
  171. return false;
  172. }
  173. /*!
  174. \private
  175. Scans the /proc/cpuinfo and /proc/meminfo files for CPU
  176. and memory information.
  177. If this files are unavailable or could not be read it will return \c false.
  178. \param $cpuinfoPath The path to the cpuinfo file, if \c false it uses '/proc/cpuinfo' which should be sufficient.
  179. \param $meminfoPath The path to the meminfo file, if \c false it uses '/proc/meminfo' which should be sufficient.
  180. */
  181. function scanProc( $cpuinfoPath = false, $meminfoPath = false )
  182. {
  183. if ( !$cpuinfoPath )
  184. $cpuinfoPath = '/proc/cpuinfo';
  185. if ( !$meminfoPath )
  186. $meminfoPath = '/proc/meminfo';
  187. if ( !file_exists( $cpuinfoPath ) )
  188. return false;
  189. if ( !file_exists( $meminfoPath ) )
  190. return false;
  191. $fileLines = file( $cpuinfoPath );
  192. foreach ( $fileLines as $line )
  193. {
  194. if ( substr( $line, 0, 7 ) == 'cpu MHz' )
  195. {
  196. $cpu = trim( substr( $line, 11, strlen( $line ) - 11 ) );
  197. $this->CPUSpeed = $cpu;
  198. $this->CPUUnit = 'MHz';
  199. }
  200. if ( substr( $line, 0, 10 ) == 'model name' )
  201. {
  202. $system = trim( substr( $line, 13, strlen( $line ) - 13 ) );
  203. $this->CPUType = $system;
  204. }
  205. if ( $this->CPUSpeed !== false and
  206. $this->CPUType !== false and
  207. $this->CPUUnit !== false )
  208. break;
  209. }
  210. $fileLines = file( $meminfoPath );
  211. foreach ( $fileLines as $line )
  212. {
  213. if ( substr( $line, 0, 8 ) == 'MemTotal' )
  214. {
  215. $mem = trim( substr( $line, 11, strlen( $line ) - 11 ) );
  216. $memBytes = $mem;
  217. if ( preg_match( "#^([0-9]+) *([a-zA-Z]+)#", $mem, $matches ) )
  218. {
  219. $memBytes = (int)$matches[1];
  220. $unit = strtolower( $matches[2] );
  221. if ( $unit == 'kb' )
  222. {
  223. $memBytes *= 1024;
  224. }
  225. else if ( $unit == 'mb' )
  226. {
  227. $memBytes *= 1024*1024;
  228. }
  229. else if ( $unit == 'gb' )
  230. {
  231. $memBytes *= 1024*1024*1024;
  232. }
  233. }
  234. else
  235. {
  236. $memBytes = (int)$memBytes;
  237. }
  238. $this->MemorySize = $memBytes;
  239. }
  240. if ( $this->MemorySize !== false )
  241. break;
  242. }
  243. return true;
  244. }
  245. /*!
  246. \private
  247. Scans the dmesg.boot file which is created by the kernel.
  248. If this files are unavailable or could not be read it will return \c false.
  249. \param $dmesgPath The path to the dmesg file, if \c false it uses '/var/run/dmesg.boot' which should be sufficient.
  250. */
  251. function scanDMesg( $dmesgPath = false )
  252. {
  253. if ( !$dmesgPath )
  254. $dmesgPath = '/var/run/dmesg.boot';
  255. if ( !file_exists( $dmesgPath ) )
  256. return false;
  257. $fileLines = file( $dmesgPath );
  258. foreach ( $fileLines as $line )
  259. {
  260. if ( substr( $line, 0, 3 ) == 'CPU' )
  261. {
  262. $system = trim( substr( $line, 4, strlen( $line ) - 4 ) );
  263. $cpu = false;
  264. $cpuunit = false;
  265. if ( preg_match( "#^(.+)\\((.+)(MHz) +([^)]+)\\)#", $system, $matches ) )
  266. {
  267. $system = trim( $matches[1] ) . ' (' . trim( $matches[4] ) . ')';
  268. $cpu = $matches[2];
  269. $cpuunit = $matches[3];
  270. }
  271. $this->CPUSpeed = $cpu;
  272. $this->CPUType = $system;
  273. $this->CPUUnit = $cpuunit;
  274. }
  275. if ( substr( $line, 0, 11 ) == 'real memory' )
  276. {
  277. $mem = trim( substr( $line, 12, strlen( $line ) - 12 ) );
  278. $memBytes = $mem;
  279. if ( preg_match( "#^= *([0-9]+)#", $mem, $matches ) )
  280. {
  281. $memBytes = $matches[1];
  282. }
  283. $memBytes = (int)$memBytes;
  284. $this->MemorySize = $memBytes;
  285. }
  286. if ( $this->CPUSpeed !== false and
  287. $this->CPUType !== false and
  288. $this->CPUUnit !== false and
  289. $this->MemorySize !== false )
  290. break;
  291. }
  292. return true;
  293. }
  294. /// \privatesection
  295. public $IsValid = false;
  296. public $CPUSpeed = false;
  297. public $CPUType = false;
  298. public $CPUUnit = false;
  299. public $MemorySize = false;
  300. }
  301. ?>