* @copyright 2003-2020 PgPool Global Development Group * @version SVN: $Id$ */ require_once('version.php'); require_once('libs/Smarty.class.php'); require_once('conf/pgmgt.conf.php'); require_once('bootstrap.php'); require_once('definePgpoolConfParam.php'); error_reporting(E_ALL); session_start(); /** * Initialize Smartry */ $tpl = new Smarty(); $tpl->error_reporting = E_ALL & ~E_NOTICE; $tpl->assign('version', $version); if (! file_exists('conf/pgmgt.conf.php')) { include('lang/en.lang.php'); $tpl->assign('message', $message); $tpl->display('pgmgtNotFound.tpl'); exit(); } /** * Check login */ $tpl->assign('isLogin', isset($_SESSION[SESSION_LOGIN_USER])); $tpl->assign('isHelp', FALSE); // If old pgmgt.conf is used, _PGPOOL2_VERSION doen't exist. // This defined var exists from pgpoolAdmin 3.2. if (! defined('_PGPOOL2_VERSION')) { $versions = versions(); define('_PGPOOL2_VERSION', $versions[0]); } /** * Check pgmgt.conf.php Parameter */ $errors = array(); if (! defined('_PGPOOL2_LANG') || ! defined('_PGPOOL2_VERSION') || ! defined('_PGPOOL2_CONFIG_FILE') || ! defined('_PGPOOL2_PASSWORD_FILE') || ! defined('_PGPOOL2_COMMAND') || ! defined('_PGPOOL2_PCP_DIR') || ! defined('_PGPOOL2_PCP_HOSTNAME') || ! defined('_PGPOOL2_STATUS_REFRESH_TIME')) { include('lang/en.lang.php'); errorPage('e7'); } // PostgreSQL connect timeout, default is 10 if (! defined('_PGPOOL2_PG_CONNECT_TIMEOUT')) { define('_PGPOOL2_PG_CONNECT_TIMEOUT', 10); } /** * Create message catalog list */ $messageList = array(); $res_dir = opendir('lang/'); while ($file_name = readdir( $res_dir )) { if (preg_match('/.*\.lang\.php$/', $file_name)) { if (@is_file('lang/' . $file_name)) { include('lang/' . $file_name); $messageList[$message['lang']] = $message['strLang']; } else { errorPage('e2'); } } } $tpl->assign('messageList', $messageList); /** * Load message catalog */ $lang = selectLanguage(_PGPOOL2_LANG, $messageList); include('lang/' . $lang . '.lang.php'); $tpl->assign('message', $message); $_SESSION[SESSION_MESSAGE] = $message; /* --------------------------------------------------------------------- */ /* function (DB) */ /* --------------------------------------------------------------------- */ /** * Open databse connection */ function openDBConnection($arr) { $conStr = generateConstr($arr); $conn = @pg_connect($conStr); return $conn; } /** * Close database connection */ function closeDBConnection($connection) { return pg_close($connection); } /** * Execute query */ function execQuery($conn, $sql) { $rs = @pg_query($conn, $sql); if (!pg_result_status($rs) == PGSQL_TUPLES_OK) { return FALSE; } return $rs; } /** * Confirmation whether node is active or is not. */ function NodeActive($nodeNum) { $params = readConfigParams(); $perNodeParams = getPerNodeHealthCheck($nodeNum); $conn = openDBConnection(array( 'host' => $params['backend_hostname'][$nodeNum], 'port' => $params['backend_port'][$nodeNum], 'dbname' => $perNodeParams['health_check_database'], 'user' => $perNodeParams['health_check_user'], 'password' => $perNodeParams['health_check_password'], 'connect_timeout' => _PGPOOL2_PG_CONNECT_TIMEOUT, )); if ($conn == FALSE) { return FALSE; } else { closeDBConnection($conn); return TRUE; } } /** * Confirmation whether node is act as a standby server */ function NodeStandby($nodeNum) { if (hasBackendClusteringMode()){ $params = readConfigParams(array('backend_clustering_mode')); if ($params['backend_clustering_mode'] != 'streaming_replication') { return -1; } } else { if (isMasterSlaveMode() == FALSE || useStreaming() == FALSE) { return -1; } } $params = readConfigParams(array( 'backend_hostname', 'backend_port', 'sr_check_user','sr_check_password' )); $conn = openDBConnection(array( 'host' => $params['backend_hostname'][$nodeNum], 'port' => $params['backend_port'][$nodeNum], 'dbname' => 'template1', 'user' => $params['sr_check_user'], 'password' => $params['sr_check_password'], 'connect_timeout' => _PGPOOL2_PG_CONNECT_TIMEOUT, )); if ($conn == FALSE) { return -1; } $result = pg_query($conn, 'SELECT pg_is_in_recovery()'); if (! pg_result_status($result) == PGSQL_TUPLES_OK) { closeDBConnection($conn); return -1; } $rr = pg_fetch_array($result); if ($rr[0][0] == 't') { $r = 1; } else { $r = 0; } @pg_free_result($result); closeDBConnection($conn); return $r; } /** * Get if loginUser is super user */ function isSuperUser($user_name) { $params = readConfigParams(array('port')); // Try to connect the backend by login user $conn = openDBConnection(array( 'host' => PGPOOLADMIN_HOST, 'port' => $params['port'], 'dbname' => 'template1', 'user' => $_SESSION[SESSION_LOGIN_USER], 'password' => $_SESSION[SESSION_LOGIN_USER_PASSWORD], 'connect_timeout' => _PGPOOL2_PG_CONNECT_TIMEOUT, )); // Try to connect health check user if ($conn === FALSE) { $params = readConfigParams(array('port', 'health_check_user', 'health_check_password')); $conn = openDBConnection(array( 'host' => PGPOOLADMIN_HOST, 'port' => $params['port'], 'dbname' => 'template1', 'user' => $params['health_check_user'], 'password' => $params['health_check_password'], 'connect_timeout' => _PGPOOL2_PG_CONNECT_TIMEOUT, )); } if ($conn === FALSE) { return NULL; } $result = pg_query($conn, "SELECT usesuper FROM pg_user WHERE usename = '{$user_name}'"); if (! pg_result_status($result) == PGSQL_TUPLES_OK) { closeDBConnection($conn); return NULL; } $rr = pg_fetch_array($result); $rtn = (isset($rr['usesuper']) && $rr['usesuper'] == 't') ? 'yes' : 'no'; @pg_free_result($result); closeDBConnection($conn); $_SESSION[SESSION_IS_SUPER_USER] = $rtn; return $rtn; } /** * Create connection str for pg_connect() */ function generateConstr($params) { $arr = array(); foreach ($params as $param => $value) { if ($value == '') { continue; } switch ($param) { case 'host': case 'port': case 'dbname': case 'user': case 'password': case 'connect_timeout': $arr[] = "{$param}='{$value}'"; } } $conStr = implode(' ', $arr); return $conStr; } /* --------------------------------------------------------------------- */ /* function (pgpool) */ /* --------------------------------------------------------------------- */ /** * Check if pgpool.pid exists */ function DoesPgpoolPidExist() { $params = readConfigParams(array('pid_file_name')); $pidFile = $params['pid_file_name']; if (file_exists($pidFile) ) { return TRUE; } return FALSE; } /* --------------------------------------------------------------------- */ /* function (parameters) */ /* --------------------------------------------------------------------- */ /** * Get the value of "logdir" */ function readLogDir() { $params = readConfigParams(array('logdir')); return $params['logdir']; } /** * Read parameters specified in $paramList from pgpool.conf. * If $paramList is not specified, all item is read from pgpool.conf. */ function readConfigParams($paramList = array()) { $rtn = array(); global $pgpoolConfigParam, $pgpoolConfigBackendParam, $pgpoolConfigWdOtherParam, $pgpoolConfigHbDestinationParam, $pgpoolConfigWdNodeParam, $pgpoolConfigWdHbNodeParam; // Try to read pgpool.conf $configFile = @file(_PGPOOL2_CONFIG_FILE); if ($configFile == FALSE) { $errTpl = new Smarty(); $errTpl->assign('message', $_SESSION[SESSION_MESSAGE]); errorPage('e4'); } // Defined array in definePgpoolConfParam.php $defines_arr = $pgpoolConfigParam + $pgpoolConfigBackendParam + $pgpoolConfigWdOtherParam + $pgpoolConfigHbDestinationParam + $pgpoolConfigWdNodeParam + $pgpoolConfigWdHbNodeParam; $arr = array(); // Convert lines in files to array foreach ($configFile as $line_num => $line) { $line = trim($line); if (substr($line, 0, 1) == '#' || strpos($line, '=') === FALSE) { continue; } list($key, $value) = explode('=', $line); // è¨å®ãã¡ã¤ã«ã®ãã©ã¡ã¼ã¿ã®ãã¼ $key = trim($key); switch ($key) { case 'recovery_1st_stage_command': case 'recovery_2nd_stage_command': $key_wo_num = $key; break; default: // In case of "health_check_*0", the number is left. $num = preg_replace('/[^0-9]/', NULL, $key); $key_wo_num = str_replace($num, NULL, $key); break; } // Ignore params not specified to read if ($paramList && is_array($paramList) && ! in_array($key_wo_num, $paramList)) { continue; } // Remove quotes and comments $value = trimValue($value); // Change true/false to on/off if ($value == 'true') { $value = 'on'; } elseif ($value == 'false') { $value = 'off'; } if (! isset($defines_arr[$key_wo_num])) { continue; // Params with multiple values // (backend_*, other_pgpool_*, heartbeat_destination_*, heartbeat_device*) } elseif (isset($defines_arr[$key_wo_num]['multiple']) && $defines_arr[$key_wo_num]['multiple'] == TRUE) { $rtn[$key_wo_num][$num] = $value; } else { // Ignore param not defined definePgpoolConfParam.php if (preg_match('/^(health_check|connect_time).*[0-9]$/', $key)) { // In case of "health_check_*0", the number is left. $rtn[$key] = $value; } else { $rtn[$key_wo_num] = $value; } } } // Set default value if there is no line about the param if ($paramList && is_array($paramList)) { foreach ($paramList as $key) { if (! isset($rtn[$key]) || $rtn[$key] == NULL) { $default_value = $defines_arr[$key]['default']; if (isset($defines_arr[$key]['multiple']) && $defines_arr[$key]['multiple']) { $rtn[$key][0] = $default_value; } else { $rtn[$key] = $default_value; } } } } elseif ($defines_arr) { foreach ($defines_arr as $key => $param_info) { if (! isset($rtn[$key])) { $default_value = (isset($defines_arr[$key]['default'])) ? $defines_arr[$key]['default'] : NULL; if (isset($defines_arr[$key]['multiple']) && $defines_arr[$key]['multiple']) { $rtn[$key][0] = $default_value; } else { $rtn[$key] = $default_value; } } } } return $rtn; } function trimValue($text) { // Remove spaces $text = trim($text); $rtn = ''; $in_quotes = FALSE; for ($i = 0; $i $period, 'health_check_timeout' => $timeout, 'health_check_user' => $user, 'health_check_password' => $password, 'health_check_database' => $database, 'health_check_max_retries' => $max_retries, 'connect_timeout' => $connect_timeout, 'health_check_retry_delay' => $retry_delay ); return $rtn; } /* --------------------------------------------------------------------- */ /* function (mode) */ /* --------------------------------------------------------------------- */ function versions() { return array('4.2', '4.1', '4.0', '3.7', '3.6', '3.5', '3.4', '3.3', '3.2', '3.1', '3.0', '2.3', '2.2', '2.1', '2.0'); } /** * Whether pgpool is operating in the replication mode or not? */ function isReplicationMode() { if (hasBackendClusteringMode()){ $params = readConfigParams(array('backend_clustering_mode')); if ($params['backend_clustering_mode'] == 'native_replication_mode'){ return TRUE; } else { return FALSE; } } else { $params = readConfigParams(array('replication_mode')); if (isTrue($params['replication_mode'])) { return TRUE; } else { return FALSE; } } } /** * Whether pgpool is operating in the master slave mode or not? */ function isMasterSlaveMode() { if (hasBackendClusteringMode()){ $params = readConfigParams(array('backend_clustering_mode')); if ($params['backend_clustering_mode'] == 'streaming_replication' || $params['backend_clustering_mode'] == 'logical_replication' || $params['backend_clustering_mode'] == 'slony') { return TRUE; } else { return FALSE; } } else { $params = readConfigParams(array('master_slave_mode')); if (isTrue($params['master_slave_mode'])) { return TRUE; } else { return FALSE; } } } /** * Whether pgpool is operating in the parallel mode or not? */ function isParallelMode() { $params = readConfigParams(array('parallel_mode')); if (isTrue($params['parallel_mode'])) { return TRUE; } else { return FALSE; } } /** * Whether pgpool is using stream sub mode in master slave mode or not? */ function useStreaming() { $params = readConfigParams(array('master_slave_sub_mode')); if (isMasterSlaveMode() && $params['master_slave_sub_mode'] == 'stream') { return TRUE; } else { return FALSE; } } /** * Whether pgpool uses syslog or not? */ function useSyslog() { if (!paramExists('log_destination')) { return FALSE; } $params = readConfigParams(array('log_destination')); if ($params['log_destination'] == 'syslog') { return TRUE; } else { return FALSE; } } /** * Return if pgpool has Backend Clustering Mode */ function hasBackendClusteringMode() { return (4.2 $value) { if (isset($params["health_check_period" . $backend_num]) && $params["health_check_period" . $backend_num] != 0) { return TRUE; } } return FALSE; } function errorPage($errorCode) { global $tpl; $tpl->assign('errorCode', $errorCode); $tpl->display('error.tpl'); exit(); } function pr($array) { echo '
';
print_r($array);
echo '';
}