diff --git a/config.c b/config.c index 366e7e0a..009aee84 100644 --- a/config.c +++ b/config.c @@ -28,17 +28,18 @@ parse_config(const char* config_file, t_configuration_options* options) FILE *fp = fopen (config_file, "r"); - if (fp == NULL) { + if (fp == NULL) + { fprintf(stderr, _("Could not find configuration file '%s'\n"), config_file); exit(ERR_BAD_CONFIG); } - + /* Initialize */ memset(options->cluster_name, 0, sizeof(options->cluster_name)); options->node = -1; memset(options->conninfo, 0, sizeof(options->conninfo)); memset(options->rsync_options, 0, sizeof(options->rsync_options)); - + /* Read next line */ while ((s = fgets (buff, sizeof buff, fp)) != NULL) { diff --git a/config.h b/config.h index 5e9cc36e..27646c65 100644 --- a/config.h +++ b/config.h @@ -22,13 +22,14 @@ #include "repmgr.h" -typedef struct { +typedef struct +{ char cluster_name[MAXLEN]; int node; char conninfo[MAXLEN]; char loglevel[MAXLEN]; char logfacility[MAXLEN]; - char rsync_options[QUERY_STR_LEN]; + char rsync_options[QUERY_STR_LEN]; } t_configuration_options; void parse_config(const char* config_file, t_configuration_options* options); diff --git a/log.c b/log.c index 24b7ac14..c458628f 100644 --- a/log.c +++ b/log.c @@ -57,11 +57,13 @@ bool logger_init(const char* ident, const char* level, const char* facility) printf("Logger initialisation (Level: %s, Facility: %s)\n", level, facility); #endif - if (!ident) { + if (!ident) + { ident = DEFAULT_IDENT; } - if (level && *level) { + if (level && *level) + { l = detect_log_level(level); #ifdef REPMGR_DEBUG printf("Assigned level for logger: %d\n", l); @@ -73,24 +75,28 @@ bool logger_init(const char* ident, const char* level, const char* facility) stderr_log_warning(_("Cannot detect log level %s (use any of DEBUG, INFO, NOTICE, WARNING, ERR, ALERT, CRIT or EMERG)\n"), level); } - if (facility && *facility) { + if (facility && *facility) + { f = detect_log_facility(facility); #ifdef REPMGR_DEBUG printf("Assigned facility for logger: %d\n", f); #endif - if (f == 0) { + if (f == 0) + { /* No syslog requested, just stderr */ #ifdef REPMGR_DEBUG printf(_("Use stderr for logging\n")); #endif } - else if (f == -1) { + else if (f == -1) + { stderr_log_warning(_("Cannot detect log facility %s (use any of LOCAL0, LOCAL1, ..., LOCAL7, USER or STDERR)\n"), facility); } #ifdef HAVE_SYSLOG - else { + else + { syslog_facility = f; log_type = REPMGR_SYSLOG; } @@ -149,42 +155,46 @@ int detect_log_level(const char* level) int detect_log_facility(const char* facility) { int local = 0; - if (!strncmp(facility, "LOCAL", 5) && strlen(facility) == 6) { + if (!strncmp(facility, "LOCAL", 5) && strlen(facility) == 6) + { local = atoi (&facility[5]); - switch(local) { - case 0: - return LOG_LOCAL0; - break; - case 1: - return LOG_LOCAL1; - break; - case 2: - return LOG_LOCAL2; - break; - case 3: - return LOG_LOCAL3; - break; - case 4: - return LOG_LOCAL4; - break; - case 5: - return LOG_LOCAL5; - break; - case 6: - return LOG_LOCAL6; - break; - case 7: - return LOG_LOCAL7; - break; + switch (local) + { + case 0: + return LOG_LOCAL0; + break; + case 1: + return LOG_LOCAL1; + break; + case 2: + return LOG_LOCAL2; + break; + case 3: + return LOG_LOCAL3; + break; + case 4: + return LOG_LOCAL4; + break; + case 5: + return LOG_LOCAL5; + break; + case 6: + return LOG_LOCAL6; + break; + case 7: + return LOG_LOCAL7; + break; } } - else if (!strcmp(facility, "USER")) { + else if (!strcmp(facility, "USER")) + { return LOG_USER; } - else if (!strcmp(facility, "STDERR")) { + else if (!strcmp(facility, "STDERR")) + { return 0; } diff --git a/repmgr.c b/repmgr.c index d701a5bc..395c7c7c 100644 --- a/repmgr.c +++ b/repmgr.c @@ -238,7 +238,7 @@ main(int argc, char **argv) else strncpy(runtime_options.dbname, DEFAULT_DBNAME, MAXLEN); } - + /* * Read the configuration file: repmgr.conf */ @@ -270,12 +270,13 @@ main(int argc, char **argv) parse_config(runtime_options.config_file, &options); - if (need_a_node) { + if (need_a_node) + { if (options.node == -1) { log_err("Node information is missing. " - "Check the configuration file.\n"); + "Check the configuration file.\n"); exit(ERR_BAD_CONFIG); } @@ -384,9 +385,9 @@ do_master_register(void) /* ... the tables */ snprintf(sqlquery, QUERY_STR_LEN, "CREATE TABLE %s.repl_nodes ( " - " id integer primary key, " - " cluster text not null, " - " conninfo text not null)", repmgr_schema); + " id integer primary key, " + " cluster text not null, " + " conninfo text not null)", repmgr_schema); log_debug("master register: %s\n", sqlquery); if (!PQexec(conn, sqlquery)) { @@ -397,13 +398,13 @@ do_master_register(void) } snprintf(sqlquery, QUERY_STR_LEN, "CREATE TABLE %s.repl_monitor ( " - " primary_node INTEGER NOT NULL, " - " standby_node INTEGER NOT NULL, " - " last_monitor_time TIMESTAMP WITH TIME ZONE NOT NULL, " - " last_wal_primary_location TEXT NOT NULL, " - " last_wal_standby_location TEXT NOT NULL, " - " replication_lag BIGINT NOT NULL, " - " apply_lag BIGINT NOT NULL) ", repmgr_schema); + " primary_node INTEGER NOT NULL, " + " standby_node INTEGER NOT NULL, " + " last_monitor_time TIMESTAMP WITH TIME ZONE NOT NULL, " + " last_wal_primary_location TEXT NOT NULL, " + " last_wal_standby_location TEXT NOT NULL, " + " replication_lag BIGINT NOT NULL, " + " apply_lag BIGINT NOT NULL) ", repmgr_schema); log_debug("master register: %s\n", sqlquery); if (!PQexec(conn, sqlquery)) { @@ -415,14 +416,14 @@ do_master_register(void) /* and the view */ snprintf(sqlquery, QUERY_STR_LEN, "CREATE VIEW %s.repl_status AS " - " WITH monitor_info AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY primary_node, standby_node " - " ORDER BY last_monitor_time desc) " - " FROM %s.repl_monitor) " - " SELECT primary_node, standby_node, last_monitor_time, last_wal_primary_location, " - " last_wal_standby_location, pg_size_pretty(replication_lag) replication_lag, " - " pg_size_pretty(apply_lag) apply_lag, age(now(), last_monitor_time) AS time_lag " - " FROM monitor_info a " - " WHERE row_number = 1", repmgr_schema, repmgr_schema); + " WITH monitor_info AS (SELECT *, ROW_NUMBER() OVER (PARTITION BY primary_node, standby_node " + " ORDER BY last_monitor_time desc) " + " FROM %s.repl_monitor) " + " SELECT primary_node, standby_node, last_monitor_time, last_wal_primary_location, " + " last_wal_standby_location, pg_size_pretty(replication_lag) replication_lag, " + " pg_size_pretty(apply_lag) apply_lag, age(now(), last_monitor_time) AS time_lag " + " FROM monitor_info a " + " WHERE row_number = 1", repmgr_schema, repmgr_schema); log_debug("master register: %s\n", sqlquery); if (!PQexec(conn, sqlquery)) { @@ -451,35 +452,35 @@ do_master_register(void) if (runtime_options.force) { snprintf(sqlquery, QUERY_STR_LEN, "DELETE FROM %s.repl_nodes " - " WHERE id = %d", - repmgr_schema, options.node); + " WHERE id = %d", + repmgr_schema, options.node); log_debug("master register: %s\n", sqlquery); if (!PQexec(conn, sqlquery)) { log_warning(_("Cannot delete node details, %s\n"), - PQerrorMessage(conn)); + PQerrorMessage(conn)); PQfinish(conn); exit(ERR_BAD_CONFIG); } } snprintf(sqlquery, QUERY_STR_LEN, "INSERT INTO %s.repl_nodes " - "VALUES (%d, '%s', '%s')", - repmgr_schema, options.node, options.cluster_name, options.conninfo); + "VALUES (%d, '%s', '%s')", + repmgr_schema, options.node, options.cluster_name, options.conninfo); log_debug("master register: %s\n", sqlquery); if (!PQexec(conn, sqlquery)) { log_warning(_("Cannot insert node details, %s\n"), - PQerrorMessage(conn)); + PQerrorMessage(conn)); PQfinish(conn); exit(ERR_BAD_CONFIG); } PQfinish(conn); log_notice(_("Master node correctly registered for cluster %s with id %d (conninfo: %s)\n"), - options.cluster_name, options.node, options.conninfo); + options.cluster_name, options.node, options.conninfo); return; } @@ -539,7 +540,8 @@ do_standby_register(void) /* check if there is a master in this cluster */ master_conn = getMasterConnection(conn, options.node, options.cluster_name, &master_id); - if (!master_conn) { + if (!master_conn) + { log_err(_("A master must be defined before configuring a slave\n")); exit(ERR_BAD_CONFIG); } @@ -569,8 +571,8 @@ do_standby_register(void) if (runtime_options.force) { snprintf(sqlquery, QUERY_STR_LEN, "DELETE FROM %s.repl_nodes " - " WHERE id = %d", - repmgr_schema, options.node); + " WHERE id = %d", + repmgr_schema, options.node); log_debug("standby register: %s\n", sqlquery); if (!PQexec(master_conn, sqlquery)) @@ -584,8 +586,8 @@ do_standby_register(void) } snprintf(sqlquery, QUERY_STR_LEN, "INSERT INTO %s.repl_nodes " - "VALUES (%d, '%s', '%s')", - repmgr_schema, options.node, options.cluster_name, options.conninfo); + "VALUES (%d, '%s', '%s')", + repmgr_schema, options.node, options.cluster_name, options.conninfo); log_debug("standby register: %s\n", sqlquery); if (!PQexec(master_conn, sqlquery)) @@ -651,7 +653,7 @@ do_standby_clone(void) case 1: /* Present but empty, fix permissions and use it */ log_info(_("fixing permissions on existing directory %s ...\n"), - runtime_options.dest_dir); + runtime_options.dest_dir); fflush(stdout); if (!set_directory_permissions(runtime_options.dest_dir)) @@ -664,15 +666,15 @@ do_standby_clone(void) case 2: /* Present and not empty */ log_warning( _("%s: directory \"%s\" exists but is not empty\n"), - progname, runtime_options.dest_dir); + progname, runtime_options.dest_dir); pg_dir = is_pg_dir(runtime_options.dest_dir); if (pg_dir && !runtime_options.force) { log_warning( _("\nThis looks like a PostgreSQL directory.\n" - "If you are sure you want to clone here, " - "please check there is no PostgreSQL server " - "running and use the --force option\n")); + "If you are sure you want to clone here, " + "please check there is no PostgreSQL server " + "running and use the --force option\n")); exit(ERR_BAD_CONFIG); } else if (pg_dir && runtime_options.force) @@ -685,7 +687,7 @@ do_standby_clone(void) default: /* Trouble accessing directory */ log_err( _("%s: could not access directory \"%s\": %s\n"), - progname, runtime_options.dest_dir, strerror(errno)); + progname, runtime_options.dest_dir, strerror(errno)); exit(ERR_BAD_CONFIG); } @@ -817,8 +819,8 @@ do_standby_clone(void) /* Get the data directory full path and the configuration files location */ snprintf(sqlquery, QUERY_STR_LEN, "SELECT name, setting " - " FROM pg_settings " - " WHERE name IN ('data_directory', 'config_file', 'hba_file', 'ident_file')"); + " FROM pg_settings " + " WHERE name IN ('data_directory', 'config_file', 'hba_file', 'ident_file')"); log_debug("standby clone: %s\n", sqlquery); res = PQexec(conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) @@ -849,7 +851,7 @@ do_standby_clone(void) */ snprintf(sqlquery, QUERY_STR_LEN, "SELECT pg_xlogfile_name(pg_start_backup('repmgr_standby_clone_%ld'))", time(NULL)); log_debug("standby clone: %s\n", sqlquery); - + res = PQexec(conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) { @@ -886,14 +888,16 @@ do_standby_clone(void) log_info("standby clone: master control file '%s'\n", master_control_file); r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_control_file, local_control_file, false); - if (r != 0) { + if (r != 0) + { log_warning("standby clone: failed copying master control file '%s'\n", master_control_file); goto stop_backup; } log_info("standby clone: master data directory '%s'\n", master_data_directory); r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_data_directory, runtime_options.dest_dir, true); - if (r != 0) { + if (r != 0) + { log_warning("standby clone: failed copying master data directory '%s'\n", master_data_directory); goto stop_backup; } @@ -916,7 +920,8 @@ do_standby_clone(void) strncpy(tblspc_dir, PQgetvalue(res, i, 0), MAXFILENAME); log_info("standby clone: master tablespace '%s'\n", tblspc_dir); r = copy_remote_files(runtime_options.host, runtime_options.remote_user, tblspc_dir, tblspc_dir, true); - if (r != 0) { + if (r != 0) + { log_warning("standby clone: failed copying tablespace directory '%s'\n", tblspc_dir); goto stop_backup; } @@ -924,21 +929,24 @@ do_standby_clone(void) log_info("standby clone: master config file '%s'\n", master_config_file); r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_config_file, runtime_options.dest_dir, false); - if (r != 0) { + if (r != 0) + { log_warning("standby clone: failed copying master config file '%s'\n", master_config_file); goto stop_backup; } log_info("standby clone: master hba file '%s'\n", master_hba_file); r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_hba_file, runtime_options.dest_dir, false); - if (r != 0) { + if (r != 0) + { log_warning("standby clone: failed copying master hba file '%s'\n", master_hba_file); goto stop_backup; } log_info("standby clone: master ident file '%s'\n", master_ident_file); r = copy_remote_files(runtime_options.host, runtime_options.remote_user, master_ident_file, runtime_options.dest_dir, false); - if (r != 0) { + if (r != 0) + { log_warning("standby clone: failed copying master ident file '%s'\n", master_ident_file); goto stop_backup; } @@ -957,7 +965,7 @@ stop_backup: snprintf(sqlquery, QUERY_STR_LEN, "SELECT pg_xlogfile_name(pg_stop_backup())"); log_debug("standby clone: %s\n", sqlquery); - + res = PQexec(conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) { @@ -981,7 +989,7 @@ stop_backup: if (!create_directory(local_control_file)) { log_err(_("%s: couldn't create directory %s, you will need to do it manually...\n"), - progname, runtime_options.dest_dir); + progname, runtime_options.dest_dir); r = ERR_NEEDS_XLOG; /* continue, but eventually exit returning error */ } @@ -991,7 +999,7 @@ stop_backup: PQclear(res); PQfinish(conn); - /* + /* * We don't start the service yet because we still may want to * move the directory */ @@ -1051,7 +1059,7 @@ do_standby_promote(void) /* Get the data directory full path and the last subdirectory */ snprintf(sqlquery, QUERY_STR_LEN, "SELECT setting " - " FROM pg_settings WHERE name = 'data_directory'"); + " FROM pg_settings WHERE name = 'data_directory'"); log_debug("standby promote: %s\n", sqlquery); res = PQexec(conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) @@ -1069,7 +1077,7 @@ do_standby_promote(void) snprintf(recovery_done_path, MAXFILENAME, "%s/%s", data_dir, RECOVERY_DONE_FILE); rename(recovery_file_path, recovery_done_path); - /* + /* * We assume the pg_ctl script is in the PATH. Restart and wait for * the server to finish starting, so that the check below will * find an active server rather than one starting up. This may @@ -1187,7 +1195,7 @@ do_standby_follow(void) /* Get the data directory full path */ snprintf(sqlquery, QUERY_STR_LEN, "SELECT setting " - " FROM pg_settings WHERE name = 'data_directory'"); + " FROM pg_settings WHERE name = 'data_directory'"); log_debug("standby follow: %s\n", sqlquery); res = PQexec(conn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) @@ -1308,10 +1316,10 @@ copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_ int r; if (strnlen(options.rsync_options, QUERY_STR_LEN) == 0) - snprintf(rsync_flags, QUERY_STR_LEN, "--archive --checksum --compress --progress --rsh=ssh"); + snprintf(rsync_flags, QUERY_STR_LEN, "--archive --checksum --compress --progress --rsh=ssh"); else - strncpy(rsync_flags, options.rsync_options, QUERY_STR_LEN); - + strncpy(rsync_flags, options.rsync_options, QUERY_STR_LEN); + if (runtime_options.force) strcat(rsync_flags, " --delete"); @@ -1328,12 +1336,12 @@ copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_ { strcat(rsync_flags, " --exclude=pg_xlog* --exclude=pg_control --exclude=*.pid"); snprintf(script, QUERY_STR_LEN, "rsync %s %s:%s/* %s", - rsync_flags, host_string, remote_path, local_path); + rsync_flags, host_string, remote_path, local_path); } else { snprintf(script, QUERY_STR_LEN, "rsync %s %s:%s %s/.", - rsync_flags, host_string, remote_path, local_path); + rsync_flags, host_string, remote_path, local_path); } log_info("rsync command line: '%s'\n", script); @@ -1364,7 +1372,7 @@ check_parameters_for_action(const int action) * all other parameters are at least useless and could be * confusing so reject them */ - if (runtime_options.host[0] || runtime_options.masterport[0] || runtime_options.username[0] || + if (runtime_options.host[0] || runtime_options.masterport[0] || runtime_options.username[0] || runtime_options.dbname[0]) { log_err("You can't use connection parameters to the master when issuing a MASTER REGISTER command.\n"); @@ -1384,7 +1392,7 @@ check_parameters_for_action(const int action) * we don't need connection parameters to the master * because we can detect the master in repl_nodes */ - if (runtime_options.host[0] || runtime_options.masterport[0] || runtime_options.username[0] || + if (runtime_options.host[0] || runtime_options.masterport[0] || runtime_options.username[0] || runtime_options.dbname[0]) { log_err("You can't use connection parameters to the master when issuing a STANDBY REGISTER command.\n"); @@ -1405,7 +1413,7 @@ check_parameters_for_action(const int action) * because we will try to detect the master in repl_nodes * if we can't find it then the promote action will be cancelled */ - if (runtime_options.host[0] || runtime_options.masterport[0] || runtime_options.username[0] || + if (runtime_options.host[0] || runtime_options.masterport[0] || runtime_options.username[0] || runtime_options.dbname[0]) { log_err("You can't use connection parameters to the master when issuing a STANDBY PROMOTE command.\n"); @@ -1426,7 +1434,7 @@ check_parameters_for_action(const int action) * because we will try to detect the master in repl_nodes * if we can't find it then the follow action will be cancelled */ - if (runtime_options.host[0] || runtime_options.masterport[0] || runtime_options.username[0] || + if (runtime_options.host[0] || runtime_options.masterport[0] || runtime_options.username[0] || runtime_options.dbname[0]) { log_err("You can't use connection parameters to the master when issuing a STANDBY FOLLOW command.\n"); diff --git a/repmgr.h b/repmgr.h index 2442d43c..6c2803e9 100644 --- a/repmgr.h +++ b/repmgr.h @@ -58,7 +58,8 @@ #define ERR_PROMOTED 8 /* Run time options type */ -typedef struct { +typedef struct +{ char dbname[MAXLEN]; char host[MAXLEN]; diff --git a/repmgrd.c b/repmgrd.c index 29943017..c02da090 100644 --- a/repmgrd.c +++ b/repmgrd.c @@ -50,7 +50,7 @@ char *config_file = DEFAULT_CONFIG_FILE; bool verbose = false; char repmgr_schema[MAXLEN]; -/* +/* * should initialize with {0} to be ANSI complaint ? but this raises * error with gcc -Wall */ t_configuration_options config = {}; @@ -189,7 +189,7 @@ main(int argc, char **argv) /* close the connection to the database and cleanup */ CloseConnections(); - + /* Shuts down logging system */ logger_shutdown(); @@ -284,8 +284,8 @@ MonitorExecute(void) /* Get local xlog info */ snprintf(sqlquery, QUERY_STR_LEN, - "SELECT CURRENT_TIMESTAMP, pg_last_xlog_receive_location(), " - "pg_last_xlog_replay_location()"); + "SELECT CURRENT_TIMESTAMP, pg_last_xlog_receive_location(), " + "pg_last_xlog_replay_location()"); res = PQexec(myLocalConn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) @@ -324,15 +324,15 @@ MonitorExecute(void) * Build the SQL to execute on primary */ snprintf(sqlquery, - QUERY_STR_LEN, "INSERT INTO %s.repl_monitor " - "VALUES(%d, %d, '%s'::timestamp with time zone, " - " '%s', '%s', " - " %lld, %lld)", repmgr_schema, - primary_options.node, local_options.node, monitor_standby_timestamp, - last_wal_primary_location, - last_wal_standby_received, - (lsn_primary - lsn_standby_received), - (lsn_standby_received - lsn_standby_applied)); + QUERY_STR_LEN, "INSERT INTO %s.repl_monitor " + "VALUES(%d, %d, '%s'::timestamp with time zone, " + " '%s', '%s', " + " %lld, %lld)", repmgr_schema, + primary_options.node, local_options.node, monitor_standby_timestamp, + last_wal_primary_location, + last_wal_standby_received, + (lsn_primary - lsn_standby_received), + (lsn_standby_received - lsn_standby_applied)); /* * Execute the query asynchronously, but don't check for a result. We @@ -340,7 +340,7 @@ MonitorExecute(void) */ if (PQsendQuery(primaryConn, sqlquery) == 0) log_warning("Query could not be sent to primary. %s\n", - PQerrorMessage(primaryConn)); + PQerrorMessage(primaryConn)); } @@ -350,8 +350,8 @@ checkClusterConfiguration(void) PGresult *res; snprintf(sqlquery, QUERY_STR_LEN, "SELECT oid FROM pg_class " - " WHERE oid = '%s.repl_nodes'::regclass", - repmgr_schema); + " WHERE oid = '%s.repl_nodes'::regclass", + repmgr_schema); res = PQexec(myLocalConn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) { @@ -388,8 +388,8 @@ checkNodeConfiguration(char *conninfo) * Check if we have my node information in repl_nodes */ snprintf(sqlquery, QUERY_STR_LEN, "SELECT * FROM %s.repl_nodes " - " WHERE id = %d AND cluster = '%s' ", - repmgr_schema, local_options.node, local_options.cluster_name); + " WHERE id = %d AND cluster = '%s' ", + repmgr_schema, local_options.node, local_options.cluster_name); res = PQexec(myLocalConn, sqlquery); if (PQresultStatus(res) != PGRES_TUPLES_OK) @@ -410,8 +410,8 @@ checkNodeConfiguration(char *conninfo) PQclear(res); /* Adding the node */ snprintf(sqlquery, QUERY_STR_LEN, "INSERT INTO %s.repl_nodes " - "VALUES (%d, '%s', '%s')", - repmgr_schema, local_options.node, local_options.cluster_name, local_options.conninfo); + "VALUES (%d, '%s', '%s')", + repmgr_schema, local_options.node, local_options.cluster_name, local_options.conninfo); if (!PQexec(primaryConn, sqlquery)) {