[icinga-checkins] icinga.org: icinga-core/next: core: keep logfile open, reducing open/ close performance decreases

git at icinga.org git at icinga.org
Sun Mar 10 22:42:26 CET 2013


Module: icinga-core
Branch: next
Commit: 7aab352a3d651e850b634ced285160a76715c7a3
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=7aab352a3d651e850b634ced285160a76715c7a3

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Sun Mar 10 15:56:00 2013 +0100

core: keep logfile open, reducing open/close performance decreases

the only caveat on this - when a kernel panic happens, the last fflush()
may be missed, since the buffers now reside on the kernel side, and we
do not open/close (commit) the log on every single line.

refs #3390

---

 Changelog         |    1 +
 base/logging.c    |   43 ++++++++++++++++++++++++++++++++++---------
 base/utils.c      |    3 +++
 include/logging.h |    2 ++
 4 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/Changelog b/Changelog
index 5262f77..9b42861 100644
--- a/Changelog
+++ b/Changelog
@@ -20,6 +20,7 @@ NOTES
 ENHANCEMENTS
 * core: introduce warning level -vv on config verification #3510 - MF
 * core: refuse to start if neb modules fail to load (Andreas Ericsson) #3812 - MF
+* core: keep logfile open, reducing open/close performance decreases (Andreas Ericsson) #3390 - MF
 
 * classic ui: Add "set_expire_ack_by_default" to cgi configuration #3476 - CF
 * classic ui: Add "send_ack_notifications" to cgi configuration (TheCry) #3467 - CF
diff --git a/base/logging.c b/base/logging.c
index b2540cd..7936378 100644
--- a/base/logging.c
+++ b/base/logging.c
@@ -63,6 +63,7 @@ extern int      debug_level;
 extern int      debug_verbosity;
 extern unsigned long max_debug_file_size;
 FILE            *debug_file_fp = NULL;
+static FILE	*log_fp;
 
 int dummy;	/* reduce compiler warnings */
 
@@ -208,10 +209,36 @@ static void write_to_all_logs_with_timestamp(char *buffer, unsigned long data_ty
 	write_to_log(buffer, data_type, timestamp);
 }
 
+FILE *open_log_file(void) {
+
+	if (log_fp) /* keep it open unless we rotate */
+		return log_fp;
+
+	log_fp = fopen(log_file, "a+");
+
+	if (log_fp == NULL && daemon_mode == FALSE) {
+		printf("Warnings: Cannot open log file '%s' for writing\n", log_file);
+	}
+
+	return log_fp;
+}
+
+int close_log_file(void) {
+
+	if (!log_fp)
+		return 0;
+
+	fflush(log_fp);
+	fclose(log_fp);
+	log_fp = NULL;
+
+	return 0;
+}
+
 
 /* write something to the icinga log file */
 int write_to_log(char *buffer, unsigned long data_type, time_t *timestamp) {
-	FILE *fp = NULL;
+	FILE *fp;
 	time_t log_time = 0L;
 
 	if (buffer == NULL)
@@ -229,12 +256,7 @@ int write_to_log(char *buffer, unsigned long data_type, time_t *timestamp) {
 	if (!(data_type & logging_options))
 		return OK;
 
-	fp = fopen(log_file, "a+");
-	if (fp == NULL) {
-		if (daemon_mode == FALSE)
-			printf("Warning: Cannot open log file '%s' for writing\n", log_file);
-		return ERROR;
-	}
+	fp = open_log_file();
 
 	/* what timestamp should we use? */
 	if (timestamp == NULL)
@@ -247,8 +269,7 @@ int write_to_log(char *buffer, unsigned long data_type, time_t *timestamp) {
 
 	/* write the buffer to the log file */
 	fprintf(fp, "[%lu] %s\n", log_time, buffer);
-
-	fclose(fp);
+	fflush(fp);
 
 #ifdef USE_EVENT_BROKER
 	/* send data to the event broker */
@@ -501,12 +522,16 @@ int rotate_log_file(time_t rotation_time) {
 
 	stat_result = stat(log_file, &log_file_stat);
 
+	close_log_file();
+
 	/* get the archived filename to use */
 	dummy = asprintf(&log_archive, "%s%sicinga-%02d-%02d-%d-%02d.log", log_archive_path, (log_archive_path[strlen(log_archive_path)-1] == '/') ? "" : "/", t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour);
 
 	/* rotate the log file */
 	rename_result = my_rename(log_file, log_archive);
 
+	log_fp = open_log_file();
+
 	if (rename_result) {
 		my_free(log_archive);
 		return ERROR;
diff --git a/base/utils.c b/base/utils.c
index 74822a4..2000268 100644
--- a/base/utils.c
+++ b/base/utils.c
@@ -4265,6 +4265,9 @@ void cleanup(void) {
 	/* free all allocated memory - including macros */
 	free_memory(get_global_macros());
 
+	/* close the log file */
+	close_log_file();
+
 	return;
 }
 
diff --git a/include/logging.h b/include/logging.h
index 8c553e3..1bf5706 100644
--- a/include/logging.h
+++ b/include/logging.h
@@ -113,6 +113,8 @@ int write_log_file_info(time_t *); 			/* records log file/version info */
 int open_debug_log(void);
 int chown_debug_log(uid_t, gid_t);
 int close_debug_log(void);
+FILE *open_log_file(void);
+int close_log_file(void);
 
 #endif /* !NSCGI */
 





More information about the icinga-checkins mailing list