ServiceInformation.php 9.0 KB

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