RrdDataCommand.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (Apr 19, 2019)
  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\Cron;
  20. use MGProvision\Proxmox\v2\VmFactory;
  21. use ModulesGarden\ProxmoxAddon\App\Models\RrdData;
  22. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  23. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
  24. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  25. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Command;
  26. use ModulesGarden\ProxmoxAddon\Core\CommandLine\Hypervisor;
  27. use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Server;
  28. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. use Symfony\Component\Console\Style\SymfonyStyle;
  32. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  33. /**
  34. * Description of Users
  35. *
  36. * @author Pawel Kopec <pawelk@modulesgardne.com>
  37. */
  38. class RrdDataCommand extends Command
  39. {
  40. use WhmcsParams;
  41. use \ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  42. use ApiService;
  43. /**
  44. * Command name
  45. * @var string
  46. */
  47. protected $name = 'rrddata';
  48. /**
  49. * Command description
  50. * @var string
  51. */
  52. protected $description = 'Updating RRD Data';
  53. /**
  54. * Command help text
  55. * @var string
  56. */
  57. protected $help = '';
  58. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  59. {
  60. $io->title('Update RRD Data: Starting');
  61. if (!function_exists('ModuleBuildParams'))
  62. {
  63. require_once ROOTDIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . "modulefunctions.php";
  64. }
  65. //Update hosting bandwidth
  66. $h = (new Hosting())->getTable();
  67. $s = (new Server())->getTable();
  68. $hostings = Hosting::select("{$h}.id", "{$h}.packageid", "{$h}.lastupdate", "{$h}.bwusage", "{$h}.bwlimit", "{$h}.regdate")
  69. ->rightJoin($s, "{$h}.server", '=', "{$s}.id")
  70. ->where("{$h}.domainstatus", "Active")
  71. ->whereIn("{$s}.type", [ "ProxmoxCloudVps"])
  72. ->orderBy("{$h}.server");
  73. $i = 0;
  74. /**
  75. * @var Hosting $hosting
  76. */
  77. foreach ($hostings->get() as $hosting)
  78. {
  79. $i++;
  80. $output->writeln(sprintf("Synchronize hosting: %s", $hosting->id));
  81. try
  82. {
  83. $params = \ModuleBuildParams($hosting->id);
  84. sl("whmcsParams")->setParams($params);
  85. unset($this->vm, $this->api, $this->configuration, $this->productId, $this->hostingId);
  86. $this->api();
  87. RrdData::ofHostingId($hosting->id)->olderThanDayAgo()->delete();
  88. if ($params['moduletype'] == "ProxmoxCloudVps"){
  89. $vservers = VmModel::ofHostingId($hosting->id);
  90. foreach ($vservers->get() as $vserver)
  91. {
  92. $vm = (new VmFactory())->fromVmModel($vserver);
  93. $rrData = $vm->rrdData(["timeframe" => "hour", "cf" => "AVERAGE"]);
  94. foreach ($rrData as $k => $rrd)
  95. {
  96. $rrdModel = new RrdData();
  97. $rrdModel->fill($rrd);
  98. $rrdModel->time = date("Y-m-d H:i:s", $rrd['time']);
  99. $rrdModel->hosting_id = $hosting->id;
  100. $rrdModel->vm_id = $vserver->id;
  101. $rrdModel->save();
  102. }
  103. }
  104. $output->writeln(sprintf("Hosting: %s has been synchronized", $hosting->id));
  105. }
  106. }
  107. catch (\Exception $ex)
  108. {
  109. $msg = $ex->getMessage();
  110. if ($hosting)
  111. {
  112. $msg = sprintf("Hosting Id #%s, %s", $hosting->id, $ex->getMessage());
  113. }
  114. $io->error($msg);
  115. }
  116. (new Hypervisor($this->getName(), $input->getOptions()))
  117. ->ping();
  118. }
  119. $output->writeln("");
  120. $io->success([
  121. sprintf("Synchronize hostings: %s Entries Processed.", $i),
  122. "Update RRD Data: Done"
  123. ]);
  124. }
  125. }