[icinga-checkins] icinga.org: icinga-core/mfriedrich/core: added lock checking on writing to cgi.log #1161

git at icinga.org git at icinga.org
Sat Apr 30 13:14:29 CEST 2011


Module: icinga-core
Branch: mfriedrich/core
Commit: 35a5d6a726de5480584295865d2b2a670229ba8f
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=35a5d6a726de5480584295865d2b2a670229ba8f

Author: Ricardo Bartels <ricardo at bitchbrothers.com>
Date:   Thu Apr 28 23:49:14 2011 +0200

added lock checking on writing to cgi.log #1161

* also added remote address to every log entry

refs: #1161

---

 cgi/cgiutils.c |   10 ++++++++--
 cgi/cmd.c      |    6 ++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/cgi/cgiutils.c b/cgi/cgiutils.c
index e40f729..f3323f7 100644
--- a/cgi/cgiutils.c
+++ b/cgi/cgiutils.c
@@ -2352,6 +2352,7 @@ char *get_export_csv_link(char *cgi) {
 int write_to_cgi_log(char *buffer) {
 	FILE *fp;
 	time_t log_time;
+	int write_retries=10, i=0;
 
 	/* we don't do anything if logging is deactivated or no logfile configured */
 	if(use_logging==FALSE || !strcmp(cgi_log_file,""))
@@ -2362,8 +2363,13 @@ int write_to_cgi_log(char *buffer) {
 	// allways check if log file has to be rotated
 	rotate_log_file();
 
-	fp=fopen(cgi_log_file,"a+");
-	if(fp==NULL)
+	// open log file and try again if failed
+	while((fp=fopen(cgi_log_file,"a+"))==NULL && i<write_retries) {
+		usleep(10);
+		i++;
+	}
+
+	if(i>=write_retries)
 		return ERROR;
 
 	/* strip any newlines from the end of the buffer */
diff --git a/cgi/cmd.c b/cgi/cmd.c
index 13856d6..3396f7e 100644
--- a/cgi/cmd.c
+++ b/cgi/cmd.c
@@ -2845,8 +2845,11 @@ int write_command_to_file(char *cmd){
 		return ERROR;
 	}
 
+	// get remote address
+	sprintf(ip_address,"%s",getenv("REMOTE_ADDR"));
+
 	/* write command to cgi log */
-	sprintf(buffer, "EXTERNAL COMMAND: %s;", current_authdata.username);
+	sprintf(buffer, "EXTERNAL COMMAND: %s;%s;", current_authdata.username,(ip_address!=NULL)?ip_address:"unknown remote address");
 	p = index(cmd, ']');
 	if (p!=NULL)
 		p+=2;
@@ -2857,7 +2860,6 @@ int write_command_to_file(char *cmd){
 
 	/* log comments if forced */
 	if(enforce_comments_on_actions==TRUE) {
-		sprintf(ip_address,"%s",getenv("REMOTE_ADDR"));
 		sprintf(buffer, "FORCED COMMENT: %s;%s;%s;%s", current_authdata.username,(ip_address!=NULL)?ip_address:"unknown remote address",comment_author,comment_data);
 		write_to_cgi_log(buffer);
 	}





More information about the icinga-checkins mailing list