Le 24/12/2010 16:06, Guillaume Lelarge a écrit :
> Hi guys,
> 
> Here are some comments after trying this patch.
> 
> First, I splitted the patch in two patches, so to have one patch per
> functionality. You'll find the first one, syslog, attached.
> 
> Le 21/12/2010 15:11, Gilles Darold a écrit :
>> [...]
>> Syslog patch :
>>
>>     It adds two configuration directives : "logsyslog = true|false" and
>>     "logfacility = 'LOCAL0'"
>>
> 
> I would much prefer to have the same name than PostgreSQL options. No
> logsyslog, but a log_destination which accepts stderr and syslog. No
> logfacility, but syslog_facility.
> 
>>     If 'logsyslog' is enabled, messages are sent to syslog (through
>>     pool_error.c) just after the call of pool_get_config() in main.c.
>>
>>     Syslog ident it hard coded to "pgpool".
>>
> 
> It shouldn't be hardcoded. I would better have a syslog_ident parameter.
> 
>>     Connection to syslog is closed from pool_shmem_exit() in
>>     pool_shmem.c because this is the only method always called at exit.
>>     A better place should be in main.c but it has to be repeated so many
>>     time so that I choose this lazy place :-)
>>
> 
> Seems enough for me.
> 
>>     Samples configuration files has been patched too.
>>
> 
> Apart from the options name, and the missing option, I have a big issue
> with the FACILITY variable. Where do you initialize it? AFAICT, nowhere.
> 
> BTW, there is no reason to set only INIT_CONFIG for the facility
> parameter. I should be INIT_CONFIG|RELOAD_CONFIG.
> 
> So, all in all, I like this patch but it still needs some work. Gilles,
> could you work on the few issues I talked about? and send another patch
> (and this time only on syslog). If you can't, I'll do it.
> 

Sorry, forgot Gilles' missing patch. Here is v2 of the syslog patch.


-- 
Guillaume
 http://www.postgresql.fr
 http://dalibo.com
Index: main.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/main.c,v
retrieving revision 1.87
diff -c -p -r1.87 main.c
*** main.c	12 Nov 2010 05:47:01 -0000	1.87
--- main.c	24 Dec 2010 16:53:01 -0000
*************** int main(int argc, char **argv)
*** 314,319 ****
--- 314,328 ----
  	}
  
  	/*
+ 	 * Open syslog connection if required
+ 	 */
+ 	if (pool_config->logsyslog == 1) {
+ 		openlog(DEFAULT_SYSLOG_IDENT, LOG_PID|LOG_NDELAY|LOG_NOWAIT, pool_config->logfacility);
+ 		/* set a flag to allow pool_error.c to begin writing to syslog instead of stdout */
+ 		pool_config->logsyslog = 2;
+ 	}
+ 
+ 	/*
  	 * Locate pool_passwd
  	 */
  	if (strcmp("", pool_config->pool_passwd))
Index: pgpool.conf.sample
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pgpool.conf.sample,v
retrieving revision 1.40
diff -c -p -r1.40 pgpool.conf.sample
*** pgpool.conf.sample	30 Oct 2010 11:12:42 -0000	1.40
--- pgpool.conf.sample	24 Dec 2010 16:53:01 -0000
*************** authentication_timeout = 60
*** 56,61 ****
--- 56,67 ----
  # Logging directory
  logdir = '/tmp'
  
+ # Send messages to syslog. Disabled by default.
+ logsyslog = false
+ 
+ # Use syslog local facility for logging. Default: LOCAL0
+ logfacility = 'LOCAL0'
+ 
  # pid file name
  pid_file_name = '/var/run/pgpool/pgpool.pid'
  
Index: pgpool.conf.sample-master-slave
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pgpool.conf.sample-master-slave,v
retrieving revision 1.13
diff -c -p -r1.13 pgpool.conf.sample-master-slave
*** pgpool.conf.sample-master-slave	30 Oct 2010 11:12:43 -0000	1.13
--- pgpool.conf.sample-master-slave	24 Dec 2010 16:53:01 -0000
*************** authentication_timeout = 60
*** 56,61 ****
--- 56,67 ----
  # Logging directory
  logdir = '/tmp'
  
+ # Send messages to syslog. Disabled by default.
+ logsyslog = false
+ 
+ # Use syslog local facility for logging. Default: LOCAL0
+ logfacility = 'LOCAL0'
+ 
  # pid file name
  pid_file_name = '/var/run/pgpool/pgpool.pid'
  
Index: pgpool.conf.sample-replication
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pgpool.conf.sample-replication,v
retrieving revision 1.12
diff -c -p -r1.12 pgpool.conf.sample-replication
*** pgpool.conf.sample-replication	30 Oct 2010 11:12:43 -0000	1.12
--- pgpool.conf.sample-replication	24 Dec 2010 16:53:01 -0000
*************** authentication_timeout = 60
*** 56,61 ****
--- 56,67 ----
  # Logging directory
  logdir = '/tmp'
  
+ # Send messages to syslog. Disabled by default.
+ logsyslog = false
+ 
+ # Use syslog local facility for logging. Default: LOCAL0
+ logfacility = 'LOCAL0'
+ 
  # pid file name
  pid_file_name = '/var/run/pgpool/pgpool.pid'
  
Index: pgpool.conf.sample-stream
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pgpool.conf.sample-stream,v
retrieving revision 1.6
diff -c -p -r1.6 pgpool.conf.sample-stream
*** pgpool.conf.sample-stream	30 Oct 2010 11:12:43 -0000	1.6
--- pgpool.conf.sample-stream	24 Dec 2010 16:53:01 -0000
*************** authentication_timeout = 60
*** 56,61 ****
--- 56,67 ----
  # Logging directory
  logdir = '/tmp'
  
+ # Send messages to syslog. Disabled by default.
+ logsyslog = false
+ 
+ # Use syslog local facility for logging. Default: LOCAL0
+ logfacility = 'LOCAL0'
+ 
  # pid file name
  pid_file_name = '/var/run/pgpool/pgpool.pid'
  
Index: pool.h
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool.h,v
retrieving revision 1.86
diff -c -p -r1.86 pool.h
*** pool.h	12 Nov 2010 08:09:32 -0000	1.86
--- pool.h	24 Dec 2010 16:53:02 -0000
***************
*** 43,48 ****
--- 43,51 ----
  #include <openssl/err.h>
  #endif
  
+ /* Add for syslog logging */
+ #include <syslog.h>
+ 
  /* undef this if you have problems with non blocking accept() */
  #define NONE_BLOCK
  
***************
*** 69,74 ****
--- 72,80 ----
  /* status file name */
  #define STATUS_FILE_NAME "pgpool_status"
  
+ /* default string used to identify pgpool on syslog output */
+ #define DEFAULT_SYSLOG_IDENT "pgpool"
+ 
  typedef enum {
  	POOL_CONTINUE = 0,
  	POOL_IDLE,
Index: pool_config.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_config.c,v
retrieving revision 1.52
diff -c -p -r1.52 pool_config.c
*** pool_config.c	17 Aug 2010 02:09:00 -0000	1.52
--- pool_config.c	24 Dec 2010 16:53:03 -0000
*************** int pool_init_config(void)
*** 1871,1876 ****
--- 1871,1878 ----
  	pool_config->child_max_connections = 0;
  	pool_config->authentication_timeout = 60;
  	pool_config->logdir = DEFAULT_LOGDIR;
+     pool_config->logsyslog = 0;
+     pool_config->logfacility = LOG_LOCAL0;
  	pool_config->pid_file_name = DEFAULT_PID_FILE_NAME;
   	pool_config->log_statement = 0;
   	pool_config->log_per_node_statement = 0;
*************** int pool_get_config(char *confpath, POOL
*** 2208,2213 ****
--- 2210,2246 ----
  			}
  			pool_config->logdir = str;
  		}
+         else if (!strcmp(key, "logsyslog") &&
+                  CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
+         {
+             int v = eval_logical(yytext);
+      
+             if (v < 0)
+             {
+                 pool_error("pool_config: invalid value %s for %s", yytext, key);
+                 fclose(fd);
+                 return(-1);
+             }
+             pool_config->logsyslog = v;
+         }
+         else if (!strcmp(key, "logfacility") && CHECK_CONTEXT(INIT_CONFIG, context))
+         {
+             char *str;
+             
+             if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
+             {
+                 PARSE_ERROR();
+                 fclose(fd);
+                 return(-1);
+             }
+             str = extract_string(yytext, token);
+             if (str == NULL)
+             {
+                 fclose(fd);
+                 return(-1);
+             }
+             pool_config->logfacility = set_syslog_facility(str);
+         }
  		else if (!strcmp(key, "pid_file_name") && CHECK_CONTEXT(INIT_CONFIG, context))
  		{
  			char *str;
*************** static void print_host_entry(int slot)
*** 3539,3542 ****
--- 3572,3614 ----
  }
  #endif
  
+ /* Use to set the syslog facility level if logsyslog is activated */
+ int set_syslog_facility(char *value)
+ {
+    int facility = LOG_LOCAL0;
+ 
+    if (value == NULL)
+      return facility;
+ 
+    if (strncmp(value, "LOCAL", 5) == 0 && strlen(value) == 6) {
+           switch (value[5]) {
+           case '0':
+                facility = LOG_LOCAL0;
+                break;
+           case '1':
+                facility = LOG_LOCAL1;
+                break;
+           case '2':
+                facility = LOG_LOCAL2;
+                break;
+           case '3':
+                facility = LOG_LOCAL3;
+                break;
+           case '4':
+                facility = LOG_LOCAL4;
+                break;
+           case '5':
+                facility = LOG_LOCAL5;
+                break;
+           case '6':
+                facility = LOG_LOCAL6;
+                break;
+           case '7':
+                facility = LOG_LOCAL7;
+                break;
+           }
+      }
+      return facility;
+ }
+ 
  
Index: pool_config.h
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_config.h,v
retrieving revision 1.8
diff -c -p -r1.8 pool_config.h
*** pool_config.h	1 Aug 2010 08:38:17 -0000	1.8
--- pool_config.h	24 Dec 2010 16:53:03 -0000
*************** typedef struct {
*** 51,56 ****
--- 51,58 ----
  	int authentication_timeout; /* maximum time in seconds to complete client authentication */
      int	max_pool;	/* max # of connection pool per child */
      char *logdir;		/* logging directory */
+     int logsyslog;		/* 0:false, 1:true - logs goes to syslog */
+     int logfacility;		/* syslog facility: LOCAL0, LOCAL1, ... */
      char *pid_file_name;		/* pid file name */
      char *backend_socket_dir;	/* Unix domain socket directory for the PostgreSQL server */
  	int replication_mode;		/* replication mode */
*************** extern int pool_init_config(void);
*** 162,165 ****
--- 164,170 ----
  extern int pool_get_config(char *confpath, POOL_CONFIG_CONTEXT context);
  extern int eval_logical(char *str);
  
+ /* function used for syslog support */
+ extern int set_syslog_facility (char *);
+ 
  #endif /* POOL_CONFIG_H */
Index: pool_config.l
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_config.l,v
retrieving revision 1.48
diff -c -p -r1.48 pool_config.l
*** pool_config.l	17 Aug 2010 02:09:00 -0000	1.48
--- pool_config.l	24 Dec 2010 16:53:03 -0000
*************** int pool_init_config(void)
*** 146,151 ****
--- 146,153 ----
  	pool_config->child_max_connections = 0;
  	pool_config->authentication_timeout = 60;
  	pool_config->logdir = DEFAULT_LOGDIR;
+ 	pool_config->logsyslog = 0;
+ 	pool_config->logfacility = LOG_LOCAL0;
  	pool_config->pid_file_name = DEFAULT_PID_FILE_NAME;
   	pool_config->log_statement = 0;
   	pool_config->log_per_node_statement = 0;
*************** int pool_get_config(char *confpath, POOL
*** 483,488 ****
--- 485,521 ----
  			}
  			pool_config->logdir = str;
  		}
+ 		else if (!strcmp(key, "logsyslog") &&
+ 				 CHECK_CONTEXT(INIT_CONFIG|RELOAD_CONFIG, context))
+ 		{
+ 			int v = eval_logical(yytext);
+ 
+ 			if (v < 0)
+ 			{
+ 				pool_error("pool_config: invalid value %s for %s", yytext, key);
+ 				fclose(fd);
+ 				return(-1);
+ 			}
+ 			pool_config->logsyslog = v;
+ 		}
+ 		else if (!strcmp(key, "logfacility") && CHECK_CONTEXT(INIT_CONFIG, context))
+ 		{
+ 			char *str;
+ 
+ 			if (token != POOL_STRING && token != POOL_UNQUOTED_STRING && token != POOL_KEY)
+ 			{
+ 				PARSE_ERROR();
+ 				fclose(fd);
+ 				return(-1);
+ 			}
+ 			str = extract_string(yytext, token);
+ 			if (str == NULL)
+ 			{
+ 				fclose(fd);
+ 				return(-1);
+ 			}
+ 			pool_config->logfacility = set_syslog_facility(str);
+ 		}
  		else if (!strcmp(key, "pid_file_name") && CHECK_CONTEXT(INIT_CONFIG, context))
  		{
  			char *str;
*************** static void print_host_entry(int slot)
*** 1814,1816 ****
--- 1847,1888 ----
  }
  #endif
  
+ /* Use to set the syslog facility level if logsyslog is activated */
+ int set_syslog_facility(char *value)
+ {
+ 	int facility = LOG_LOCAL0;
+ 
+ 	if (value == NULL)
+ 	  return facility;
+ 
+ 	if (strncmp(value, "LOCAL", 5) == 0 && strlen(value) == 6) {
+           switch (value[5]) {
+           case '0':
+                facility = LOG_LOCAL0;
+                break;
+           case '1':
+                facility = LOG_LOCAL1;
+                break;
+           case '2':
+                facility = LOG_LOCAL2;
+                break;
+           case '3':
+                facility = LOG_LOCAL3;
+                break;
+           case '4':
+                facility = LOG_LOCAL4;
+                break;
+           case '5':
+                facility = LOG_LOCAL5;
+                break;
+           case '6':
+                facility = LOG_LOCAL6;
+                break;
+           case '7':
+                facility = LOG_LOCAL7;
+                break;
+           }
+      }
+      return facility;
+ }
+ 
Index: pool_error.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_error.c,v
retrieving revision 1.8
diff -c -p -r1.8 pool_error.c
*** pool_error.c	10 Aug 2010 15:08:32 -0000	1.8
--- pool_error.c	24 Dec 2010 16:53:03 -0000
***************
*** 34,39 ****
--- 34,40 ----
  #define MAXSTRFTIME 128
  
  extern int debug;
+ extern int FACILITY;
  
  static char *nowsec(void);
  
*************** void pool_error(const char *fmt,...)
*** 50,55 ****
--- 51,63 ----
  #else
  	int	oldmask;
  #endif
+ 	/* Write error message to syslog */
+ 	if (pool_config->logsyslog > 1) {
+ 	   va_start(ap, fmt);
+ 	   vsyslog(LOG_MAKEPRI(FACILITY, LOG_ERR), fmt, ap);
+ 	   va_end(ap);
+ 	   return;
+ 	}
  
  	POOL_SETMASK2(&BlockSig, &oldmask);
  
*************** void pool_debug(const char *fmt,...)
*** 106,111 ****
--- 114,127 ----
  			return;
  	}
  
+ 	/* Write debug message to syslog */
+ 	if (pool_config->logsyslog > 1) {
+ 	   va_start(ap, fmt);
+ 	   vsyslog(LOG_MAKEPRI(FACILITY, LOG_DEBUG), fmt, ap);
+ 	   va_end(ap);
+ 	   return;
+ 	}
+ 
  	POOL_SETMASK2(&BlockSig, &oldmask);
  
  	if (pool_config->print_timestamp)
*************** void pool_log(const char *fmt,...)
*** 150,155 ****
--- 166,179 ----
  	int	oldmask;
  #endif
  
+ 	/* Write log message to syslog */
+ 	if (pool_config->logsyslog > 1) {
+ 	   va_start(ap, fmt);
+ 	   vsyslog(LOG_MAKEPRI(FACILITY, LOG_NOTICE), fmt, ap);
+ 	   va_end(ap);
+ 	   return;
+ 	}
+ 
  	POOL_SETMASK2(&BlockSig, &oldmask);
  
  	if (pool_config->print_timestamp)
Index: pool_globals.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_globals.c,v
retrieving revision 1.1
diff -c -p -r1.1 pool_globals.c
*** pool_globals.c	27 Jun 2010 13:02:57 -0000	1.1
--- pool_globals.c	24 Dec 2010 16:53:03 -0000
***************
*** 25,27 ****
--- 25,28 ----
  int debug = 0;	/* non 0 if debug option is given (-d). pcp only */
  pid_t mypid;	/* pgpool parent process id */
  bool run_as_pcp_child;
+ int FACILITY;
Index: pool_process_reporting.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_process_reporting.c,v
retrieving revision 1.16
diff -c -p -r1.16 pool_process_reporting.c
*** pool_process_reporting.c	28 Nov 2010 14:57:17 -0000	1.16
--- pool_process_reporting.c	24 Dec 2010 16:53:04 -0000
*************** void config_reporting(POOL_CONNECTION *f
*** 176,181 ****
--- 176,194 ----
  	strncpy(status[i].desc, "logging directory", POOLCONFIG_MAXDESCLEN);
  	i++;
  
+         strncpy(status[i].desc, "logging directory", POOLCONFIG_MAXDESCLEN);
+         i++;
+ 
+         strncpy(status[i].name, "logsyslog", POOLCONFIG_MAXNAMELEN);
+         snprintf(status[i].value, POOLCONFIG_MAXVALLEN, "%d", (pool_config->logsyslog > 1) ? pool_config->logsyslog -1 : 0);
+         strncpy(status[i].desc, "non 0 if logging to syslog", POOLCONFIG_MAXDESCLEN);
+         i++;
+ 
+         strncpy(status[i].name, "logfacility", POOLCONFIG_MAXNAMELEN);
+         snprintf(status[i].value, POOLCONFIG_MAXVALLEN, "LOCAL%d", (pool_config->logfacility/8) - 16);
+         strncpy(status[i].desc, "log facility when logsyslog is enabled", POOLCONFIG_MAXDESCLEN);
+         i++;
+ 
  	strncpy(status[i].name, "pid_file_name", POOLCONFIG_MAXNAMELEN);
  	snprintf(status[i].value, POOLCONFIG_MAXVALLEN, "%s", pool_config->pid_file_name);
  	strncpy(status[i].desc, "path to pid file", POOLCONFIG_MAXDESCLEN);
Index: pool_shmem.c
===================================================================
RCS file: /cvsroot/pgpool/pgpool-II/pool_shmem.c,v
retrieving revision 1.5
diff -c -p -r1.5 pool_shmem.c
*** pool_shmem.c	22 Aug 2009 04:04:21 -0000	1.5
--- pool_shmem.c	24 Dec 2010 16:53:04 -0000
*************** void
*** 127,132 ****
--- 127,134 ----
  pool_shmem_exit(int code)
  {
  	shmem_exit(code);
+ 	/* Close syslog connection here as this function is always called on exit */
+ 	closelog();
  }
  
  /*
_______________________________________________
Pgpool-hackers mailing list
[email protected]
http://pgfoundry.org/mailman/listinfo/pgpool-hackers

Reply via email to