[sheepdog] [PATCH v2] dog: log dog operation in syslog

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Fri Dec 27 09:25:18 CET 2013


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>
---

v2: support logging with a custom file

 dog/dog.c |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/dog/dog.c b/dog/dog.c
index b092e62..4bb22a3 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"
@@ -351,6 +353,63 @@ 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;
+
+		printf("custom_log_path: %s\n", custom_log_path);
+		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;
@@ -363,6 +422,8 @@ int main(int argc, char **argv)
 	uint8_t sdhost[16];
 	int sdport;
 
+	log_dog_operation(argc, argv);
+
 	install_crash_handler(crash_handler);
 
 	init_commands(&commands);
-- 
1.7.10.4




More information about the sheepdog mailing list