[sheepdog] [PATCH stable-0.7] dog: log dog operation in syslog

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Mon Jan 6 09:55:03 CET 2014


This patch is based on a request from our internal team. Currently,
sheepdog doesn't provide a way of logging admin's operation. The lack
of this feature is serious because we cannot find any evidence of
operation.

This patch adds the logging feature in dog command. If the environment
variable, SHEEPDOG_DOG_LOG, has value, dog command logs its command
line argument with syslog. If SHEEPDOG_DOG_LOG_PATH is specified
additionally, the log is written to the file of the path.

Below is an example of the log:
- with standard syslog
  Dec 27 10:57:42 dhcpff33 sheepdog admin operation[9809]:  dog
  cluster format -c 1
- with custom path
  2013 Dec 27 17:20:56 :  dog vdi list

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
[ remove extra blank lines - Yuan]
Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 dog/dog.c |   58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/dog/dog.c b/dog/dog.c
index ee7125c..df45920 100644
--- a/dog/dog.c
+++ b/dog/dog.c
@@ -12,6 +12,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <signal.h>
+#include <sys/time.h>
+#include <fcntl.h>
 
 #include "sheepdog_proto.h"
 #include "sheep.h"
@@ -343,6 +345,60 @@ static size_t get_nr_nodes(void)
 	return sd_nodes_nr;
 }
 
+static void log_dog_operation(int argc, char **argv)
+{
+	int length = 0, printed = 0;
+	char *msg;
+	const char *custom_log_path;
+
+	if (!getenv("SHEEPDOG_DOG_LOG"))
+		/* don't log operation of dog */
+		return;
+
+	for (int i = 0; i < argc; i++)
+		length += 1 + strlen(argv[i]); /* 1 is for space */
+
+	length++; /* 1 is for '\0' */
+	msg = xcalloc(length, sizeof(char));
+
+	for (int i = 0; i < argc; i++)
+		printed += snprintf(msg + printed, length - printed,
+				    " %s", argv[i]);
+
+	custom_log_path = getenv("SHEEPDOG_DOG_LOG_PATH");
+	if (custom_log_path) {
+		struct timeval tv;
+		struct tm tm;
+		char time_str[256];
+		int fd;
+
+		fd = open(custom_log_path, O_WRONLY | O_APPEND | O_CREAT,
+			  S_IRUSR | S_IWUSR);
+		if (fd < 0) {
+			fprintf(stderr, "error at opening log file of dog"
+				"(%s): %m\n", custom_log_path);
+			goto out;
+		}
+
+		gettimeofday(&tv, NULL);
+		localtime_r(&tv.tv_sec, &tm);
+		strftime(time_str, sizeof(time_str),
+			 "%Y %b %2d %H:%M:%S ", &tm);
+
+		dprintf(fd, "%s: %s\n", time_str, msg);
+		close(fd);
+	} else {
+		/* if the path is not specified, we use standard syslog */
+
+		openlog("sheepdog admin operation", LOG_PID, LOG_USER);
+		syslog(LOG_INFO, "%s\n", msg);
+		closelog();
+	}
+
+out:
+	free(msg);
+}
+
 int main(int argc, char **argv)
 {
 	int ch, longindex, ret;
@@ -353,6 +409,8 @@ int main(int argc, char **argv)
 	char *p;
 	const struct sd_option *sd_opts;
 
+	log_dog_operation(argc, argv);
+
 	install_crash_handler(crash_handler);
 
 	init_commands(&commands);
-- 
1.7.10.4




More information about the sheepdog mailing list