summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Paquier2012-08-21 00:24:19 +0000
committerMichael Paquier2012-08-21 00:24:19 +0000
commit67e59f579f549d18b6a661bd3c77f1bbbd690619 (patch)
treed4fd783382a0da7aac043cbfcebdad2a897dcc39
parent0c99b9b71eff391af867977dc0913e55b2020f37 (diff)
Add options -U (username) and -d (database) in pgxc_monitor
This allows to monitor a node with a simple command using a wanted user name and/or database for a Coordinator and a Datanode. In case a password is necessary, it is necessary to provide it through .pgpass. Feature design and patch by Nikhil Sontakke. I just reviewed and corrected a couple of things.
-rw-r--r--contrib/pgxc_monitor/pgxc_monitor.c79
-rw-r--r--doc-xc/src/sgml/pgxcmonitor.sgmlin28
2 files changed, 85 insertions, 22 deletions
diff --git a/contrib/pgxc_monitor/pgxc_monitor.c b/contrib/pgxc_monitor/pgxc_monitor.c
index 5db91b7eaa..e1aafae5f2 100644
--- a/contrib/pgxc_monitor/pgxc_monitor.c
+++ b/contrib/pgxc_monitor/pgxc_monitor.c
@@ -7,7 +7,7 @@
*
* Command syntax:
*
- * pgxc_monitor -Z nodetype -p port -h host
+ * pgxc_monitor [ options ]
*
* Options are:
* -Z nodetype What node type to monitor, gtm or node.
@@ -18,6 +18,8 @@
* -n nodename Specifies pgxc_monitor node name. Default is "pgxc_monitor"
* -q Run in quiet mode. Default is quiet mode.
* -v Run in verbose mode.
+ * -d database Database name to connect to.
+ * -U username Connect as specified database user.
* --help Prints the help message and exits with 0.
*
* When monitoring Coordinator or Datanode, -p and -h options can be
@@ -32,6 +34,9 @@
* When testing Coordinator/Datanode, you must setup .pgpass file if you
* need to supply password, as well as non-default database name and username.
*
+ * The username and database name can be specified via command line options
+ * too. If password is needed, it must be supplied via .pgpass file though.
+ *
* If invalid parameters are given, error message will be printed even if
* -q is specified.
*
@@ -59,7 +64,7 @@ static char *progname;
static void usage(void);
static int do_gtm_ping(char *host, char *node, nodetype_t nodetype, char *nodename, bool verbose);
-static int do_node_ping(char *host, char *node, bool verbose);
+static int do_node_ping(char *host, char *node, char *username, char *database, bool verbose);
int
main(int ac, char *av[])
@@ -70,6 +75,8 @@ main(int ac, char *av[])
char *host = NULL;
char *nodename = NULL;
bool verbose = false;
+ char *username = NULL;
+ char *database = NULL;
progname = strdup(av[0]);
@@ -84,7 +91,7 @@ main(int ac, char *av[])
}
/* Scan options */
- while ((opt = getopt(ac, av, "Z:h:n:p:qv")) != -1)
+ while ((opt = getopt(ac, av, "Z:U:d:h:n:p:qv")) != -1)
{
switch(opt)
{
@@ -117,6 +124,12 @@ main(int ac, char *av[])
case 'v':
verbose = true;
break;
+ case 'U':
+ username = strdup(optarg);
+ break;
+ case 'd':
+ database = strdup(optarg);
+ break;
default:
fprintf(stderr, "%s: unknow option %c.\n", progname, opt);
exit(3);
@@ -136,7 +149,7 @@ main(int ac, char *av[])
case GTM:
exit(do_gtm_ping(host, port, nodetype, nodename, verbose));
case NODE:
- exit(do_node_ping(host, port, verbose));
+ exit(do_node_ping(host, port, username, database, verbose));
case NONE:
default:
break;
@@ -150,7 +163,8 @@ main(int ac, char *av[])
/*
* Ping a given GTM or GTM-proxy
*/
-static int do_gtm_ping(char *host, char* port, nodetype_t nodetype, char *nodename, bool verbose)
+static int
+do_gtm_ping(char *host, char* port, nodetype_t nodetype, char *nodename, bool verbose)
{
char connect_str[256];
GTM_Conn *conn;
@@ -170,7 +184,7 @@ static int do_gtm_ping(char *host, char* port, nodetype_t nodetype, char *nodena
if ((conn = PQconnectGTM(connect_str)) == NULL)
{
if (verbose)
- fprintf(stderr, "%s: Could not connect to %s\n", progname, nodetype == GTM ? "GTM" : "GTM_Proxy");
+ fprintf(stderr, "%s: Could not connect to %s\n", progname, "GTM");
exit(1);
}
GTMPQfinish(conn);
@@ -182,12 +196,13 @@ static int do_gtm_ping(char *host, char* port, nodetype_t nodetype, char *nodena
/*
* Ping a given node
*/
-static int do_node_ping(char *host, char *port, bool verbose)
+static int
+do_node_ping(char *host, char *port, char *username, char *database, bool verbose)
{
int rc;
int exitStatus;
- char command_line[512];
- char *quiet_out = "> /dev/null 2> /dev/null";
+ char command_line[1024];
+ char *quiet_out = " > /dev/null 2> /dev/null";
char *verbose_out = "";
char *out = verbose ? verbose_out : quiet_out;
@@ -195,14 +210,34 @@ static int do_node_ping(char *host, char *port, bool verbose)
sprintf(command_line, "psql -w -q -c \"select 1 a\"");
/* Then add options if necessary */
+ if (username)
+ {
+ strcat(command_line, " -U ");
+ strcat(command_line, username);
+ }
+
+ /* Add database name, default is "postgres" */
+ if (database)
+ {
+ strcat(command_line, " -d ");
+ strcat(command_line, database);
+ }
+ else
+ strcat(command_line, " -d postgres ");
+
if (host)
- sprintf(command_line, "%s -h %s", command_line, host);
+ {
+ strcat(command_line, " -h ");
+ strcat(command_line, host);
+ }
+
if (port)
- sprintf(command_line, "%s -p %s", command_line, port);
+ {
+ strcat(command_line, " -p ");
+ strcat(command_line, port);
+ }
- /* Add database name, here default database, postgres, is used */
- sprintf(command_line, "%s postgres", command_line);
- sprintf(command_line, "%s %s", command_line, out);
+ strcat(command_line, out);
/* Launch the command and output result if necessary */
rc = system(command_line);
@@ -221,18 +256,22 @@ static int do_node_ping(char *host, char *port, bool verbose)
/*
* Show help information
*/
-static void usage(void)
+static void
+usage(void)
{
printf("pgxc_monitor -Z nodetype -p port -h host\n\n");
printf("Options are:\n");
- printf(" -Z nodetype What node type to monitor, gtm, gtm_proxy,\n");
- printf(" coordinator, or datanode.\n");
+ printf(" -Z nodetype What node type to monitor, GTM, GTM-Proxy,\n");
+ printf(" Coordinator, or Datanode.\n");
+ printf(" Use \"gtm\" for GTM and GTM-proxy, \"node\" for Coordinator and Datanode.\n");
printf(" -h host Host name or IP address of the monitored node.\n");
- printf(" Mandatory for -Z gtm or -Z gtm_proxy\n");
+ printf(" Mandatory for -Z gtm\n");
printf(" -n nodename Nodename of this pgxc_monitor.\n");
- printf(" Only for -Z gtm or -Z gtm_proxy. Default is pgxc_monitor\n");
+ printf(" Only for -Z gtm. Default is pgxc_monitor\n");
printf(" This identifies what is the name of component connecting to GTM.\n");
- printf(" -p port Port number of the monitored node. Mandatory for -Z gtm or -Z gtm_proxy\n");
+ printf(" -p port Port number of the monitored node. Mandatory for -Z gtm\n");
+ printf(" -d database Database name to connect to. Default is \"postgres\". \n");
+ printf(" -U username Connect as specified database user. \n");
printf(" -q Quiet mode.\n");
printf(" -v Verbose mode.\n");
printf(" --help Prints the help message and exits with 0.\n");
diff --git a/doc-xc/src/sgml/pgxcmonitor.sgmlin b/doc-xc/src/sgml/pgxcmonitor.sgmlin
index eac36d4d8e..f798f2c15b 100644
--- a/doc-xc/src/sgml/pgxcmonitor.sgmlin
+++ b/doc-xc/src/sgml/pgxcmonitor.sgmlin
@@ -47,8 +47,9 @@ pgxc_monitor <optional> <replaceable>option</> </optional>
<term><option>-Z <replaceable class="parameter">nodetype</replaceable></option></term>
<listitem>
<para>
- Type of node type to test. Specify gtm as <replaceable>nodetype</replaceable> for gtm and gtm_proxy.
- Specify node as <replaceable>nodeopt</replaceable> for a Coordinator or a Datanode.
+ Type of node type to test. Specify <literal>gtm</> as <replaceable>nodetype</replaceable>
+ for gtm and gtm_proxy and <literal>node</> as <replaceable>nodetype</replaceable> for a
+ Coordinator or a Datanode.
</para>
</listitem>
</varlistentry>
@@ -82,6 +83,24 @@ pgxc_monitor <optional> <replaceable>option</> </optional>
</varlistentry>
<varlistentry>
+ <term><option>-U <replaceable class="parameter">username</replaceable></></term>
+ <listitem>
+ <para>
+ Specifies the database username to connect as.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><option>-d <replaceable class="parameter">database</replaceable></></term>
+ <listitem>
+ <para>
+ Specifies the database name to connect to. Default is "postgres".
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><option>-q</></term>
<listitem>
<para>
@@ -133,6 +152,11 @@ pgxc_monitor <optional> <replaceable>option</> </optional>
Also, because this uses psql command, psql must be in your PATH.
</para>
<para>
+ The username and database name can be specified via command line
+ options too. If password is needed, it must be supplied via
+ <option>.pgpass</option> file though.
+ </para>
+ <para>
If invalid parameters are given,
error message will be printed even if <option>-q</option> is specified.
</para>