summaryrefslogtreecommitdiffstats
path: root/plugins/check_pgsql.c
diff options
context:
space:
mode:
authorSebastian Harl <sh@teamix.net>2011-04-07 08:40:11 (GMT)
committerSebastian Harl <sh@teamix.net>2012-07-05 09:36:57 (GMT)
commitf3e2ebd974c2b0c07321663fc7255c7ace916ba5 (patch)
treeb6ed2872ce4d003bf7728242568ad7dc87eb32e9 /plugins/check_pgsql.c
parent58ef38e2bb9503f4fbcca5fb8b17ccaf47f9ed67 (diff)
downloadmonitoring-plugins-f3e2ebd974c2b0c07321663fc7255c7ace916ba5.tar.gz
check_pgsql: Use PQconnectdb() rather than PQsetdbLogin().
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.
Diffstat (limited to 'plugins/check_pgsql.c')
-rw-r--r--plugins/check_pgsql.c55
1 files changed, 48 insertions, 7 deletions
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c
index 2acddc4..cf526a6 100644
--- a/plugins/check_pgsql.c
+++ b/plugins/check_pgsql.c
@@ -42,6 +42,20 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
42#define DEFAULT_DB "template1" 42#define DEFAULT_DB "template1"
43#define DEFAULT_HOST "127.0.0.1" 43#define DEFAULT_HOST "127.0.0.1"
44 44
45/* return the PSQL server version as a 3-tuple */
46#define PSQL_SERVER_VERSION3(server_version) \
47 (server_version) / 10000, \
48 (server_version) / 100 - (int)((server_version) / 10000) * 100, \
49 (server_version) - (int)((server_version) / 100) * 100
50/* return true if the given host is a UNIX domain socket */
51#define PSQL_IS_UNIX_DOMAIN_SOCKET(host) \
52 ((NULL == (host)) || ('\0' == *(host)) || ('/' == *(host)))
53/* return a 3-tuple identifying a host/port independent of the socket type */
54#define PSQL_SOCKET3(host, port) \
55 ((NULL == (host)) || ('\0' == *(host))) ? DEFAULT_PGSOCKET_DIR : host, \
56 PSQL_IS_UNIX_DOMAIN_SOCKET (host) ? "/.s.PGSQL." : ":", \
57 port
58
45enum { 59enum {
46 DEFAULT_PORT = 5432, 60 DEFAULT_PORT = 5432,
47 DEFAULT_WARN = 2, 61 DEFAULT_WARN = 2,
@@ -133,6 +147,7 @@ int
133main (int argc, char **argv) 147main (int argc, char **argv)
134{ 148{
135 PGconn *conn; 149 PGconn *conn;
150 char *conninfo = NULL;
136 151
137 int elapsed_time; 152 int elapsed_time;
138 int status = STATE_UNKNOWN; 153 int status = STATE_UNKNOWN;
@@ -164,18 +179,30 @@ main (int argc, char **argv)
164 } 179 }
165 alarm (timeout_interval); 180 alarm (timeout_interval);
166 181
167 if (verbose) 182 asprintf (&conninfo, "dbname = '%s'", dbName);
168 printf("Connecting to database:\n DB: %s\n User: %s\n Host: %s\n Port: %d\n", dbName, 183 if (pghost)
169 (pguser != NULL) ? pguser : "unspecified", 184 asprintf (&conninfo, "%s host = '%s'", conninfo, pghost);
170 (pghost != NULL) ? pghost : "unspecified", 185 if (pgport)
171 (pgport != NULL) ? atoi(pgport) : DEFAULT_PORT); 186 asprintf (&conninfo, "%s port = '%s'", conninfo, pgport);
187 if (pgoptions)
188 asprintf (&conninfo, "%s options = '%s'", conninfo, pgoptions);
189 /* if (pgtty) -- ignored by PQconnectdb */
190 if (pguser)
191 asprintf (&conninfo, "%s user = '%s'", conninfo, pguser);
192
193 if (verbose) /* do not include password (see right below) in output */
194 printf ("Connecting to PostgreSQL using conninfo: %s%s\n", conninfo,
195 pgpasswd ? " password = <hidden>" : "");
196
197 if (pgpasswd)
198 asprintf (&conninfo, "%s password = '%s'", conninfo, pgpasswd);
172 199
173 /* make a connection to the database */ 200 /* make a connection to the database */
174 time (&start_time); 201 time (&start_time);
175 conn = 202 conn = PQconnectdb (conninfo);
176 PQsetdbLogin (pghost, pgport, pgoptions, pgtty, dbName, pguser, pgpasswd);
177 time (&end_time); 203 time (&end_time);
178 elapsed_time = (int) (end_time - start_time); 204 elapsed_time = (int) (end_time - start_time);
205
179 if (verbose) 206 if (verbose)
180 printf("Time elapsed: %d\n", elapsed_time); 207 printf("Time elapsed: %d\n", elapsed_time);
181 208
@@ -197,6 +224,20 @@ main (int argc, char **argv)
197 else { 224 else {
198 status = STATE_OK; 225 status = STATE_OK;
199 } 226 }
227
228 if (verbose) {
229 char *server_host = PQhost (conn);
230 int server_version = PQserverVersion (conn);
231
232 printf ("Successfully connected to database %s (user %s) "
233 "at server %s%s%s (server version: %d.%d.%d, "
234 "protocol version: %d, pid: %d)\n",
235 PQdb (conn), PQuser (conn),
236 PSQL_SOCKET3 (server_host, PQport (conn)),
237 PSQL_SERVER_VERSION3 (server_version),
238 PQprotocolVersion (conn), PQbackendPID (conn));
239 }
240
200 printf (_(" %s - database %s (%d sec.)|%s\n"), 241 printf (_(" %s - database %s (%d sec.)|%s\n"),
201 state_text(status), dbName, elapsed_time, 242 state_text(status), dbName, elapsed_time,
202 fperfdata("time", elapsed_time, "s", 243 fperfdata("time", elapsed_time, "s",