#!/bin/sh # -*- sh -*- : << =cut =head1 NAME lxc_guests - collect statistics about containers virtualized via LXC =head1 CONFIGURATION [lxc_guests] user root group root # The memory usage of containers are by default drawn as stacked area # charts. Alternatively a non-stacked graph with lines can be configured. # Default: true #env.ram_display_stacked true # The cpu usage in percent of containers are by default drawn as stacked # area charts. Alternatively a non-stacked graph with lines can be # configured. # Default: true #env.cpu_usage_stacked true # lxc container path, default below #env.lxcpath /var/lib/lxc # exclude the following containers # (default none excluded) #env.exclude container1 container2 =head1 INTERPRETATION This version of the plugin replaces the old lxc_guests plugin and works with newer lxc versions, with cgroup version 2 and systemd (systemd-cgtop is used to get cpu usage). Use an older revision of this plugin for systems with cgroup version 1. This plugin needs root (user and group) privilege. This plugin has been tested with lxc 4 and lxc 5 (on Debian bullseye and Debian booksworm, respectively). For the logins graph, the "users" command is required in each container and user/group has to be set to root for lxc-attach. =head1 AUTHOR vajtsz vajtsz@gmail.com mitty mitty@mitty.jp alphanet schaefer@alphanet.ch (many changes and multigraph) Lars Kruse Sebastian L. =head1 LICENSE 2-clause BSD License or GPLv3 license or later, at your option =head1 MAGIC MARKERS #%# family=auto #%# capabilities=autoconf =cut set -eu . "$MUNIN_LIBDIR/plugins/plugin.sh" lxcpath=${lxcpath:-/var/lib/lxc} # containers to be ignored exclude=${exclude:-} ram_display_stacked=${ram_display_stacked:-true} cpu_usage_display_stacked=${cpu_usage_display_stacked:-true} # --- FUNCTIONS get_active_guests() { local excludes="$1" local guest_name for guest_name in $(lxc-ls) do # handle optional exclude list in $1 if ! echo "$excludes" | grep -qwF "$guest_name"; then if lxc-info -n "$guest_name" --state 2>/dev/null | grep -qw RUNNING; then echo "$guest_name" fi fi done } get_lxc_cgroup_info() { local guest_name="$1" local field="$2" lxc-cgroup -o /dev/stdout -l INFO -n "$guest_name" "$field" | grep -v set_config_idmaps } lxc_count_processes () { local guest_name="$1" local processes [ -z "$guest_name" ] && return 0 processes=$(find /sys/fs/cgroup/lxc.payload."$guest_name"/ -name cgroup.procs -exec cat {} \; | wc -l) if [ -n "$processes" ]; then echo "$processes" fi } # change the first character of a string to upper case title_case() { local text="$1" printf "%s%s" "$(echo "$text" | cut -c 1 | tr "[:lower:]" "[:upper:]")" "$(echo "$text" | cut -c 2-)" } do_autoconf() { if [ ! -r /proc/net/dev ]; then echo "no (/proc/net/dev cannot be read)" elif [ ! -e "$lxcpath" ]; then echo "no ($lxcpath is not present)" elif [ -z "$(which lxc-ls)" ]; then echo "no ('lxc-ls' is not available in PATH)" else echo yes fi } do_config() { local active_guests guest_name draw_style NCPU graphlimit active_guests=$(get_active_guests "$exclude") cat <&2 "Invalid action requested (none of: autoconf / config / '')" exit 1 esac