Sponsored Content
Top Forums Shell Programming and Scripting Need help to get the parsed output of "iostat" command Post 302974473 by gopivallabha on Tuesday 31st of May 2016 03:05:09 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy