diff --git a/plugins/lxc/lxc-lib b/plugins/lxc/lxc-lib new file mode 100644 index 00000000..90f90e6b --- /dev/null +++ b/plugins/lxc/lxc-lib @@ -0,0 +1,63 @@ +: << =cut + +=head1 NAME + +lxc-lib -- some base functions for the lxc_* plugins + +=head1 AUTHOR + +schaefer@alphanet.ch + +=head1 LICENSE + +GPLv3 + +=cut + +lxcpath=${lxcpath:-/var/lib/lxc} + +function active_guests { + local g active i ok + for g in $(lxc-ls | sort -u) + do + # handle optional exclude list in $1 + ok=1 + for i in $1 + do + if [ "$i" = "$g" ]; then + ok=0 + fi + done + + if [ $ok = 1 ]; then + if lxc-info -n $g 2>&1 | grep -qs RUNNING; then + active="$active $g" + fi + fi + done + + echo $active +} + +function lxc_cgroup { + # lxc3 + lxc-cgroup -o /dev/stdout -l INFO $* | sed 's/^.*lxc_cgroup.c:main:[0-9][0-9]* - //' + + # lxc < 3 + #lxc-cgroup $* +} + +function lxc_clean_fieldname { + # workaround for bug leaving out the first number + echo $(clean_fieldname ${1::1}$1) +} + +function lxc_netdev { + local g=$1 dev + + if [ -f $lxcpath/$g/config ]; then + grep lxc.net.0.veth.pair $lxcpath/$g/config | awk '{print $NF;}' + else + echo unknown + fi +} diff --git a/plugins/lxc/lxc_cpu b/plugins/lxc/lxc_cpu index 909a4628..2f76ff85 100755 --- a/plugins/lxc/lxc_cpu +++ b/plugins/lxc/lxc_cpu @@ -20,10 +20,12 @@ This plugin needs root privilege. vajtsz vajtsz@gmail.com mitty mitty@mitty.jp +(many changes schaefer@alphanet.ch) =head1 LICENSE -Unknown license +2-clause BSD License +or GPLv3 license, at your option =head1 MAGIC MARKERS @@ -34,59 +36,49 @@ Unknown license . $MUNIN_LIBDIR/plugins/plugin.sh -guest_names=`lxc-ls | sort -u` -for guest in $guest_names; do - if lxc-info -n $guest 2>&1 | grep -qs RUNNING ; then - active="$active $guest" - fi -done -guest_names="$active" +. $MUNIN_LIBDIR/plugins/lxc-lib - - -f_comm='lxc-cgroup ' +active_guests=$(active_guests) if [ "$1" = "autoconf" ]; then - if [ -r /proc/stat ]; then - echo yes - exit 0 - else - echo "no (no /proc/stat)" - exit 0 - fi + if [ -r /proc/stat ]; then + echo yes + exit 0 + else + echo "no (no /proc/stat)" + exit 0 + fi fi if [ "$1" = "config" ]; then - - echo 'graph_title CPU Usage ' - echo 'graph_args -l 0 --base 1000' - echo 'graph_vlabel USER_HZ' - echo 'graph_category cpu' - - - for guest_name in $guest_names; do - guest="$(clean_fieldname $guest_name)" - echo 'cpu_user_'$guest'.label '$guest_name': User' - echo 'cpu_user_'$guest'.type DERIVE' - echo 'cpu_user_'$guest'.min 0' - echo 'cpu_system_'$guest'.label '$guest_name': System' - echo 'cpu_system_'$guest'.type DERIVE' - echo 'cpu_system_'$guest'.min 0' - done - exit 0 + cat <&1 | grep -qs RUNNING ; then - active="$active $guest" - fi -done -guest_names="$active" +. $MUNIN_LIBDIR/plugins/lxc-lib - -f_comm='lxc-cgroup ' +active_guests=$(active_guests) if [ "$1" = "autoconf" ]; then - if [ -r /proc/stat ]; then - echo yes - exit 0 - else - echo "no (no /proc/stat)" - exit 0 - fi + if [ -r /proc/stat ]; then + echo yes + exit 0 + else + echo "no (no /proc/stat)" + exit 0 + fi fi if [ "$1" = "config" ]; then + cat </tasks # Ubuntu 12.04 with fstab: /sys/fs/cgroup/lxc//tasks # Ubuntu 12.04 with cgroup-lite: /sys/fs/cgroup/cpuacct/lxc//tasks # Ubuntu 12.04 with cgroup-bin: /sys/fs/cgroup/cpuacct/sysdefault/lxc//tasks -# Ubuntu 14.04: /sys/fs/cgroup/systemd/lxc//tasks +# and with cgmanager on jessie count_processes () { [ -z "$1" ] && return 0 @@ -51,11 +57,10 @@ count_processes () { return `wc -l < $SYSFS` fi fi - + for SYSFS in \ /sys/fs/cgroup/$1/tasks \ /sys/fs/cgroup/lxc/$1/tasks \ - /sys/fs/cgroup/systemd/lxc/$1/tasks \ /sys/fs/cgroup/cpuacct/lxc/$1/tasks \ /sys/fs/cgroup/cpuacct/sysdefault/lxc/$1/tasks \ ; do @@ -64,60 +69,51 @@ count_processes () { fi done + if [ -e /usr/bin/cgm ]; then + return `cgm getvalue cpu lxc/$1 tasks 2>/dev/null | wc -l` + fi + return 0 } - -guest_names=`lxc-ls | sort -u` -for guest in $guest_names; do - if lxc-info -n $guest 2>&1 | grep -qs RUNNING ; then - active="$active $guest" - fi -done -guest_names="$active" - - - -f_comm='lxc-cgroup ' - if [ "$1" = "autoconf" ]; then - if [ -r /proc/stat ]; then - echo yes - exit 0 - else - echo "no (no /proc/stat)" - exit 0 - fi + if [ -r /proc/stat ]; then + echo yes + exit 0 + else + echo "no (no /proc/stat)" + exit 0 + fi fi if [ "$1" = "config" ]; then - - echo 'graph_title Processes ' - echo 'graph_args -l 0 --base 1000' - echo 'graph_vlabel Number of processes' - echo 'graph_category processes' - - - for guest_name in $guest_names; do - guest="$(clean_fieldname $guest_name)" - echo 'lxc_proc_'$guest'.label '$guest_name': processes' - echo 'lxc_proc_'$guest'.type GAUGE' - echo 'lxc_proc_'$guest'.min 0' - done - exit 0 + cat <&1 | grep -qs RUNNING ; then - active="$active $guest" - fi -done -guest_names="$active" - - -f_comm='lxc-cgroup ' - - -do_autoconf() { - if [ -r /proc/stat ]; then - echo yes - else - echo "no (no /proc/stat)" - fi -} - - -do_config() { - echo 'graph_title Memory ' - echo 'graph_args -l 0 --base 1024' - echo 'graph_vlabel byte' - echo 'graph_category memory' - - for guest_name in $guest_names; do - guest="$(clean_fieldname "$guest_name")" - echo "mem_usage_$guest.label $guest_name: Mem usage" - echo "mem_usage_$guest.type GAUGE" - if [ "$areastack" = "true" ]; then - echo "mem_usage_$guest.draw AREASTACK" - fi - echo "mem_cache_$guest.label $guest_name: Cache" - echo "mem_cache_$guest.type GAUGE" - echo "mem_active_$guest.label $guest_name: Active" - echo "mem_active_$guest.type GAUGE" - echo "mem_inactive_$guest.label $guest_name: Inactive" - echo "mem_inactive_$guest.type GAUGE" - done -} - - -do_fetch() { - for guest_name in $guest_names; do - guest="$(clean_fieldname "$guest_name")" - - value=$($f_comm -n "$guest_name" memory.usage_in_bytes) - echo "mem_usage_$guest.value $value" - - value=$($f_comm -n "$guest_name" memory.stat | grep total_cache | awk '{print($2)}') - echo "mem_cache_$guest.value $value" - - value=$($f_comm -n "$guest_name" memory.stat | grep total_active_anon | awk '{print($2)}') - echo "mem_active_$guest.value $value" - - value=$($f_comm -n "$guest_name" memory.stat | grep total_inactive_anon | awk '{print($2)}') - echo "mem_inactive_$guest.value $value" - done -} +. $MUNIN_LIBDIR/plugins/lxc-lib +active_guests=$(active_guests) if [ "$1" = "autoconf" ]; then - do_autoconf -elif [ "$1" = "config" ]; then - do_config - if [ "${MUNIN_CAP_DIRTYCONFIG:-0}" = "1" ]; then do_fetch; fi -else - do_fetch + if [ -r /proc/stat ]; then + echo yes + exit 0 + else + echo "no (no /proc/stat)" + exit 0 + fi fi + +if [ "$1" = "config" ]; then + cat <