Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

So I have a bash script that creates logs like this:

traceroute to -------, 30 hops max, 60 byte packets
1  router.Belkin (192.168.2.1)  2.275 ms  2.263 ms  2.249 ms
2  -------  16.961 ms  21.060 ms  21.069 ms
3  -------  21.025 ms  21.009 ms  20.996 ms

I would like to filter out the ms part and see if any of them are above 350ms. If any are, the whole log file would be sent to my email with subject "..." and body "..." any help?
Thank you!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Here is a script. Provide the log file name as the first argument. This checks for any time over 350ms. If such a time is found, it sends email.

#!/bin/sh
logfile="$1"
flag="$(awk -v RS=" " '$1 == "ms" && last > 350 {flag=1} {last=$1} END{print flag}' "$logfile")"
[ "$flag" ] && mail you@host -s "Over 350ms Report for $logfile" <"$logfile"

This does require that your system has mail properly installed and configured.

Combined Script

The above can be combined with the script in Script won't loop through files like planned as follows:

#!/bin/sh
n=
while true
do
    fname=~/"Scripts/logs/trace$n.log"
    [ -f "$fname" ] || break
    n=$(($n+1))
done
traceroute google.com >"$fname"
flag="$(awk -v RS=" " '$1 == "ms" && last > 350 {flag=1} {last=$1} END{print flag}' "$fname")"
[ "$flag" ] && mail you@host -s "Over 350ms Report for $fname" <"$fname"

Cooperative Form

Suppose that the script in Script won't loop through files like planned is running separately. Then, a possible script to inspect the files created by it for >350ms delays would be:

#!/bin/sh
for logfile in ~/Scripts/logs/trace*.log
do
    flag="$(awk -v RS=" " '$1 == "ms" && last > 350 {flag=1} {last=$1} END{print flag}' "$logfile")"
    [ "$flag" ] && mail you@host -s "Over 350ms Report for $logfile" <"$logfile"
done

Cooperative form with move and delete

#!/bin/sh
otherdir=~/"Scripts/logs-with-long-delays/"
for logfile in ~/Scripts/logs/trace*.log
do
    flag="$(awk -v RS=" " '$1 == "ms" && last > 350 {flag=1} {last=$1} END{print flag}' "$logfile")"
    if [ "$flag" ]
    then
        mail you@host -s "Over 350ms Report for $logfile" <"$logfile"
        mv "$logfile" "$otherdir"
    else
        rm "$logfile"
    fi
done
share|improve this answer
    
How do I make it work with this? superuser.com/questions/789383/… –  QuyNguyen2013 Jul 29 at 0:57
    
@Autospamfighter See updated answer. –  John1024 Jul 29 at 1:09
    
Would there be a way to separate the scripts for the combined script so their function is same? Or do I just need to remove the "traceroute google.com >"$fname"" part? –  QuyNguyen2013 Jul 29 at 1:12
    
@Autospamfighter OK. I added a separated version. –  John1024 Jul 29 at 1:29
1  
Let us continue this discussion in chat. –  QuyNguyen2013 Jul 29 at 2:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.