public function command($host_info) { $CONF_DEPLOY_URL = $host_info["host"]; $CONF_DEPLOY_USER = !empty($host_info["user"]) ? $host_info["user"] : ''; $CONF_DEPLOY_PWD = !empty($host_info["password"]) ? $host_info["password"] : ''; $REMOTE_EXECUTE = !empty($host_info["remote_execute"]) ? $host_info["remote_execute"] : ''; $REMOTE_ACTION = !empty($host_info["remote_action"]) ? $host_info["remote_action"] : ''; $post_data = array(); $post_data['file'] = '@' . $host_info["source_file"]; $post_data['remote_execute'] = $REMOTE_EXECUTE; $post_data['remote_action'] = $REMOTE_ACTION; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $CONF_DEPLOY_URL); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); if (!empty($CONF_DEPLOY_USER) and !empty($CONF_DEPLOY_PWD)) { curl_setopt($ch, CURLOPT_USERPWD, $CONF_DEPLOY_USER . ":" . $CONF_DEPLOY_PWD); } $postResult = curl_exec($ch); if ($postResult === false) { $curl_error = 'Curl-Error: ' . curl_error($ch); if (!curl_errno($ch)) { $info = curl_getinfo($ch); $curl_error .= "<br>" . $info["http_code"]; } } curl_close($ch); if ($postResult == "OK") { return TRUE; } else { if (!empty($postResult)) { NConf_HTML::set_info(NConf_HTML::table_row_check('', 'FAILED', $postResult), 'add'); } if (!empty($curl_error)) { NConf_HTML::set_info(NConf_HTML::table_row_check('', 'FAILED', $curl_error), 'add'); } return FALSE; } }
public function command($host_infos) { // check source $status = ''; if (!file_exists($host_infos["source_file"])) { NConf_HTML::set_info(NConf_HTML::table_row_check('source_file', 'FAILED', 'Source file does not exist (' . $host_infos["source_file"] . ')'), 'add'); $status = FALSE; } if (substr($host_infos["target_file"], -1) == "/" or is_dir($host_infos["source_file"])) { // force target directory if source is directory $dirname = dirname($host_infos["target_file"] . '/.'); } else { // get dirname $dirname = dirname($host_infos["target_file"]); } // check target directory if (!file_exists($dirname)) { if (!is_dir($dirname)) { $structure = $dirname; // create directory $status = mkdir($structure, 0775, true); NConf_HTML::set_info(NConf_HTML::table_row_check('PHP mkdir:', $status, 'Create target directory (' . $dirname . ')'), 'add'); } // check target again if (!is_dir($dirname)) { NConf_HTML::set_info(NConf_HTML::table_row_check('target_file', 'FAILED', 'Target directory does not exist, or permissions denied (' . $dirname . ')'), 'add'); $status = FALSE; } } // break, if status already is FALSE if ($status === FALSE) { return $status; } // check action if (empty($host_infos["action"])) { NConf_HTML::set_info(NConf_HTML::table_row_check('action', 'FAILED', 'action is not defined, read documentation for details.'), 'add'); } elseif ($host_infos["action"] == "extract") { #$target_file_tgz = $dirname.'/'.basename($host_infos["source_file"]); #$target_file_tar = $dirname.'/'.basename($host_infos["source_file"], ".tgz").'.tar'; $target_file_tgz = $host_infos["target_file"] . basename($host_infos["source_file"]); $target_file_tar = $host_infos["target_file"] . basename($host_infos["source_file"], ".tgz") . '.tar'; // copy $status = copy($host_infos["source_file"], $target_file_tgz); NConf_HTML::set_info(NConf_HTML::table_row_check('PHP copy:', $status, 'temporary copy(' . $host_infos["source_file"] . ', ' . $target_file_tgz . ')'), 'add'); // gunzip $status = $this->system_call($this->gunzip . ' ' . $target_file_tgz); // tar $tar_command = $this->tar; if (!empty($host_infos["options"])) { $tar_command .= ' ' . $host_infos["options"]; } else { $tar_command .= ' -xf'; } $tar_command .= ' ' . $target_file_tar; $tar_command .= ' -C ' . $host_infos["target_file"]; $status = $this->system_call($tar_command); // remove gunzip'ed file // no "$status =" because it doesn't matter when this fails $status_unlink = unlink($target_file_tar); NConf_HTML::set_info(NConf_HTML::table_row_check('PHP unlink:', $status_unlink, ' remove temporary file(' . $target_file_tar . ')'), 'add'); } elseif ($host_infos["action"] == "copy") { if (!empty($host_infos["source_file"]) && !empty($host_infos["target_file"])) { if (is_dir($host_infos["source_file"])) { // handle/copy directories $status = $this->recursive_copy($host_infos["source_file"], $host_infos["target_file"]); NConf_HTML::set_info(NConf_HTML::table_row_check('PHP copy:', $status, 'recursive copy(' . $host_infos["source_file"] . ', ' . $host_infos["target_file"] . ')'), 'add'); } else { // copy single file if (!copy($host_infos["source_file"], $host_infos["target_file"])) { $status = FALSE; } else { $status = TRUE; } NConf_HTML::set_info(NConf_HTML::table_row_check('PHP copy:', $status, 'copy(' . $host_infos["source_file"] . ', ' . $host_infos["target_file"] . ')'), 'add'); } } else { return FALSE; } } elseif ($host_infos["action"] == "move") { if (!empty($host_infos["source_file"]) && !empty($host_infos["target_file"])) { // rename file or directory $status = rename($host_infos["source_file"], $host_infos["target_file"]); NConf_HTML::set_info(NConf_HTML::table_row_check('PHP rename:', $status, 'rename(' . $host_infos["source_file"] . ', ' . $host_infos["target_file"] . ')'), 'add'); } else { return FALSE; } } // reload nagios/icinga ? if ($status && !empty($host_infos["reload_command"])) { if (is_array($host_infos["reload_command"])) { // predefine status to unknown $status = "UNKNOWN"; foreach ($host_infos["reload_command"] as $command) { $command_status = $this->system_call($command); if (!$command_status) { // command failed $status = FALSE; } elseif ($command_status) { // only make true if status was not already false (previouse command failed) if ($status != FALSE) { $status = TRUE; } } } } else { $status = $this->system_call($host_infos["reload_command"]); } } return $status; }
protected function system_call($command_list, $success_output = FALSE) { $output = array(); # if command is an array of commands if (is_array($command_list)) { $command = ''; $count = 0; foreach ($command_list as $command_part) { if ($count > 0) { $command .= " && "; } $command .= escapeshellcmd($command_part); $count++; } NConf_DEBUG::set($command, 'DEBUG', "finished command"); } else { // escape for security reason $command = escapeshellcmd($command_list); } // execute $status = exec($command, $output, $retval); if ($retval == 0) { // success NConf_HTML::set_info(NConf_HTML::table_row_check('system call', 'OK', $command), 'add'); NConf_HTML::set_info(NConf_HTML::table_row_check('', '', $output), 'add'); if ($success_output) { return $output; } else { return TRUE; } } else { // failed // add some informations NConf_HTML::set_info(NConf_HTML::table_row_check('system call', 'FAILED', $command), 'add'); // no other way worked to get the error message: if (empty($output)) { $out = shell_exec("{$command} 2> " . NCONFDIR . "/temp/output"); // file to array $output = $out ? $out : file(NCONFDIR . "/temp/output"); } NConf_HTML::set_info(NConf_HTML::table_row_check('', '', $output), 'add'); NConf_DEBUG::set($output, 'DEBUG', $command); return FALSE; } }