Skip to content

Instantly share code, notes, and snippets.

@davidmfoley
Forked from porjo/README.md
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save davidmfoley/2c188914e16e505cd9fb to your computer and use it in GitHub Desktop.

Select an option

Save davidmfoley/2c188914e16e505cd9fb to your computer and use it in GitHub Desktop.

Revisions

  1. @porjo porjo revised this gist Jul 8, 2014. 2 changed files with 6 additions and 7 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    *Forked from: https://gist.github.com/mmoulton/6224509*

    # Docker stats collection for collectd

    This script can be used to feed collectd with cpu and memory usage statistics for running docker containers using the collectd `exec` plugin.
    11 changes: 4 additions & 7 deletions collectd-docker.sh
    Original file line number Diff line number Diff line change
    @@ -20,9 +20,6 @@ INTERVAL="${COLLECTD_INTERVAL:-60}"
    collect () {
    cd -- "$1"

    # If the directory length is 64, it's likely a docker instance
    #LENGTH=$(expr length $1);
    #if [ "$LENGTH" -eq "64" ]; then
    echo $1 | grep "^docker-" > /dev/null
    if [ $? -eq 0 ]; then

    @@ -33,16 +30,16 @@ collect () {
    if [ -e cpuacct.stat ]; then
    USER=$(cat cpuacct.stat | grep '^user' | awk '{ print $2; }');
    SYSTEM=$(cat cpuacct.stat | grep '^system' | awk '{ print $2; }');
    echo "PUTVAL \"$HOSTNAME/$NAME/cpu-user\" interval=$INTERVAL N:$USER"
    echo "PUTVAL \"$HOSTNAME/$NAME/cpu-system\" interval=$INTERVAL N:$SYSTEM"
    echo "PUTVAL \"$NAME/cpu-0/cpu-user\" interval=$INTERVAL N:$USER"
    echo "PUTVAL \"$NAME/cpu-0/cpu-system\" interval=$INTERVAL N:$SYSTEM"
    fi;

    # If we are in a memory cgroup, we can collect memory usage stats
    if [ -e memory.stat ]; then
    CACHE=$(cat memory.stat | grep '^cache' | awk '{ print $2; }');
    RSS=$(cat memory.stat | grep '^rss ' | awk '{ print $2; }');
    echo "PUTVAL \"$HOSTNAME/$NAME/memory-cached\" interval=$INTERVAL N:$CACHE"
    echo "PUTVAL \"$HOSTNAME/$NAME/memory-used\" interval=$INTERVAL N:$RSS"
    echo "PUTVAL \"$NAME/memory-0/memory-cached\" interval=$INTERVAL N:$CACHE"
    echo "PUTVAL \"$NAME/memory-0/memory-used\" interval=$INTERVAL N:$RSS"
    fi;

    fi;
  2. @porjo porjo revised this gist Jul 8, 2014. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions collectd-docker.sh
    Original file line number Diff line number Diff line change
    @@ -27,22 +27,22 @@ collect () {
    if [ $? -eq 0 ]; then

    # Shorten the name to 12 for brevity, like docker does
    NAME=$(expr substr $1 8 20);
    NAME=$(expr substr $1 8 12);

    # If we are in a cpuacct cgroup, we can collect cpu usage stats
    if [ -e cpuacct.stat ]; then
    USER=$(cat cpuacct.stat | grep '^user' | awk '{ print $2; }');
    SYSTEM=$(cat cpuacct.stat | grep '^system' | awk '{ print $2; }');
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/cpu-user\" interval=$INTERVAL N:$USER"
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/cpu-system\" interval=$INTERVAL N:$SYSTEM"
    echo "PUTVAL \"$HOSTNAME/$NAME/cpu-user\" interval=$INTERVAL N:$USER"
    echo "PUTVAL \"$HOSTNAME/$NAME/cpu-system\" interval=$INTERVAL N:$SYSTEM"
    fi;

    # If we are in a memory cgroup, we can collect memory usage stats
    if [ -e memory.stat ]; then
    CACHE=$(cat memory.stat | grep '^cache' | awk '{ print $2; }');
    RSS=$(cat memory.stat | grep '^rss ' | awk '{ print $2; }');
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/memory-cached\" interval=$INTERVAL N:$CACHE"
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/memory-used\" interval=$INTERVAL N:$RSS"
    echo "PUTVAL \"$HOSTNAME/$NAME/memory-cached\" interval=$INTERVAL N:$CACHE"
    echo "PUTVAL \"$HOSTNAME/$NAME/memory-used\" interval=$INTERVAL N:$RSS"
    fi;

    fi;
  3. @porjo porjo revised this gist Jul 8, 2014. 1 changed file with 18 additions and 10 deletions.
    28 changes: 18 additions & 10 deletions collectd-docker.sh
    Original file line number Diff line number Diff line change
    @@ -11,21 +11,23 @@
    #

    # Location of the cgroup mount point, adjust for your system
    CGROUP_MOUNT="/sys/fs/cgroup"
    #CGROUP_MOUNT="/sys/fs/cgroup"
    CGROUP_MOUNT="/cgroup"

    HOSTNAME="${COLLECTD_HOSTNAME:-localhost}"
    INTERVAL="${COLLECTD_INTERVAL:-60}"

    collect ()
    {
    cd "$1"
    collect () {
    cd -- "$1"

    # If the directory length is 64, it's likely a docker instance
    LENGTH=$(expr length $1);
    if [ "$LENGTH" -eq "64" ]; then
    #LENGTH=$(expr length $1);
    #if [ "$LENGTH" -eq "64" ]; then
    echo $1 | grep "^docker-" > /dev/null
    if [ $? -eq 0 ]; then

    # Shorten the name to 12 for brevity, like docker does
    NAME=$(expr substr $1 1 12);
    NAME=$(expr substr $1 8 20);

    # If we are in a cpuacct cgroup, we can collect cpu usage stats
    if [ -e cpuacct.stat ]; then
    @@ -38,7 +40,7 @@ collect ()
    # If we are in a memory cgroup, we can collect memory usage stats
    if [ -e memory.stat ]; then
    CACHE=$(cat memory.stat | grep '^cache' | awk '{ print $2; }');
    RSS=$(cat memory.stat | grep '^rss' | awk '{ print $2; }');
    RSS=$(cat memory.stat | grep '^rss ' | awk '{ print $2; }');
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/memory-cached\" interval=$INTERVAL N:$CACHE"
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/memory-used\" interval=$INTERVAL N:$RSS"
    fi;
    @@ -54,10 +56,16 @@ collect ()
    done
    }

    while sleep "$INTERVAL"; do
    mainLoop () {
    # Collect stats on memory usage
    ( collect "$CGROUP_MOUNT/memory" )

    # Collect stats on cpu usage
    ( collect "$CGROUP_MOUNT/cpuacct" )
    done
    }

    mainLoop

    while sleep "$INTERVAL"; do
    mainLoop
    done
  4. @mmoulton mmoulton revised this gist Aug 13, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion collectd-docker.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    #
    # CollectD - Docker
    #
    # This script will collect cpu/memory stats on running docer containers using cgroups
    # This script will collect cpu/memory stats on running docker containers using cgroup
    # and output the data in a collectd-exec plugin friendly format.
    #
    # Author: Mike Moulton ([email protected])
  5. @mmoulton mmoulton created this gist Aug 13, 2013.
    21 changes: 21 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # Docker stats collection for collectd

    This script can be used to feed collectd with cpu and memory usage statistics for running docker containers using the collectd `exec` plugin.

    This script will report the *used* and *cached* memory as well as the *user* and *system* cpu usage by inspecting the appropriate cgroup stat file for each running container.

    ## Usage

    This script is intented to be executed by collectd on a host with running docker containers. To use, simply configure the `exec` plugin in collectd to execute the `collectd-docker.sh` script. You may need to adjust the script to match your particulars, such as the mount location for cgroup.

    ### Example collectd.conf snippet

    ```
    LoadPlugin exec
    <Plugin exec>
    Exec ubuntu "/usr/share/collectd/collectd-docker.sh"
    </Plugin>
    ```

    This will execute the `collectd-docker.sh` you placed in `/usr/share/collectd` using the `ubuntu` user.
    63 changes: 63 additions & 0 deletions collectd-docker.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #!/bin/bash

    #
    # CollectD - Docker
    #
    # This script will collect cpu/memory stats on running docer containers using cgroups
    # and output the data in a collectd-exec plugin friendly format.
    #
    # Author: Mike Moulton ([email protected])
    # License: MIT
    #

    # Location of the cgroup mount point, adjust for your system
    CGROUP_MOUNT="/sys/fs/cgroup"

    HOSTNAME="${COLLECTD_HOSTNAME:-localhost}"
    INTERVAL="${COLLECTD_INTERVAL:-60}"

    collect ()
    {
    cd "$1"

    # If the directory length is 64, it's likely a docker instance
    LENGTH=$(expr length $1);
    if [ "$LENGTH" -eq "64" ]; then

    # Shorten the name to 12 for brevity, like docker does
    NAME=$(expr substr $1 1 12);

    # If we are in a cpuacct cgroup, we can collect cpu usage stats
    if [ -e cpuacct.stat ]; then
    USER=$(cat cpuacct.stat | grep '^user' | awk '{ print $2; }');
    SYSTEM=$(cat cpuacct.stat | grep '^system' | awk '{ print $2; }');
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/cpu-user\" interval=$INTERVAL N:$USER"
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/cpu-system\" interval=$INTERVAL N:$SYSTEM"
    fi;

    # If we are in a memory cgroup, we can collect memory usage stats
    if [ -e memory.stat ]; then
    CACHE=$(cat memory.stat | grep '^cache' | awk '{ print $2; }');
    RSS=$(cat memory.stat | grep '^rss' | awk '{ print $2; }');
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/memory-cached\" interval=$INTERVAL N:$CACHE"
    echo "PUTVAL \"$HOSTNAME/docker-$NAME/memory-used\" interval=$INTERVAL N:$RSS"
    fi;

    fi;

    # Iterate over all sub directories
    for d in *
    do
    if [ -d "$d" ]; then
    ( collect "$d" )
    fi;
    done
    }

    while sleep "$INTERVAL"; do
    # Collect stats on memory usage
    ( collect "$CGROUP_MOUNT/memory" )

    # Collect stats on cpu usage
    ( collect "$CGROUP_MOUNT/cpuacct" )
    done