Conditional mailing as command result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional mailing as command result
# 1  
Old 05-11-2009
Conditional mailing as command result

Hi !

I would like to be informed by mail when a ping conmand exceeds a certain avarage value.

This is the output of the ping command
rtt min/avg/max/mdev = 164.505/165.522/166.540/1.095 ms

I need to get the second value (165.522) and if greater than 200, send a mail.

This should repeat forever every 1 minute.
What's the best way to do that ?

Thanks
# 2  
Old 05-11-2009
If this is so important that it requires testing every minute, mail is a poor choice. Consider sending an email to a pager for the person on-call. Otherwise you will come back in from a weekend to thousands of emails....

you need a shell script:
Code:
#!/bin/ksh
typeset -i value=0
ping_it()
{
    ping somenode -n 1 | awk -F'\/' {print $5} ' | read value
    export value
}

ping_it
[[ $value -gt 200 ]]  && ping_it
if [[ $value -gt 200 ]] ; then
     echo 'somenode failed ping test' | mailx -s 'ping failure' me@myaddress.com
fi

call your script ping_test.sh or something and create an entry in your crontab for it that runs every minute. see man crontab for examples.
# 3  
Old 05-11-2009
Quote:
Originally Posted by gadile
I would like to be informed by mail when a ping conmand exceeds a certain avarage value.

This is the output of the ping command
rtt min/avg/max/mdev = 164.505/165.522/166.540/1.095 ms

I need to get the second value (165.522) and if greater than 200, send a mail.

What you actually need is the integer portion of that number: 165

The shell (execpt for ksh93) cannot compare floating point numbers.
Quote:
This should repeat forever every 1 minute.
What's the best way to do that ?

Thanks

Put the following script into a file, make it executable, place the name of the script in your crontab file, e.g.:

Code:
* * * * * /path/to/script

Code:
ping="rtt min/avg/max/mdev = 164.505/165.522/166.540/1.095 ms"

temp=${ping#*=*/}  ## remove everything to the left of the average
avg=${temp%%/*}    ## remove everything to the right of the average
avg=${avg%.*}      ## extract the integer

if [ $avg -gt 200 ]
then
   printf "Ping time: %s\n" "$avg" |
    mail -s "Ping time exceeded" someone@somewhere.tld
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Grep command giving different result for different users for same command

Hello, I am running below command as root user #nodetool cfstats tests | grep "Memtable switch count" Memtable switch count: 12 Where as when I try to run same command as another user it gives different result. #su -l zabbix -s /bin/bash -c "nodetool cfstats tests | grep "Memtable switch... (10 Replies)
Discussion started by: Pushpraj
10 Replies

3. UNIX for Dummies Questions & Answers

Bashrc File - Conditional Command Execution?

Hello All, I was wondering if there is a way to execute a command in my ".bashrc" file based on how I logged into the PC? I was thinking maybe there is a way to check how the user (*myself) logged in, maybe somehow with the who command along with something else, but I'm not sure... I know I... (7 Replies)
Discussion started by: mrm5102
7 Replies

4. Programming

Python Conditional Statements Based on Success of Last Command

In shell scripting, I can create a conditional statement based on the success or failure (exit status)of a command such as: pinger() { ping -c 2 $remote_host >/dev/null 2>&1 ping_stat=$? } pinger if ]; then echo "blahblahblah" exit 0 fi how is this done using Python using... (3 Replies)
Discussion started by: metallica1973
3 Replies

5. Shell Programming and Scripting

Conditional grep command to search entire file

Let me give you a complete example what I am trying to achieve. 1. Below is the log file structure where I need 2,5 and 14th column of the logs after grepping through the linkId=1ddoic. Log file structure:- abc.com 20120829001415 127.0.0.1 app none11111 sas 0 0 N clk Mozilla/5.0... (3 Replies)
Discussion started by: kmajumder
3 Replies

6. UNIX for Advanced & Expert Users

Advance conditional grep command

Hi guys, I need your help to use advance grep command, In my example below, I have 5 container which has some information on it, and I need to grep specific word "PLMNCode=454F00" which will not only grep the line contains that word, but I need to print the output for the whole container. Input... (6 Replies)
Discussion started by: hapalon
6 Replies

7. Shell Programming and Scripting

Conditional checking for VMWARE command

Hello, I have to runt he following VMWARE script to take a snap shot of my machine: vmware-cmd -v /vmfs/volumes/44e9c20f-71ded630-3ac2-00137221e12a/orion/orion.vmx createsnapshot Weekly_Backup I need to check if successfully executes or not so I thought I would put it in a IF STATEMENT ... (2 Replies)
Discussion started by: mojoman
2 Replies

8. Shell Programming and Scripting

mailing customized and formatted ls -lt command output

I'm trying to write a script to email the output of 'ls -lt' command that are 30 days old along with headers for eg. like owner, date, timestamp and a portion of some special character files like 'slfpay$/#:032508AA' in /home/test directory, I just want the numbers from the last field ($9), also... (3 Replies)
Discussion started by: mbak
3 Replies

9. UNIX for Advanced & Expert Users

mailing with attachments in mail command in HP-UX Release 11i

Hi Can some one tell me how to send a mail with body and file attached in a shell sript, in HP-UX Release 11i. i did code in AIX and it works fine but i guess here in HP-UX release.. there's some syntax change which i;m not able to make out.. TIA, Ronnie (1 Reply)
Discussion started by: rosh0623
1 Replies

10. Shell Programming and Scripting

Looping and conditional branching on the command line

I am piping STDOUT from commands such as ifconfig and dmesg through grep, sed and awk to get the information I need. I need to now perform some looping and branching now and have been trying to figure out how to do this on the command line. You may ask "Why the command line? - Why not put it... (2 Replies)
Discussion started by: karlgo
2 Replies
Login or Register to Ask a Question