From 46a483ed36916d9f8c42428a661eaf664ff3feb8 Mon Sep 17 00:00:00 2001 From: brknkfr <4104421+brknkfr@users.noreply.github.com> Date: Sat, 29 Apr 2023 17:38:44 +0000 Subject: [PATCH] lxc_guests: Enhanced and fixed lxc_guests plugin (#1371) * lxc_guests2: Enhanced and fixed lxc_guests plugin - Working with systemd and cgroup version 2 (tested on debian bullseye and debian booksworm) - Fixed processes (with cgroup version 2) - Simplified labels - Simplified memory usage graph - Added CPU usage in percent (using systemd-cgtop) - Added Tasks - No cgrouppath guessing * Rename lxc_guests2 to lxc_guests to replace old plugin --------- Co-authored-by: Sebastian L --- plugins/lxc/lxc_guests | 181 +++++++++++++++++++++++------------------ 1 file changed, 102 insertions(+), 79 deletions(-) diff --git a/plugins/lxc/lxc_guests b/plugins/lxc/lxc_guests index 1e4f2fb4..f32179a8 100755 --- a/plugins/lxc/lxc_guests +++ b/plugins/lxc/lxc_guests @@ -11,11 +11,18 @@ lxc_guests - collect statistics about containers virtualized via LXC [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. + # 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 @@ -24,32 +31,20 @@ lxc_guests - collect statistics about containers virtualized via LXC # (default none excluded) #env.exclude container1 container2 - # path where tasks sysfs files are stored, - # set this if the various attempts in the - # code don't work - # (default none) - #env.cgrouppath /sys/fs/cgroup/cpuacct/lxc/ - =head1 INTERPRETATION -This plugin needs root privilege. +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 has been tested with lxc 3 and -lx2 (on Debian buster and Debian jessie, -respectively). +This plugin needs root (user and group) privilege. -If using lxc 2, make sure you do not have cruft -in your container config files, you can test -it with: - lxc-cgroup -o /dev/stdout -l INFO -n 104 cpuacct.usage --- with 104 a valid lxc instance), if you -get a warning, fix the config file. - -For the logins graph, the "users" command is required in each -container. - -Tested on Debian buster and Debian jessie. +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 @@ -57,6 +52,7 @@ vajtsz vajtsz@gmail.com mitty mitty@mitty.jp alphanet schaefer@alphanet.ch (many changes and multigraph) Lars Kruse +Sebastian L. =head1 LICENSE @@ -80,9 +76,7 @@ lxcpath=${lxcpath:-/var/lib/lxc} # containers to be ignored exclude=${exclude:-} ram_display_stacked=${ram_display_stacked:-true} -# try to guess the location, if empty -cgrouppath=${cgrouppath:-} - +cpu_usage_display_stacked=${cpu_usage_display_stacked:-true} # --- FUNCTIONS @@ -104,51 +98,21 @@ get_active_guests() { get_lxc_cgroup_info() { local guest_name="$1" local field="$2" - # lxc3 (lxc < 3: may output some warnings if there is cruft in your config dir) - lxc-cgroup -o /dev/stdout -l INFO -n "$guest_name" "$field" | sed 's/^.*lxc_cgroup.c:main:[0-9][0-9]* - //' | grep -v set_config_idmaps + lxc-cgroup -o /dev/stdout -l INFO -n "$guest_name" "$field" | grep -v set_config_idmaps } -# find proper sysfs and count it -# Debian 6.0: /sys/fs/cgroup//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 lxc_count_processes () { local guest_name="$1" - local SYSFS + local processes [ -z "$guest_name" ] && return 0 - if [ -n "$cgrouppath" ]; then - SYSFS="$cgrouppath/$guest_name/tasks" - if [ -e "$SYSFS" ]; then - wc -l <"$SYSFS" - return - fi + processes=$(find /sys/fs/cgroup/lxc.payload."$guest_name"/ -name cgroup.procs -exec cat {} \; | wc -l) + if [ -n "$processes" ]; then + echo "$processes" fi - for SYSFS in \ - "/sys/fs/cgroup/$guest_name/tasks" \ - "/sys/fs/cgroup/lxc/$guest_name/tasks" \ - "/sys/fs/cgroup/cpuacct/lxc/$guest_name/tasks" \ - "/sys/fs/cgroup/systemd/lxc/$guest_name/tasks" \ - "/sys/fs/cgroup/cpuacct/sysdefault/lxc/$guest_name/tasks" \ - "/sys/fs/cgroup/cpu/lxc.payload.$guest_name/tasks" - do - if [ -e "$SYSFS" ]; then - wc -l <"$SYSFS" - return - fi - done - - if [ -e /usr/bin/cgm ]; then - cgm getvalue cpu "lxc/$guest_name" tasks 2>/dev/null | wc -l - else - get_lxc_cgroup_info "$guest_name" "tasks" | wc -l - fi } @@ -173,7 +137,7 @@ do_autoconf() { do_config() { - local active_guests guest_name draw_style + 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 +