type = $type; $this->physical_location_id = $physical_location_id; $this->manager_scripts = $manager_scripts; } /** * Executes multi-threaded CURL handles * * @param array $ch_arr Array of CURL Handle objects * @return array Result set */ private function execute_curl_multi($ch_arr = array()){ if(count($ch_arr)){ $result = array(); $mh = curl_multi_init(); foreach($ch_arr as $ch){ curl_multi_add_handle($mh, $ch); } $active = null; //execute the handles do{ $mrc = curl_multi_exec($mh, $active); }while($mrc == CURLM_CALL_MULTI_PERFORM); while($active && $mrc == CURLM_OK){ if(curl_multi_select($mh) != -1){ do{ $mrc = curl_multi_exec($mh, $active); }while($mrc == CURLM_CALL_MULTI_PERFORM); } } while($done = curl_multi_info_read($mh)){ $ch = $done['handle']; $done_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); $done_content = curl_multi_getcontent($ch); $result[] = @unserialize($done_content); curl_multi_remove_handle($mh, $ch); curl_close($ch); } curl_multi_close($mh); return $result; } } /** * Generates CURL handle * * @param string $url * @param string $post_data * @param int $timeout * @return object CURL Handle */ private function generate_curl_handle($url, $post_data, $timeout){ $cs = curl_init(); $options = array(CURLOPT_URL=>$url, CURLOPT_HEADER=>false, CURLOPT_BINARYTRANSFER=>true, CURLOPT_RETURNTRANSFER=>true, CURLOPT_POST=>true, CURLOPT_CONNECTTIMEOUT=>120, CURLOPT_TIMEOUT=>$timeout, CURLOPT_POSTFIELDS=>$post_data); curl_setopt_array($cs, $options); return $cs; } /** * Get complete information on processes running php.exe * * @param array $ip_arr * @param string $base_url * @return array Array of process information */ private function get_php_info($machine_list = array(), MysqliWrapper $wrapperObj_dd) { $this->set_user(); $this->set_base_url(); $this->lookup_ip_user($wrapperObj_dd); $ch_arr = array(); $ip_arr = array(); if(count($machine_list) > 0){ foreach($this->ip_arr as $user => $ips){ foreach($ips as $name => $ip){ if(in_array($name, $machine_list)){ $ip_arr[] = $ip; } } } }else{ $ip_arr = $this->ip_arr[$this->user]; } if(count($ip_arr) > 0 and is_array($ip_arr)){ foreach($ip_arr as $cname => $ip){ $post_data = 'user=' . $this->user . '&action[]=get_encode_tracks&action[]=get_machine_name'; if($ip != '10.10.40.101'){ $post_data .= '&ip=' . $cname; } $ch_arr[] = $this->generate_curl_handle($this->base_url, $post_data, 120); } } $this->php_process_info = $this->execute_curl_multi($ch_arr); } /** * IP lookup table for machines based on the user that they are logged on as * * @param int $physical_location_id * @return array List of IPs */ public function lookup_ip_user(MysqliWrapper $wrapperObj_dd) { $dataParams = new QueryData(); $sql = 'SELECT * FROM ip_lookup WHERE physical_location_id = ? and active = "Y"'; $dataParams->addDataParam('i', $this->physical_location_id); if($this->manager_scripts){ $sql .= " and manager_scripts = 'Y'"; } $result = $wrapperObj_dd->executeQuery($sql, $dataParams); if ($result === false) { showierror($sql); } $this->ip_arr = array(); foreach ($result as $row) { $this->ip_arr[$row['active_user']][$row['name']] = $row['local_ip']; } } /** * Generate base URL based on the physical location selected * */ public function set_base_url(){ switch($this->physical_location_id){ // Ashville case 1: $this->base_url = "http://10.10.40.101/win_proc_manager.php"; //$this->base_url = "http://208.91.130.151/win_proc_manager.php"; break; // NYC case 2: $this->base_url = "http://207.237.185.6:8082/win_proc_manager.php"; //$this->base_url = "http://192.168.31.9:8082/win_proc_manager.php"; break; // UK case 3: $this->base_url = "http://79.99.69.70/win_proc_manager.php"; break; // base case default: $this->base_url = ""; break; } } /** * Depending on the type selected encoding/delivering, get all processing job info from DD db. * * @param string $type accepted values encode or delivery * @return array $jobs information regarding processing jobs from the db */ public function set_db_jobs(){ $connection_dd = $GLOBALS['connection_dd']; $sql = 'select eqd.encoding_queue_detail_id'; if($this->type == 'encode'){ $sql .= ', eqd.encoding_server as server'; }elseif($this->type == 'delivery'){ $sql .= ', eqd.delivery_server as server'; } $sql .= ', p.pid from `direct_delivery`.encoding_queue_detail eqd inner join `direct_delivery`.process_pid p on p.encoding_queue_detail_id = eqd.encoding_queue_detail_id inner join encoding_queue eq on eq.encoding_queue_id = eqd.encoding_queue_id where eqd.status = "'; if($this->type == 'encode'){ $sql .= "encoding"; }elseif($this->type == 'delivery'){ $sql .= "delivering"; } $sql .= '" order by server'; if(!$result = mysql_query($sql, $connection_dd)) showerror($sql, $connection_dd); $this->jobs = array(); while($row = mysql_fetch_array($result)){ $this->jobs[$row['server']][$row['pid']]['encoding_queue_detail_ids'][] = $row['encoding_queue_detail_id']; $this->jobs[$row['server']][$row['pid']]['running'] = false; } } /** * Returns actual state of the given PIDs based on comparison of the DB entries against the Process info from the machines * * @param string $type accepted values encode or delivery * @param array $jobs_arr array of jobs obtained from DB * @param array $php_process_info array of process info obtained from the various machines * @return array $pid_status Status of PIDs upon comparison */ public function set_pid_status($machine_list = array(), MysqliWrapper $wrapperObj_dd) { $this->set_db_jobs(); $this->get_php_info($machine_list, $wrapperObj_dd); $this->accessible_machines = array(); if (count($this->php_process_info) && is_array($this->php_process_info)) { foreach ($this->php_process_info as $machines) { if (count($machines) && is_array($machines)) { foreach ($machines as $ip => $proc_info_arr) { $this->accessible_machines[$ip] = $machines[$ip]['NAME']; if (count($proc_info_arr['PROCESSINFO']) && is_array($proc_info_arr['PROCESSINFO'])) { foreach ($proc_info_arr['PROCESSINFO'] as $php_proc) { if (count($php_proc) && is_array($php_proc)) { if ($this->type == 'encode') { $cmd_line = 'php c:\\direct_delivery\\scripts\\encode_tracks.php'; } elseif ($this->type == 'delivery') { $cmd_line = 'php c:\\direct_delivery\\scripts\\deliver_tracks.php'; } if (isset($php_proc['CommandLine'])) { if ($php_proc['CommandLine'] == $cmd_line) { if (isset($this->jobs[$php_proc['CSName']][$php_proc['ProcessId']])) { $this->jobs[$php_proc['CSName']][$php_proc['ProcessId']]['running'] = true; } else { $this->jobs[$php_proc['CSName']][$php_proc['ProcessId']]['running'] = false; $this->jobs[$php_proc['CSName']][$php_proc['ProcessId']]['encoding_queue_detail_ids'] = array(); } } } } } } } } } } $this->accessible_machines = array_unique($this->accessible_machines); $pid_status['valid'] = array(); $pid_status['waiting'] = array(); $pid_status['invalid'] = array(); if(count($this->accessible_machines) > 0 && is_array($this->accessible_machines)){ foreach($this->accessible_machines as $name){ if(isset($this->jobs[$name]) && count($this->jobs[$name]) > 0 && is_array($this->jobs[$name])){ foreach($this->jobs[$name] as $pid => $info){ if(count($info['encoding_queue_detail_ids']) > 0 && $info['running']){ $pid_status['valid'][$name][$pid] = $info['encoding_queue_detail_ids']; }elseif(count($info['encoding_queue_detail_ids']) == 0 && !$info['running']){ $pid_status['waiting'][$name][$pid] = $info['encoding_queue_detail_ids']; }elseif(count($info['encoding_queue_detail_ids']) > 0 && !$info['running']){ $pid_status['invalid'][$name][$pid] = $info['encoding_queue_detail_ids']; } } } } } $this->pid_status = $pid_status; } public function set_user(){ if($this->type == 'encode'){ $this->user = 'encagent'; }elseif($this->type == 'delivery'){ $this->user = 'ddagent'; } } public function get_machine_names(){ return $this->accessible_machines; } public function get_machine_pids($machine_name){ $result = array(); foreach($this->pid_status as $status => $info){ foreach($info as $name => $pids){ if($name == $machine_name){ $result[$status] = $pids; } } } return $result; } public function get_free_space($machine_names, MysqliWrapper $wrapperObj_dd) { $this->set_user(); $this->set_base_url(); $this->lookup_ip_user($wrapperObj_dd); foreach($machine_names as $machine_name){ //Machine running the webservice itself needs to be accessed without an ip $post_data = 'user=' . $this->user . '&action[]=get_freespace'; if($this->ip_arr[$this->user][$machine_name] != '10.10.40.101'){ $post_data .= '&ip=' . $this->ip_arr[$this->user][$machine_name]; } $ch_arr[] = $this->generate_curl_handle($this->base_url, $post_data, 120); } $info = $this->execute_curl_multi($ch_arr); $result = array(); if(is_array($info) and count($info)){ foreach($info as $value){ if(is_array($value) and count($value)){ foreach($value as $ip => $space_info){ foreach($space_info as $drives){ foreach($drives as $drive_letter => $free_space){ $result[$ip][$drive_letter] = $free_space; } } } } } } return $result; } public function get_status_count($category){ $result = 0; foreach($this->pid_status as $status => $info){ if($status == $category){ foreach($info as $name => $pids){ if($category == 'waiting'){ $result += count($pids); }else{ foreach($pids as $pid => $eqd_ids){ $result = $result + count($eqd_ids); } } } } } return $result; } public function get_status_count_by_machine($category){ $result = array(); foreach($this->pid_status as $status => $info){ if($status == $category){ foreach($info as $name => $pids){ if($category == 'waiting'){ $result[$name] += count($pids); }else{ foreach($pids as $pid => $eqd_ids){ $result[$name] = $result[$name] + count($eqd_ids); } } } } } return $result; } public function get_process_count($category){ $result = 0; foreach($this->pid_status as $status => $info){ if($status == $category){ foreach($info as $name => $pids){ $result += count($pids); } } } return $result; } public function get_invalid_eqd_ids($machine_name = false){ $result = array(); foreach($this->pid_status as $status => $info){ if($status == 'invalid'){ foreach($info as $name => $pids){ $result[$name] = array(); foreach($pids as $pid => $eqd_ids){ $result[$name] = array_merge($result[$name], $eqd_ids); } } } } if($machine_name){ return $result[$machine_name]; } return $result; } public function get_process_overview(){ $result = array(); foreach($this->pid_status as $status => $info){ foreach($info as $name => $pids){ foreach($pids as $pid => $eqd_ids){ $result[$status][$name][$pid] = $eqd_ids; } } } return $result; } public function get_manager_scripts_status( MysqliWrapper $wrapperObj_dd) { $connection = $GLOBALS['connection']; $result = array('php c:\direct_delivery\scripts\obtain_hd_encoding_orders.php'=>false, 'php c:\direct_delivery\scripts\obtain_oa_encoding_orders.php'=>false, 'php c:\direct_delivery\scripts\delivery_queue_manager.php'=>false, 'php c:\direct_delivery\scripts\delivery_pickup_queue_manager.php'=>false); $manager_machines = array(); $sql = "SELECT name from ip_lookup WHERE manager_scripts = 'Y' AND active = 'Y'"; if(!$rs = mysql_query($sql, $connection)) showerror($sql, $connection); while($row = mysql_fetch_array($rs)){ $manager_machines[] = $row['name']; } $this->get_php_info($manager_machines, $wrapperObj_dd); if (count($this->php_process_info) && is_array($this->php_process_info)) { foreach ($this->php_process_info as $machines) { if (count($machines) && is_array($machines)) { foreach ($machines as $ip => $proc_info_arr) { if (count($proc_info_arr) && is_array($proc_info_arr)) { foreach ($proc_info_arr['PROCESSINFO'] as $php_proc) { if (in_array($php_proc['CommandLine'], array_keys($result))) { $result[$php_proc['CommandLine']] = $php_proc['ProcessId']; } } } } } } } return $result; } public function kill_processes($kill_list_arr, MysqliWrapper $wrapperObj_dd) { $this->set_user(); $this->set_base_url(); $this->lookup_ip_user($wrapperObj_dd); $ch_arr = array(); foreach($kill_list_arr as $machine_name => $pids){ $post_data = 'user=' . $this->user . '&action[]=kill_process'; if($this->ip_arr[$this->user][$machine_name] != '10.10.40.101'){ $post_data .= '&ip=' . $this->ip_arr[$this->user][$machine_name]; } foreach($pids as $pid){ $post_data .= '&arg[]=' . $pid; } $ch_arr[] = $this->generate_curl_handle($this->base_url, $post_data, 120); } $this->execute_curl_multi($ch_arr); } public function start_dead_processes($machine_list, MysqliWrapper $wrapperObj, MysqliWrapper $wrapperObj_dd) { $this->set_user(); $this->set_base_url(); $this->lookup_ip_user($wrapperObj_dd); $ch_arr = array(); foreach($machine_list as $machine_name){ $dataParams = new QueryData(); $sql = 'SELECT name, max_function_jobs from ip_lookup WHERE name = ?'; $dataParams->addDataParam('s', $machine_name); $result = $wrapperObj->executeQuery($sql, $dataParams); if ($result === false) { showierror($sql); } foreach ($result as $row) { $post_data = 'user=' . $this->user . '&action[]=execute_cmd'; if($this->ip_arr[$this->user][$machine_name] != '10.10.40.101'){ $post_data .= '&ip=' . $this->ip_arr[$this->user][$machine_name]; } for($i = 1; $i <= $row['max_function_jobs']; $i++){ if($this->type == 'encode'){ $post_data .= "&arg[]=schtasks /run /tn start_encoding_" . $i; }else{ $post_data .= "&arg[]=schtasks /run /tn start_delivery_" . $i; } } } $ch_arr[] = $this->generate_curl_handle($this->base_url, $post_data, 120); } $this->execute_curl_multi($ch_arr); } public function start_scheduled_task($machine_name, $schtask, MysqliWrapper $wrapperObj_dd) { $this->set_user(); $this->set_base_url(); $this->lookup_ip_user($wrapperObj_dd); $ch_arr = array(); $post_data = 'user=' . $this->user . '&action[]=execute_cmd'; if($this->ip_arr[$this->user][$machine_name] != '10.10.40.101'){ $post_data .= '&ip=' . $this->ip_arr[$this->user][$machine_name]; } $post_data .= "&arg[]=schtasks /run /tn " . $schtask; $ch_arr[] = $this->generate_curl_handle($this->base_url, $post_data, 120); $this->execute_curl_multi($ch_arr); } public function reboot_machine($machine_names, MysqliWrapper $wrapperObj_dd) { $this->set_user(); $this->set_base_url(); $this->lookup_ip_user($wrapperObj_dd); foreach($machine_names as $machine_name){ //Machine running the webservice itself needs to be accessed without an ip $post_data = 'user=' . $this->user . '&action[]=reboot_machine'; if($this->ip_arr[$this->user][$machine_name] != '10.10.40.101'){ $post_data .= '&ip=' . $this->ip_arr[$this->user][$machine_name]; } $ch_arr[] = $this->generate_curl_handle($this->base_url, $post_data, 120); } $this->execute_curl_multi($ch_arr); } public function svn_update($machine_list, MysqliWrapper $wrapperObj_dd) { $this->set_user(); $this->set_base_url(); $this->lookup_ip_user($wrapperObj_dd); $ch_arr = array(); $arg = "schtasks /run /tn update_svn"; foreach($machine_list as $machine_name){ $post_data = 'user=' . $this->user . '&action[]=execute_cmd&arg[]=' . $arg; if($this->ip_arr[$this->user][$machine_name] != '10.10.40.101'){ $post_data .= '&ip=' . $this->ip_arr[$this->user][$machine_name]; } $ch_arr[] = $this->generate_curl_handle($this->base_url, $post_data, 120); } $ch_arr[] = $this->generate_curl_handle($this->base_url, $post_data, 120); $this->execute_curl_multi($ch_arr); } } ?>