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
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.3931
Let us continue this discussion in chat.
– QuyNguyen2013 – 2014-07-29T02:13:16.780