PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/System/includes/os/class.BSD.common.inc.php

https://bitbucket.org/yousef_fadila/vtiger
PHP | 298 lines | 214 code | 45 blank | 39 comment | 38 complexity | e12b881b2625cf2d28bd98993dae45fb MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. // phpSysInfo - A PHP System Information Script
  3. // http://phpsysinfo.sourceforge.net/
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU General Public License
  6. // as published by the Free Software Foundation; either version 2
  7. // of the License, or (at your option) any later version.
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License
  13. // along with this program; if not, write to the Free Software
  14. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. // $Id: class.BSD.common.inc.php,v 1.50 2006/04/22 14:35:57 bigmichi1 Exp $
  16. if (!defined('IN_PHPSYSINFO')) {
  17. die("No Hacking");
  18. }
  19. require_once(APP_ROOT . '/includes/os/class.parseProgs.inc.php');
  20. class bsd_common {
  21. var $dmesg;
  22. var $parser;
  23. // Our constructor
  24. // this function is run on the initialization of this class
  25. function bsd_common () {
  26. $this->parser = new Parser();
  27. $this->parser->df_param = "";
  28. }
  29. // read /var/run/dmesg.boot, but only if we haven't already.
  30. function read_dmesg () {
  31. if (! $this->dmesg) {
  32. $parts = explode("rebooting", rfts( '/var/run/dmesg.boot' ) );
  33. $this->dmesg = explode("\n", $parts[count($parts) - 1]);
  34. }
  35. return $this->dmesg;
  36. }
  37. // grabs a key from sysctl(8)
  38. function grab_key ($key) {
  39. return execute_program('sysctl', "-n $key");
  40. }
  41. // get our apache SERVER_NAME or vhost
  42. function hostname () {
  43. if (!($result = getenv('SERVER_NAME'))) {
  44. $result = "N.A.";
  45. }
  46. return $result;
  47. }
  48. // get our canonical hostname
  49. function chostname () {
  50. return execute_program('hostname');
  51. }
  52. // get the IP address of our canonical hostname
  53. function ip_addr () {
  54. if (!($result = getenv('SERVER_ADDR'))) {
  55. $result = gethostbyname($this->chostname());
  56. }
  57. return $result;
  58. }
  59. function kernel () {
  60. $s = $this->grab_key('kern.version');
  61. $a = explode(':', $s);
  62. return $a[0] . $a[1] . ':' . $a[2];
  63. }
  64. function uptime () {
  65. $result = $this->get_sys_ticks();
  66. return $result;
  67. }
  68. function users () {
  69. return execute_program('who', '| wc -l');
  70. }
  71. function loadavg ($bar = false) {
  72. $s = $this->grab_key('vm.loadavg');
  73. $s = ereg_replace('{ ', '', $s);
  74. $s = ereg_replace(' }', '', $s);
  75. $results['avg'] = explode(' ', $s);
  76. if ($bar) {
  77. if ($fd = $this->grab_key('kern.cp_time')) {
  78. // Find out the CPU load
  79. // user + sys = load
  80. // total = total
  81. preg_match($this->cpu_regexp2, $fd, $res );
  82. $load = $res[2] + $res[3] + $res[4]; // cpu.user + cpu.sys
  83. $total = $res[2] + $res[3] + $res[4] + $res[5]; // cpu.total
  84. // we need a second value, wait 1 second befor getting (< 1 second no good value will occour)
  85. sleep(1);
  86. $fd = $this->grab_key('kern.cp_time');
  87. preg_match($this->cpu_regexp2, $fd, $res );
  88. $load2 = $res[2] + $res[3] + $res[4];
  89. $total2 = $res[2] + $res[3] + $res[4] + $res[5];
  90. $results['cpupercent'] = (100*($load2 - $load)) / ($total2 - $total);
  91. }
  92. }
  93. return $results;
  94. }
  95. function cpu_info () {
  96. $results = array();
  97. $ar_buf = array();
  98. $results['model'] = $this->grab_key('hw.model');
  99. $results['cpus'] = $this->grab_key('hw.ncpu');
  100. for ($i = 0, $max = count($this->read_dmesg()); $i < $max; $i++) {
  101. $buf = $this->dmesg[$i];
  102. if (preg_match("/$this->cpu_regexp/", $buf, $ar_buf)) {
  103. $results['cpuspeed'] = round($ar_buf[2]);
  104. break;
  105. }
  106. }
  107. return $results;
  108. }
  109. // get the scsi device information out of dmesg
  110. function scsi () {
  111. $results = array();
  112. $ar_buf = array();
  113. for ($i = 0, $max = count($this->read_dmesg()); $i < $max; $i++) {
  114. $buf = $this->dmesg[$i];
  115. if (preg_match("/$this->scsi_regexp1/", $buf, $ar_buf)) {
  116. $s = $ar_buf[1];
  117. $results[$s]['model'] = $ar_buf[2];
  118. $results[$s]['media'] = 'Hard Disk';
  119. } elseif (preg_match("/$this->scsi_regexp2/", $buf, $ar_buf)) {
  120. $s = $ar_buf[1];
  121. $results[$s]['capacity'] = $ar_buf[2] * 2048 * 1.049;
  122. }
  123. }
  124. // return array_values(array_unique($results));
  125. // 1. more useful to have device names
  126. // 2. php 4.1.1 array_unique() deletes non-unique values.
  127. asort($results);
  128. return $results;
  129. }
  130. // get the pci device information out of dmesg
  131. function pci () {
  132. $results = array();
  133. if( !( is_array($results = $this->parser->parse_lspci()) || is_array($results = $this->parser->parse_pciconf() ))) {
  134. for ($i = 0, $s = 0; $i < count($this->read_dmesg()); $i++) {
  135. $buf = $this->dmesg[$i];
  136. if(!isset($this->pci_regexp1) && !isset($this->pci_regexp2)) {
  137. $this->pci_regexp1 = '/(.*): <(.*)>(.*) pci[0-9]$/';
  138. $this->pci_regexp2 = '/(.*): <(.*)>.* at [.0-9]+ irq/';
  139. }
  140. if (preg_match($this->pci_regexp1, $buf, $ar_buf)) {
  141. $results[$s++] = $ar_buf[1] . ": " . $ar_buf[2];
  142. } elseif (preg_match($this->pci_regexp2, $buf, $ar_buf)) {
  143. $results[$s++] = $ar_buf[1] . ": " . $ar_buf[2];
  144. }
  145. }
  146. asort($results);
  147. }
  148. return $results;
  149. }
  150. // get the ide device information out of dmesg
  151. function ide () {
  152. $results = array();
  153. $s = 0;
  154. for ($i = 0, $max = count($this->read_dmesg()); $i < $max; $i++) {
  155. $buf = $this->dmesg[$i];
  156. if (preg_match('/^(ad[0-9]+): (.*)MB <(.*)> (.*) (.*)/', $buf, $ar_buf)) {
  157. $s = $ar_buf[1];
  158. $results[$s]['model'] = $ar_buf[3];
  159. $results[$s]['media'] = 'Hard Disk';
  160. $results[$s]['capacity'] = $ar_buf[2] * 2048 * 1.049;
  161. } elseif (preg_match('/^(acd[0-9]+): (.*) <(.*)> (.*)/', $buf, $ar_buf)) {
  162. $s = $ar_buf[1];
  163. $results[$s]['model'] = $ar_buf[3];
  164. $results[$s]['media'] = 'CD-ROM';
  165. }
  166. }
  167. // return array_values(array_unique($results));
  168. // 1. more useful to have device names
  169. // 2. php 4.1.1 array_unique() deletes non-unique values.
  170. asort($results);
  171. return $results;
  172. }
  173. // place holder function until we add acual usb detection
  174. function usb () {
  175. return array();
  176. }
  177. function sbus () {
  178. $results = array();
  179. $_results[0] = "";
  180. // TODO. Nothing here yet. Move along.
  181. $results = $_results;
  182. return $results;
  183. }
  184. function memory () {
  185. $s = $this->grab_key('hw.physmem');
  186. if (PHP_OS == 'FreeBSD' || PHP_OS == 'OpenBSD') {
  187. // vmstat on fbsd 4.4 or greater outputs kbytes not hw.pagesize
  188. // I should probably add some version checking here, but for now
  189. // we only support fbsd 4.4
  190. $pagesize = 1024;
  191. } else {
  192. $pagesize = $this->grab_key('hw.pagesize');
  193. }
  194. $results['ram'] = array();
  195. $pstat = execute_program('vmstat');
  196. $lines = split("\n", $pstat);
  197. for ($i = 0, $max = sizeof($lines); $i < $max; $i++) {
  198. $ar_buf = preg_split("/\s+/", $lines[$i], 19);
  199. if ($i == 2) {
  200. if(PHP_OS == 'NetBSD') {
  201. $results['ram']['free'] = $ar_buf[5];
  202. } else {
  203. $results['ram']['free'] = $ar_buf[5] * $pagesize / 1024;
  204. }
  205. }
  206. }
  207. $results['ram']['total'] = $s / 1024;
  208. $results['ram']['shared'] = 0;
  209. $results['ram']['buffers'] = 0;
  210. $results['ram']['used'] = $results['ram']['total'] - $results['ram']['free'];
  211. $results['ram']['cached'] = 0;
  212. $results['ram']['t_used'] = $results['ram']['used'];
  213. $results['ram']['t_free'] = $results['ram']['free'];
  214. $results['ram']['percent'] = round(($results['ram']['used'] * 100) / $results['ram']['total']);
  215. if (PHP_OS == 'OpenBSD' || PHP_OS == 'NetBSD') {
  216. $pstat = execute_program('swapctl', '-l -k');
  217. } else {
  218. $pstat = execute_program('swapinfo', '-k');
  219. }
  220. $lines = split("\n", $pstat);
  221. $results['swap']['total'] = 0;
  222. $results['swap']['used'] = 0;
  223. $results['swap']['free'] = 0;
  224. for ($i = 1, $max = sizeof($lines); $i < $max; $i++) {
  225. $ar_buf = preg_split("/\s+/", $lines[$i], 6);
  226. if ($ar_buf[0] != 'Total') {
  227. $results['swap']['total'] = $results['swap']['total'] + $ar_buf[1];
  228. $results['swap']['used'] = $results['swap']['used'] + $ar_buf[2];
  229. $results['swap']['free'] = $results['swap']['free'] + $ar_buf[3];
  230. $results['devswap'][$i - 1] = array();
  231. $results['devswap'][$i - 1]['dev'] = $ar_buf[0];
  232. $results['devswap'][$i - 1]['total'] = $ar_buf[1];
  233. $results['devswap'][$i - 1]['used'] = $ar_buf[2];
  234. $results['devswap'][$i - 1]['free'] = ($results['devswap'][$i - 1]['total'] - $results['devswap'][$i - 1]['used']);
  235. $results['devswap'][$i - 1]['percent'] = $ar_buf[2] > 0 ? round(($ar_buf[2] * 100) / $ar_buf[1]) : 0;
  236. }
  237. }
  238. $results['swap']['percent'] = round(($results['swap']['used'] * 100) / $results['swap']['total']);
  239. if( is_callable( array( 'sysinfo', 'memory_additional' ) ) ) {
  240. $results = $this->memory_additional( $results );
  241. }
  242. return $results;
  243. }
  244. function filesystems () {
  245. return $this->parser->parse_filesystems();
  246. }
  247. function distro () {
  248. $distro = execute_program('uname', '-s');
  249. $result = $distro;
  250. return($result);
  251. }
  252. }
  253. ?>