Remove nested if-blocks at cpu plugin

This commit is contained in:
Christian Schrötter 2020-05-08 16:33:55 +02:00
parent 2c20918fd4
commit eadf11e04d
No known key found for this signature in database
GPG Key ID: 8038DEBE14AD09A4
1 changed files with 37 additions and 45 deletions

View File

@ -1,18 +1,10 @@
config_cpu() {
extinfo=""
fields=$(grep '^cpu ' /proc/stat | wc -w)
if [ "$fields" -gt 5 ]; then
extinfo="$extinfo iowait irq softirq"
if [ "$fields" -gt 8 ]; then
extinfo="$extinfo steal"
if [ "$fields" -gt 9 ]; then
extinfo="$extinfo guest"
if [ "$fields" -gt 10 ]; then
extinfo="$extinfo guest_nice"
fi
fi
fi
fi
if [ "$fields" -gt 5 ]; then extinfo="$extinfo iowait irq softirq"; fi
if [ "$fields" -gt 8 ]; then extinfo="$extinfo steal"; fi
if [ "$fields" -gt 9 ]; then extinfo="$extinfo guest"; fi
if [ "$fields" -gt 10 ]; then extinfo="$extinfo guest_nice"; fi
# shellcheck disable=SC2126
NCPU=$(grep '^cpu[0-9]\+ ' /proc/stat | wc -l)
PERCENT=$((NCPU * 100))
@ -74,30 +66,30 @@ config_cpu() {
echo "softirq.max 5000"
echo "softirq.type DERIVE"
echo "softirq.info CPU time spent handling 'batched' interrupts"
if [ "$fields" -gt 8 ]; then
echo "steal.label steal"
echo "steal.draw STACK"
echo "steal.min 0"
echo "steal.max 5000"
echo "steal.type DERIVE"
echo "steal.info The time that a virtual CPU had runnable tasks, but the virtual CPU itself was not running"
if [ "$fields" -gt 9 ]; then
echo "guest.label guest"
echo "guest.draw STACK"
echo "guest.min 0"
echo "guest.max 5000"
echo "guest.type DERIVE"
echo "guest.info The time spent running a virtual CPU for guest operating systems"
if [ "$fields" -gt 10 ]; then
echo "guest_nice.label guest_nice"
echo "guest_nice.draw STACK"
echo "guest_nice.min 0"
echo "guest_nice.max 5000"
echo "guest_nice.type DERIVE"
echo "guest_nice.info The time spent running a virtual CPU for a niced guest operating system"
fi
fi
fi
fi
if [ "$fields" -gt 8 ]; then
echo "steal.label steal"
echo "steal.draw STACK"
echo "steal.min 0"
echo "steal.max 5000"
echo "steal.type DERIVE"
echo "steal.info The time that a virtual CPU had runnable tasks, but the virtual CPU itself was not running"
fi
if [ "$fields" -gt 9 ]; then
echo "guest.label guest"
echo "guest.draw STACK"
echo "guest.min 0"
echo "guest.max 5000"
echo "guest.type DERIVE"
echo "guest.info The time spent running a virtual CPU for guest operating systems"
fi
if [ "$fields" -gt 10 ]; then
echo "guest_nice.label guest_nice"
echo "guest_nice.draw STACK"
echo "guest_nice.min 0"
echo "guest_nice.max 5000"
echo "guest_nice.type DERIVE"
echo "guest_nice.info The time spent running a virtual CPU for a niced guest operating system"
fi
}
fetch_cpu() {
@ -111,14 +103,14 @@ fetch_cpu() {
echo "iowait.value" "$(echo "$CINFO" | cut -d " " -f 5)"
echo "irq.value" "$(echo "$CINFO" | cut -d " " -f 6)"
echo "softirq.value" "$(echo "$CINFO" | cut -d " " -f 7)"
if [ "$fields" -gt 8 ]; then
echo "steal.value" "$(echo "$CINFO" | cut -d " " -f 8)"
if [ "$fields" -gt 9 ]; then
echo "guest.value" "$(echo "$CINFO" | cut -d " " -f 9)"
if [ "$fields" -gt 10 ]; then
echo "guest_nice.value" "$(echo "$CINFO" | cut -d " " -f 10)"
fi
fi
fi
fi
if [ "$fields" -gt 8 ]; then
echo "steal.value" "$(echo "$CINFO" | cut -d " " -f 8)"
fi
if [ "$fields" -gt 9 ]; then
echo "guest.value" "$(echo "$CINFO" | cut -d " " -f 9)"
fi
if [ "$fields" -gt 10 ]; then
echo "guest_nice.value" "$(echo "$CINFO" | cut -d " " -f 10)"
fi
}