LoadBalancerService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS product developed. (Jan 16, 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\Services;
  20. use MGProvision\Proxmox\v2\models\Kvm;
  21. use MGProvision\Proxmox\v2\models\Lxc;
  22. use ModulesGarden\ProxmoxAddon\App\Models\NodeSetting;
  23. use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Hosting;
  24. use ModulesGarden\ProxmoxAddon\Core\Models\ModuleSettings\Model as Settings;
  25. /**
  26. * Description of LoadBalancerService
  27. *
  28. * @author Pawel Kopec <pawelk@modulesgardne.com>
  29. */
  30. class LoadBalancerService
  31. {
  32. protected $nodesResurces = [];
  33. private $serverId;
  34. /**
  35. *
  36. * @var \MGProvision\Proxmox\v2\Api
  37. */
  38. private $api;
  39. private $usage = false;
  40. private $filter = [];
  41. private $excludeVmid;
  42. private $vmsWeight;
  43. private $ramWeight;
  44. private $diskWeight;
  45. private $cpuWeight;
  46. function __construct($serverId)
  47. {
  48. $this->serverId = $serverId;
  49. }
  50. public function getExcludeVmid()
  51. {
  52. return $this->excludeVmid;
  53. }
  54. public function setExcludeVmid($excludeVmid)
  55. {
  56. $this->excludeVmid = $excludeVmid;
  57. return $this;
  58. }
  59. /**
  60. * @return LoadBalancerService
  61. */
  62. public function findByVmCreate()
  63. {
  64. //Node settings
  65. $this->nodesResurces = [];
  66. $query = NodeSetting::ofServer($this->serverId)->ofSetting('vmCreate')->ofValue(1);
  67. foreach ($query->get() as $entity)
  68. {
  69. $this->nodesResurces[] = $this->format($entity);
  70. }
  71. return clone $this;
  72. }
  73. private function format(NodeSetting $entity)
  74. {
  75. $nodesResurce = [
  76. 'node' => $entity->node,
  77. 'vmCreate' => NodeSetting::ofServer($this->serverId)->ofNode($entity->node)->ofSetting('vmCreate')->value('value'),
  78. 'vmsMax' => NodeSetting::ofServer($this->serverId)->ofNode($entity->node)->ofSetting('vmsMax')->value('value'),
  79. 'vmsWeight' => NodeSetting::ofServer($this->serverId)->ofNode($entity->node)->ofSetting('vmsWeight')->value('value'),
  80. 'vmsUsed' => 0,
  81. 'vmsFree' => null,
  82. 'cpuMax' => NodeSetting::ofServer($this->serverId)->ofNode($entity->node)->ofSetting('cpuMax')->value('value'),
  83. 'cpuWeight' => NodeSetting::ofServer($this->serverId)->ofNode($entity->node)->ofSetting('cpuWeight')->value('value'),
  84. 'cpuUsed' => 0,
  85. 'cpuFree' => null,
  86. 'diskUsed' => 0,
  87. 'diskMax' => NodeSetting::ofServer($this->serverId)->ofNode($entity->node)->ofSetting('diskMax')->value('value'),
  88. 'diskWeight' => NodeSetting::ofServer($this->serverId)->ofNode($entity->node)->ofSetting('diskWeight')->value('value'),
  89. 'diskFree' => null,
  90. 'ramMax' => NodeSetting::ofServer($this->serverId)->ofNode($entity->node)->ofSetting('ramMax')->value('value'),
  91. 'ramWeight' => NodeSetting::ofServer($this->serverId)->ofNode($entity->node)->ofSetting('ramWeight')->value('value'),
  92. 'ramUsed' => 0,
  93. 'ramFree' => 0,
  94. ];
  95. if ($nodesResurce['diskMax'])
  96. {
  97. $nodesResurce['diskMax'] = $nodesResurce['diskMax'] * pow(1024, 3);
  98. }
  99. if ($nodesResurce['ramMax'])
  100. {
  101. $nodesResurce['ramMax'] = $nodesResurce['ramMax'] * pow(1024, 3);
  102. }
  103. return $nodesResurce;
  104. }
  105. /**
  106. * @return LoadBalancerService
  107. */
  108. public function findByNode($node, $vmCreate = true)
  109. {
  110. $this->filter['node'] = $node;
  111. $this->usage = false;
  112. //Node settings
  113. $this->nodesResurces = [];
  114. $query = NodeSetting::ofServer($this->serverId)
  115. ->ofNode($node)
  116. ->ofSetting('vmCreate');
  117. if ($vmCreate)
  118. {
  119. $query->ofValue(1);
  120. }
  121. if(!$query->count()){
  122. $entity = new NodeSetting();
  123. $entity->node = $node;
  124. $entity->server_id = $this->serverId;
  125. $entity->setting = 'vmsMax';
  126. $entity->value = "";
  127. $this->nodesResurces[] = $this->format($entity);
  128. }
  129. foreach ($query->get() as $entity)
  130. {
  131. $this->nodesResurces[] = $this->format($entity);
  132. }
  133. return clone $this;
  134. }
  135. /**
  136. * @return LoadBalancerService
  137. */
  138. public function findNotUser($userId)
  139. {
  140. $this->filter['notUserId'] = $userId;
  141. foreach ($this->nodesResurces as $k => $nodeResurce)
  142. {
  143. if ($this->hasNode($userId, $nodeResurce['node']))
  144. {
  145. unset($this->nodesResurces[$k]);
  146. }
  147. }
  148. return clone $this;
  149. }
  150. private function hasNode($userId, $node)
  151. {
  152. $total = Hosting::ofUserId($userId)
  153. ->ofServerId($this->serverId)
  154. ->active()
  155. ->ofCustomFieldNode($node)
  156. ->count();
  157. try
  158. {
  159. $total += Hosting::ofUserId($userId)
  160. ->ofServerId($this->serverId)
  161. ->active()
  162. ->ofvServerNode($node)
  163. ->count();
  164. }
  165. catch (\Exception $ex)
  166. {//Table does not exist
  167. }
  168. return $total > 0;
  169. }
  170. /**
  171. * @return LoadBalancerService
  172. */
  173. public function findByRam($bytes)
  174. {
  175. $this->filter['ram'] = $bytes;
  176. $this->usage();
  177. foreach ($this->nodesResurces as $k => $nodeResurce)
  178. {
  179. if (!is_null($nodeResurce['ramFree']) && $nodeResurce['ramFree'] < $bytes)
  180. {
  181. unset($this->nodesResurces[$k]);
  182. }
  183. }
  184. return clone $this;
  185. }
  186. private function usage()
  187. {
  188. if ($this->usage)
  189. {
  190. return true;
  191. }
  192. if (!$this->api instanceof \MGProvision\Proxmox\v2\Api)
  193. {
  194. throw new \Exception("API instance is not initialized");
  195. }
  196. $nodes = [];
  197. foreach ($this->api->get("/nodes") as $node)
  198. {
  199. if ($node['status'] != "online")
  200. {
  201. continue;
  202. }
  203. $nodes[] = $node['node'];
  204. }
  205. foreach ($this->nodesResurces as $k => &$nodeResurce)
  206. {
  207. if (!in_array($nodeResurce['node'], $nodes))
  208. {
  209. continue;
  210. }
  211. //kvm
  212. $qemu = $this->api->get("/nodes/{$nodeResurce['node']}/qemu");
  213. foreach ($qemu as $record)
  214. {
  215. if ($record['template'] || ($this->excludeVmid && $this->excludeVmid == $record['vmid']) || !$this->hasVmid($record['vmid']))
  216. {
  217. continue;
  218. }
  219. $vm = new Kvm($nodeResurce['node'], $record['vmid']);
  220. $vm->setApi($this->api);
  221. foreach ($vm->getHardDisks() as $hardDisk)
  222. {
  223. $nodeResurce['diskUsed'] += $hardDisk->getBytes();
  224. }
  225. $nodeResurce['ramUsed'] += $record['maxmem'];
  226. $nodeResurce['vmsUsed']++;
  227. $nodeResurce['cpuUsed'] += $record['cpus'];
  228. }
  229. unset($qemu, $record, $vm);
  230. //lxc
  231. $lxc = $this->api->get("/nodes/{$nodeResurce['node']}/lxc");
  232. foreach ($lxc as $record)
  233. {
  234. if ($record['template'] || ($this->excludeVmid && $this->excludeVmid == $record['vmid']) || !$this->hasVmid($record['vmid']))
  235. {
  236. continue;
  237. }
  238. $vm = new Lxc($nodeResurce['node'], $record['vmid']);
  239. $vm->setApi($this->api);
  240. foreach ($vm->getMounPoints()->fetch() as $mounPoint)
  241. {
  242. /*@var $mounPoint MountPoint */
  243. $nodeResurce['diskUsed'] += $mounPoint->getBytes();
  244. }
  245. $nodeResurce['ramUsed'] += $record['maxmem'];
  246. $nodeResurce['vmsUsed']++;
  247. $nodeResurce['cpuUsed'] += $record['cpus'];
  248. }
  249. unset($lxc, $record, $vm);
  250. }
  251. //calculate free resurces
  252. foreach ($this->nodesResurces as $k => &$nodeResurce)
  253. {
  254. $nodeResurce['ramFree'] = $nodeResurce['ramMax'] - $nodeResurce['ramUsed'];
  255. $nodeResurce['vmsFree'] = $nodeResurce['vmsMax'] - $nodeResurce['vmsUsed'];
  256. $nodeResurce['cpuFree'] = $nodeResurce['cpuMax'] - $nodeResurce['cpuUsed'];
  257. $nodeResurce['diskFree'] = $nodeResurce['diskMax'] - $nodeResurce['diskUsed'];
  258. }
  259. $this->usage = true;
  260. }
  261. private function hasVmid($vmid)
  262. {
  263. $total = Hosting::ofServerId($this->serverId)
  264. ->ofStatus(['Active', 'Suspended'])
  265. ->ofCustomFielVmid($vmid)
  266. ->count();
  267. try
  268. {
  269. $total += Hosting::ofServerId($this->serverId)
  270. ->active()
  271. ->ofvServerVmid($vmid)
  272. ->count();
  273. }
  274. catch (\Exception $ex)
  275. {//Table does not exist
  276. }
  277. return $total > 0;
  278. }
  279. /**
  280. * @return LoadBalancerService
  281. */
  282. public function findByDisk($bytes)
  283. {
  284. $this->filter['disk'] = $bytes;
  285. $this->usage();
  286. foreach ($this->nodesResurces as $k => $nodeResurce)
  287. {
  288. if (!is_null($nodeResurce['diskFree']) && $nodeResurce['diskFree'] < $bytes)
  289. {
  290. unset($this->nodesResurces[$k]);
  291. }
  292. }
  293. return clone $this;
  294. }
  295. /**
  296. * @return LoadBalancerService
  297. */
  298. public function findByCpu($cpu)
  299. {
  300. $this->filter['cpu'] = $cpu;
  301. $this->usage();
  302. foreach ($this->nodesResurces as $k => $nodeResurce)
  303. {
  304. if (!is_null($nodeResurce['cpuFree']) && $nodeResurce['cpuFree'] < $cpu)
  305. {
  306. unset($this->nodesResurces[$k]);
  307. }
  308. }
  309. return clone $this;
  310. }
  311. /**
  312. * @return LoadBalancerService
  313. */
  314. public function findByVms($number)
  315. {
  316. $this->filter['vms'] = $number;
  317. $this->usage();
  318. foreach ($this->nodesResurces as $k => $nodeResurce)
  319. {
  320. if (!is_null($nodeResurce['vmsFree']) && $nodeResurce['vmsFree'] < $number)
  321. {
  322. unset($this->nodesResurces[$k]);
  323. }
  324. }
  325. return clone $this;
  326. }
  327. public function getApi()
  328. {
  329. return $this->api;
  330. }
  331. public function setApi(\MGProvision\Proxmox\v2\Api $api)
  332. {
  333. $this->api = $api;
  334. return $this;
  335. }
  336. public function getNodesResurces()
  337. {
  338. $this->usage();
  339. return $this->nodesResurces;
  340. }
  341. public function count()
  342. {
  343. return count($this->nodesResurces);
  344. }
  345. public function isEmpty()
  346. {
  347. return empty($this->nodesResurces);
  348. }
  349. public function nodeLowLoad()
  350. {
  351. $this->usage();
  352. $weight = Settings::where("setting", 'vmsWeight')->value("value");
  353. $this->vmsWeight = $weight ? $weight : 1;
  354. $weight = Settings::where("setting", 'cpuWeight')->value("value");
  355. $this->cpuWeight = $weight ? $weight : 1;
  356. $weight = Settings::where("setting", 'ramWeight')->value("value");
  357. $this->ramWeight = $weight ? $weight : 1;
  358. $weight = Settings::where("setting", 'diskWeight')->value("value");
  359. $this->diskWeight = $weight ? $weight : 1;
  360. $nodes = [];
  361. //By ram
  362. $nodes = $this->nodesUsage($nodes, $this->orderByRamUsage(), $this->ramWeight);
  363. //By cpu
  364. $nodes = $this->nodesUsage($nodes, $this->orderByCpuUsage(), $this->cpuWeight);
  365. //By Disk
  366. $nodes = $this->nodesUsage($nodes, $this->orderByDiskUsage(), $this->diskWeight);
  367. //By Vms
  368. $nodes = $this->nodesUsage($nodes, $this->orderByVmsUsage(), $this->vmsWeight);
  369. if (empty($nodes))
  370. {
  371. throw new \Exception("Load Balancer: Cannot find node with free resources");
  372. }
  373. asort($nodes);
  374. $nodes = array_reverse($nodes);
  375. return key($nodes);
  376. }
  377. private function nodesUsage($nodes, $values, $weight = 1)
  378. {
  379. foreach (array_reverse($values) as $index => $nodeName)
  380. {
  381. if (!array_key_exists($nodeName, $nodes))
  382. {
  383. $nodes[$nodeName] = 0;
  384. }
  385. $nodes[$nodeName] += ($index + 1) * $weight; // we don't want zero value so we add "1"
  386. }
  387. return $nodes;
  388. }
  389. /**
  390. * @return LoadBalancerService
  391. */
  392. public function orderByRamUsage()
  393. {
  394. return $this->orderBy('ramUsed');
  395. }
  396. private function orderBy($column)
  397. {
  398. $this->usage();
  399. $column = array_column($this->nodesResurces, $column);
  400. array_multisort($column, SORT_ASC, $this->nodesResurces);
  401. $nodes = [];
  402. foreach ($column as $k => $v)
  403. {
  404. $nodes[] = $this->nodesResurces[$k]['node'];
  405. }
  406. return $nodes;
  407. }
  408. public function orderByCpuUsage()
  409. {
  410. return $this->orderBy('ramUsed');
  411. }
  412. public function orderByDiskUsage()
  413. {
  414. return $this->orderBy('vmsUsed');
  415. }
  416. public function orderByVmsUsage()
  417. {
  418. return $this->orderBy('vmsUsed');
  419. }
  420. }