Help Reading from Command Like vmstat


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Reading from Command Like vmstat
# 1  
Old 11-17-2010
Question Help Reading from Command Like vmstat

I wanna to read the output of various commands like mpstat, vmstat, etc... but want to output this to a file with each line preceeded by a time stamp.

I've played around with various loops, shell redirection, custom descriptors and etc.. but can't seem to get something to work. I did this years ago and know it can be done but my scripting skills have gotten way rusty.

Anyone have any tricks for doing something like this?

Thx
# 2  
Old 11-17-2010
Step 1:
Create a perl script to display the time and the output.

Filename : dispTime.pl

Code:
#!/usr/bin/perl
while (<>) { print localtime() . ": $_"; }

Step2:
chmod 755 dispTime.pl

Step3:
Execute any command and pipe the above perl script. See the output now :-)

ex:
$ vmstat 1 5 | dispTime.pl
Wed Nov 17 10:12:06 2010: kthr memory page disk faults cpu
Wed Nov 17 10:12:06 2010: r b w swap free re mf pi po fr de sr m0 m1 m2 m9 in sy cs us sy id
Wed Nov 17 10:12:06 2010: 0 0 0 18427800 7747944 59 488 47 3 3 0 0 4 3 3 1 479 4262 1344 1 3 96
Wed Nov 17 10:12:07 2010: 0 0 0 9142184 449832 124 200 0 0 0 0 0 0 0 0 0 465 1334 1160 0 2 98
Wed Nov 17 10:12:08 2010: 0 0 0 9142184 449832 4 4 0 0 0 0 0 0 0 0 0 378 935 884 0 1 98
Wed Nov 17 10:12:09 2010: 0 0 0 9142184 449832 4 4 0 0 0 0 0 0 0 0 0 389 1035 889 0 1 98
Wed Nov 17 10:12:10 2010: 0 1 0 9142184 449832 4 4 0 0 0 0 0 1 1 1 0 421 7476 1529 1 10 90
# 3  
Old 11-17-2010
Would you like unix time, gregorian date-time, microseconds, delta microseconds? Simplest with min latency:
Code:
... | while [ 1 ]
 do
   l=`line`
  date '+%whatever'"$l"
 done

Commands that read stdin with a buffered FILE* often get the pipe data several K at a time, delayed. I have found that reading one byte at a time, like line does, overcomes this latency. Commands that write() one line at a time may not have this problem. Your UNIX may vary!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading from file bash command

Hello, I have a file in the following format id sample platform R1 R2 gene1 gene2 gene3 1 abc llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp asp 2 def llumina R1_001.fastq.gz R2_001.fastq.gz apoe prnpp 3 ghi llumina ... (3 Replies)
Discussion started by: nans
3 Replies

2. UNIX for Dummies Questions & Answers

Help with reading and calling a command in a script

The problem I am having now is calling and reading a command and The Main script reads the data file and passes the input to the two calculation scripts, and than output to a file. 1. The Main Script ----------------- input=inputfilepj3 output=outfilepj3 echo "*** Converting... (2 Replies)
Discussion started by: TheUnitedOSI
2 Replies

3. Shell Programming and Scripting

Error in reading the output from command

I getting error as test1.sh: syntax error at line 3: `do' unexpected while i was trying to run below code.. What was going wrong here? find /usr/tmp/SB/reports/ -type f -name *.rdf |read file;do echo "Copying $file to /usr/tmp/SB1" done (1 Reply)
Discussion started by: millan
1 Replies

4. Shell Programming and Scripting

help with reading command line into array

set -A title set -A author count=1 index=0 while do read -A ${title}?"Booktitle: " read -A ${author}?"Author(s): " (( count = count + 1 )) (( index = index + 1 )) done Hi... (1 Reply)
Discussion started by: bjhum33
1 Replies

5. Shell Programming and Scripting

Reading command options one by one

Hi, Just some questions on the script below...? Given: bash-2.03$ command -a option1 name1 name2 ParseOptions() { local Len=${#@} local Ctr=2 #always start at 2 local Name=() local Iter=0 while ; do if <- Is this correct? so I can get the $2... (2 Replies)
Discussion started by: h0ujun
2 Replies

6. Shell Programming and Scripting

egrep -w command by reading from file

Dear All, I want to get the rows from file1.txt whose first column is exactly as the numbers (not subset) from file1.txt. so, i have to read by line from file1.txt and search in file2.txt. So the first column of the output must be the same as the file1.txt. I used % egrep -w -f... (5 Replies)
Discussion started by: senayasma
5 Replies

7. UNIX and Linux Applications

Reading values from the command line

Hi I want to give the user the choice of whether or not they want to include a certain option when they run the script. This is my getops: while getopts " s: d: r f: e h " option do case $option in f ) dsxfile="$OPTARG";; d ) dbname="$OPTARG";; s ) dsn="$OPTARG";; r )... (0 Replies)
Discussion started by: ladyAnne
0 Replies

8. Shell Programming and Scripting

VMSTAT script / command help

Hello all, I know it can be done but don't know the command. I would like to run vmstat via cron, I don't know a good interval and don't want huge logs but want stats pretty often. Please someone who uses vmstat via cron let me know what your script looks like. Thank you (11 Replies)
Discussion started by: komputersman
11 Replies

9. UNIX for Dummies Questions & Answers

Does command for reading from memory exist?

Does exist in unix any command which could read data from any memory address or port and return result on screen? (2 Replies)
Discussion started by: linuxchemist
2 Replies

10. UNIX for Dummies Questions & Answers

Reading the output of df command

Can any body please explain the following df command to me:filesystem kbytes used avail capacity mounted on /dev/root 6474195 2649052 3825143 41% / /dev/stand 24097 5757 18340 24% /stand /proc 0 0 0 ... (7 Replies)
Discussion started by: nervous
7 Replies
Login or Register to Ask a Question