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!

1 Answer 1

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
9

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .