I need to convert the output of iostat command to json. Information is needed only for sd[a-z] discs. It is advisable to do the conversion using awk.
iostat -xd
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sdb 0.01 0.15 0.00 0.00 0.07 0.61 246.51 0.00 3.33 2.85 3.71 1.07 0.00
sda 0.00 0.40 1.59 22.31 47.43 1507.39 130.10 0.03 1.22 4.05 1.02 0.20 0.47
sdc 0.00 0.59 0.14 10.06 18.40 199.05 42.62 0.03 2.48 7.68 2.40 0.16 0.17
sdd 0.01 0.97 1.58 13.13 53.29 190.60 33.15 0.12 7.92 12.03 7.42 0.79 1.17
sde 0.00 0.06 0.00 0.48 0.76 84.47 354.97 0.00 4.33 7.59 4.30 0.86 0.04
dm-0 0.00 0.00 0.00 0.00 0.05 0.03 22.11 0.00 1.03 1.98 0.38 0.84 0.00
dm-1 0.00 0.00 0.02 0.15 0.07 0.61 8.01 0.00 3.22 2.67 3.28 0.03 0.00
dm-2 0.00 0.00 0.14 0.01 6.04 0.10 82.39 0.00 2.54 2.71 0.57 0.49 0.01
dm-3 0.00 0.00 0.14 10.65 18.40 199.05 40.30 0.03 2.56 7.68 2.49 0.15 0.17
dm-4 0.00 0.00 0.01 0.01 0.69 0.05 70.39 0.00 2.22 5.30 0.27 0.35 0.00
dm-5 0.00 0.00 0.00 0.00 0.00 0.00 43.29 0.00 0.70 1.43 0.37 0.63 0.00
I am expecting the following result:
[ { "device":"sdb", "rrqm_s":"0.01", "wrqm_s":"0.15", "r_s":"0.00", "w_s":"0.00", "rkb_s":"0.07", "wkb_s":"0.61", "avgrq_sz":"246.39", "avgqu_sz":"0.00", "await":"3.33", "r_await":"2.85", "w_await":"3.71", "svctm":"1.07", "util":"0.00" },
{ "device":"sda", "rrqm_s":"0.00", "wrqm_s":"0.40", "r_s":"1.59", "w_s":"22.31", "rkb_s":"47.43", "wkb_s":"1507.39", "avgrq_sz":"130.09", "avgqu_sz":"0.03", "await":"1.22", "r_await":"4.05", "w_await":"1.02", "svctm":"0.20", "util":"0.47" },
{ "device":"sdc", "rrqm_s":"0.00", "wrqm_s":"0.59", "r_s":"0.14", "w_s":"10.06", "rkb_s":"18.40", "wkb_s":"199.03", "avgrq_sz":"42.62", "avgqu_sz":"0.03", "await":"2.48", "r_await":"7.68", "w_await":"2.40", "svctm":"0.16", "util":"0.17" },
{ "device":"sdd", "rrqm_s":"0.01", "wrqm_s":"0.97", "r_s":"1.58", "w_s":"13.13", "rkb_s":"53.29", "wkb_s":"190.61", "avgrq_sz":"33.15", "avgqu_sz":"0.12", "await":"7.92", "r_await":"12.03", "w_await":"7.42", "svctm":"0.79", "util":"1.17" },
{ "device":"sde", "rrqm_s":"0.00", "wrqm_s":"0.06", "r_s":"0.00", "w_s":"0.48", "rkb_s":"0.76", "wkb_s":"84.48", "avgrq_sz":"355.01", "avgqu_sz":"0.00", "await":"4.33", "r_await":"7.59", "w_await":"4.30", "svctm":"0.86", "util":"0.04" } ]
CodePudding user response:
I used the following code:
iostat -dx | awk '
BEGIN { printf "[ " }
/^Device:/ {
gsub(":", "")
gsub("/", "_")
gsub("-", "_")
gsub("%", "")
split(tolower($0), FIELDS)
}
/^sd/ {
if ( PRINT_LINE > 1 ) { print " }," }
printf "{ "
for (II=1; II<=NF; II) {
printf "\"" FIELDS[II] "\":\"" $II "\""
if (II != NF) {printf ", "}
}
}
END { printf " } ]" }'
I got the following JSON:
[ { "device":"sdb", "rrqm_s":"0.01", "wrqm_s":"0.15", "r_s":"0.00", "w_s":"0.00", "rkb_s":"0.07", "wkb_s":"0.61", "avgrq_sz":"246.39", "avgqu_sz":"0.00", "await":"3.33", "r_await":"2.85", "w_await":"3.71", "svctm":"1.07", "util":"0.00" },
{ "device":"sda", "rrqm_s":"0.00", "wrqm_s":"0.40", "r_s":"1.59", "w_s":"22.31", "rkb_s":"47.43", "wkb_s":"1507.39", "avgrq_sz":"130.09", "avgqu_sz":"0.03", "await":"1.22", "r_await":"4.05", "w_await":"1.02", "svctm":"0.20", "util":"0.47" },
{ "device":"sdc", "rrqm_s":"0.00", "wrqm_s":"0.59", "r_s":"0.14", "w_s":"10.06", "rkb_s":"18.40", "wkb_s":"199.03", "avgrq_sz":"42.62", "avgqu_sz":"0.03", "await":"2.48", "r_await":"7.68", "w_await":"2.40", "svctm":"0.16", "util":"0.17" },
{ "device":"sdd", "rrqm_s":"0.01", "wrqm_s":"0.97", "r_s":"1.58", "w_s":"13.13", "rkb_s":"53.29", "wkb_s":"190.61", "avgrq_sz":"33.15", "avgqu_sz":"0.12", "await":"7.92", "r_await":"12.03", "w_await":"7.42", "svctm":"0.79", "util":"1.17" },
{ "device":"sde", "rrqm_s":"0.00", "wrqm_s":"0.06", "r_s":"0.00", "w_s":"0.48", "rkb_s":"0.76", "wkb_s":"84.48", "avgrq_sz":"355.01", "avgqu_sz":"0.00", "await":"4.33", "r_await":"7.59", "w_await":"4.30", "svctm":"0.86", "util":"0.04" } ]
CodePudding user response:
just run it with -o JSON
iostat -xd -o JSON