Added new log system to both repmgr and repmgrd. Needs cleaning, but it is a good starting point

This commit is contained in:
Gabriele Bartolini
2010-12-29 18:08:40 +01:00
committed by Greg Smith
parent 2c1eafd7a9
commit f6a6632169
3 changed files with 127 additions and 99 deletions

15
log.c
View File

@@ -50,7 +50,10 @@ bool logger_init(const char* ident, const char* level, const char* facility)
#ifdef HAVE_SYSLOG #ifdef HAVE_SYSLOG
int syslog_facility = DEFAULT_SYSLOG_FACILITY; int syslog_facility = DEFAULT_SYSLOG_FACILITY;
#endif #endif
printf("Logger init: detect stuff: %s, %s\n", level, facility);
#ifdef REPMGR_DEBUG
printf("Logger initialisation (Level: %s, Facility: %s)\n", level, facility);
#endif
if (!ident) { if (!ident) {
ident = DEFAULT_IDENT; ident = DEFAULT_IDENT;
@@ -58,7 +61,9 @@ printf("Logger init: detect stuff: %s, %s\n", level, facility);
if (level) { if (level) {
l = detect_log_level(level); l = detect_log_level(level);
printf("Logger level: %d\n", l); #ifdef REPMGR_DEBUG
printf("Assigned level for logger: %d\n", l);
#endif
if (l > 0) if (l > 0)
log_level = l; log_level = l;
@@ -67,8 +72,12 @@ printf("Logger level: %d\n", l);
} }
if (facility) { if (facility) {
f = detect_log_facility(facility); f = detect_log_facility(facility);
printf("Logger facility: %d\n", f); #ifdef REPMGR_DEBUG
printf("Assigned facility for logger: %d\n", f);
#endif
if (f == 0) { if (f == 0) {
/* No syslog requested, just stderr */ /* No syslog requested, just stderr */
stderr_log_notice(_("Use stderr for logging")); stderr_log_notice(_("Use stderr for logging"));

156
repmgr.c
View File

@@ -55,9 +55,11 @@ static void do_standby_register(void);
static void do_standby_clone(void); static void do_standby_clone(void);
static void do_standby_promote(void); static void do_standby_promote(void);
static void do_standby_follow(void); static void do_standby_follow(void);
static void help(const char* progname);
static void usage(void);
/* Global variables */
const char *progname; const char *progname;
const char *keywords[6]; const char *keywords[6];
const char *values[6]; const char *values[6];
@@ -153,7 +155,7 @@ main(int argc, char **argv)
verbose = true; verbose = true;
break; break;
default: default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
exit(1); exit(1);
} }
} }
@@ -171,7 +173,7 @@ main(int argc, char **argv)
server_mode = argv[optind++]; server_mode = argv[optind++];
if (strcasecmp(server_mode, "STANDBY") != 0 && strcasecmp(server_mode, "MASTER") != 0) if (strcasecmp(server_mode, "STANDBY") != 0 && strcasecmp(server_mode, "MASTER") != 0)
{ {
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
exit(1); exit(1);
} }
} }
@@ -200,7 +202,7 @@ main(int argc, char **argv)
action = STANDBY_FOLLOW; action = STANDBY_FOLLOW;
else else
{ {
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
exit(1); exit(1);
} }
} }
@@ -212,7 +214,8 @@ main(int argc, char **argv)
{ {
if (host != NULL) if (host != NULL)
{ {
fprintf(stderr, _("Conflicting parameters you can't use -h while providing a node separately. Try \"%s --help\" for more information.\n"), progname); log_err(_("Conflicting parameters you can't use -h while providing a node separately."));
usage();
exit(1); exit(1);
} }
host = argv[optind++]; host = argv[optind++];
@@ -224,9 +227,9 @@ main(int argc, char **argv)
case 0: case 0:
break; break;
default: default:
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), log_err(_("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]); progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
exit(1); exit(1);
} }
@@ -278,7 +281,7 @@ main(int argc, char **argv)
parse_config(config_file, &options); parse_config(config_file, &options);
if (options.node == -1) if (options.node == -1)
{ {
fprintf(stderr, "Node information is missing. " log_err("Node information is missing. "
"Check the configuration file.\n"); "Check the configuration file.\n");
exit(1); exit(1);
} }
@@ -303,7 +306,7 @@ main(int argc, char **argv)
do_standby_follow(); do_standby_follow();
break; break;
default: default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
exit(1); exit(1);
} }
logger_shutdown(); logger_shutdown();
@@ -494,14 +497,14 @@ do_standby_register(void)
if (strcmp(standby_version, "") == 0) if (strcmp(standby_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);
fprintf(stderr, _("%s needs standby to be PostgreSQL 9.0 or better\n"), progname); log_err(_("%s needs standby to be PostgreSQL 9.0 or better\n"), progname);
return; return;
} }
/* Check we are a standby */ /* Check we are a standby */
if (!is_standby(conn)) if (!is_standby(conn))
{ {
fprintf(stderr, "repmgr: This node should be a standby\n"); log_err(_("repmgr: This node should be a standby (%s)\n"), options.conninfo);
PQfinish(conn); PQfinish(conn);
return; return;
} }
@@ -511,7 +514,7 @@ do_standby_register(void)
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "Can't get info about tablespaces: %s\n", PQerrorMessage(conn)); log_err("Can't get info about tablespaces: %s\n", PQerrorMessage(conn));
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
return; return;
@@ -519,7 +522,7 @@ do_standby_register(void)
if (PQntuples(res) == 0) /* schema doesn't exists */ if (PQntuples(res) == 0) /* schema doesn't exists */
{ {
fprintf(stderr, "Schema repmgr_%s doesn't exists.", options.cluster_name); log_err("Schema repmgr_%s doesn't exists.", options.cluster_name);
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
return; return;
@@ -529,7 +532,7 @@ do_standby_register(void)
/* check if there is a master in this cluster */ /* check if there is a master in this cluster */
master_conn = getMasterConnection(conn, options.node, options.cluster_name, &master_id); master_conn = getMasterConnection(conn, options.node, options.cluster_name, &master_id);
if (!master_conn) { if (!master_conn) {
fprintf(stderr, _("Cannot retrieve information about the connection to the master\n")); log_err(_("Cannot retrieve information about the connection to the master\n"));
return; return;
} }
@@ -539,7 +542,7 @@ do_standby_register(void)
{ {
PQfinish(conn); PQfinish(conn);
PQfinish(master_conn); PQfinish(master_conn);
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"), progname); log_err(_("%s needs master to be PostgreSQL 9.0 or better\n"), progname);
return; return;
} }
@@ -548,7 +551,7 @@ do_standby_register(void)
{ {
PQfinish(conn); PQfinish(conn);
PQfinish(master_conn); PQfinish(master_conn);
fprintf(stderr, _("%s needs versions of both master (%s) and standby (%s) to match.\n"), log_err(_("%s needs versions of both master (%s) and standby (%s) to match.\n"),
progname, master_version, standby_version); progname, master_version, standby_version);
return; return;
} }
@@ -563,7 +566,7 @@ do_standby_register(void)
if (!PQexec(master_conn, sqlquery)) if (!PQexec(master_conn, sqlquery))
{ {
fprintf(stderr, "Cannot delete node details, %s\n", log_err("Cannot delete node details, %s\n",
PQerrorMessage(master_conn)); PQerrorMessage(master_conn));
PQfinish(master_conn); PQfinish(master_conn);
PQfinish(conn); PQfinish(conn);
@@ -574,11 +577,11 @@ do_standby_register(void)
sprintf(sqlquery, "INSERT INTO repmgr_%s.repl_nodes " sprintf(sqlquery, "INSERT INTO repmgr_%s.repl_nodes "
"VALUES (%d, '%s', '%s')", "VALUES (%d, '%s', '%s')",
options.cluster_name, options.node, options.cluster_name, options.conninfo); options.cluster_name, options.node, options.cluster_name, options.conninfo);
fprintf(stderr, "QUERY: %s\n", sqlquery); log_debug("QUERY: %s\n", sqlquery);
if (!PQexec(master_conn, sqlquery)) if (!PQexec(master_conn, sqlquery))
{ {
fprintf(stderr, "Cannot insert node details, %s\n", log_err("Cannot insert node details, %s\n",
PQerrorMessage(master_conn)); PQerrorMessage(master_conn));
PQfinish(master_conn); PQfinish(master_conn);
PQfinish(conn); PQfinish(conn);
@@ -735,7 +738,7 @@ do_standby_clone(void)
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "Can't get info about tablespaces: %s\n", PQerrorMessage(conn)); log_err("Can't get info about tablespaces: %s\n", PQerrorMessage(conn));
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
return; return;
@@ -756,7 +759,7 @@ do_standby_clone(void)
if (!create_directory(tblspc_dir)) if (!create_directory(tblspc_dir))
{ {
fprintf(stderr, _("%s: couldn't create directory \"%s\"... "), log_err(_("%s: couldn't create directory \"%s\"... "),
progname, tblspc_dir); progname, tblspc_dir);
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
@@ -772,7 +775,7 @@ do_standby_clone(void)
if (!set_directory_permissions(tblspc_dir)) if (!set_directory_permissions(tblspc_dir))
{ {
fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"), log_err(_("%s: could not change permissions of directory \"%s\": %s\n"),
progname, tblspc_dir, strerror(errno)); progname, tblspc_dir, strerror(errno));
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
@@ -792,7 +795,7 @@ do_standby_clone(void)
} }
default: default:
/* Trouble accessing directory */ /* Trouble accessing directory */
fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"), log_err(_("%s: could not access directory \"%s\": %s\n"),
progname, tblspc_dir, strerror(errno)); progname, tblspc_dir, strerror(errno));
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
@@ -800,7 +803,7 @@ do_standby_clone(void)
} }
} }
fprintf(stderr, "Starting backup...\n"); log_notice("Starting backup...\n");
/* Get the data directory full path and the configuration files location */ /* Get the data directory full path and the configuration files location */
sprintf(sqlquery, "SELECT name, setting " sprintf(sqlquery, "SELECT name, setting "
@@ -809,7 +812,7 @@ do_standby_clone(void)
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "Can't get info about data directory and configuration files: %s\n", PQerrorMessage(conn)); log_err("Can't get info about data directory and configuration files: %s\n", PQerrorMessage(conn));
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
return; return;
@@ -825,7 +828,7 @@ do_standby_clone(void)
else if (strcmp(PQgetvalue(res, i, 0), "ident_file") == 0) else if (strcmp(PQgetvalue(res, i, 0), "ident_file") == 0)
strcpy(master_ident_file, PQgetvalue(res, i, 1)); strcpy(master_ident_file, PQgetvalue(res, i, 1));
else else
fprintf(stderr, _("uknown parameter: %s"), PQgetvalue(res, i, 0)); log_warning(_("unknown parameter: %s"), PQgetvalue(res, i, 0));
} }
PQclear(res); PQclear(res);
@@ -834,10 +837,12 @@ do_standby_clone(void)
* so we can say to the user we need those files * so we can say to the user we need those files
*/ */
sprintf(sqlquery, "SELECT pg_xlogfile_name(pg_start_backup('repmgr_standby_clone_%ld'))", time(NULL)); sprintf(sqlquery, "SELECT pg_xlogfile_name(pg_start_backup('repmgr_standby_clone_%ld'))", time(NULL));
log_debug("standby clone: %s", sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "Can't start backup: %s\n", PQerrorMessage(conn)); log_err("Can't start backup: %s\n", PQerrorMessage(conn));
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
return; return;
@@ -863,7 +868,7 @@ do_standby_clone(void)
sprintf(local_control_file, "%s/global", dest_dir); sprintf(local_control_file, "%s/global", dest_dir);
if (!create_directory(local_control_file)) if (!create_directory(local_control_file))
{ {
fprintf(stderr, _("%s: couldn't create directory %s ... "), log_err(_("%s: couldn't create directory %s ... "),
progname, dest_dir); progname, dest_dir);
goto stop_backup; goto stop_backup;
} }
@@ -884,7 +889,7 @@ do_standby_clone(void)
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "Can't get info about tablespaces: %s\n", PQerrorMessage(conn)); log_err("Can't get info about tablespaces: %s\n", PQerrorMessage(conn));
PQclear(res); PQclear(res);
goto stop_backup; goto stop_backup;
} }
@@ -912,18 +917,20 @@ stop_backup:
conn = PQconnectdbParams(keywords, values, true); conn = PQconnectdbParams(keywords, values, true);
if (!conn) if (!conn)
{ {
fprintf(stderr, _("%s: could not connect to master\n"), log_err(_("%s: could not connect to master\n"),
progname); progname);
return; return;
} }
fprintf(stderr, "Finishing backup...\n"); log_notice("Finishing backup...\n");
sprintf(sqlquery, "SELECT pg_xlogfile_name(pg_stop_backup())"); sprintf(sqlquery, "SELECT pg_xlogfile_name(pg_stop_backup())");
log_debug("standby clone: %s", sqlquery);
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "Can't stop backup: %s\n", PQerrorMessage(conn)); log_err("Can't stop backup: %s\n", PQerrorMessage(conn));
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
return; return;
@@ -944,7 +951,7 @@ stop_backup:
sprintf(local_control_file, "%s/pg_xlog", dest_dir); sprintf(local_control_file, "%s/pg_xlog", dest_dir);
if (!create_directory(local_control_file)) if (!create_directory(local_control_file))
{ {
fprintf(stderr, _("%s: couldn't create directory %s, you will need to do it manually...\n"), log_err(_("%s: couldn't create directory %s, you will need to do it manually...\n"),
progname, dest_dir); progname, dest_dir);
} }
@@ -982,14 +989,14 @@ do_standby_promote(void)
if (strcmp(standby_version, "") == 0) if (strcmp(standby_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);
fprintf(stderr, _("%s needs standby to be PostgreSQL 9.0 or better\n"), progname); log_err(_("%s needs standby to be PostgreSQL 9.0 or better\n"), progname);
return; return;
} }
/* Check we are in a standby node */ /* Check we are in a standby node */
if (!is_standby(conn)) if (!is_standby(conn))
{ {
fprintf(stderr, "repmgr: The command should be executed in a standby node\n"); log_err("repmgr: The command should be executed on a standby node\n");
return; return;
} }
@@ -998,7 +1005,7 @@ do_standby_promote(void)
if (old_master_conn != NULL) if (old_master_conn != NULL)
{ {
PQfinish(old_master_conn); PQfinish(old_master_conn);
fprintf(stderr, "There is a master already in this cluster"); log_err("There is a master already in this cluster");
return; return;
} }
@@ -1011,7 +1018,7 @@ do_standby_promote(void)
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "Can't get info about data directory: %s\n", PQerrorMessage(conn)); log_err("Can't get info about data directory: %s\n", PQerrorMessage(conn));
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
return; return;
@@ -1029,7 +1036,7 @@ do_standby_promote(void)
r = system(script); r = system(script);
if (r != 0) if (r != 0)
{ {
fprintf(stderr, "Can't restart service\n"); log_err("Can't restart service\n");
return; return;
} }
@@ -1039,9 +1046,9 @@ do_standby_promote(void)
* but is just the server starting up * but is just the server starting up
* conn = establishDBConnection(options.conninfo, true); * conn = establishDBConnection(options.conninfo, true);
* if (is_standby(conn)) * if (is_standby(conn))
* fprintf(stderr, "\n%s: STANDBY PROMOTE failed, this is still a standby node.\n", progname); * log_err("\n%s: STANDBY PROMOTE failed, this is still a standby node.\n", progname);
* else * else
* fprintf(stderr, "\n%s: you should REINDEX any hash indexes you have.\n", progname); * log_err("\n%s: you should REINDEX any hash indexes you have.\n", progname);
* PQfinish(conn); * PQfinish(conn);
*/ */
@@ -1072,7 +1079,7 @@ do_standby_follow(void)
/* Check we are in a standby node */ /* Check we are in a standby node */
if (!is_standby(conn)) if (!is_standby(conn))
{ {
fprintf(stderr, "\n%s: The command should be executed in a standby node\n", progname); log_err("\n%s: The command should be executed in a standby node\n", progname);
return; return;
} }
@@ -1081,7 +1088,7 @@ do_standby_follow(void)
if (strcmp(standby_version, "") == 0) if (strcmp(standby_version, "") == 0)
{ {
PQfinish(conn); PQfinish(conn);
fprintf(stderr, _("\n%s needs standby to be PostgreSQL 9.0 or better\n"), progname); log_err(_("\n%s needs standby to be PostgreSQL 9.0 or better\n"), progname);
return; return;
} }
@@ -1090,7 +1097,7 @@ do_standby_follow(void)
if (master_conn == NULL) if (master_conn == NULL)
{ {
PQfinish(conn); PQfinish(conn);
fprintf(stderr, "There isn't a master to follow in this cluster"); log_err("There isn't a master to follow in this cluster");
return; return;
} }
@@ -1098,7 +1105,7 @@ do_standby_follow(void)
if (is_standby(master_conn)) if (is_standby(master_conn))
{ {
PQfinish(conn); PQfinish(conn);
fprintf(stderr, "%s: The node to follow should be a master\n", progname); log_err("%s: The node to follow should be a master\n", progname);
return; return;
} }
@@ -1108,7 +1115,7 @@ do_standby_follow(void)
{ {
PQfinish(conn); PQfinish(conn);
PQfinish(master_conn); PQfinish(master_conn);
fprintf(stderr, _("%s needs master to be PostgreSQL 9.0 or better\n"), progname); log_err(_("%s needs master to be PostgreSQL 9.0 or better\n"), progname);
return; return;
} }
@@ -1117,7 +1124,7 @@ do_standby_follow(void)
{ {
PQfinish(conn); PQfinish(conn);
PQfinish(master_conn); PQfinish(master_conn);
fprintf(stderr, _("%s needs versions of both master (%s) and standby (%s) to match.\n"), log_err(_("%s needs versions of both master (%s) and standby (%s) to match.\n"),
progname, master_version, standby_version); progname, master_version, standby_version);
return; return;
} }
@@ -1142,7 +1149,7 @@ do_standby_follow(void)
res = PQexec(conn, sqlquery); res = PQexec(conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "Can't get info about data directory: %s\n", PQerrorMessage(conn)); log_err("Can't get info about data directory: %s\n", PQerrorMessage(conn));
PQclear(res); PQclear(res);
PQfinish(conn); PQfinish(conn);
return; return;
@@ -1161,7 +1168,7 @@ do_standby_follow(void)
r = system(script); r = system(script);
if (r != 0) if (r != 0)
{ {
fprintf(stderr, "Can't restart service\n"); log_err("Can't restart service\n");
return; return;
} }
@@ -1169,8 +1176,13 @@ do_standby_follow(void)
} }
static void void usage(void)
help(const char *progname) {
log_err(_("\n\n%s: Replicator manager \n"), progname);
log_err(_("Try \"%s --help\" for more information.\n"), progname);
}
void help(const char *progname)
{ {
printf(_("\n%s: Replicator manager \n"), progname); printf(_("\n%s: Replicator manager \n"), progname);
printf(_("Usage:\n")); printf(_("Usage:\n"));
@@ -1216,14 +1228,14 @@ create_recovery_file(const char *data_dir)
recovery_file = fopen(recovery_file_path, "w"); recovery_file = fopen(recovery_file_path, "w");
if (recovery_file == NULL) if (recovery_file == NULL)
{ {
fprintf(stderr, "could not create recovery.conf file, it could be necessary to create it manually\n"); log_err("could not create recovery.conf file, it could be necessary to create it manually\n");
return false; return false;
} }
sprintf(line, "standby_mode = 'on'\n"); sprintf(line, "standby_mode = 'on'\n");
if (fputs(line, recovery_file) == EOF) if (fputs(line, recovery_file) == EOF)
{ {
fprintf(stderr, "recovery file could not be written, it could be necessary to create it manually\n"); log_err("recovery file could not be written, it could be necessary to create it manually\n");
fclose(recovery_file); fclose(recovery_file);
return false; return false;
} }
@@ -1231,7 +1243,7 @@ create_recovery_file(const char *data_dir)
sprintf(line, "primary_conninfo = 'host=%s port=%s'\n", host, ((masterport==NULL) ? "5432" : masterport)); sprintf(line, "primary_conninfo = 'host=%s port=%s'\n", host, ((masterport==NULL) ? "5432" : masterport));
if (fputs(line, recovery_file) == EOF) if (fputs(line, recovery_file) == EOF)
{ {
fprintf(stderr, "recovery file could not be written, it could be necessary to create it manually\n"); log_err("recovery file could not be written, it could be necessary to create it manually\n");
fclose(recovery_file); fclose(recovery_file);
return false; return false;
} }
@@ -1286,7 +1298,7 @@ copy_remote_files(char *host, char *remote_user, char *remote_path, char *local_
r = system(script); r = system(script);
if (r != 0) if (r != 0)
fprintf(stderr, _("Can't rsync from remote file or directory (%s:%s)\n"), log_err(_("Can't rsync from remote file or directory (%s:%s)\n"),
host_string, remote_path); host_string, remote_path);
return r; return r;
@@ -1312,14 +1324,14 @@ check_parameters_for_action(const int action)
if ((host != NULL) || (masterport != NULL) || (username != NULL) || if ((host != NULL) || (masterport != NULL) || (username != NULL) ||
(dbname != NULL)) (dbname != NULL))
{ {
fprintf(stderr, "\nYou can't use connection parameters to the master when issuing a MASTER REGISTER command."); log_err("\nYou can't use connection parameters to the master when issuing a MASTER REGISTER command.");
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
ok = false; ok = false;
} }
if (dest_dir != NULL) if (dest_dir != NULL)
{ {
fprintf(stderr, "\nYou don't need a destination directory for MASTER REGISTER command"); log_err("\nYou don't need a destination directory for MASTER REGISTER command");
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
ok = false; ok = false;
} }
break; break;
@@ -1332,14 +1344,14 @@ check_parameters_for_action(const int action)
if ((host != NULL) || (masterport != NULL) || (username != NULL) || if ((host != NULL) || (masterport != NULL) || (username != NULL) ||
(dbname != NULL)) (dbname != NULL))
{ {
fprintf(stderr, "\nYou can't use connection parameters to the master when issuing a STANDBY REGISTER command."); log_err("\nYou can't use connection parameters to the master when issuing a STANDBY REGISTER command.");
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
ok = false; ok = false;
} }
if (dest_dir != NULL) if (dest_dir != NULL)
{ {
fprintf(stderr, "\nYou don't need a destination directory for STANDBY REGISTER command"); log_err("\nYou don't need a destination directory for STANDBY REGISTER command");
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
ok = false; ok = false;
} }
break; break;
@@ -1353,14 +1365,14 @@ check_parameters_for_action(const int action)
if ((host != NULL) || (masterport != NULL) || (username != NULL) || if ((host != NULL) || (masterport != NULL) || (username != NULL) ||
(dbname != NULL)) (dbname != NULL))
{ {
fprintf(stderr, "\nYou can't use connection parameters to the master when issuing a STANDBY PROMOTE command."); log_err("\nYou can't use connection parameters to the master when issuing a STANDBY PROMOTE command.");
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
ok = false; ok = false;
} }
if (dest_dir != NULL) if (dest_dir != NULL)
{ {
fprintf(stderr, "\nYou don't need a destination directory for STANDBY PROMOTE command"); log_err("\nYou don't need a destination directory for STANDBY PROMOTE command");
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
ok = false; ok = false;
} }
break; break;
@@ -1374,14 +1386,14 @@ check_parameters_for_action(const int action)
if ((host != NULL) || (masterport != NULL) || (username != NULL) || if ((host != NULL) || (masterport != NULL) || (username != NULL) ||
(dbname != NULL)) (dbname != NULL))
{ {
fprintf(stderr, "\nYou can't use connection parameters to the master when issuing a STANDBY FOLLOW command."); log_err("\nYou can't use connection parameters to the master when issuing a STANDBY FOLLOW command.");
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
ok = false; ok = false;
} }
if (dest_dir != NULL) if (dest_dir != NULL)
{ {
fprintf(stderr, "\nYou don't need a destination directory for STANDBY FOLLOW command"); log_err("\nYou don't need a destination directory for STANDBY FOLLOW command");
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
ok = false; ok = false;
} }
break; break;
@@ -1393,8 +1405,8 @@ check_parameters_for_action(const int action)
*/ */
if (config_file != NULL) if (config_file != NULL)
{ {
fprintf(stderr, "\nYou need to use connection parameters to the master when issuing a STANDBY CLONE command."); log_err("\nYou need to use connection parameters to the master when issuing a STANDBY CLONE command.");
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
ok = false; ok = false;
} }
break; break;

View File

@@ -51,7 +51,8 @@ bool verbose = false;
// should initialize with {0} to be ANSI complaint ? but this raises error with gcc -Wall // should initialize with {0} to be ANSI complaint ? but this raises error with gcc -Wall
repmgr_config config = {}; repmgr_config config = {};
static void help(const char *progname); static void help(const char* progname);
static void usage(void);
static void checkClusterConfiguration(void); static void checkClusterConfiguration(void);
static void checkNodeConfiguration(char *conninfo); static void checkNodeConfiguration(char *conninfo);
static void CancelQuery(void); static void CancelQuery(void);
@@ -125,7 +126,7 @@ main(int argc, char **argv)
verbose = true; verbose = true;
break; break;
default: default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); usage();
exit(1); exit(1);
} }
} }
@@ -138,7 +139,7 @@ main(int argc, char **argv)
parse_config(config_file, &local_options); parse_config(config_file, &local_options);
if (local_options.node == -1) if (local_options.node == -1)
{ {
fprintf(stderr, "Node information is missing. " log_err("Node information is missing. "
"Check the configuration file.\n"); "Check the configuration file.\n");
exit(1); exit(1);
} }
@@ -151,7 +152,7 @@ main(int argc, char **argv)
if (strcmp(standby_version, "") == 0) if (strcmp(standby_version, "") == 0)
{ {
PQfinish(myLocalConn); PQfinish(myLocalConn);
fprintf(stderr, _("%s needs standby to be PostgreSQL 9.0 or better\n"), progname); log_err(_("%s needs standby to be PostgreSQL 9.0 or better\n"), progname);
exit(1); exit(1);
} }
@@ -184,6 +185,9 @@ main(int argc, char **argv)
/* close the connection to the database and cleanup */ /* close the connection to the database and cleanup */
CloseConnections(); CloseConnections();
/* Shuts down logging system */
logger_shutdown();
return 0; return 0;
} }
@@ -216,7 +220,7 @@ MonitorExecute(void)
{ {
if (PQstatus(primaryConn) != CONNECTION_OK) if (PQstatus(primaryConn) != CONNECTION_OK)
{ {
fprintf(stderr, "\n%s: Connection to master has been lost, trying to recover...\n", progname); log_warning(_("Connection to master has been lost, trying to recover..."));
/* wait 20 seconds between retries */ /* wait 20 seconds between retries */
sleep(20); sleep(20);
@@ -224,26 +228,25 @@ MonitorExecute(void)
} }
else else
{ {
fprintf(stderr, "\n%s: Connection to master has been restored, continue monitoring.\n", progname); log_notice(_("Connection to master has been restored, continue monitoring."));
break; break;
} }
} }
if (PQstatus(primaryConn) != CONNECTION_OK) if (PQstatus(primaryConn) != CONNECTION_OK)
{ {
fprintf(stderr, "\n%s: We couldn't reconnect to master, checking if " log_err(_("We couldn't reconnect to master. Now checking if another node has been promoted."));
"another node has been promoted.\n", progname);
for (connection_retries = 0; connection_retries < 6; connection_retries++) for (connection_retries = 0; connection_retries < 6; connection_retries++)
{ {
primaryConn = getMasterConnection(myLocalConn, local_options.node, local_options.cluster_name, &primary_options.node); primaryConn = getMasterConnection(myLocalConn, local_options.node, local_options.cluster_name, &primary_options.node);
if (PQstatus(primaryConn) == CONNECTION_OK) if (PQstatus(primaryConn) == CONNECTION_OK)
{ {
/* Connected, we can continue the process so break the loop */ /* Connected, we can continue the process so break the loop */
fprintf(stderr, "\n%s: Connected to node %d, continue monitoring.\n", progname, primary_options.node); log_err(_("Connected to node %d, continue monitoring."), primary_options.node);
break; break;
} }
else else
{ {
fprintf(stderr, "\n%s: We haven't found a new master, waiting before retry...\n", progname); log_err(_("We haven't found a new master, waiting before retry..."));
/* wait 5 minutes before retries, after 6 failures (30 minutes) we stop trying */ /* wait 5 minutes before retries, after 6 failures (30 minutes) we stop trying */
sleep(300); sleep(300);
} }
@@ -251,15 +254,14 @@ MonitorExecute(void)
} }
if (PQstatus(primaryConn) != CONNECTION_OK) if (PQstatus(primaryConn) != CONNECTION_OK)
{ {
fprintf(stderr, "\n%s: We couldn't reconnect for long enough, exiting...\n", progname); log_err(_("We couldn't reconnect for long enough, exiting..."));
exit(1); exit(1);
} }
/* Check if we still are a standby, we could have been promoted */ /* Check if we still are a standby, we could have been promoted */
if (!is_standby(myLocalConn)) if (!is_standby(myLocalConn))
{ {
fprintf(stderr, "\n%s: seems like we have been promoted, so exit from monitoring...\n", log_err(_("It seems like we have been promoted, so exit from monitoring..."));
progname);
CloseConnections(); CloseConnections();
exit(1); exit(1);
} }
@@ -280,7 +282,7 @@ MonitorExecute(void)
res = PQexec(myLocalConn, sqlquery); res = PQexec(myLocalConn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "PQexec failed: %s\n", PQerrorMessage(myLocalConn)); log_err("PQexec failed: %s\n", PQerrorMessage(myLocalConn));
PQclear(res); PQclear(res);
/* if there is any error just let it be and retry in next loop */ /* if there is any error just let it be and retry in next loop */
return; return;
@@ -297,7 +299,7 @@ MonitorExecute(void)
res = PQexec(primaryConn, sqlquery); res = PQexec(primaryConn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "PQexec failed: %s\n", PQerrorMessage(primaryConn)); log_err("PQexec failed: %s\n", PQerrorMessage(primaryConn));
PQclear(res); PQclear(res);
return; return;
} }
@@ -329,7 +331,7 @@ MonitorExecute(void)
* will check the result next time we pause for a monitor step. * will check the result next time we pause for a monitor step.
*/ */
if (PQsendQuery(primaryConn, sqlquery) == 0) if (PQsendQuery(primaryConn, sqlquery) == 0)
fprintf(stderr, "Query could not be sent to primary. %s\n", log_warning("Query could not be sent to primary. %s\n",
PQerrorMessage(primaryConn)); PQerrorMessage(primaryConn));
} }
@@ -345,7 +347,7 @@ checkClusterConfiguration(void)
res = PQexec(myLocalConn, sqlquery); res = PQexec(myLocalConn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "PQexec failed: %s\n", PQerrorMessage(myLocalConn)); log_err("PQexec failed: %s\n", PQerrorMessage(myLocalConn));
PQclear(res); PQclear(res);
PQfinish(myLocalConn); PQfinish(myLocalConn);
PQfinish(primaryConn); PQfinish(primaryConn);
@@ -359,7 +361,7 @@ checkClusterConfiguration(void)
*/ */
if (PQntuples(res) == 0) if (PQntuples(res) == 0)
{ {
fprintf(stderr, "The replication cluster is not configured\n"); log_err("The replication cluster is not configured\n");
PQclear(res); PQclear(res);
PQfinish(myLocalConn); PQfinish(myLocalConn);
PQfinish(primaryConn); PQfinish(primaryConn);
@@ -384,7 +386,7 @@ checkNodeConfiguration(char *conninfo)
res = PQexec(myLocalConn, sqlquery); res = PQexec(myLocalConn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
fprintf(stderr, "PQexec failed: %s\n", PQerrorMessage(myLocalConn)); log_err("PQexec failed: %s\n", PQerrorMessage(myLocalConn));
PQclear(res); PQclear(res);
PQfinish(myLocalConn); PQfinish(myLocalConn);
PQfinish(primaryConn); PQfinish(primaryConn);
@@ -405,7 +407,7 @@ checkNodeConfiguration(char *conninfo)
if (!PQexec(primaryConn, sqlquery)) if (!PQexec(primaryConn, sqlquery))
{ {
fprintf(stderr, "Cannot insert node details, %s\n", log_err("Cannot insert node details, %s\n",
PQerrorMessage(primaryConn)); PQerrorMessage(primaryConn));
PQfinish(myLocalConn); PQfinish(myLocalConn);
PQfinish(primaryConn); PQfinish(primaryConn);
@@ -424,15 +426,20 @@ walLocationToBytes(char *wal_location)
if (sscanf(wal_location, "%X/%X", &xlogid, &xrecoff) != 2) if (sscanf(wal_location, "%X/%X", &xlogid, &xrecoff) != 2)
{ {
fprintf(stderr, "wrong log location format: %s\n", wal_location); log_err("wrong log location format: %s\n", wal_location);
return 0; return 0;
} }
return ((xlogid * 16 * 1024 * 1024 * 255) + xrecoff); return ((xlogid * 16 * 1024 * 1024 * 255) + xrecoff);
} }
static void void usage(void)
help(const char *progname) {
log_err(_("\n\n%s: Replicator manager daemon \n"), progname);
log_err(_("Try \"%s --help\" for more information.\n"), progname);
}
void help(const char *progname)
{ {
printf(_("\n%s: Replicator manager daemon \n"), progname); printf(_("\n%s: Replicator manager daemon \n"), progname);
printf(_("Usage:\n")); printf(_("Usage:\n"));
@@ -471,7 +478,7 @@ CancelQuery(void)
pgcancel = PQgetCancel(primaryConn); pgcancel = PQgetCancel(primaryConn);
if (!pgcancel || PQcancel(pgcancel, errbuf, 256) == 0) if (!pgcancel || PQcancel(pgcancel, errbuf, 256) == 0)
fprintf(stderr, "Can't stop current query: %s", errbuf); log_warning("Can't stop current query: %s", errbuf);
PQfreeCancel(pgcancel); PQfreeCancel(pgcancel);
} }