I have the very same problem and I can't get it fixed.
From my debugging, it seems to be a problem with awk behaving differently between versions:
Buggy host:
./cpu.sh --debug
CPU pctUser pctNice pctSystem pctIowait pctIdle
1.00 0.00 0.00 0.00 0.00 99.00
1.00 0.00 0.00 0.00 0.00 99.00
Linux 3.16.0-4-686-pae (myhost.domain.de) 07/23/15 i686 (1 CPU)
04:58:15 CPU %user %nice %system %iowait %steal %idle
04:58:16 all 1.00 0.00 0.00 0.00 0.00 99.00
04:58:16 0 1.00 0.00 0.00 0.00 0.00 99.00
Average: CPU %user %nice %system %iowait %steal %idle
Average: all 1.00 0.00 0.00 0.00 0.00 99.00
Average: 0 1.00 0.00 0.00 0.00 0.00 99.00
Cmd = [sar -P ALL 1 1]; | awk 'BEGIN {print "CPU pctUser pctNice pctSystem pctIowait pctIdle"} /Average|Linux|^$|%/ {next} (NR==1) {next} {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF} {printf "%-3s %9s %9s %9s %9s %9s\n", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' header="CPU pctUser pctNice pctSystem pctIowait pctIdle"
Running it manually in the shell works, though:
myhost:/opt/splunkforwarder/etc/apps/Splunk_TA_nix/bin# sar -P ALL 1 1 | awk 'BEGIN {print "CPU pctUser pctNice pctSystem pctIowait pctIdle"} /Average|Linux|^$|%/ {next} (NR==1) {next} {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF} {printf "%-3s %9s %9s %9s %9s %9s", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' header="CPU pctUser pctNice pctSystem pctIowait pctIdle"
CPU pctUser pctNice pctSystem pctIowait pctIdle
all 0.00 0.00 1.00 0.00 99.000 0.00 0.00 1.00 0.00 99.00
myhost:/opt/splunkforwarder/etc/apps/Splunk_TA_nix/bin# awk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan
compiled limits:
max NF 32767
sprintf buffer 1020
Working host:
[root|/opt/splunk/etc/apps/Splunk_TA_nix/bin] ./cpu.sh --debug
CPU pctUser pctNice pctSystem pctIowait pctIdle
all 7.58 0.00 0.88 41.79 49.75
0 3.12 0.00 1.04 10.42 85.42
[root|/opt/splunk/etc/apps/Splunk_TA_nix/bin] cat debug--cpu.sh--Thu_Jul_23_05-03-52_CEST_2015
Linux 3.13.0-57-generic (host2.domain.de) 07/23/2015 x86_64 (1 CPU)
05:03:52 AM CPU %user %nice %system %iowait %steal %idle
05:03:53 AM all 7.58 0.00 0.88 41.79 0.00 49.75
05:03:53 AM 0 3.12 0.00 1.04 10.42 0.00 85.42
Average: CPU %user %nice %system %iowait %steal %idle
Average: all 7.58 0.00 0.88 41.79 0.00 49.75
Average: 0 3.12 0.00 1.04 10.42 0.00 85.42
Cmd = [sar -P ALL 1 1]; | awk 'BEGIN {print "CPU pctUser pctNice pctSystem pctIowait pctIdle"} /Average|Linux|^$|%/ {next} (NR==1) {next} {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF} {printf "%-3s %9s %9s %9s %9s %9s
", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' header="CPU pctUser pctNice pctSystem pctIowait pctIdle"
correct output with shell script, but incorrect from command line:
[root|/opt/splunk/etc/apps/Splunk_TA_nix/bin] sar -P ALL 1 1 | awk 'BEGIN {print "CPU pctUser pctNice pctSystem pctIowait pctIdle"} /Average|Linux|^$|%/ {next} (NR==1) {next} {cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF} {printf "%-3s %9s %9s %9s %9s %9s", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' header="CPU pctUser pctNice pctSystem pctIowait pctIdle"
CPU pctUser pctNice pctSystem pctIowait pctIdle
all 2.52 0.00 0.38 0.50 96.600 1.04 0.00 1.04 2.08 95.831 0.98 0.00 1.96 1.96 94.122
[root|/opt/splunk/etc/apps/Splunk_TA_nix/bin] awk --version [bernd]
GNU Awk 4.0.1
Copyright (C) 1989, 1991-2012 Free Software Foundation.
Any ideas on how to fix this?
... View more