So im trying to search for the most recent occurance of a string in the most recently updated .log file in a certain directory. The string i am searching for is a value, now if this value is greater than 800 i would like an email sent out with some text (blah blah blah).
This is what i have so far
ls -Art | tail -n 1| xargs tac | grep -m1 "Total number of clients: "
Running this command will output the following
19:55:51.815 Total number of clients: 338
xargs: tac: terminated by signal 13
As you can see the total number of clients is smaller than 800, so we dont need an email notification sent out. But in the instance of the value being greater than 800 we will need a notifcation sent.
I need to make this as a cronjob that runs every 3 minutes however, this seems to send an email out regardless if the value is larger or smaller than 800. I need it ONLY to email if the value is larger than 800, any ideas?
Linux 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
*I created a shell script and gave it execute access E.g. ./connectioncount.sh
*I havent set up the cron yet, once i get the shell script working i will implement as a cronjob
*The directory is in Genesyslogs\confserv
Hope that answers your questions.
Reading in, ive decided to rewrite the script as i prefer to use tac rather than awk as the file is quite large and the job will need to run every 3 minutes. Im half way there, Please see below
#!/bin/bash
fname=$(ls -Art | tail -n 1)
echo $fname
value=$(tac $fname | grep -m1 "Total number of clients:")
echo $value
All i need now is to capture the last numeric values which appear after Total number of clients: xxxx
If this value is greater than 800 than an email will be sent out.
Thanks.
---------- Post updated at 10:25 PM ---------- Previous update was at 10:24 PM ----------
Almost there, though i cannot seem to get the if statement working, it hangs on that particular command and i dont receive an email. Did i use the correct syntax?
#!/bin/bash
fname=$(ls -Art | tail -n 1)
echo $fname
value=$(tac $fname | grep -m1 "Total number of clients:")
echo $value
variable=${value##* }
echo $variable
if [ $variable -lt 800 ]; then
mailx -s "vpsi1ftc has exceeded the connection: $variable" mycompany@company.com.au
fi