]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
Handle postgresql schemas correctly
authorBob Hockney <zeus@ix.netcom.com>
Wed, 19 Dec 2012 19:38:57 +0000 (12:38 -0700)
committerEric Leblond <eric@regit.org>
Wed, 19 Dec 2012 22:47:57 +0000 (23:47 +0100)
Add 'schema' variable to look into corresponding schema.

output/pgsql/ulogd_output_PGSQL.c
ulogd.conf.in

index cd793ca3314cc0c464bf7c9090d3b3a13558b904..f246153d5c6c17385e6ceaa8e6912f92685ab17d 100644 (file)
@@ -105,9 +105,16 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi)
        }
 
        if (PQresultStatus(pi->pgres) == PGRES_TUPLES_OK) {
-               ulogd_log(ULOGD_DEBUG, "using schema %s\n",
-                         schema_ce(upi->config_kset).u.string);
-               pi->db_inst.schema = schema_ce(upi->config_kset).u.string;
+               if (PQntuples(pi->pgres)) {
+                       ulogd_log(ULOGD_DEBUG, "using schema %s\n",
+                                 schema_ce(upi->config_kset).u.string);
+                       pi->db_inst.schema = schema_ce(upi->config_kset).u.string;
+               } else {
+                       ulogd_log(ULOGD_ERROR, "schema %s not found: %s\n",
+                                schema_ce(upi->config_kset).u.string, PQerrorMessage(pi->dbh));
+                       PQclear(pi->pgres);
+                       return -1;
+               }
        } else {
                pi->db_inst.schema = NULL;
        }
@@ -223,6 +230,8 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi)
        char *user = user_ce(upi->config_kset).u.string;
        char *pass = pass_ce(upi->config_kset).u.string;
        char *db = db_ce(upi->config_kset).u.string;
+       char *schema = NULL;
+       char pgbuf[128];
 
        /* 80 is more than what we need for the fixed parts below */
        len = 80 + strlen(user) + strlen(db);
@@ -270,11 +279,29 @@ static int open_db_pgsql(struct ulogd_pluginstance *upi)
        }
 
        if (pgsql_namespace(upi)) {
-               ulogd_log(ULOGD_ERROR, "unable to test for pgsql schemas\n");
+               ulogd_log(ULOGD_ERROR, "problem testing for pgsql schemas\n");
                close_db_pgsql(upi);
                return -1;
        }
 
+       pi=(struct pgsql_instance *)upi->private;
+       schema = pi->db_inst.schema;
+
+       if (!(schema == NULL) && (strcmp(schema,"public"))) {
+               snprintf(pgbuf, 128, "SET search_path='%.63s', \"$user\", 'public'", schema);
+               pi->pgres = PQexec(pi->dbh, pgbuf);
+               if ((PQresultStatus(pi->pgres) == PGRES_COMMAND_OK)) {
+                       PQclear(pi->pgres);
+               } else {
+                       ulogd_log(ULOGD_ERROR, "could not set search path to (%s): %s\n",
+                                schema, PQerrorMessage(pi->dbh));
+                       PQclear(pi->pgres);
+                       close_db_pgsql(upi);
+                       return -1;
+               }
+
+       }
+
        return 0;
 }
 
index 3bd464b25054aa20791677be8e61e3da5fcc5725..c630b887a8765e0d6ca61150c8bedf9fa1f11590 100644 (file)
@@ -213,6 +213,7 @@ db="nulog"
 host="localhost"
 user="nupik"
 table="ulog"
+#schema="public"
 pass="changeme"
 procedure="INSERT_PACKET_FULL"
 
@@ -221,6 +222,7 @@ db="nulog"
 host="localhost"
 user="nupik"
 table="ulog2_ct"
+#schema="public"
 pass="changeme"
 procedure="INSERT_CT"
 
@@ -229,6 +231,7 @@ db="nulog"
 host="localhost"
 user="nupik"
 table="ulog2_ct"
+#schema="public"
 pass="changeme"
 procedure="INSERT_OR_REPLACE_CT"
 
@@ -237,6 +240,7 @@ db="nulog"
 host="localhost"
 user="nupik"
 table="nfacct"
+#schema="public"
 pass="changeme"
 procedure="INSERT_NFACCT"