Merge branch 'collectd-5.7'
authorRuben Kerkhof <ruben@rubenkerkhof.com>
Tue, 29 Nov 2016 14:47:53 +0000 (15:47 +0100)
committerRuben Kerkhof <ruben@rubenkerkhof.com>
Tue, 29 Nov 2016 14:47:53 +0000 (15:47 +0100)
contrib/README
contrib/docker/50docker-apt-conf [new file with mode: 0644]
contrib/docker/Dockerfile [new file with mode: 0644]
contrib/docker/collectd.conf [new file with mode: 0644]
contrib/docker/collectd.conf.d/sample.conf [new file with mode: 0644]
contrib/docker/rootfs_prefix/Makefile [new file with mode: 0644]
contrib/docker/rootfs_prefix/rootfs_prefix.c [new file with mode: 0644]

index 5f401f0..6e2ea19 100644 (file)
@@ -49,6 +49,21 @@ should look something like this:
   datadir: "/var/lib/collectd/rrd/"
   libdir: "/usr/lib/collectd/"
 
+docker/
+-------
+Sample docker setup using an LD_PRELOAD wrapper to redirect system calls
+accessing /proc and /sys to prefixed bind-mounts inside the container.
+
+Drop your collectd configuration snippets in the
+contrib/docker/collectd.conf.d/ directory, and build an image including them:
+ $ docker build -t my_collectd ./contrib/docker/
+
+Then run it with the required bind-mounts:
+ $ docker run -it --rm \
+    -v /proc:/rootfs/proc:ro -v /sys:/rootfs/sys:ro \
+    --name collectd my_collectd
+ $ docker exec -it collectd collectdctl listval
+
 exec-munin.px
 -------------
   Script to be used with the exec-plugin (see collectd-exec(5) for details)
diff --git a/contrib/docker/50docker-apt-conf b/contrib/docker/50docker-apt-conf
new file mode 100644 (file)
index 0000000..3f898b3
--- /dev/null
@@ -0,0 +1,4 @@
+APT::Install-Recommends "0";
+APT::Install-Suggests "0";
+APT::Get::Assume-Yes "1";
+APT::Get::AutomaticRemove "1";
diff --git a/contrib/docker/Dockerfile b/contrib/docker/Dockerfile
new file mode 100644 (file)
index 0000000..a691f2e
--- /dev/null
@@ -0,0 +1,24 @@
+FROM debian:stable
+
+ENV DEBIAN_FRONTEND noninteractive
+COPY 50docker-apt-conf /etc/apt/apt.conf.d/
+
+COPY rootfs_prefix/ /usr/src/rootfs_prefix/
+
+RUN apt-get update \
+ && apt-get upgrade \
+ && apt-get install \
+    collectd-core \
+    collectd-utils \
+    build-essential \
+ && make -C /usr/src/rootfs_prefix/ \
+ && apt-get --purge remove build-essential \
+ && apt-get clean \
+ && rm -rf /var/lib/apt/lists/*
+
+COPY collectd.conf /etc/collectd/collectd.conf
+COPY collectd.conf.d /etc/collectd/collectd.conf.d
+
+ENV LD_PRELOAD /usr/src/rootfs_prefix/rootfs_prefix.so
+
+CMD [ "/usr/sbin/collectd", "-f"]
diff --git a/contrib/docker/collectd.conf b/contrib/docker/collectd.conf
new file mode 100644 (file)
index 0000000..bbcd079
--- /dev/null
@@ -0,0 +1,16 @@
+LoadPlugin logfile
+<Plugin logfile>
+       LogLevel "info"
+       File STDOUT
+       Timestamp true
+       PrintSeverity true
+</Plugin>
+
+LoadPlugin unixsock
+<Plugin unixsock>
+        SocketGroup "nogroup"
+</Plugin>
+
+<Include "/etc/collectd/collectd.conf.d">
+        Filter "*.conf"
+</Include>
diff --git a/contrib/docker/collectd.conf.d/sample.conf b/contrib/docker/collectd.conf.d/sample.conf
new file mode 100644 (file)
index 0000000..cbd7ce1
--- /dev/null
@@ -0,0 +1,4 @@
+LoadPlugin cpu
+LoadPlugin memory
+LoadPlugin disk
+LoadPlugin df
diff --git a/contrib/docker/rootfs_prefix/Makefile b/contrib/docker/rootfs_prefix/Makefile
new file mode 100644 (file)
index 0000000..f26bebd
--- /dev/null
@@ -0,0 +1,2 @@
+rootfs_prefix.so: rootfs_prefix.c
+       $(CC) -Wall -Werror -fPIC -shared -o rootfs_prefix.so rootfs_prefix.c -ldl
diff --git a/contrib/docker/rootfs_prefix/rootfs_prefix.c b/contrib/docker/rootfs_prefix/rootfs_prefix.c
new file mode 100644 (file)
index 0000000..c27a67a
--- /dev/null
@@ -0,0 +1,64 @@
+#define _GNU_SOURCE
+
+#include <dirent.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <error.h>
+#include <stdio.h>
+#include <string.h>
+
+#define PREFIX "/rootfs"
+#define BUFSIZE 256
+
+const char *add_prefix(const char *orig, char *prefixed) {
+  int status;
+  int errno;
+
+  if ((strncmp(orig, "/proc", 5) == 0) || (strncmp(orig, "/sys", 4) == 0)) {
+
+    status = snprintf(prefixed, BUFSIZE, "%s%s", PREFIX, orig);
+    if ((unsigned int)status >= BUFSIZE) {
+      error(status, ENAMETOOLONG,
+            "'%s' got truncated when adding '%s' prefix: '%s'", orig, PREFIX,
+            prefixed);
+      return orig;
+    } else if (status < 1) {
+      error(status, errno,
+            "adding '%s' prefix to file path failed: '%s' -> '%s'", PREFIX,
+            orig, prefixed);
+      return orig;
+    } else {
+      return (const char *)prefixed;
+    }
+
+  } else {
+    return orig;
+  }
+}
+
+FILE *fopen(const char *path, const char *mode) {
+  char filename[BUFSIZE] = "\0";
+
+  FILE *(*original_fopen)(const char *, const char *);
+  original_fopen = dlsym(RTLD_NEXT, "fopen");
+
+  return (*original_fopen)(add_prefix(path, filename), mode);
+}
+
+DIR *opendir(const char *name) {
+  char filename[BUFSIZE] = "\0";
+
+  DIR *(*original_opendir)(const char *);
+  original_opendir = dlsym(RTLD_NEXT, "opendir");
+
+  return (*original_opendir)(add_prefix(name, filename));
+}
+
+int *open(const char *pathname, int flags) {
+  char filename[BUFSIZE] = "\0";
+
+  int *(*original_open)(const char *, int);
+  original_open = dlsym(RTLD_NEXT, "open");
+
+  return (*original_open)(add_prefix(pathname, filename), flags);
+}