Need help to get the parsed output of "iostat" command

Hi,

I have a requirement where parsed output from various linux commands like top, netstat, iostat, etc. will be the input for one javascript with the parsed output from these commands converted to JSON format

For "iostat" command, since there are two outputs - one w.r.t CPU utilization and another w.r.t device statistics, ex.:

iostat -c
[root@rhel64 ~]# iostat -c
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.59    0.00    0.32    0.46    0.00   98.63

and

iostat -d -p
[root@rhel64 ~]# iostat -d -p -k
Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
scd0              0.00         0.00         0.00        180          0
sda               1.21         1.00        22.27    1521773   33955953
sda1              0.00         0.01         0.00      10487          9
sda2              1.21         0.99        22.27    1510670   33955944
dm-0              5.60         0.99        22.27    1508485   33949224
dm-1              0.00         0.00         0.00       1288       6708
dm-2              0.00         0.00         0.00        489         12

My requirement here is to have a shell script or perl script which can awk the output for the above two usages of iostat command (i.e., for CPU and device statistics) but without any sampling interval taken into account (sampling interval will be done later by calling the code multiple time. For now, output displayed once is enough)
There should be two parsed outputs, one for CPU statistics and one for Device statistics both of which shall later be used as input data for conversion to JSON format (in node.js )

Can someone help me out here?

Please describe what you believe JSON format is and show us EXACTLY what output you hope to produce in your output file (in CODE tags) corresponding to the two input samples you have shown us.

What shell are you using?

What operating system are you using?

Why would you use perl to run an awk script?

I am using bash, and the Linux version is RHEL 6.4
The reason I mentioned that I would need awk is because of the parsing. And perl would be, I considered, an appropriate choice here.
I am just giving an example code in javascript which uses the JSON format. Basically, this is the output I need to finally get. Before getting this is where I need the help, for getting CPU and device statistics, just like the code I have given for example provides output for "ps" command with paramters like rss, pcpu, vsz, etc. On similar lines, I need the output for parameters like:
avg-cpu: %user %nice %system %iowait %steal %idle
and
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn

Once I get the parsed output, I need to convert it into JSON format which provides an output based readily with parameters like:
Objects: CPU and Device
Metric...kilobytes (kB)
CPU-Properties....%user %nice %system %iowait %steal %idle;
Device-Properties.... tps kB_read/s kB_wrtn/s kB_read kB_wrtn
Value.....the values of the above CPU and device properties

Template Code:

//import process library
var spawn = require('child_process').spawn;

var check = '<process>';

function ping() {
    //call OS command
    var ps = spawn('<command>', ['<options>', '<options>']);
    //reading and processing data from OS command
    ps.stdout.on('data', function(data) {

        //Place your parsing code here



        process.stdout.write(data);
    });

}

ping();

Linux Example Code for "ps" command

//import process library
var spawn = require('child_process').spawn;
//checking for process node
var check = ["node"];
function ping() {
    //call OS command
    var ps = spawn('ps', ['-eo', 'vsz,rss,pcpu,command']);
    //reading and processing data from OS command
    ps.stdout.on('data', function(data) {

        var dA = data.toString().split('\n');
        var dAl = dA.length;
        var dS, dO, i, j, k, dSl, flag;
        var now = new Date().getTime();
        for (i = 1; i < dAl; i++) {
            flag = false;
            for (j = 0; j < check.length; j++) {
                var re = new RegExp("\\b" + check[j] + "\\b", "g");

                if (dA.match(re) ? dA.match(re).length : 0) {
                    flag = true;
                }
            }

            if (flag) {
                dS = dA.replace(/ +/g, ' ').split(' ');
                dO = {};
                dSl = dS.length;
                for (j = 0, k = 0; j < dSl; k++ , j++) {
                    if (dS[j].length == 0) {
                        k--;
                    }
                    if (k == 0) {
                        dO['vsz'] = dS[j];
                    }
                    else if (k == 1) {
                        dO['rss'] = dS[j];
                    }
                    else if (k == 2) {
                        dO['pcpu'] = dS[j];
                    }
                    else if (k == 3) {
                        dO['command'] = dS[j];
                    }
                    else if (k > 3) {
                        dO['command'] = dO['command'] + ' ' + dS[j];
                    }
                }
                dO['date'] = now;
                //write JSON to stdout
                process.stdout.write(JSON.stringify(dO) + "\n");
                dO = null;
                dS = null;
            }
        }
        dA = null;
    });

}

ping();

---------- Post updated 06-01-16 at 12:35 AM ---------- Previous update was 05-31-16 at 12:47 PM ----------

Any update on this query folks?
Kindly let me know