Find 350ms in traceroute logs?

0

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!

QuyNguyen2013

Posted 2014-07-28T22:06:07.590

Reputation: 499

Answers

1

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

John1024

Posted 2014-07-28T22:06:07.590

Reputation: 13 893

How do I make it work with this? http://superuser.com/questions/789383/script-wont-loop-through-files-like-planned

– QuyNguyen2013 – 2014-07-29T00:57:57.527

@Autospamfighter See updated answer. – John1024 – 2014-07-29T01:09:07.007

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 – 2014-07-29T01:12:48.827

@Autospamfighter OK. I added a separated version. – John1024 – 2014-07-29T01:29:08.500

So what would the body of this script be? And can I add the file as an attachment? And could the script delete files that aren't over 350ms? – QuyNguyen2013 – 2014-07-29T01:31:17.523

@Autospamfighter As it is written, the body of the email would be the contents of the log file. – John1024 – 2014-07-29T01:34:12.410

Ok, what about the delete function? – QuyNguyen2013 – 2014-07-29T01:34:55.520

Do you want to delete every log file after it is inspected? If so, just add the line rm -f "$logfile" to the bottom of the script. – John1024 – 2014-07-29T01:37:19.393

1

Let us continue this discussion in chat.

– QuyNguyen2013 – 2014-07-29T02:13:16.780