ServerResources.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Oct 4, 2018)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\UI\Servers\Pages;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. use ModulesGarden\ProxmoxAddon as main;
  22. use WHMCS\Database\Capsule as DB;
  23. use WHMCS\Service\Service;
  24. /**
  25. * Description of ServerResources
  26. *
  27. * @author Pawel Kopec <pawelk@modulesgardne.com>
  28. */
  29. trait ServerResources
  30. {
  31. use main\App\Services\BaseService;
  32. use main\Core\UI\Traits\RequestObjectHandler;
  33. protected $errors = [];
  34. protected $hostingStatus = ["Active"];
  35. private $ramAssigned = 0;
  36. private $ramTotal = 0;
  37. private $ramFree = 0;
  38. private $ramSuspended = 0;
  39. public function getHostingStatus()
  40. {
  41. return $this->hostingStatus;
  42. }
  43. public function setHostingStatus($hostingStatus)
  44. {
  45. $this->hostingStatus = $hostingStatus;
  46. return $this;
  47. }
  48. public function ramAssignedToSring()
  49. {
  50. return main\App\Libs\Format::convertBytes($this->ramAssigned);
  51. }
  52. public function ramTotalToSring()
  53. {
  54. return main\App\Libs\Format::convertBytes($this->ramTotal);
  55. }
  56. public function ramFreeToSring()
  57. {
  58. return main\App\Libs\Format::convertBytes($this->ramFree);
  59. }
  60. public function ramSuspendedToSring()
  61. {
  62. return main\App\Libs\Format::convertBytes($this->ramSuspended);
  63. }
  64. protected function ramDetails()
  65. {
  66. $nodeRepository = new proxmox\repository\NodeRepository();
  67. $serverAssignedIps = $this->getServer()->assignedips;
  68. $serverAssignedIps = preg_replace('/\s+/', '', $serverAssignedIps);
  69. $serverAssignedIps = explode(PHP_EOL, $serverAssignedIps);
  70. $privateIp = current($serverAssignedIps);
  71. $serverIp = $privateIp ? $privateIp : $this->getServer()->ipaddress;
  72. $node = $nodeRepository->findWithHostOrIp($this->getServer()->hostname, $serverIp );
  73. //Get hosting for this server with status active and suspended
  74. foreach (Service::where('server', $this->getServerId())->whereIn('domainstatus', $this->hostingStatus)->get() as $hosting)
  75. {
  76. try
  77. {
  78. if ($this->getServer()->type == "proxmoxVPS")
  79. {
  80. $customFields = new \stdClass;
  81. foreach ($hosting->customFieldValues as $cf)
  82. {
  83. if ($cf->value && preg_match("/node/", $cf->customField->fieldName))
  84. {
  85. $customFields->node = $cf->value;
  86. }
  87. if ($cf->value && preg_match("/vmid/", $cf->customField->fieldName))
  88. {
  89. $customFields->vmid = $cf->value;
  90. }
  91. }
  92. if (!empty($customFields->vmid) && !empty($customFields->node) && $node->getNode() == $customFields->node)
  93. {
  94. $customFields->type = DB::table('ProxmoxAddon_ProductConfiguration')
  95. ->where('product_id', $hosting->packageId)
  96. ->where("setting", "virtualization")
  97. ->value("value");
  98. $customFields->type = \json_decode(strtolower($customFields->type), true);
  99. $customFields->virtualization = $customFields->type;
  100. $vm = proxmox\Factory::vm($customFields);
  101. $this->vmDetails($vm, $hosting);
  102. }
  103. }
  104. else
  105. {
  106. if ($this->getServer()->type == "ProxmoxCloudVps")
  107. {
  108. $query = DB::table('ProxmoxAddon_Vm')
  109. ->where('hosting_id', $hosting->id)
  110. ->where('node', $node->getNode())
  111. ->select('node', 'vmid', 'virtualization', 'node');
  112. foreach ($query->get() as $vserver)
  113. {
  114. $vm = proxmox\Factory::vm($vserver);
  115. $this->vmDetails($vm, $hosting);
  116. }
  117. }
  118. }
  119. $this->ramTotal = $node->getStatus()['memory']['total'];
  120. $this->ramFree = $this->ramTotal - $this->ramAssigned;
  121. }
  122. catch (\Exception $ex)
  123. {
  124. if( main\App\Models\Job::ofHostingId($hosting->id)->waiting()->count()){
  125. continue;
  126. }
  127. $this->errors[] = sprintf("Hosting #%s %s", $hosting->id, $ex->getMessage() );
  128. }
  129. }
  130. }
  131. private function vmDetails(proxmox\interfaces\VmInterface $vm, $hosting)
  132. {
  133. $stat = $vm->status();
  134. if (!empty($stats['maxmem']))
  135. {
  136. $memory = $stats['maxmem'];
  137. }
  138. else
  139. {
  140. $config = $vm->config();
  141. $memory = (int)$config['memory'] * 1048576;
  142. }
  143. if ($hosting->domainStatus == "Active")
  144. {
  145. $this->ramAssigned += $memory;
  146. }
  147. else
  148. {
  149. if ($hosting->domainStatus == "Suspended")
  150. {
  151. $this->ramSuspended += $memory;
  152. }
  153. }
  154. }
  155. }