Create event record for rempgrd termination

Also fix a few incorrect exit codes.
This commit is contained in:
Ian Barwick
2015-03-17 19:08:59 +09:00
parent 9e2736be4c
commit 7f98bb7aec

144
repmgrd.c
View File

@@ -268,11 +268,28 @@ main(int argc, char **argv)
server_version_num = get_server_version(my_local_conn, NULL); server_version_num = get_server_version(my_local_conn, NULL);
if(server_version_num < MIN_SUPPORTED_VERSION_NUM) if(server_version_num < MIN_SUPPORTED_VERSION_NUM)
{ {
PQExpBufferData errmsg;
initPQExpBuffer(&errmsg);
if (server_version_num > 0) if (server_version_num > 0)
log_err(_("%s requires PostgreSQL %s or later\n"), {
progname, appendPQExpBuffer(&errmsg,
MIN_SUPPORTED_VERSION _("%s requires PostgreSQL %s or later"),
); progname,
MIN_SUPPORTED_VERSION);
}
else
{
appendPQExpBuffer(&errmsg,
_("unable to determine PostgreSQL server version"));
}
log_err("%s\n", errmsg.data);
create_event_record(my_local_conn,
&local_options,
local_options.node,
"repmgrd_shutdown",
false,
errmsg.data);
terminate(ERR_BAD_CONFIG); terminate(ERR_BAD_CONFIG);
} }
@@ -386,7 +403,7 @@ main(int argc, char **argv)
case STANDBY: case STANDBY:
/* We need the node id of the primary server as well as a connection to it */ /* We need the node id of the primary server as well as a connection to it */
log_info(_("connecting to master for cluster '%s'\n"), log_info(_("connecting to master node '%s'\n"),
local_options.cluster_name); local_options.cluster_name);
primary_conn = get_master_connection(my_local_conn, primary_conn = get_master_connection(my_local_conn,
@@ -395,6 +412,19 @@ main(int argc, char **argv)
if (primary_conn == NULL) if (primary_conn == NULL)
{ {
PQExpBufferData errmsg;
initPQExpBuffer(&errmsg);
appendPQExpBuffer(&errmsg,
_("unable to connect to master node '%s'"),
local_options.cluster_name);
create_event_record(my_local_conn,
&local_options,
local_options.node,
"repmgrd_shutdown",
false,
errmsg.data);
terminate(ERR_BAD_CONFIG); terminate(ERR_BAD_CONFIG);
} }
@@ -560,10 +590,21 @@ witness_monitor(void)
if(connection_ok == false) if(connection_ok == false)
{ {
log_err(_("unable to determine a valid master server, exiting...\n")); PQExpBufferData errmsg;
initPQExpBuffer(&errmsg);
appendPQExpBuffer(&errmsg,
_("unable to determine a valid master node, terminating..."));
log_err("%s\n", errmsg.data);
create_event_record(my_local_conn,
&local_options,
local_options.node,
"repmgrd_shutdown",
false,
errmsg.data);
terminate(ERR_DB_CON); terminate(ERR_DB_CON);
} }
} }
/* Fast path for the case where no history is requested */ /* Fast path for the case where no history is requested */
@@ -662,9 +703,24 @@ standby_monitor(void)
if (!check_connection(my_local_conn, "standby")) if (!check_connection(my_local_conn, "standby"))
{ {
PQExpBufferData errmsg;
set_local_node_failed(); set_local_node_failed();
log_err(_("failed to connect to local node, terminating!\n"));
terminate(1); initPQExpBuffer(&errmsg);
appendPQExpBuffer(&errmsg,
_("failed to connect to local node, node marked as failed and terminating!"));
log_err("%s\n", errmsg.data);
create_event_record(my_local_conn,
&local_options,
local_options.node,
"repmgrd_shutdown",
false,
errmsg.data);
terminate(ERR_DB_CON);
} }
upstream_conn = get_upstream_connection(my_local_conn, upstream_conn = get_upstream_connection(my_local_conn,
@@ -724,8 +780,21 @@ standby_monitor(void)
if (PQstatus(primary_conn) != CONNECTION_OK) if (PQstatus(primary_conn) != CONNECTION_OK)
{ {
log_err(_("Unable to reconnet to master after %i attempts, terminating...\n"), PQExpBufferData errmsg;
local_options.reconnect_attempts); initPQExpBuffer(&errmsg);
appendPQExpBuffer(&errmsg,
_("Unable to reconnect to master after %i attempts, terminating..."),
local_options.reconnect_attempts);
log_err("%s\n", errmsg.data);
create_event_record(my_local_conn,
&local_options,
local_options.node,
"repmgrd_shutdown",
false,
errmsg.data);
terminate(ERR_DB_CON); terminate(ERR_DB_CON);
} }
} }
@@ -755,8 +824,22 @@ standby_monitor(void)
if(!do_upstream_standby_failover(upstream_node)) if(!do_upstream_standby_failover(upstream_node))
{ {
log_err(_("unable to reconnect to new upstream node, terminating...\n")); PQExpBufferData errmsg;
terminate(1); initPQExpBuffer(&errmsg);
appendPQExpBuffer(&errmsg,
_("unable to reconnect to new upstream node, terminating...")
);
log_err("%s\n", errmsg.data);
create_event_record(my_local_conn,
&local_options,
local_options.node,
"repmgrd_shutdown",
false,
errmsg.data);
terminate(ERR_DB_CON);
} }
} }
return; return;
@@ -1993,8 +2076,21 @@ update_registration(void)
res = PQexec(primary_conn, sqlquery); res = PQexec(primary_conn, sqlquery);
if (PQresultStatus(res) != PGRES_COMMAND_OK) if (PQresultStatus(res) != PGRES_COMMAND_OK)
{ {
log_err(_("Cannot update registration: %s\n"), PQExpBufferData errmsg;
PQerrorMessage(primary_conn)); initPQExpBuffer(&errmsg);
appendPQExpBuffer(&errmsg,
_("unable to update registration: %s"),
PQerrorMessage(primary_conn));
log_err("%s\n", errmsg.data);
create_event_record(my_local_conn,
&local_options,
local_options.node,
"repmgrd_shutdown",
false,
errmsg.data);
terminate(ERR_DB_CON); terminate(ERR_DB_CON);
} }
PQclear(res); PQclear(res);
@@ -2150,7 +2246,23 @@ get_node_info(PGconn *conn, char *cluster, int node_id)
res = PQexec(my_local_conn, sqlquery); res = PQexec(my_local_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK) if (PQresultStatus(res) != PGRES_TUPLES_OK)
{ {
log_err(_("Unable to retrieve record for node %i: %s\n"), node_id, PQerrorMessage(conn)); PQExpBufferData errmsg;
initPQExpBuffer(&errmsg);
appendPQExpBuffer(&errmsg,
_("unable to retrieve record for node %i: %s"),
node_id,
PQerrorMessage(conn));
log_err("%s\n", errmsg.data);
create_event_record(my_local_conn,
&local_options,
local_options.node,
"repmgrd_shutdown",
false,
errmsg.data);
PQclear(res); PQclear(res);
terminate(ERR_DB_QUERY); terminate(ERR_DB_QUERY);
} }