1
1
config_cpu() {
2
2
extinfo=""
3
- if grep -q '^cpu \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\}' /proc/stat; then
4
- extinfo="iowait irq softirq"
5
- fi
3
+ fields=$(grep '^cpu ' /proc/stat | wc -w)
4
+ if [ "$fields" -gt 5 ]; then extinfo="$extinfo iowait irq softirq"; fi
5
+ if [ "$fields" -gt 8 ]; then extinfo="$extinfo steal"; fi
6
+ if [ "$fields" -gt 9 ]; then extinfo="$extinfo guest"; fi
7
+ if [ "$fields" -gt 10 ]; then extinfo="$extinfo guest_nice"; fi
6
8
# shellcheck disable=SC2126
7
9
NCPU=$(grep '^cpu[0-9]\+ ' /proc/stat | wc -l)
8
10
PERCENT=$((NCPU * 100))
@@ -45,7 +47,7 @@ config_cpu() {
45
47
echo "idle.max 5000"
46
48
echo "idle.type DERIVE"
47
49
echo "idle.info Idle CPU time"
48
- if [ -n "$extinfo" ]; then
50
+ if [ "$fields" -gt 5 ]; then
49
51
echo "iowait.label iowait"
50
52
echo "iowait.draw STACK"
51
53
echo "iowait.min 0"
@@ -65,20 +67,50 @@ config_cpu() {
65
67
echo "softirq.type DERIVE"
66
68
echo "softirq.info CPU time spent handling 'batched' interrupts"
67
69
fi
70
+ if [ "$fields" -gt 8 ]; then
71
+ echo "steal.label steal"
72
+ echo "steal.draw STACK"
73
+ echo "steal.min 0"
74
+ echo "steal.max 5000"
75
+ echo "steal.type DERIVE"
76
+ echo "steal.info The time that a virtual CPU had runnable tasks, but the virtual CPU itself was not running"
77
+ fi
78
+ if [ "$fields" -gt 9 ]; then
79
+ echo "guest.label guest"
80
+ echo "guest.draw STACK"
81
+ echo "guest.min 0"
82
+ echo "guest.max 5000"
83
+ echo "guest.type DERIVE"
84
+ echo "guest.info The time spent running a virtual CPU for guest operating systems"
85
+ fi
86
+ if [ "$fields" -gt 10 ]; then
87
+ echo "guest_nice.label guest_nice"
88
+ echo "guest_nice.draw STACK"
89
+ echo "guest_nice.min 0"
90
+ echo "guest_nice.max 5000"
91
+ echo "guest_nice.type DERIVE"
92
+ echo "guest_nice.info The time spent running a virtual CPU for a niced guest operating system"
93
+ fi
68
94
}
69
95
fetch_cpu() {
70
- extinfo=""
71
- if grep -q '^cpu \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\}' /proc/stat; then
72
- extinfo="iowait irq softirq"
73
- fi
96
+ fields=$(grep '^cpu ' /proc/stat | wc -w)
74
97
CINFO=$(grep '^cpu ' /proc/stat | cut -c6-)
75
98
echo "user.value" "$(echo "$CINFO" | cut -d " " -f 1)"
76
99
echo "nice.value" "$(echo "$CINFO" | cut -d " " -f 2)"
77
100
echo "system.value" "$(echo "$CINFO" | cut -d " " -f 3)"
78
101
echo "idle.value" "$(echo "$CINFO" | cut -d " " -f 4)"
79
- if [ -n "$extinfo" ]; then
102
+ if [ "$fields" -gt 5 ]; then
80
103
echo "iowait.value" "$(echo "$CINFO" | cut -d " " -f 5)"
81
104
echo "irq.value" "$(echo "$CINFO" | cut -d " " -f 6)"
82
105
echo "softirq.value" "$(echo "$CINFO" | cut -d " " -f 7)"
83
106
fi
107
+ if [ "$fields" -gt 8 ]; then
108
+ echo "steal.value" "$(echo "$CINFO" | cut -d " " -f 8)"
109
+ fi
110
+ if [ "$fields" -gt 9 ]; then
111
+ echo "guest.value" "$(echo "$CINFO" | cut -d " " -f 9)"
112
+ fi
113
+ if [ "$fields" -gt 10 ]; then
114
+ echo "guest_nice.value" "$(echo "$CINFO" | cut -d " " -f 10)"
115
+ fi
84
116
}
0 commit comments