ServiceInformation.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (26.03.19)
  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\Servers\ProxmoxVps\App\UI\ServiceInformation\Pages;
  20. use MGProvision\Proxmox\v2\models\Kvm;
  21. use MGProvision\Proxmox\v2\models\Lxc;
  22. use MGProvision\Proxmox\v2\models\NetworkDeviceKvm;
  23. use MGProvision\Proxmox\v2\models\NetworkDeviceLxc;
  24. use ModulesGarden\ProxmoxAddon\App\Libs\Format;
  25. use ModulesGarden\ProxmoxAddon\App\Models\KeyPair;
  26. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  27. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  28. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  29. use ModulesGarden\ProxmoxAddon\App\Services\Vps\UrlService;
  30. use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Buttons\RedirectButton;
  31. use ModulesGarden\Servers\ProxmoxVps\App\UI\ServiceInformation\Buttons\SshPrivateKeyDownloadButton;
  32. use ModulesGarden\Servers\ProxmoxVps\App\UI\ServiceInformation\Buttons\UpdateButton;
  33. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Builder\BaseContainer;
  34. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
  35. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AjaxElementInterface;
  36. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  37. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\RawDataJsonResponse;
  38. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\isAdmin;
  39. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  40. use ModulesGarden\Servers\ProxmoxVps\App\Enum\ConfigurableOption;
  41. class ServiceInformation extends BaseContainer implements ClientArea, AdminArea, AjaxElementInterface
  42. {
  43. use ProductService;
  44. use ApiService;
  45. protected $id = 'serviceInformationDataTable';
  46. protected $title = 'serviceInformationDataTable';
  47. protected $vueComponent = true;
  48. protected $defaultVueComponentName = 'mg-serviceInformationDataTable';
  49. public function initContent()
  50. {
  51. $this->addClass('lu-text-left');
  52. //Update
  53. $this->addButton(new UpdateButton());
  54. if ($this->configuration()->isLxc() && KeyPair::ofHostingId($this->getWhmcsParamByKey('serviceid'))->count())
  55. {
  56. $keyPairs = KeyPair::ofHostingId($this->getWhmcsParamByKey('serviceid'))->first();
  57. //URL
  58. $urlService = new UrlService();
  59. //Public Key
  60. $rd = new RedirectButton("sshPublicKeyDownloadButton");
  61. $rd->replaceClasses(['lu-btn lu-btn--sm lu-btn lu-btn--link lu-btn--icon lu-btn--plain lu-btn--default']);
  62. $rd->setIcon('lu-zmdi lu-zmdi-shield-security');
  63. $rd->setHref($urlService->getUrl("home", "sshPublicKeyDownload"));
  64. $this->addButton($rd);
  65. //Private Key
  66. $deletePrivateKey = $this->configuration()->isSshDeletePrivateKey();
  67. if (!$deletePrivateKey && $keyPairs->isPrivate())
  68. {
  69. $rd = new RedirectButton("sshPrivateKeyDownloadButton");
  70. $rd->replaceClasses(['lu-btn lu-btn--sm lu-btn lu-btn--link lu-btn--icon lu-btn--plain lu-btn--default']);
  71. $rd->setIcon('lu-zmdi lu-zmdi-shield-check');
  72. $rd->setHref($urlService->getUrl("home", "sshPrivateKeyDownload"));
  73. $this->addButton($rd);
  74. }
  75. else
  76. {
  77. if ($deletePrivateKey && $keyPairs->isPrivate())
  78. {
  79. $this->addButton(new SshPrivateKeyDownloadButton());
  80. }
  81. }
  82. }
  83. }
  84. public function returnAjaxData()
  85. {
  86. $vars = [];
  87. //Status
  88. $status = $this->vm()->status();
  89. $vars['status'] = [
  90. "uptime" => Utility::uptime($status['uptime']),
  91. 'cpu' => Utility::cpuUsage($status['cpu']),
  92. 'cpus' => (int) $status['cpus']
  93. ];
  94. switch ($status['status'])
  95. {
  96. case 'running':
  97. $vars['status']['status'] = '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr($status['status']) . '</span>';
  98. break;
  99. case 'stoped':
  100. $vars['status']['status'] = '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($status['status']) . '</span>';
  101. default:
  102. $vars['status']['status'] = '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($status['status']) . '</span>';
  103. }
  104. //Memory
  105. $vars['status']['memPercent'] = (int) round($status['mem'] / $status['maxmem'] * 100);
  106. $vars['status']['maxmem'] = Format::convertBytes($status['maxmem']);
  107. $vars['status']['mem'] = Format::convertBytes($status['mem']);
  108. if ($this->vm() instanceof Lxc)
  109. {
  110. //SWAP
  111. $vars['status']['swapPercent'] = (int) round($status['swap'] / $status['maxswap'] * 100);
  112. $vars['status']['swap'] = Format::convertBytes($status['swap']);
  113. $vars['status']['maxswap'] = Format::convertBytes($status['maxswap']);
  114. //Disk
  115. $vars['status']['diskPercent'] = (int) round($status['disk'] / $status['maxdisk'] * 100);
  116. $vars['status']['disk'] = Format::convertBytes($status['disk']);
  117. $vars['status']['maxdisk'] = Format::convertBytes($status['maxdisk']);
  118. $vars['status']['diskwrite'] = Format::convertBytes($status['diskwrite']);
  119. //Key Pairs
  120. if (KeyPair::ofHostingId($this->getWhmcsParamByKey('serviceid'))->count())
  121. {
  122. $keyPairs = KeyPair::ofHostingId($this->getWhmcsParamByKey('serviceid'))->first();
  123. $vars['keyPairs']['public'] = $keyPairs->isPrivate();
  124. $vars['keyPairs']['private'] = $keyPairs->isPrivate();
  125. }
  126. }
  127. if ($status['balloon_min'])
  128. {
  129. $vars['status']['balloon_min'] = Format::convertBytes($status['balloon_min']);
  130. }
  131. //Config
  132. $vars['config'] = [
  133. "name" => $this->vm()->config()['name'] ? $this->vm()->config()['name'] : $this->vm()->config()['hostname']
  134. ];
  135. if (isAdmin())
  136. {
  137. $vars['node'] = $this->vm()->getNode();
  138. }
  139. $vars['virtualization'] = $this->vm()->getVirtualization();
  140. // Default configuration option
  141. if (!$this->isWhmcsConfigOption(ConfigurableOption::IPV4) && $this->configuration()->getIpv4())
  142. {
  143. $vars['ipAddresses'] = $this->configuration()->getIpv4();
  144. }
  145. //BackupMaxFiles
  146. if (!$this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) && $this->configuration()->getBackupMaxFiles())
  147. {
  148. $vars['backupsFilesLimit'] = $this->configuration()->getBackupMaxFiles();
  149. if($this->configuration()->getBackupMaxFiles() =="-1"){
  150. $vars['backupsFilesLimit'] = sl('lang')->abtr("Unlimited");
  151. }
  152. }
  153. //NetworkRate
  154. /** @var NetworkDeviceKvm|NetworkDeviceLxc $networkDevice */
  155. $networkDevice = $this->vm()->getNetworkDevices($this->configuration()->getBridge())[0];
  156. if ($networkDevice && $networkDevice->getRate())
  157. {
  158. $vars['networkRate'] = "{$networkDevice->getRate()} MB/s ({$networkDevice->getRateMbps()} Mbps)";
  159. }
  160. //KVM
  161. if ($this->vm() instanceof Kvm)
  162. {
  163. //CD-ROM
  164. $vars['cdrom'] = $this->vm()->getIso();
  165. //Boot order
  166. $vars['bootOrder'] = [];
  167. foreach ($this->vm()->getBootOrder() as $device)
  168. {
  169. $vars['bootOrder'][] = sl("lang")->tr("bootOrder", $device);
  170. }
  171. $vars['bootOrder'] = implode(", ", $vars['bootOrder']);
  172. //SSH Public Key
  173. $vars['sshkeysName'] = $this->vm()->sshkeysName();
  174. }
  175. //OS Info
  176. if (isAdmin() && $this->vm()->config()['agent'] == 1)
  177. {
  178. try
  179. {
  180. $vars['osinfo'] = $this->vm()->agent()->getOsinfo()['result'];
  181. $vars['osinfo']['kernelrelease'] = $vars['osinfo']['kernel-release'];
  182. $vars['hostname'] = $this->vm()->agent()->getHostname()['result']['host-name'];
  183. }
  184. catch (\Exception $ex)
  185. {
  186. $vars['quemuAgent'] = $ex->getMessage();
  187. }
  188. }
  189. if (isAdmin() && $this->vm() instanceof Lxc)
  190. {
  191. $features = $this->vm()->config()['features'];
  192. $vars['features'] = $features ? $features : "-";
  193. }
  194. return (new RawDataJsonResponse(['data' => $vars]));
  195. }
  196. }