PageRenderTime 65ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/3.0.17/includes/os/class.Darwin.inc.php

#
PHP | 320 lines | 195 code | 17 blank | 108 comment | 26 complexity | e4c36a5178e6db6b850685cec4a99649 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Darwin System Class
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PSI_OS
  9. * @author Michael Cramer <BigMichi1@users.sourceforge.net>
  10. * @copyright 2009 phpSysInfo
  11. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  12. * @version SVN: $Id: class.Darwin.inc.php 567 2012-04-11 08:46:29Z namiltd $
  13. * @link http://phpsysinfo.sourceforge.net
  14. */
  15. /**
  16. * Darwin sysinfo class
  17. * get all the required information from Darwin system
  18. * information may be incomplete
  19. *
  20. * @category PHP
  21. * @package PSI_OS
  22. * @author Michael Cramer <BigMichi1@users.sourceforge.net>
  23. * @copyright 2009 phpSysInfo
  24. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  25. * @version Release: 3.0
  26. * @link http://phpsysinfo.sourceforge.net
  27. */
  28. class Darwin extends BSDCommon
  29. {
  30. /**
  31. * define the regexp for log parser
  32. */
  33. /* public function __construct()
  34. {
  35. parent::__construct();
  36. $this->error->addWarning("The Darwin version of phpSysInfo is work in progress, some things currently don't work!");
  37. $this->setCPURegExp1("CPU: (.*) \((.*)-MHz (.*)\)");
  38. $this->setCPURegExp2("/(.*) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/");
  39. $this->setSCSIRegExp1("^(.*): <(.*)> .*SCSI.*device");
  40. } */
  41. /**
  42. * get a value from sysctl command
  43. *
  44. * @param string $key key of the value to get
  45. *
  46. * @return string
  47. */
  48. protected function grabkey($key)
  49. {
  50. if (CommonFunctions::executeProgram('sysctl', $key, $s, PSI_DEBUG)) {
  51. $s = preg_replace('/'.$key.': /', '', $s);
  52. $s = preg_replace('/'.$key.' = /', '', $s);
  53. return $s;
  54. } else {
  55. return '';
  56. }
  57. }
  58. /**
  59. * get a value from ioreg command
  60. *
  61. * @param string $key key of the value to get
  62. *
  63. * @return string
  64. */
  65. private function _grabioreg($key)
  66. {
  67. if (CommonFunctions::executeProgram('ioreg', '-c "'.$key.'"', $s, PSI_DEBUG)) {
  68. /* delete newlines */
  69. $s = preg_replace("/\s+/", " ", $s);
  70. /* new newlines */
  71. $s = preg_replace("/[\|\t ]*\+\-\o/", "\n", $s);
  72. /* combine duplicate whitespaces and some chars */
  73. $s = preg_replace("/[\|\t ]+/", " ", $s);
  74. $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
  75. $out = "";
  76. foreach ($lines as $line) {
  77. if (preg_match('/^([^<]*) <class '.$key.',/', $line)) {
  78. $out .= $line."\n";
  79. }
  80. }
  81. return $out;
  82. } else {
  83. return '';
  84. }
  85. }
  86. /**
  87. * UpTime
  88. * time the system is running
  89. *
  90. * @return void
  91. */
  92. private function _uptime()
  93. {
  94. if (CommonFunctions::executeProgram('sysctl', '-n kern.boottime', $a, PSI_DEBUG)) {
  95. $tmp = explode(" ", $a);
  96. if ($tmp[0]=="{") { /* kern.boottime= { sec = 1096732600, usec = 885425 } Sat Oct 2 10:56:40 2004 */
  97. $data = trim($tmp[3],",");
  98. $this->sys->setUptime(time() - $data);
  99. } else { /* kern.boottime= 1096732600 */
  100. $this->sys->setUptime(time() - $a);
  101. }
  102. }
  103. }
  104. /**
  105. * get CPU information
  106. *
  107. * @return void
  108. */
  109. protected function cpuinfo()
  110. {
  111. $dev = new CpuDevice();
  112. if (CommonFunctions::executeProgram('hostinfo', '| grep "Processor type"', $buf, PSI_DEBUG)) {
  113. $dev->setModel(preg_replace('/Processor type: /', '', $buf));
  114. $buf=$this->grabkey('hw.model');
  115. if (CommonFunctions::rfts(APP_ROOT.'/data/ModelTranslation.txt', $buffer)) {
  116. $buffer = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY);
  117. foreach ($buffer as $line) {
  118. $ar_buf = preg_split("/:/", $line, 2);
  119. if (trim($buf) === trim($ar_buf[0])) {
  120. $dev->setModel(trim($ar_buf[1]));
  121. }
  122. }
  123. }
  124. }
  125. $dev->setCpuSpeed(round($this->grabkey('hw.cpufrequency') / 1000000));
  126. $dev->setBusSpeed(round($this->grabkey('hw.busfrequency') / 1000000));
  127. $dev->setCache(round($this->grabkey('hw.l2cachesize')));
  128. for ($i = $this->grabkey('hw.ncpu') ; $i > 0; $i--) {
  129. $this->sys->setCpus($dev);
  130. }
  131. }
  132. /**
  133. * get the pci device information out of ioreg
  134. *
  135. * @return void
  136. */
  137. protected function pci()
  138. {
  139. $s = $this->_grabioreg('IOPCIDevice');
  140. $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
  141. foreach ($lines as $line) {
  142. $dev = new HWDevice();
  143. if (!preg_match('/"IOName" = "([^"]*)"/', $line, $ar_buf ))
  144. $ar_buf = preg_split("/[\s@]+/", $line, 19);
  145. $dev->setName(trim($ar_buf[1]));
  146. $this->sys->setPciDevices($dev);
  147. }
  148. }
  149. /**
  150. * get the ide device information out of ioreg
  151. *
  152. * @return void
  153. */
  154. protected function ide()
  155. {
  156. $s = $this->_grabioreg('IOATABlockStorageDevice');
  157. $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
  158. foreach ($lines as $line) {
  159. $dev = new HWDevice();
  160. if (!preg_match('/"Product Name"="([^"]*)"/', $line, $ar_buf ))
  161. $ar_buf = preg_split("/[\s]+/", $line, 19);
  162. $dev->setName(trim($ar_buf[1]));
  163. $this->sys->setIdeDevices($dev);
  164. }
  165. $s = $this->_grabioreg('IOAHCIBlockStorageDevice');
  166. $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
  167. foreach ($lines as $line) {
  168. $dev = new HWDevice();
  169. if (!preg_match('/"Product Name"="([^"]*)"/', $line, $ar_buf ))
  170. $ar_buf = preg_split("/[\s]+/", $line, 19);
  171. $dev->setName(trim($ar_buf[1]));
  172. $this->sys->setIdeDevices($dev);
  173. }
  174. }
  175. /**
  176. * get the usb device information out of ioreg
  177. *
  178. * @return void
  179. */
  180. protected function usb()
  181. {
  182. $s = $this->_grabioreg('IOUSBDevice');
  183. $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
  184. foreach ($lines as $line) {
  185. $dev = new HWDevice();
  186. if (!preg_match('/"USB Product Name"="([^"]*)"/', $line, $ar_buf ))
  187. $ar_buf = preg_split("/[\s]+/", $line, 19);
  188. $dev->setName(trim($ar_buf[1]));
  189. $this->sys->setUsbDevices($dev);
  190. }
  191. }
  192. /**
  193. * get the scsi device information out of ioreg
  194. *
  195. * @return void
  196. */
  197. protected function scsi()
  198. {
  199. $s = $this->_grabioreg('IOBlockStorageServices');
  200. $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
  201. foreach ($lines as $line) {
  202. $dev = new HWDevice();
  203. if (!preg_match('/"Product Name"="([^"]*)"/', $line, $ar_buf ))
  204. $ar_buf = preg_split("/[\s]+/", $line, 19);
  205. $dev->setName(trim($ar_buf[1]));
  206. $this->sys->setScsiDevices($dev);
  207. }
  208. }
  209. /**
  210. * get memory and swap information
  211. *
  212. * @return void
  213. */
  214. protected function memory()
  215. {
  216. $s = $this->grabkey('hw.memsize');
  217. if (CommonFunctions::executeProgram('vm_stat', '', $pstat, PSI_DEBUG)) {
  218. $lines = preg_split("/\n/", $pstat, -1, PREG_SPLIT_NO_EMPTY);
  219. $ar_buf = preg_split("/\s+/", $lines[1], 19);
  220. // calculate free memory from page sizes (each page = 4MB)
  221. $this->sys->setMemTotal($s);
  222. $this->sys->setMemFree($ar_buf[2] * 4 * 1024);
  223. $this->sys->setMemUsed($this->sys->getMemTotal() - $this->sys->getMemFree());
  224. if (CommonFunctions::executeProgram('sysctl', 'vm.swapusage | colrm 1 22', $swapBuff, PSI_DEBUG)) {
  225. $swap1 = preg_split('/M/', $swapBuff);
  226. $swap2 = preg_split('/=/', $swap1[1]);
  227. $swap3 = preg_split('/=/', $swap1[2]);
  228. $dev = new DiskDevice();
  229. $dev->setName('SWAP');
  230. $dev->setMountPoint('SWAP');
  231. $dev->setFsType('swap');
  232. $dev->setTotal($swap1[0] * 1024 * 1024);
  233. $dev->setUsed($swap2[1] * 1024 * 1024);
  234. $dev->setFree($swap3[1] * 1024 * 1024);
  235. $this->sys->setSwapDevices($dev);
  236. }
  237. }
  238. }
  239. /**
  240. * get network information
  241. *
  242. * @return void
  243. */
  244. private function _network()
  245. {
  246. if (CommonFunctions::executeProgram('netstat', '-nbdi | cut -c1-24,42- | grep Link', $netstat, PSI_DEBUG)) {
  247. $lines = preg_split("/\n/", $netstat, -1, PREG_SPLIT_NO_EMPTY);
  248. foreach ($lines as $line) {
  249. $ar_buf = preg_split("/\s+/", $line, 10);
  250. if (! empty($ar_buf[0])) {
  251. $dev = new NetDevice();
  252. $dev->setName($ar_buf[0]);
  253. $dev->setTxBytes($ar_buf[8]);
  254. $dev->setRxBytes($ar_buf[5]);
  255. $dev->setErrors($ar_buf[4] + $ar_buf[7]);
  256. if (isset($ar_buf[10])) {
  257. $dev->setDrops($ar_buf[10]);
  258. }
  259. $this->sys->setNetDevices($dev);
  260. }
  261. }
  262. }
  263. }
  264. /**
  265. * get icon name
  266. *
  267. * @return void
  268. */
  269. protected function distro()
  270. {
  271. $this->sys->setDistributionIcon('Darwin.png');
  272. if (!CommonFunctions::executeProgram('system_profiler', 'SPSoftwareDataType', $buffer, PSI_DEBUG)) {
  273. parent::distro();
  274. } else {
  275. $arrBuff = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY);
  276. foreach ($arrBuff as $line) {
  277. $arrLine = preg_split("/:/", $line, -1, PREG_SPLIT_NO_EMPTY);
  278. if (trim($arrLine[0]) === "System Version") {
  279. $distro = trim($arrLine[1]);
  280. if (preg_match('/^Mac OS/', $distro)) {
  281. $this->sys->setDistributionIcon('Apple.png');
  282. }
  283. $this->sys->setDistribution($distro);
  284. return;
  285. }
  286. }
  287. }
  288. }
  289. /**
  290. * get the information
  291. *
  292. * @see PSI_Interface_OS::build()
  293. *
  294. * @return Void
  295. */
  296. function build()
  297. {
  298. parent::build();
  299. $this->_uptime();
  300. $this->_network();
  301. }
  302. }
  303. ?>