ServiceInformation.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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\ProxmoxAddon\App\Enum\Vps\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. $updateButton = new UpdateButton();
  54. if($this->configuration()->isKvm() || ($this->configuration()->isLxc() && $this->configuration()->hasPermissionChangeHostname())){
  55. $this->addButton($updateButton);
  56. }
  57. if ($this->configuration()->isLxc() && KeyPair::ofHostingId($this->getWhmcsParamByKey('serviceid'))->count())
  58. {
  59. $keyPairs = KeyPair::ofHostingId($this->getWhmcsParamByKey('serviceid'))->first();
  60. //URL
  61. $urlService = new UrlService();
  62. //Public Key
  63. $rd = new RedirectButton("sshPublicKeyDownloadButton");
  64. $rd->replaceClasses(['lu-btn lu-btn--sm lu-btn lu-btn--link lu-btn--icon lu-btn--plain lu-btn--default']);
  65. $rd->setIcon('lu-zmdi lu-zmdi-shield-security');
  66. $rd->setHref($urlService->getUrl("home", "sshPublicKeyDownload"));
  67. $this->addButton($rd);
  68. //Private Key
  69. $deletePrivateKey = $this->configuration()->isSshDeletePrivateKey();
  70. if (!$deletePrivateKey && $keyPairs->isPrivate())
  71. {
  72. $rd = new RedirectButton("sshPrivateKeyDownloadButton");
  73. $rd->replaceClasses(['lu-btn lu-btn--sm lu-btn lu-btn--link lu-btn--icon lu-btn--plain lu-btn--default']);
  74. $rd->setIcon('lu-zmdi lu-zmdi-shield-check');
  75. $rd->setHref($urlService->getUrl("home", "sshPrivateKeyDownload"));
  76. $this->addButton($rd);
  77. }
  78. else
  79. {
  80. if ($deletePrivateKey && $keyPairs->isPrivate())
  81. {
  82. $this->addButton(new SshPrivateKeyDownloadButton());
  83. }
  84. }
  85. }
  86. }
  87. public function returnAjaxData()
  88. {
  89. $vars = [];
  90. //Status
  91. $status = $this->vm()->status();
  92. $vars['status'] = [];
  93. if($this->configuration()->hasPermissionInformation("uptime")){
  94. $vars['status']['uptime'] = Utility::uptime($status['uptime']);
  95. }
  96. if($this->configuration()->hasPermissionInformation("cpu")){
  97. $vars['status']['cpu'] = Utility::cpuUsage($status['cpu']);
  98. $vars['status']['cpus'] =(int) $status['cpus'];
  99. }
  100. switch ($status['status'])
  101. {
  102. case 'running':
  103. $vars['status']['status'] = '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr($status['status']) . '</span>';
  104. break;
  105. case 'stoped':
  106. $vars['status']['status'] = '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($status['status']) . '</span>';
  107. default:
  108. $vars['status']['status'] = '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($status['status']) . '</span>';
  109. }
  110. //Memory
  111. if($this->configuration()->hasPermissionInformation("mem")){
  112. $vars['status']['memPercent'] = (int) round($status['mem'] / $status['maxmem'] * 100);
  113. $vars['status']['maxmem'] = Format::convertBytes($status['maxmem']);
  114. $vars['status']['mem'] = Format::convertBytes($status['mem']);
  115. }
  116. if ($this->vm() instanceof Lxc)
  117. {
  118. //SWAP
  119. $vars['status']['swapPercent'] = (int) round($status['swap'] / $status['maxswap'] * 100);
  120. $vars['status']['swap'] = Format::convertBytes($status['swap']);
  121. $vars['status']['maxswap'] = Format::convertBytes($status['maxswap']);
  122. //Disk
  123. $vars['status']['diskPercent'] = (int) round($status['disk'] / $status['maxdisk'] * 100);
  124. $vars['status']['disk'] = Format::convertBytes($status['disk']);
  125. $vars['status']['maxdisk'] = Format::convertBytes($status['maxdisk']);
  126. $vars['status']['diskwrite'] = Format::convertBytes($status['diskwrite']);
  127. //Key Pairs
  128. if (KeyPair::ofHostingId($this->getWhmcsParamByKey('serviceid'))->count())
  129. {
  130. $keyPairs = KeyPair::ofHostingId($this->getWhmcsParamByKey('serviceid'))->first();
  131. $vars['keyPairs']['public'] = $keyPairs->isPrivate();
  132. $vars['keyPairs']['private'] = $keyPairs->isPrivate();
  133. }
  134. }
  135. if ($status['balloon_min'])
  136. {
  137. $vars['status']['balloon_min'] = Format::convertBytes($status['balloon_min']);
  138. }
  139. //Config
  140. $vars['config'] = [ ];
  141. if($this->configuration()->hasPermissionInformation("name")){
  142. $vars['config']['name'] = $this->vm()->config()['name'] ? $this->vm()->config()['name'] : $this->vm()->config()['hostname'];
  143. }
  144. if (isAdmin())
  145. {
  146. $vars['node'] = $this->vm()->getNode();
  147. }
  148. $vars['virtualization'] = $this->vm()->getVirtualization();
  149. // Default configuration option
  150. if (!$this->isWhmcsConfigOption(ConfigurableOption::IPV4) && $this->configuration()->getIpv4() && $this->configuration()->hasPermissionInformation("ipAddresses") )
  151. {
  152. $vars['ipAddresses'] = $this->configuration()->getIpv4();
  153. }
  154. //BackupMaxFiles
  155. if (!$this->isWhmcsConfigOption(ConfigurableOption::BACKUPS_FILES) && $this->configuration()->getBackupMaxFiles() && $this->configuration()->hasPermissionInformation("backupFilesLimit"))
  156. {
  157. $vars['backupsFilesLimit'] = $this->configuration()->getBackupMaxFiles();
  158. if($this->configuration()->getBackupMaxFiles() =="-1"){
  159. $vars['backupsFilesLimit'] = sl('lang')->abtr("Unlimited");
  160. }
  161. }
  162. //NetworkRate
  163. if( $this->configuration()->hasPermissionInformation("networkRate")){
  164. /** @var NetworkDeviceKvm|NetworkDeviceLxc $networkDevice */
  165. $networkDevice = $this->vm()->getNetworkDevices($this->configuration()->getBridge())[0];
  166. if ($networkDevice && $networkDevice->getRate())
  167. {
  168. $vars['networkRate'] = "{$networkDevice->getRate()} MB/s ({$networkDevice->getRateMbps()} Mbps)";
  169. }
  170. }
  171. //KVM
  172. if ($this->vm() instanceof Kvm)
  173. {
  174. //CD-ROM
  175. if($this->configuration()->isPermissionIsoImage()){
  176. $vars['cdrom'] = $this->vm()->getIso();
  177. }
  178. //Boot order
  179. $vars['bootOrder'] = [];
  180. foreach ($this->vm()->getBootOrder() as $device)
  181. {
  182. $vars['bootOrder'][] = sl("lang")->tr("bootOrder", $device);
  183. }
  184. $vars['bootOrder'] = implode(", ", $vars['bootOrder']);
  185. //SSH Public Key
  186. $vars['sshkeysName'] = $this->vm()->sshkeysName();
  187. }
  188. //OS Info
  189. if (isAdmin() && $this->vm()->config()['agent'] == 1)
  190. {
  191. try
  192. {
  193. $vars['osinfo'] = $this->vm()->agent()->getOsinfo()['result'];
  194. $vars['osinfo']['kernelrelease'] = $vars['osinfo']['kernel-release'];
  195. $vars['hostname'] = $this->vm()->agent()->getHostname()['result']['host-name'];
  196. }
  197. catch (\Exception $ex)
  198. {
  199. $vars['quemuAgent'] = $ex->getMessage();
  200. }
  201. }
  202. if (isAdmin() && $this->vm() instanceof Lxc)
  203. {
  204. $features = $this->vm()->config()['features'];
  205. $vars['features'] = $features ? $features : "-";
  206. }
  207. return (new RawDataJsonResponse(['data' => $vars]));
  208. }
  209. }