api.systemhwinfo.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. /**
  3. * Represents a class for retrieving system information.
  4. */
  5. class SystemHwInfo {
  6. /**
  7. * This variable represents the operating system name of the system.
  8. *
  9. * @var string
  10. */
  11. protected $os = '';
  12. /**
  13. * This variable represents the operating system release of the system.
  14. *
  15. * @var string
  16. */
  17. protected $osRelease = '';
  18. /**
  19. * Represents the full system OS information.
  20. *
  21. * @var string $osFullRelease The full release of the operating system.
  22. */
  23. protected $osFullRelease = '';
  24. /**
  25. * This variable represents the hostname of the system.
  26. *
  27. * @var string
  28. */
  29. protected $hostname = '';
  30. /**
  31. * This variable represents the PHP version used in the system. like 7.4.29.
  32. *
  33. * @var string
  34. */
  35. protected $phpVersion = '';
  36. /**
  37. * Property that holds the name of the CPU.
  38. *
  39. * @var string
  40. */
  41. protected $cpuName = '';
  42. /**
  43. * This variable represents the number of CPU cores.
  44. *
  45. * @var int
  46. */
  47. protected $cpuCores = 1;
  48. /**
  49. * This variable represents the total memory available in bytes.
  50. *
  51. * @var int
  52. */
  53. protected $memTotal = 0;
  54. /**
  55. * This variable represents the free memory in bytes.
  56. *
  57. * @var int
  58. */
  59. protected $memFree = 0;
  60. /**
  61. * This variable represents the used memory in bytes.
  62. *
  63. * @var int
  64. */
  65. protected $memUsed = 0;
  66. /**
  67. * This variable represents the number of seconds the system has been running.
  68. *
  69. * @var int
  70. */
  71. protected $uptimeSeconds = 0;
  72. /**
  73. * This variable represents the load average of the system.
  74. *
  75. * @var array
  76. */
  77. protected $loadAverage = array();
  78. /**
  79. * This variable represents the load average of the system in 1 minute
  80. *
  81. * @var float
  82. */
  83. protected $la1 = 0;
  84. /**
  85. * This variable represents the load average of the system in 5 minute
  86. *
  87. * @var float
  88. */
  89. protected $la5 = 0;
  90. /**
  91. * This variable represents the load average of the system in 15 minutes
  92. *
  93. * @var float
  94. */
  95. protected $la15 = 0;
  96. /**
  97. * This variable represents the latest system resources load percent depends on cores count.
  98. *
  99. * @var float
  100. */
  101. protected $systemLoadPercent = 0;
  102. /**
  103. * This variable represents the system resources load percent depends on cores count in 1 minute
  104. *
  105. * @var float
  106. */
  107. protected $loadPercent1 = 0;
  108. /**
  109. * This variable represents the system resources load percent depends on cores count in 5 minutes
  110. *
  111. * @var float
  112. */
  113. protected $loadPercent5 = 0;
  114. /**
  115. * This variable represents the system resources load percent depends on cores count in 15 minutes
  116. *
  117. * @var float
  118. */
  119. protected $loadPercent15 = 0;
  120. /**
  121. * This variable represents the average system resources load percent depends on cores count
  122. *
  123. * @var float
  124. */
  125. protected $loadAvgPercent = 0;
  126. /**
  127. * Contains mountpoints to load disk stats
  128. *
  129. * @var array
  130. */
  131. protected $mountPoints = array();
  132. /**
  133. * Contains loaded all mountpoints stats
  134. *
  135. * @var array
  136. */
  137. protected $diskStats = array();
  138. /**
  139. * The paths for some of system executable binaries
  140. *
  141. * @var string
  142. */
  143. protected $sysctlPath = '/sbin/sysctl';
  144. protected $vmstatPath = '/usr/bin/vmstat';
  145. protected $catPath = '/bin/cat';
  146. protected $grepPath = '/usr/bin/grep';
  147. protected $headPath = '/usr/bin/head';
  148. public function __construct() {
  149. $this->setOS();
  150. $this->setPhpVersion();
  151. $this->setPaths();
  152. $this->setLoadAverage();
  153. $this->setCpuCores();
  154. $this->setCpuName();
  155. $this->setSystemLoadPercent();
  156. $this->setUptime();
  157. $this->setMemory();
  158. }
  159. /**
  160. * Retrieves the output of a command execution.
  161. *
  162. * @param string $command The command to execute.
  163. * @param string $params Additional parameters for the command (optional).
  164. * @return string The output of the command execution.
  165. */
  166. protected function grabCmdOutput($command, $params = '') {
  167. $result = '';
  168. if (file_exists($command)) {
  169. if (!empty($params)) {
  170. $params = ' ' . $params;
  171. }
  172. $rawOutput = shell_exec($command . $params);
  173. $result .= trim($rawOutput);
  174. }
  175. return ($result);
  176. }
  177. /**
  178. * Sets the operating system information.
  179. *
  180. * @return void
  181. */
  182. protected function setOS() {
  183. $this->os = trim(php_uname('s'));
  184. $this->osRelease = trim(php_uname('r'));
  185. $this->osFullRelease = trim(php_uname('a'));
  186. $this->hostname = trim(php_uname('n'));
  187. }
  188. /**
  189. * Sets the paths for some binaries
  190. *
  191. * @return void
  192. */
  193. protected function setPaths() {
  194. switch ($this->os) {
  195. case 'FreeBSD':
  196. $this->sysctlPath = '/sbin/sysctl';
  197. $this->catPath = '/bin/cat';
  198. break;
  199. case 'Linux':
  200. $this->sysctlPath = '/usr/sbin/sysctl';
  201. $this->catPath = '/usr/bin/cat';
  202. break;
  203. }
  204. }
  205. /**
  206. * Sets the load average.
  207. *
  208. * This method is responsible for setting the load average.
  209. *
  210. * @return void
  211. */
  212. protected function setLoadAverage() {
  213. $this->loadAverage = sys_getloadavg();
  214. $this->la1 = round($this->loadAverage[0], 2);
  215. $this->la5 = round($this->loadAverage[1], 2);
  216. $this->la15 = round($this->loadAverage[2], 2);
  217. }
  218. /**
  219. * Sets the CPU name.
  220. *
  221. * @return void
  222. */
  223. protected function setCpuName() {
  224. $cpuName = '';
  225. switch ($this->os) {
  226. case 'FreeBSD':
  227. $cpuName = $this->grabCmdOutput($this->sysctlPath, '-n hw.model');
  228. break;
  229. case 'Linux':
  230. $raw = $this->grabCmdOutput($this->catPath, ' /proc/cpuinfo | ' . $this->grepPath . ' "model name" | ' . $this->headPath . ' -n 1');
  231. $raw = str_replace('model name :', '', $raw);
  232. $cpuName = $raw;
  233. break;
  234. }
  235. $this->cpuName = trim($cpuName);
  236. }
  237. /**
  238. * Sets the number of CPU cores.
  239. *
  240. * @return void
  241. */
  242. protected function setCpuCores() {
  243. $coresCount = 0;
  244. switch ($this->os) {
  245. case 'FreeBSD':
  246. $coresCount = $this->grabCmdOutput($this->sysctlPath, '-n hw.ncpu');
  247. break;
  248. case 'Linux':
  249. $raw = $this->grabCmdOutput($this->catPath, ' /proc/cpuinfo | ' . $this->grepPath . ' "siblings" | ' . $this->headPath . ' -n 1');
  250. $coresCount = preg_replace("#[^0-9]#Uis", '', $raw);
  251. break;
  252. }
  253. if ($coresCount > 0) {
  254. $this->cpuCores = $coresCount;
  255. }
  256. }
  257. /**
  258. * Sets the uptime proterty for the system.
  259. *
  260. * @return void
  261. */
  262. protected function setUptime() {
  263. $uptime = 0;
  264. $currentTimestamp = time();
  265. switch ($this->os) {
  266. case 'FreeBSD':
  267. $uptimeRaw = $this->grabCmdOutput($this->sysctlPath, '-n kern.boottime');
  268. if (preg_match("/sec = ([0-9]+)/", $uptimeRaw, $parts)) {
  269. $uptime = $currentTimestamp - $parts[1];
  270. }
  271. break;
  272. case 'Linux':
  273. $uptimeRaw = $this->grabCmdOutput($this->catPath, '/proc/uptime');
  274. if (!empty($uptimeRaw)) {
  275. $uptimeRaw = explode(' ', $uptimeRaw);
  276. $uptime = round($uptimeRaw[0]);
  277. }
  278. break;
  279. }
  280. $this->uptimeSeconds = $uptime;
  281. }
  282. /**
  283. * Sets the system load percentages.
  284. *
  285. * @return void
  286. */
  287. protected function setSystemLoadPercent() {
  288. if ($this->cpuCores != 0) {
  289. $this->loadPercent1 = round(($this->la1 / $this->cpuCores) * 100, 2);
  290. $this->loadPercent5 = round(($this->la5 / $this->cpuCores) * 100, 2);
  291. $this->loadPercent15 = round(($this->la15 / $this->cpuCores) * 100, 2);
  292. $this->systemLoadPercent = $this->loadPercent1;
  293. $this->loadAvgPercent = round(($this->loadPercent1 + $this->loadPercent5 + $this->loadPercent15) / 3, 2);
  294. }
  295. }
  296. /**
  297. * Sets the PHP version prop.
  298. *
  299. * @return void
  300. */
  301. protected function setPhpVersion() {
  302. $this->phpVersion = phpversion();
  303. }
  304. /**
  305. * Sets the memory stats for the system.
  306. *
  307. * @return void
  308. */
  309. protected function setMemory() {
  310. $memTotal = 0;
  311. $memFree = 0;
  312. $memUsed = 0;
  313. switch ($this->os) {
  314. case 'FreeBSD':
  315. $pageSize = $this->grabCmdOutput($this->sysctlPath, '-n hw.pagesize');
  316. $memTotal = $this->grabCmdOutput($this->sysctlPath, '-n hw.physmem');
  317. $vmStatRaw = $this->grabCmdOutput($this->vmstatPath);
  318. $lines = preg_split("/\n/", $vmStatRaw, -1, PREG_SPLIT_NO_EMPTY);
  319. $mem_buf = preg_split("/\s+/", trim($lines[2]), 19);
  320. $memFree = $mem_buf[4] * $pageSize;
  321. $memUsed = $memTotal - $memFree;
  322. break;
  323. case 'Linux':
  324. $memInfoRaw = $this->grabCmdOutput($this->catPath, '/proc/meminfo');
  325. $bufe = preg_split("/\n/", $memInfoRaw, -1, PREG_SPLIT_NO_EMPTY);
  326. foreach ($bufe as $buf) {
  327. if (preg_match('/^MemTotal:\s+(\d+)\s*kB/i', $buf, $ar_buf)) {
  328. $memTotal = $ar_buf[1] * 1024;
  329. } elseif (preg_match('/^MemFree:\s+(\d+)\s*kB/i', $buf, $ar_buf)) {
  330. $memFree = $ar_buf[1] * 1024;
  331. }
  332. $memUsed = $memTotal - $memFree;
  333. }
  334. break;
  335. }
  336. $this->memTotal = $memTotal;
  337. $this->memFree = $memFree;
  338. $this->memUsed = $memUsed;
  339. }
  340. /**
  341. * Sets the mount points for the system hardware information.
  342. *
  343. * @param array $mountPoints An array of mount points.
  344. * @return void
  345. */
  346. public function setMountPoints($mountPoints = array()) {
  347. if (!empty($mountPoints)) {
  348. foreach ($mountPoints as $idx => $eachMountPoint) {
  349. if (strpos($eachMountPoint, '/') !== false) {
  350. $this->mountPoints[] = $eachMountPoint;
  351. }
  352. }
  353. }
  354. }
  355. /**
  356. * Counts percentage between two values
  357. *
  358. * @param float $valueTotal
  359. * @param float $value
  360. *
  361. * @return float
  362. */
  363. protected function calcPercentValue($valueTotal, $value) {
  364. $result = 0;
  365. if ($valueTotal != 0) {
  366. $result = round((($value * 100) / $valueTotal), 2);
  367. }
  368. return ($result);
  369. }
  370. /**
  371. * Retrieves disk statistics for a given mount point. Returns An array containing
  372. * disk statistics including mount point, total space, free space, used space, used percentage, and free percentage.
  373. *
  374. * @param string $mountPoint The mount point of the disk.
  375. *
  376. * @return array
  377. */
  378. public function getDiskStat($mountPoint) {
  379. $result = array();
  380. if (!empty($mountPoint)) {
  381. $totalSpace = disk_total_space($mountPoint);
  382. if (!empty($totalSpace)) {
  383. $freeSpace = disk_free_space($mountPoint);
  384. $usedSpace = $totalSpace - $freeSpace;
  385. $result['mountpoint'] = $mountPoint;
  386. $result['total'] = $totalSpace;
  387. $result['free'] = $freeSpace;
  388. $result['used'] = $usedSpace;
  389. $result['usedpercent'] = $this->calcPercentValue($totalSpace, $usedSpace);
  390. $result['freepercent'] = $this->calcPercentValue($totalSpace, $freeSpace);
  391. }
  392. }
  393. return ($result);
  394. }
  395. /**
  396. * Sets the disk statistics property dewpends on preset mountpoints
  397. *
  398. * @return void
  399. */
  400. protected function setDiskStats() {
  401. if (!empty($this->mountPoints)) {
  402. foreach ($this->mountPoints as $idx => $eachMountPoint) {
  403. $eachDiskStat = $this->getDiskStat($eachMountPoint);
  404. if (!empty($eachDiskStat)) {
  405. $this->diskStats[$eachMountPoint] = $eachDiskStat;
  406. }
  407. }
  408. }
  409. }
  410. /**
  411. * Retrieves all disk statistics.
  412. *
  413. * @return array
  414. */
  415. public function getAllDiskStats() {
  416. $this->setDiskStats();
  417. return ($this->diskStats);
  418. }
  419. /**
  420. * Gets the operating system name.
  421. *
  422. * @return string
  423. */
  424. public function getOs() {
  425. return ($this->os);
  426. }
  427. /**
  428. * Gets the release of the operating system.
  429. *
  430. * @return string
  431. */
  432. public function getOsRelease() {
  433. return ($this->osRelease);
  434. }
  435. /**
  436. * Gets the full release of the operating system.
  437. *
  438. * @return string
  439. */
  440. public function getOsFullRelease() {
  441. return ($this->osFullRelease);
  442. }
  443. /**
  444. * Gets the hostname of the system.
  445. *
  446. * @return string
  447. */
  448. public function getHostname() {
  449. return ($this->hostname);
  450. }
  451. /**
  452. * Gets the PHP version used in the system.
  453. *
  454. * @return string
  455. */
  456. public function getPhpVersion() {
  457. return ($this->phpVersion);
  458. }
  459. /**
  460. * Gets the name of the CPU.
  461. *
  462. * @return string
  463. */
  464. public function getCpuName() {
  465. return ($this->cpuName);
  466. }
  467. /**
  468. * Gets the number of CPU cores.
  469. *
  470. * @return int
  471. */
  472. public function getCpuCores() {
  473. return ($this->cpuCores);
  474. }
  475. /**
  476. * Gets the total memory available in bytes.
  477. *
  478. * @return int
  479. */
  480. public function getMemTotal() {
  481. return ($this->memTotal);
  482. }
  483. /**
  484. * Gets the free memory in bytes.
  485. *
  486. * @return int
  487. */
  488. public function getMemFree() {
  489. return ($this->memFree);
  490. }
  491. /**
  492. * Gets the used memory in bytes.
  493. *
  494. * @return int
  495. */
  496. public function getMemUsed() {
  497. return ($this->memUsed);
  498. }
  499. /**
  500. * Gets the number of seconds the system has been running.
  501. *
  502. * @return int
  503. */
  504. public function getUptime() {
  505. return ($this->uptimeSeconds);
  506. }
  507. /**
  508. * Gets the system's load average over 1, 5, and 15 minutes.
  509. *
  510. * @return array
  511. */
  512. public function getLoadAverage() {
  513. return ($this->loadAverage);
  514. }
  515. /**
  516. * Gets the system's load average over 1 minute.
  517. *
  518. * @return float
  519. */
  520. public function getLa1() {
  521. return ($this->la1);
  522. }
  523. /**
  524. * Gets the system's load average over 5 minutes.
  525. *
  526. * @return float
  527. */
  528. public function getLa5() {
  529. return ($this->la5);
  530. }
  531. /**
  532. * Gets the system's load average over 15 minutes.
  533. *
  534. * @return float
  535. */
  536. public function getLa15() {
  537. return ($this->la15);
  538. }
  539. /**
  540. * Gets the system's load percentage based on the number of CPU cores.
  541. *
  542. * @return float
  543. */
  544. public function getSystemLoadPercent() {
  545. return ($this->systemLoadPercent);
  546. }
  547. /**
  548. * Gets the system's load percentage over 1 minute.
  549. *
  550. * @return float
  551. */
  552. public function getLoadPercent1() {
  553. return ($this->loadPercent1);
  554. }
  555. /**
  556. * Gets the system's load percentage over 5 minutes.
  557. *
  558. * @return float
  559. */
  560. public function getLoadPercent5() {
  561. return ($this->loadPercent5);
  562. }
  563. /**
  564. * Gets the system's load percentage over 15 minutes.
  565. *
  566. * @return float
  567. */
  568. public function getLoadPercent15() {
  569. return ($this->loadPercent15);
  570. }
  571. /**
  572. * Gets the average system load percentage.
  573. *
  574. * @return float
  575. */
  576. public function getLoadAvgPercent() {
  577. return ($this->loadAvgPercent);
  578. }
  579. /**
  580. * Gets the mount points used for disk statistics.
  581. *
  582. * @return array
  583. */
  584. public function getMountPoints() {
  585. return ($this->mountPoints);
  586. }
  587. }