running vmstat every 300 millisecond

1

Is there any way to run vmstat, iostat, sar commands repeatedly with an interval of 300 milliseconds?

I know the defaults is 1 second but I want to run every 300 millisecond for system performance monitoring.

karthikeyan vijayan

Posted 2015-09-22T09:36:29.397

Reputation: 7

Answers

0

You could write a script which calls the command repeatedly with a 0.3 second interval:

  • Some systems (Linux, FreeBSD, Solaris for example) provide a command-level sleep command which accepts intervals smaller than 1 second. For those, it is possible to write an ordinary shell loop such as

    #!/bin/sh CMD=vmstat $CMD while : do $CMD | tail -n 1 sleep 0.3 done

    That is not in POSIX, of course.

  • Other scripting languages, such as Perl provide functions which can be used instead. For Perl, there is the usleep function in Time::HiRes, e.g.,
    #!/usr/bin/perl -w
    use strict;
    use Time::HiRes qw(usleep);

    our $CMD="vmstat";
    system($CMD);
    while (1) {
        system("$CMD | tail -n 1");
        usleep (300000);
    }

Thomas Dickey

Posted 2015-09-22T09:36:29.397

Reputation: 6 891

Just a note vmstat is "near real time" so the difference between 300/500/800 milliseconds might not actually be much. – Unfundednut – 2015-09-22T12:11:25.363

1But the accumulated error over several minutes would be noticeable (even after accounting for startup time of vmstat itself). – Thomas Dickey – 2015-09-22T12:18:28.583

-1

I tried this approch, rapping the vmstat in a while loop so that i could easliy break out when i had finished my test, then parsing the collected log file to csv for easy import for graphing. It worked, BUT vmstats 1st line was nearly alway the same so this was USELESS. I had to run vmstat manualy and collect the logs to a file then process them after.

#!/bin/bash
OUTPUT="/dropinbox/vmstat_$(hostname)_$(date +%Y-%m-%d_%H-%M).csv"

echo Starting Recording...
echo Press Q to exit.

# create blank temp file
echo '' > /tmp/vmstat.log

while true; do

    # -t time -a active and inactive memory reporting -n no repeate headers -S M size
    vmstat 1 1 -tanwS M >> /tmp/vmstat.log

    # In the following line -t for timeout, -N for just 1 character
    # -t 0.25 therefore loop will run 4 times a second.
    read -t 0.25 -N 1 input
    if [[ $input = "q" ]] || [[ $input = "Q" ]]; then
        # The following line is for the prompt to appear on a new line.
        echo Finshed
        break
    fi
done

# remove blank lines
sed -i '/^$/d' /tmp/vmstat.log
# remove headers
sed -i '/procs/d' /tmp/vmstat.log
# Keep 1 line of the 2nd headers
grep -m1 r /tmp/vmstat.log > /tmp/headers
tr -s ' ' , < /tmp/headers | sed s/,// > $OUTPUT
# remove all 2nd line headers
sed -i '/r/d' /tmp/vmstat.log
# process columns into csv output missing out column 18 (Date) as this got in myway
awk 'BEGIN { OFS = "," }!($18="")' /tmp/vmstat.log |tr -s ',' >> $OUTPUT
cat -vet $OUTPUT
echo finished saving to $OUTPUT

rhys stevens

Posted 2015-09-22T09:36:29.397

Reputation: 1