summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f606589)
raw | patch | inline | side by side (parent: f606589)
author | Sebastian Harl <sh@teamix.net> | |
Thu, 7 Apr 2011 08:40:11 +0000 (10:40 +0200) | ||
committer | Sebastian Harl <sh@teamix.net> | |
Thu, 7 Apr 2011 09:13:29 +0000 (11:13 +0200) |
This is more flexible and the recommended way to connect to a PostgreSQL
database.
Also, the verbose output now includes detailed information about the
connection.
database.
Also, the verbose output now includes detailed information about the
connection.
plugins/check_pgsql.c | patch | blob | history |
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c
index 0f6eda479dee2f5c0862f853e0111310f9dc0b47..b2e1876f1801605a4fafc2d28d415c2c940f932b 100644 (file)
--- a/plugins/check_pgsql.c
+++ b/plugins/check_pgsql.c
#define DEFAULT_DB "template1"
#define DEFAULT_HOST "127.0.0.1"
+/* return the PSQL server version as a 3-tuple */
+#define PSQL_SERVER_VERSION3(server_version) \
+ (server_version) / 10000, \
+ (server_version) / 100 - (int)((server_version) / 10000) * 100, \
+ (server_version) - (int)((server_version) / 100) * 100
+/* return true if the given host is a UNIX domain socket */
+#define PSQL_IS_UNIX_DOMAIN_SOCKET(host) \
+ ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host)))
+/* return a 3-tuple identifying a host/port independent of the socket type */
+#define PSQL_SOCKET3(host, port) \
+ ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, \
+ PSQL_IS_UNIX_DOMAIN_SOCKET (host) ? "/.s.PGSQL." : ":", \
+ port
+
enum {
DEFAULT_PORT = 5432,
DEFAULT_WARN = 2,
main (int argc, char **argv)
{
PGconn *conn;
+ char *conninfo = NULL;
int elapsed_time;
int status = STATE_UNKNOWN;
}
alarm (timeout_interval);
- if (verbose)
- printf("Connecting to database:\n DB: %s\n User: %s\n Host: %s\n Port: %d\n", dbName,
- (pguser != NULL) ? pguser : "unspecified",
- (pghost != NULL) ? pghost : "unspecified",
- (pgport != NULL) ? atoi(pgport) : DEFAULT_PORT);
+ asprintf (&conninfo, "dbname = '%s'", dbName);
+ if (pghost)
+ asprintf (&conninfo, "%s host = '%s'", conninfo, pghost);
+ if (pgport)
+ asprintf (&conninfo, "%s port = '%s'", conninfo, pgport);
+ if (pgoptions)
+ asprintf (&conninfo, "%s options = '%s'", conninfo, pgoptions);
+ /* if (pgtty) -- ignored by PQconnectdb */
+ if (pguser)
+ asprintf (&conninfo, "%s user = '%s'", conninfo, pguser);
+
+ if (verbose) /* do not include password (see right below) in output */
+ printf ("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo,
+ pgpasswd ? " password = <hidden>" : "");
+
+ if (pgpasswd)
+ asprintf (&conninfo, "%s password = '%s'", conninfo, pgpasswd);
/* make a connection to the database */
time (&start_time);
- conn =
- PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd);
+ conn = PQconnectdb (conninfo);
time (&end_time);
elapsed_time = (int) (end_time - start_time);
+
if (verbose)
printf("Time elapsed: %d\n", elapsed_time);
else {
status = STATE_OK;
}
+
+ if (verbose) {
+ char *server_host = PQhost (conn);
+ int server_version = PQserverVersion (conn);
+
+ printf ("Successfully connected to database %s (user %s) "
+ "at server %s%s%s (server version: %d.%d.%d, "
+ "protocol version: %d, pid: %d)\n",
+ PQdb (conn), PQuser (conn),
+ PSQL_SOCKET3 (server_host, PQport (conn)),
+ PSQL_SERVER_VERSION3 (server_version),
+ PQprotocolVersion (conn), PQbackendPID (conn));
+ }
+
printf (_(" %s - database %s (%d sec.)|%s\n"),
state_text(status), dbName, elapsed_time,
fperfdata("time", elapsed_time, "s",