LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need to Help on the script (https://www.linuxquestions.org/questions/programming-9/need-to-help-on-the-script-4175603772/)

vijaymaurya 04-13-2017 09:18 AM

Need to Help on the script
 
Code:

Hi All,

Hope you all are doing well.

Kindly help me to understand this script, basically this script pull the %io wait.


#!/usr/bin/perl
@sa=split("\n",`vmstat`);
@line=split(" ",$sa[1]);
for ($i=0; $i <= @line; $i++) {
        if ( $line[$i] eq "wa" ) {
                $j=$i;
                @stat=split(" ",$sa[2]);
                print "Message: CPU wait IO in percentage: $stat[$j]\n";
                print "Statistic: $stat[$j]\n";
                exit 0;
                }
        }
print "Message: ERROR: Can't find CPU wait IO (wa) in output of -vmstat- command. \n";
exit 1;

How can i understand this script.[/PHP]


pan64 04-13-2017 09:37 AM

would be nice to insert your code between [code]here comes your script[/code] tags.
which part is unclear?

MadeInGermany 04-13-2017 01:31 PM

It processes the output of the "vmstat" command.
It splits the output into lines and stores them in sa[] array.
Then it splits a line into words and stores them in line[] array.
Then it loops through the words until it finds "wa". When found, it splits the following line into words and catches the one from the same column.
In effect it catches the word under the "wa".
There are some shortcomings though, the biggest might be the assumption that a plain "vmstat" gives values that are current. In fact they are the average since the system was booted

pan64 04-14-2017 02:32 AM

if you really want to understand it I suggest you to execute vmstat (just to see what will it print) and run this code inside the perl debugger, step by step and you can inspect these variables easily (according to the explanation of MadeInGermany).
But again, what is unclear?

Laserbeak 04-23-2017 10:37 AM

My version (MacOS X vm_stat) prints out this:

Code:

Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free:                              15163.
Pages active:                          1778914.
Pages inactive:                        1702917.
Pages speculative:                        7243.
Pages throttled:                              0.
Pages wired down:                        674434.
Pages purgeable:                        115842.
"Translation faults":                736505527.
Pages copy-on-write:                  22345683.
Pages zero filled:                    388912589.
Pages reactivated:                      1824966.
Pages purged:                          1109523.
File-backed pages:                      692054.
Anonymous pages:                        2797020.
Pages stored in compressor:              569208.
Pages occupied by compressor:            13530.
Decompressions:                        3094397.
Compressions:                          4877052.
Pageins:                              11044324.
Pageouts:                                52868.
Swapins:                                394575.
Swapouts:                                657836.
______________________________________

So the output of different vmstats on different Unix versions appears different, I don't think this script would be helpful on my machine at all.

pan64 04-23-2017 11:05 AM

yes, you are right, this perl script and this vmstat are not really compatible (with each other)


All times are GMT -5. The time now is 03:48 PM.