api.sysproc.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * System background process lookup
  4. */
  5. class SysProc {
  6. /**
  7. * Contains binpaths.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $binPaths = array();
  12. /**
  13. * Contains processes list loaded at instance creation as pid=>processString
  14. *
  15. * @var array
  16. */
  17. protected $runningProcessList = array();
  18. public function __construct($loadProcList = true) {
  19. $this->loadConfigs();
  20. if ($loadProcList) {
  21. $this->setProcessList();
  22. }
  23. }
  24. /**
  25. * Loads some required configs
  26. *
  27. * @global $ubillingConfig
  28. *
  29. * @return void
  30. */
  31. protected function loadConfigs() {
  32. global $ubillingConfig;
  33. $this->binPaths = $ubillingConfig->getBinpaths();
  34. }
  35. /**
  36. * Sets current instance process list
  37. *
  38. * @return void
  39. */
  40. public function setProcessList() {
  41. $this->runningProcessList = $this->getAllProcessPids();
  42. }
  43. /**
  44. * Returns all running process PID-s array as pid=>processString
  45. *
  46. * @return array
  47. */
  48. public function getAllProcessPids() {
  49. $result = array();
  50. $command = $this->binPaths['PS'] . ' ax';
  51. $rawResult = shell_exec($command);
  52. if (!empty($rawResult)) {
  53. $rawResult = explodeRows($rawResult);
  54. foreach ($rawResult as $io => $eachLine) {
  55. $eachLine = trim($eachLine);
  56. $rawLine = $eachLine;
  57. $eachLine = explode(' ', $eachLine);
  58. if (isset($eachLine[0])) {
  59. $eachPid = $eachLine[0];
  60. if (is_numeric($eachPid)) {
  61. $result[$eachPid] = $rawLine;
  62. }
  63. }
  64. }
  65. }
  66. return ($result);
  67. }
  68. /**
  69. * Searches for process PIDs that match a given query string.
  70. *
  71. * This method iterates through the list of running processes and checks if the
  72. * process string contains the specified query. If a match is found, the PID and
  73. * process string are added to the result array.
  74. *
  75. * @param string $query The query string to search for within the process strings.
  76. *
  77. * @return array
  78. */
  79. public function searchProcessPids($query = '') {
  80. $result = array();
  81. if (!empty($query)) {
  82. if (!empty($this->runningProcessList)) {
  83. foreach ($this->runningProcessList as $eachPid => $eachProcessString) {
  84. if (ispos($eachProcessString, $query)) {
  85. $result[$eachPid] = $eachProcessString;
  86. }
  87. }
  88. }
  89. }
  90. return ($result);
  91. }
  92. /**
  93. * Checks if any process is available that matches the given substring.
  94. *
  95. * This method searches for process IDs (PIDs) that contain the specified substring.
  96. * If any matching PIDs are found, it returns true; otherwise, it returns false.
  97. *
  98. * @param string $subString The substring to search for in process names.
  99. *
  100. * @return bool
  101. */
  102. public function isAnyProcAvail($subString = '') {
  103. $result = false;
  104. if (!empty($subString)) {
  105. $filteredProcessPids = $this->searchProcessPids($subString);
  106. if (!empty($filteredProcessPids)) {
  107. $result = true;
  108. }
  109. }
  110. return ($result);
  111. }
  112. /**
  113. * Shortcut to fast check is some filename being somewhere in runninng process strings or not?
  114. *
  115. * @param string $fileName
  116. *
  117. * @return bool
  118. */
  119. public function isFileInUse($fileName) {
  120. return ($this->isAnyProcAvail($fileName));
  121. }
  122. }