2023-03-04 23:45:46 +00:00
< ? php
2023-11-12 18:12:42 +00:00
if ( ! DEFINED ( 'EGP' ))
2023-12-23 01:50:14 +00:00
exit ( header ( 'Refresh: 0; URL=http://' . $_SERVER [ 'HTTP_HOST' ] . '/404' ));
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
class control_scan_servers_route extends cron
{
function __construct ()
{
global $cfg , $sql , $argv , $start_point ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$servers = $argv ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
unset ( $servers [ 0 ], $servers [ 1 ], $servers [ 2 ]);
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$sql -> query ( 'SELECT `address` FROM `control` WHERE `id`="' . $servers [ 4 ] . '" LIMIT 1' );
if ( ! $sql -> num ())
return NULL ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$unit = $sql -> get ();
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$game = $servers [ 3 ];
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
unset ( $servers [ 3 ], $servers [ 4 ]);
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$sql -> query ( 'SELECT `unit` FROM `control_servers` WHERE `id`="' . $servers [ 5 ] . '" LIMIT 1' );
$server = $sql -> get ();
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$sql -> query ( 'SELECT `address`, `passwd`, `fcpu` FROM `control` WHERE `id`="' . $server [ 'unit' ] . '" LIMIT 1' );
$unit = $sql -> get ();
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
include ( LIB . 'ssh.php' );
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
// Проверка ssh соедниения пу с локацией
if ( ! $ssh -> auth ( $unit [ 'passwd' ], $unit [ 'address' ]))
return NULL ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$first = $ssh -> get ( 'cat /proc/stat' );
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
sleep ( 1 );
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$aCpu = sys :: cpu_idle ( array ( $first , $ssh -> get ( 'cat /proc/stat' )), $unit [ 'fcpu' ], true );
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
array_shift ( $aCpu );
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$idle = array ();
$uses = array ();
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
foreach ( $aCpu as $cpu => $data ) {
$core = sys :: int ( $cpu ) + 1 ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$sql -> query ( 'SELECT `id` FROM `control_servers` WHERE `unit`="' . $server [ 'unit' ] . '" AND `core_fix`="' . $core . '" AND `core_fix`="1" LIMIT 1' );
if ( $sql -> num ()) {
unset ( $aCpu [ $cpu ]);
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
continue ;
}
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
if ( $data [ 'idle' ] > 50 )
$idle [ $core ] = $data [ 'idle' ];
else
$uses [ $core ] = 100 - $data [ 'idle' ];
}
2023-05-05 01:17:19 +00:00
2023-11-12 18:12:42 +00:00
if ( ! count ( $idle ))
return NULL ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
foreach ( $uses as $use_core => $use ) {
if ( ! count ( $idle ))
break ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$sql -> query ( 'SELECT `id`, `uid` FROM `control_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' );
if ( $sql -> num () > 1 ) {
$server = $sql -> get ();
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$core = array_search ( max ( $idle ), $idle );
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$aPid = explode ( " \n " , $ssh -> get ( 'ps aux | grep -v grep | grep ' . $server [ 'uid' ] . ' | awk \'{print $2}\'' ));
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
if ( count ( $aPid ) < 2 )
continue ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
array_pop ( $aPid );
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$taskset = '' ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
foreach ( $aPid as $pid )
$taskset .= 'taskset -cp ' . ( $core - 1 ) . ' ' . $pid . ';' ;
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$ssh -> set ( $taskset );
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
unset ( $idle [ $core ]);
2023-03-04 23:45:46 +00:00
2023-11-12 18:12:42 +00:00
$sql -> query ( 'UPDATE `control_servers` set `core_use`="' . $core . '" WHERE `id`="' . $server [ 'id' ] . '" LIMIT 1' );
}
}
return NULL ;
}
}
2023-05-05 01:17:19 +00:00
2023-03-04 23:45:46 +00:00
?>