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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to get the parsed output of "iostat" command
# 1  
Old 05-31-2016
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.:
Code:
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

Code:
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?

Last edited by Don Cragun; 05-31-2016 at 02:03 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 05-31-2016
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?
# 3  
Old 05-31-2016
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:
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

Code:
//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[i].match(re) ? dA[i].match(re).length : 0) {
                    flag = true;
                }
            }

            if (flag) {
                dS = dA[i].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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Redirecting output to new file for command "perldoc perllocal"

Hi, I have to redirect output of the command "perldoc perllocal" to new file which contains all the perl module installed. Currently using perldoc perllocal >> mod_data This does not contain all perl modules installed locally on machine, and each character is doubled. Please... (3 Replies)
Discussion started by: asak
3 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. UNIX for Advanced & Expert Users

AIX - io info get from "libperfstat" not match "iostat"

Hi, everyone. I need to write a program to get io info based on libperfstat. But the "write time" of a disk is just half of the value get from iostat. I'm confused and can't explain. Help please. How I calculate "write service time per sec": In iostat: write service... (0 Replies)
Discussion started by: jackliang
0 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Retrieve RAM memory size from "top" command output

Hi, I am trying to get the system RAM size from "top" command's output by the following but it is not working. top | sed "s/^Mem.**\(*\), *//" (10 Replies)
Discussion started by: royalibrahim
10 Replies

6. Shell Programming and Scripting

Storing output of "time" command to a variable

Hi all, I am new to Linux/shell scripting having moderate knowledge. In my script, I need to get execution time of a command (say 'ls') in mili seconds level. For this i tried using "time" command to retrieve the total execution time in milli seconds. But, the problem is that, how to save... (9 Replies)
Discussion started by: happening_linux
9 Replies

7. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

8. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

9. Debian

Debian: doubt in "top" %CPU and "sar" output

Hi All, I am running my application on a dual cpu debian linux 3.0 (2.4.19 kernel). For my application: <sar -U ALL> CPU %user %nice %system %idle ... 10:58:04 0 153.10 0.00 38.76 0.00 10:58:04 1 3.88 0.00 4.26 ... (0 Replies)
Discussion started by: jaduks
0 Replies

10. Shell Programming and Scripting

sed substitute / for \ : error "Function can not be parsed"

Hello all, I have a weird issue when trying to substitute the slashes into backslashes. If I execute this on the command-line (bash / ksh) I get the path correctly translated with backslashes instead of slashes. > echo $PWD | sed 's/\//\\/g' However, when I put this in my script to... (3 Replies)
Discussion started by: candyflip2000
3 Replies
Login or Register to Ask a Question