PageRenderTime 58ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/System/includes/os/class.Linux.inc.php

https://bitbucket.org/yousef_fadila/vtiger
PHP | 528 lines | 437 code | 53 blank | 38 comment | 96 complexity | cbed08213c77526fdc4f446e15239f1a 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.Linux.inc.php,v 1.73 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.BSD.common.inc.php');
  20. class sysinfo {
  21. var $inifile = "distros.ini";
  22. var $icon = "unknown.png";
  23. var $distro = "unknown";
  24. var $parser;
  25. // get the distro name and icon when create the sysinfo object
  26. function sysinfo() {
  27. $this->parser = new Parser();
  28. $this->parser->df_param = 'P';
  29. $list = @parse_ini_file(APP_ROOT . "/" . $this->inifile, true);
  30. if (!$list) {
  31. return;
  32. }
  33. foreach ($list as $section => $distribution) {
  34. if (!isset($distribution["Files"])) {
  35. continue;
  36. } else {
  37. foreach (explode(";", $distribution["Files"]) as $filename) {
  38. if (file_exists($filename)) {
  39. $buf = rfts( $filename );
  40. $this->icon = isset($distribution["Image"]) ? $distribution["Image"] : $this->icon;
  41. $this->distro = isset($distribution["Name"]) ? $distribution["Name"] . " " . trim($buf) : trim($buf);
  42. break 2;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. // get our apache SERVER_NAME or vhost
  49. function vhostname () {
  50. if (! ($result = getenv('SERVER_NAME'))) {
  51. $result = 'N.A.';
  52. }
  53. return $result;
  54. }
  55. // get our canonical hostname
  56. function chostname () {
  57. $result = rfts( '/proc/sys/kernel/hostname', 1 );
  58. if ( $result == "ERROR" ) {
  59. $result = "N.A.";
  60. } else {
  61. $result = gethostbyaddr( gethostbyname( trim( $result ) ) );
  62. }
  63. return $result;
  64. }
  65. // get the IP address of our canonical hostname
  66. function ip_addr () {
  67. if (!($result = getenv('SERVER_ADDR'))) {
  68. $result = gethostbyname($this->chostname());
  69. }
  70. return $result;
  71. }
  72. function kernel () {
  73. $buf = rfts( '/proc/version', 1 );
  74. if ( $buf == "ERROR" ) {
  75. $result = "N.A.";
  76. } else {
  77. if (preg_match('/version (.*?) /', $buf, $ar_buf)) {
  78. $result = $ar_buf[1];
  79. if (preg_match('/SMP/', $buf)) {
  80. $result .= ' (SMP)';
  81. }
  82. }
  83. }
  84. return $result;
  85. }
  86. function uptime () {
  87. $buf = rfts( '/proc/uptime', 1 );
  88. $ar_buf = split( ' ', $buf );
  89. $result = trim( $ar_buf[0] );
  90. return $result;
  91. }
  92. function users () {
  93. $who = split('=', execute_program('who', '-q'));
  94. $result = $who[1];
  95. return $result;
  96. }
  97. function loadavg ($bar = false) {
  98. $buf = rfts( '/proc/loadavg' );
  99. if( $buf == "ERROR" ) {
  100. $results['avg'] = array('N.A.', 'N.A.', 'N.A.');
  101. } else {
  102. $results['avg'] = preg_split("/\s/", $buf, 4);
  103. unset($results['avg'][3]); // don't need the extra values, only first three
  104. }
  105. if ($bar) {
  106. $buf = rfts( '/proc/stat', 1 );
  107. if( $buf != "ERROR" ) {
  108. sscanf($buf, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
  109. // Find out the CPU load
  110. // user + sys = load
  111. // total = total
  112. $load = $ab + $ac + $ad; // cpu.user + cpu.sys
  113. $total = $ab + $ac + $ad + $ae; // cpu.total
  114. // we need a second value, wait 1 second befor getting (< 1 second no good value will occour)
  115. sleep(1);
  116. $buf = rfts( '/proc/stat', 1 );
  117. sscanf($buf, "%*s %Ld %Ld %Ld %Ld", $ab, $ac, $ad, $ae);
  118. $load2 = $ab + $ac + $ad;
  119. $total2 = $ab + $ac + $ad + $ae;
  120. $results['cpupercent'] = (100*($load2 - $load)) / ($total2 - $total);
  121. }
  122. }
  123. return $results;
  124. }
  125. function cpu_info () {
  126. $bufr = rfts( '/proc/cpuinfo' );
  127. if ( $bufr != "ERROR" ) {
  128. $bufe = explode("\n", $bufr);
  129. $results = array('cpus' => 0, 'bogomips' => 0);
  130. $ar_buf = array();
  131. foreach( $bufe as $buf ) {
  132. if(trim($buf) != "") {
  133. list($key, $value) = preg_split('/\s+:\s+/', trim($buf), 2);
  134. // All of the tags here are highly architecture dependant.
  135. // the only way I could reconstruct them for machines I don't
  136. // have is to browse the kernel source. So if your arch isn't
  137. // supported, tell me you want it written in.
  138. switch ($key) {
  139. case 'model name':
  140. $results['model'] = $value;
  141. break;
  142. case 'cpu MHz':
  143. $results['cpuspeed'] = sprintf('%.2f', $value);
  144. break;
  145. case 'cycle frequency [Hz]': // For Alpha arch - 2.2.x
  146. $results['cpuspeed'] = sprintf('%.2f', $value / 1000000);
  147. break;
  148. case 'clock': // For PPC arch (damn borked POS)
  149. $results['cpuspeed'] = sprintf('%.2f', $value);
  150. break;
  151. case 'cpu': // For PPC arch (damn borked POS)
  152. $results['model'] = $value;
  153. break;
  154. case 'L2 cache': // More for PPC
  155. $results['cache'] = $value;
  156. break;
  157. case 'revision': // For PPC arch (damn borked POS)
  158. $results['model'] .= ' ( rev: ' . $value . ')';
  159. break;
  160. case 'cpu model': // For Alpha arch - 2.2.x
  161. $results['model'] .= ' (' . $value . ')';
  162. break;
  163. case 'cache size':
  164. $results['cache'] = $value;
  165. break;
  166. case 'bogomips':
  167. $results['bogomips'] += $value;
  168. break;
  169. case 'BogoMIPS': // For alpha arch - 2.2.x
  170. $results['bogomips'] += $value;
  171. break;
  172. case 'BogoMips': // For sparc arch
  173. $results['bogomips'] += $value;
  174. break;
  175. case 'cpus detected': // For Alpha arch - 2.2.x
  176. $results['cpus'] += $value;
  177. break;
  178. case 'system type': // Alpha arch - 2.2.x
  179. $results['model'] .= ', ' . $value . ' ';
  180. break;
  181. case 'platform string': // Alpha arch - 2.2.x
  182. $results['model'] .= ' (' . $value . ')';
  183. break;
  184. case 'processor':
  185. $results['cpus'] += 1;
  186. break;
  187. case 'Cpu0ClkTck': // Linux sparc64
  188. $results['cpuspeed'] = sprintf('%.2f', hexdec($value) / 1000000);
  189. break;
  190. case 'Cpu0Bogo': // Linux sparc64 & sparc32
  191. $results['bogomips'] = $value;
  192. break;
  193. case 'ncpus probed': // Linux sparc64 & sparc32
  194. $results['cpus'] = $value;
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. // sparc64 specific code follows
  201. // This adds the ability to display the cache that a CPU has
  202. // Originally made by Sven Blumenstein <bazik@gentoo.org> in 2004
  203. // Modified by Tom Weustink <freshy98@gmx.net> in 2004
  204. $sparclist = array('SUNW,UltraSPARC@0,0', 'SUNW,UltraSPARC-II@0,0', 'SUNW,UltraSPARC@1c,0', 'SUNW,UltraSPARC-IIi@1c,0', 'SUNW,UltraSPARC-II@1c,0');
  205. foreach ($sparclist as $name) {
  206. $buf = rfts( '/proc/openprom/' . $name . '/ecache-size',1 , 32, false );
  207. if( $buf != "ERROR" ) {
  208. $results['cache'] = base_convert($buf, 16, 10)/1024 . ' KB';
  209. }
  210. }
  211. // sparc64 specific code ends
  212. // XScale detection code
  213. if ( $results['cpus'] == 0 ) {
  214. foreach( $bufe as $buf ) {
  215. if($buf != "\n") {
  216. list($key, $value) = preg_split('/\s+:\s+/', trim($buf), 2);
  217. switch($key) {
  218. case 'Processor':
  219. $results['cpus'] += 1;
  220. $results['model'] = $value;
  221. break;
  222. case 'BogoMIPS': //BogoMIPS are not BogoMIPS on this CPU, it's the speed, no BogoMIPS available
  223. $results['cpuspeed'] = $value;
  224. break;
  225. case 'I size':
  226. $results['cache'] = $value;
  227. break;
  228. case 'D size':
  229. $results['cache'] += $value;
  230. break;
  231. }
  232. }
  233. }
  234. $results['cache'] = $results['cache'] / 1024 . " KB";
  235. }
  236. $keys = array_keys($results);
  237. $keys2be = array('model', 'cpuspeed', 'cache', 'bogomips', 'cpus');
  238. while ($ar_buf = each($keys2be)) {
  239. if (! in_array($ar_buf[1], $keys)) {
  240. $results[$ar_buf[1]] = 'N.A.';
  241. }
  242. }
  243. return $results;
  244. }
  245. function pci () {
  246. $results = array();
  247. if( !$results = $this->parser->parse_lspci() ) {
  248. $bufr = rfts( '/proc/pci' );
  249. //Pinaki : Fix for ticket #4462 and #5147
  250. if(!is_null($bufr) && is_array($bufr)) {
  251. foreach( $bufr as $buf ) {
  252. if (preg_match('/Bus/', $buf)) {
  253. $device = true;
  254. continue;
  255. }
  256. if ($device) {
  257. list($key, $value) = split(': ', $buf, 2);
  258. if (!preg_match('/bridge/i', $key) && !preg_match('/USB/i', $key)) {
  259. $results[] = preg_replace('/\([^\)]+\)\.$/', '', trim($value));
  260. }
  261. $device = false;
  262. }
  263. }
  264. asort($results);
  265. }
  266. }
  267. return $results;
  268. }
  269. function ide () {
  270. $results = array();
  271. $bufd = gdc( '/proc/ide' );
  272. //Pinaki: Fix for ticket #4462
  273. if(!is_null($bufd)){
  274. foreach( $bufd as $file ) {
  275. if (preg_match('/^hd/', $file)) {
  276. $results[$file] = array();
  277. $buf = rfts("/proc/ide/" . $file . "/media", 1 );
  278. if ( $buf != "ERROR" ) {
  279. $results[$file]['media'] = trim($buf);
  280. if ($results[$file]['media'] == 'disk') {
  281. $results[$file]['media'] = 'Hard Disk';
  282. $buf = rfts( "/proc/ide/" . $file . "/capacity", 1, 4096, false);
  283. if( $buf == "ERROR" ) {
  284. $buf = rfts( "/sys/block/" . $file . "/size", 1, 4096, false);
  285. }
  286. if ( $buf != "ERROR" ) {
  287. $results[$file]['capacity'] = trim( $buf );
  288. }
  289. } elseif ($results[$file]['media'] == 'cdrom') {
  290. $results[$file]['media'] = 'CD-ROM';
  291. unset($results[$file]['capacity']);
  292. }
  293. }
  294. $buf = rfts( "/proc/ide/" . $file . "/model", 1 );
  295. if ( $buf != "ERROR" ) {
  296. $results[$file]['model'] = trim( $buf );
  297. if (preg_match('/WDC/', $results[$file]['model'])) {
  298. $results[$file]['manufacture'] = 'Western Digital';
  299. } elseif (preg_match('/IBM/', $results[$file]['model'])) {
  300. $results[$file]['manufacture'] = 'IBM';
  301. } elseif (preg_match('/FUJITSU/', $results[$file]['model'])) {
  302. $results[$file]['manufacture'] = 'Fujitsu';
  303. } else {
  304. $results[$file]['manufacture'] = 'Unknown';
  305. }
  306. }
  307. }
  308. }
  309. asort($results);
  310. }
  311. return $results;
  312. }
  313. function scsi () {
  314. $results = array();
  315. $dev_vendor = '';
  316. $dev_model = '';
  317. $dev_rev = '';
  318. $dev_type = '';
  319. $s = 1;
  320. $get_type = 0;
  321. $bufr = execute_program('lsscsi', '-c', false);
  322. if( is_null( $bufr )) {
  323. $bufr = rfts( '/proc/scsi/scsi', 0, 4096, false );
  324. }
  325. if ( $bufr != "ERROR" ) {
  326. $bufe = explode("\n", $bufr);
  327. foreach( $bufe as $buf ) {
  328. if (preg_match('/Vendor/', $buf)) {
  329. preg_match('/Vendor: (.*) Model: (.*) Rev: (.*)/i', $buf, $dev);
  330. list($key, $value) = split(': ', $buf, 2);
  331. $dev_str = $value;
  332. $get_type = true;
  333. continue;
  334. }
  335. if ($get_type) {
  336. preg_match('/Type:\s+(\S+)/i', $buf, $dev_type);
  337. $results[$s]['model'] = "$dev[1] $dev[2] ($dev_type[1])";
  338. $results[$s]['media'] = "Hard Disk";
  339. $s++;
  340. $get_type = false;
  341. }
  342. }
  343. }
  344. asort($results);
  345. return $results;
  346. }
  347. function usb () {
  348. $results = array();
  349. $devnum = -1;
  350. $bufr = execute_program('lsusb', '', false);
  351. if( is_null( $bufr )) {
  352. $bufr = rfts( '/proc/bus/usb/devices' );
  353. if ( $bufr != "ERROR" ) {
  354. $bufe = explode("\n", $bufr);
  355. foreach( $bufe as $buf ) {
  356. if (preg_match('/^T/', $buf)) {
  357. $devnum += 1;
  358. $results[$devnum] = "";
  359. } elseif (preg_match('/^S:/', $buf)) {
  360. list($key, $value) = split(': ', $buf, 2);
  361. list($key, $value2) = split('=', $value, 2);
  362. if (trim($key) != "SerialNumber") {
  363. $results[$devnum] .= " " . trim($value2);
  364. $devstring = 0;
  365. }
  366. }
  367. }
  368. }
  369. } else {
  370. $bufe = explode( "\n", $bufr );
  371. foreach( $bufe as $buf ) {
  372. $device = preg_split("/ /", $buf, 7);
  373. if( isset( $device[6] ) && trim( $device[6] ) != "" ) {
  374. $results[$devnum++] = trim( $device[6] );
  375. }
  376. }
  377. }
  378. return $results;
  379. }
  380. function sbus () {
  381. $results = array();
  382. $_results[0] = "";
  383. // TODO. Nothing here yet. Move along.
  384. $results = $_results;
  385. return $results;
  386. }
  387. function network () {
  388. $results = array();
  389. $bufr = rfts( '/proc/net/dev' );
  390. //Pinaki: Fix for ticket #4462
  391. if ( !is_null($bufr) && $bufr != "ERROR") {
  392. $bufe = explode("\n", $bufr);
  393. foreach( $bufe as $buf ) {
  394. if (preg_match('/:/', $buf)) {
  395. list($dev_name, $stats_list) = preg_split('/:/', $buf, 2);
  396. $stats = preg_split('/\s+/', trim($stats_list));
  397. $results[$dev_name] = array();
  398. $results[$dev_name]['rx_bytes'] = $stats[0];
  399. $results[$dev_name]['rx_packets'] = $stats[1];
  400. $results[$dev_name]['rx_errs'] = $stats[2];
  401. $results[$dev_name]['rx_drop'] = $stats[3];
  402. $results[$dev_name]['tx_bytes'] = $stats[8];
  403. $results[$dev_name]['tx_packets'] = $stats[9];
  404. $results[$dev_name]['tx_errs'] = $stats[10];
  405. $results[$dev_name]['tx_drop'] = $stats[11];
  406. $results[$dev_name]['errs'] = $stats[2] + $stats[10];
  407. $results[$dev_name]['drop'] = $stats[3] + $stats[11];
  408. }
  409. }
  410. }
  411. return $results;
  412. }
  413. function memory () {
  414. $results['ram'] = array();
  415. $results['swap'] = array();
  416. $results['devswap'] = array();
  417. $bufr = rfts( '/proc/meminfo' );
  418. if ( $bufr != "ERROR" ) {
  419. $bufe = explode("\n", $bufr);
  420. foreach( $bufe as $buf ) {
  421. if (preg_match('/^MemTotal:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
  422. $results['ram']['total'] = $ar_buf[1];
  423. } else if (preg_match('/^MemFree:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
  424. $results['ram']['t_free'] = $ar_buf[1];
  425. } else if (preg_match('/^Cached:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
  426. $results['ram']['cached'] = $ar_buf[1];
  427. } else if (preg_match('/^Buffers:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
  428. $results['ram']['buffers'] = $ar_buf[1];
  429. } else if (preg_match('/^SwapTotal:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
  430. $results['swap']['total'] = $ar_buf[1];
  431. } else if (preg_match('/^SwapFree:\s+(.*)\s*kB/i', $buf, $ar_buf)) {
  432. $results['swap']['free'] = $ar_buf[1];
  433. }
  434. }
  435. $results['ram']['t_used'] = $results['ram']['total'] - $results['ram']['t_free'];
  436. $results['ram']['percent'] = round(($results['ram']['t_used'] * 100) / $results['ram']['total']);
  437. $results['swap']['used'] = $results['swap']['total'] - $results['swap']['free'];
  438. $results['swap']['percent'] = round(($results['swap']['used'] * 100) / $results['swap']['total']);
  439. // values for splitting memory usage
  440. if (isset($results['ram']['cached']) && isset($results['ram']['buffers'])) {
  441. $results['ram']['app'] = $results['ram']['t_used'] - $results['ram']['cached'] - $results['ram']['buffers'];
  442. $results['ram']['app_percent'] = round(($results['ram']['app'] * 100) / $results['ram']['total']);
  443. $results['ram']['buffers_percent'] = round(($results['ram']['buffers'] * 100) / $results['ram']['total']);
  444. $results['ram']['cached_percent'] = round(($results['ram']['cached'] * 100) / $results['ram']['total']);
  445. }
  446. $bufr = rfts( '/proc/swaps' );
  447. //Pinaki: Fix for ticket #4462
  448. if ( !is_null($bufr) && $bufr != "ERROR") {
  449. $swaps = explode("\n", $bufr);
  450. for ($i = 1; $i < (sizeof($swaps)); $i++) {
  451. if( trim( $swaps[$i] ) != "" ) {
  452. $ar_buf = preg_split('/\s+/', $swaps[$i], 6);
  453. $results['devswap'][$i - 1] = array();
  454. $results['devswap'][$i - 1]['dev'] = $ar_buf[0];
  455. $results['devswap'][$i - 1]['total'] = $ar_buf[2];
  456. $results['devswap'][$i - 1]['used'] = $ar_buf[3];
  457. $results['devswap'][$i - 1]['free'] = ($results['devswap'][$i - 1]['total'] - $results['devswap'][$i - 1]['used']);
  458. $results['devswap'][$i - 1]['percent'] = round(($ar_buf[3] * 100) / $ar_buf[2]);
  459. }
  460. }
  461. }
  462. }
  463. return $results;
  464. }
  465. function filesystems () {
  466. return $this->parser->parse_filesystems();
  467. }
  468. function distro () {
  469. return $this->distro;
  470. }
  471. function distroicon () {
  472. return $this->icon;
  473. }
  474. }
  475. ?>