Grabbing value from command output and monitoring for changes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grabbing value from command output and monitoring for changes
# 1  
Old 08-23-2010
Grabbing value from command output and monitoring for changes

Hi all,

Very new to shell scripting so appreciate some help!

There is a process count that I need to monitor, I have the AIX command that gives this value and I've cleaned it up with grep/awk so it only spits out the value I'm interested in:

echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------' | awk '{print $7}'

I need to create something to monitor this value (numerical) at given time intervals and then write to a log file when it changes, along with a timestamp.

I got something that'll put this into a file:

#!/bin/sh
echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------' | awk '{print $7}' | while read newvalue;
do
echo $newvalue>>test.log
done

..but now I'm stuck reading that file and comparing it to the new value. Or perhaps there's a way to continuously monitor the value and do things more efficiently?

Any help or suggestions would be much appreciated!!
Thanks,
Monty
# 2  
Old 08-23-2010
Try something like this:

Code:
last_value=x
log_file=/tmp/log.file
while true
do
   your-command-string | read new_value
   if [[ $last_value != $new_value ]]
   then
       echo "$(date) $new_value" >>$log_file
       last_value=$new_value
   fi
   sleep 300      # every 5 minutes
done

This assumes bash -- most modern UNIXes link /usr/bin/sh to bash -- the [[ expression on the if are much more efficient. If AIX doesn't link to bash, and/or ksh isn't available, you'll have to use if [...].
This User Gave Thanks to agama For This Post:
# 3  
Old 08-23-2010
Quote:
Originally Posted by agama
Try something like this:

Code:
last_value=x
log_file=/tmp/log.file
while true
do
   your-command-string | read new_value
   if [[ $last_value != $new_value ]]
   then
       echo "$(date) $new_value" >>$log_file
       last_value=$new_value
   fi
   sleep 300      # every 5 minutes
done

This assumes bash -- most modern UNIXes link /usr/bin/sh to bash -- the [[ expression on the if are much more efficient. If AIX doesn't link to bash, and/or ksh isn't available, you'll have to use if [...].
Thanks!! Looking good, however I think there's a problem with my command as it seems to be pulling blanks instead of the value I want. I put an echo $new_value in there to prove this and sure enough it's blank.

How do I use grep -vE (or something else if more appropriate) to strip and blanks from my command output and just catch the value itself?

Thanks!
Monty
# 4  
Old 08-23-2010
I assumed that your command ran a single time, though looking back at your example maybe it executes continuously.

Small changes:
Code:
last_value=x
log_file=/tmp/log.file

your-command-string | while read new_value
do
   if [[ $last_value != $new_value ]]
   then
       echo "$(date) $new_value" >>$log_file
       last_value=$new_value
   fi
done

The only catch to this is that your command needs to block otherwise you'll be executing a pretty tight loop and possibly taxing resources. My first example is preferred as it puts you in control of how often you execute the command that checks on the system state.

---------- Post updated at 21:22 ---------- Previous update was at 21:14 ----------

Quote:
Originally Posted by monty77
How do I use grep -vE (or something else if more appropriate) to strip and blanks from my command output and just catch the value itself?
I don't think it's the grep command. Can you post the output of the first few lines of just this part of your command:

Code:
echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------'

I suspect that maybe the line is bar separated and not space/tab separated and thus printing $7 might be producing null output because there is no field 7. If the output is bar separated, then you can change your awk command to supply -F "|" to specify the field separator.
This User Gave Thanks to agama For This Post:
# 5  
Old 08-23-2010
My command runs a single time, looks like this:

tmadmin - Copyright (c) 1996-1999 BEA Systems, Inc.
Portions * Copyright 1986-1997 RSA Data Security, Inc.
All Rights Reserved.
Distributed under license by BEA Systems, Inc.
Tuxedo is a registered trademark.
TMADMIN_CAT:199: WARN: Cannot become administrator.Limited set of commands available.

3

...I think there's a blank on top of the 3 that needs stripping somehow.

If I put an echo in your code:

Code:
#!/bin/sh
last_value=x
log_file=/tmp/log.file
while true
do
   echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------' | awk '{print $7}' | read new_value
   echo $new_value
   if [[ $last_value != $new_value ]]
   then
       echo "$(date) $new_value" >>$log_file
       last_value=$new_value
   fi
   sleep 300      # every 5 minutes
done

..it doesn't show the new_value it should have read from my command.

Thanks!
Monty
# 6  
Old 08-23-2010
Good -- a single shot command is much easier to deal with.

I miss read the grep -- realise now that you are ditching the 'name' line. If the value that you are interested in is always the last line, which it appears to be, then this will be better:

Code:
echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------' | tail -1| read new_value;

If there is more output following the value you want, but it is always line seven, then try this:
Code:
echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------' | awk 'NR==7 {print}' | read new_value;

Have a go with one of these and see if that works.
This User Gave Thanks to agama For This Post:
# 7  
Old 08-23-2010
Quote:
Originally Posted by agama
Good -- a single shot command is much easier to deal with.

I miss read the grep -- realise now that you are ditching the 'name' line. If the value that you are interested in is always the last line, which it appears to be, then this will be better:

Code:
echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------' | tail -1| read new_value;

If there is more output following the value you want, but it is always line seven, then try this:
Code:
echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------' | awk 'NR==7 {print}' | read new_value;

Have a go with one of these and see if that works.
Thanks again, either one of those might work but I went with this in the end:

Code:
echo "psc -i 10050 -s RELOAD_SERVICE" | tmadmin | grep -vE 'Name|------|'^$'' | awk '{print $7}'

..only thing that's not perfect now is that it (understandably) will always write a row to the log when you first fire it up. Any way to avoid this?

Thanks a million!!
Monty
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

2. Shell Programming and Scripting

Grabbing value from file and run command in ``

Hi ALL, How can I make a script take data from a file and execute the commands within `` in the file n put that that in a variable? for i in `cat file` do file=`grep -i key file` cp ${file} test done file /tmp/`date +y`log /tmp/unix`date +y`log (1 Reply)
Discussion started by: 3junior
1 Replies

3. Shell Programming and Scripting

Grabbing IP and zonename from multiline 'ifconfig' output

Hi There, I have a Solaris server that has a bunch of zones configured and I am trying to write a script that will take all interfaces other than the loopback ones (e.g. lo0:3 etc) and present them so that I can easily determine the zone that owns the IP So in the case of the following... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

4. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

5. Infrastructure Monitoring

AIX monitoring tools for graphical output

Hi , I am new for Aix i am using IBM AIX server in our org. I am using tomcat and JDK 1.6 for our own ERP software the data base was stored in another server (windows ) i want to monitor my AIX server with graphical output from another system it is possible please help me, any other... (7 Replies)
Discussion started by: krishna_vnr`
7 Replies

6. Solaris

Monitoring the output of 'top' command on hourly basis.

I need to capture the following data on an hourly basis through cronjob scheduling:- 1. load averages 2. Total no. of processes. 3. CPU state 4. Memory 5. Top 3 process details. All the above information is available through the command 'top'. But here we need to automate the same and... (4 Replies)
Discussion started by: subharai
4 Replies

7. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

8. UNIX for Dummies Questions & Answers

File Missing When Grabbing Files from SFTP Server using SCP Command

Hi, I have this problem where sometimes my files would go missing when I schedule my crontab to run the SCP command to get file from the SFTP server. My crontab will run the scripts at an interval of 3 minutes (between the two scripts) The following is the setting in my crontab. ... (1 Reply)
Discussion started by: gingervitus
1 Replies

9. UNIX for Dummies Questions & Answers

Grabbing a value from an output file

I am executing a stored proc and sending the results in a log file. I then want to grab one result from the output parameters (bolded below, 2) so that I can store it in a variable which will then be called in another script. There are more details that get printed in the beginning of the log file,... (3 Replies)
Discussion started by: hern14
3 Replies

10. UNIX for Dummies Questions & Answers

Grabbing result of sql command

Hi guys, Is there a way a script can run an SQL statement and dump the results into a variable which can then be used later in the script? Thanks. (3 Replies)
Discussion started by: hern14
3 Replies
Login or Register to Ask a Question