scan_servers_route.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. if(!DEFINED('EGP'))
  3. exit(header('Refresh: 0; URL=http://'.$_SERVER['SERVER_NAME'].'/404'));
  4. class scan_servers_route extends cron
  5. {
  6. function __construct()
  7. {
  8. global $cfg, $sql, $argv, $start_point;
  9. $servers = $argv;
  10. unset($servers[0], $servers[1], $servers[2]);
  11. $sql->query('SELECT `address` FROM `units` WHERE `id`="'.$servers[4].'" LIMIT 1');
  12. if(!$sql->num())
  13. return NULL;
  14. $unit = $sql->get();
  15. $game = $servers[3];
  16. unset($servers[3], $servers[4]);
  17. $sql->query('SELECT `unit` FROM `servers` WHERE `id`="'.$servers[5].'" LIMIT 1');
  18. $server = $sql->get();
  19. $sql->query('SELECT `address`, `passwd`, `ram` FROM `units` WHERE `id`="'.$server['unit'].'" LIMIT 1');
  20. $unit = $sql->get();
  21. include(LIB.'ssh.php');
  22. // Проверка ssh соедниения пу с локацией
  23. if(!$ssh->auth($unit['passwd'], $unit['address']))
  24. return NULL;
  25. $first = $ssh->get('cat /proc/stat');
  26. sleep(1);
  27. $aCpu = sys::cpu_idle(array($first, $ssh->get('cat /proc/stat')), false, true);
  28. array_shift($aCpu);
  29. $idle = array();
  30. $uses = array();
  31. foreach($aCpu as $cpu => $data)
  32. {
  33. $core = sys::int($cpu)+1;
  34. $sql->query('SELECT `id` FROM `servers` WHERE `unit`="'.$server['unit'].'" AND `core_fix`="'.$core.'" AND `core_fix`="1" LIMIT 1');
  35. if($sql->num())
  36. {
  37. unset($aCpu[$cpu]);
  38. continue;
  39. }
  40. if($data['idle'] > 50)
  41. $idle[$core] = $data['idle'];
  42. else
  43. $uses[$core] = 100-$data['idle'];
  44. }
  45. if(!count($idle))
  46. return NULL;
  47. foreach($uses as $use_core => $use)
  48. {
  49. if(!count($idle))
  50. break;
  51. $sql->query('SELECT `id`, `uid` FROM `servers` WHERE `unit`="'.$server['unit'].'" AND `game`="'.$game.'" AND `core_use`="'.$use_core.'" AND `status`="working" AND `core_fix`="0" ORDER BY `slots_start` DESC, `online` DESC LIMIT 3');
  52. if($sql->num() > 1)
  53. {
  54. $server = $sql->get();
  55. $core = array_search(max($idle), $idle);
  56. $aPid = explode("\n", $ssh->get('ps aux | grep -v grep | grep '.$server['uid'].' | awk \'{print $2}\''));
  57. if(count($aPid) < 2)
  58. continue;
  59. array_pop($aPid);
  60. $taskset = '';
  61. foreach($aPid as $pid)
  62. $taskset .= 'taskset -cp '.($core-1).' '.$pid.';';
  63. $ssh->set($taskset);
  64. unset($idle[$core]);
  65. $sql->query('UPDATE `servers` set `core_use`="'.$core.'" WHERE `id`="'.$server['id'].'" LIMIT 1');
  66. }
  67. }
  68. return NULL;
  69. }
  70. }
  71. ?>