[SOLVED] Linux Script that takes a part of a log file and emails it
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Linux Script that takes a part of a log file and emails it
Hello
I'm new to linux (specifically to ubuntu server edition 10.10) so excuse me for my lack of knowledge in linux scripting language. Anyhow, I work for a company that does remote computer support, we use VNC protocol to help our clients. I installed a VNC repeater that allows my clients to connect to me going through all firewalls and port forwarding. The linux VNC repeater outputs all connection information to /var/log/vnc.log and looks something like
Code:
UltraVnc Linux Repeater version 0.14
UltraVnc Tue Mar 22 03:37:02 2011 > routeConnections(): starting select() loop, terminate with ctrl+c
UltraVnc Tue Mar 22 03:37:12 2011 > acceptConnection(): connection accepted ok from ip: 55.555.555.55
UltraVnc Tue Mar 22 03:37:25 2011 > acceptConnection(): connection accepted ok from ip: 55.555.555.55
UltraVnc Tue Mar 22 03:37:28 2011 > cleanUpAfterRepeaterProcExit(): closing connection (server=5, viewer=6)
^CUltraVnc Tue Mar 22 03:37:36 2011 > main(): relaying done
I need a script that reads this log file every so often (30 seconds to 5 minutes) and sends an email when an connection has been accepted. I looked into reading log files and got this so far
Code:
LOG=/var/adm/sqllog
while true
do
tail -100 $LOG | grep "ORA" > /dev/null
if [ $? -eq 0 ];then
mail -s "Accepted connection in $LOG" "me@host.com"
exit
fi
done
this only a start, I need the scipt to take only accepted connections. Could someone help a noob out!
VERY much appreciated!
Thanks,
Phillip K
P.S. FYI I'm doing this b/c I want the email sent to my cell provider that will send a SMS to my phone (incase anybody wanted to do the same)
To run a script or program in the "background," you can use the & after the command to run it. And as far as constantly checking the log file, are you looking to use a loop or to run it every X seconds or minutes?
To run a script or program in the "background," you can use the & after the command to run it. And as far as constantly checking the log file, are you looking to use a loop or to run it every X seconds or minutes?
I think that running a loop is better, I need to know right away when a customer connects to our servers. I tried creating a while loop script in perl with no success, your help is GREATLY appreciated!
#!/bin/bash
while [ 1 = 1 ]
do
grep -e "acceptConnection():" /var/log/vnc.log | tail -1 | mail -s "subject" email@email.com
done
Of course that is only an example. You just need to modify it and do the check BEFORE sending the email, otherwise doing that without changing anything up is going to bomb your mail account.... And if you don't notice, that script won't ever break until you break it manually.
#!/bin/bash
while [ 1 = 1 ]
do
grep -e "acceptConnection():" /var/log/vnc.log | tail -1 | mail -s "subject" email@email.com
done
Of course that is only an example. You just need to modify it and do the check BEFORE sending the email, otherwise doing that without changing anything up is going to bomb your mail account.... And if you don't notice, that script won't ever break until you break it manually.
Tested the above code and yes it does bomb the mail account like you said, the hardest part is finding a way to "check if it sent the last notice, if it did do not send again" I got the logic behind it, using an if statement inside the while loop stating that "if that line exists and has not been already sent then do mail -s, otherwise ignore and do not send". I just need help writing that into code, sorry I just don't know bash/linux code I'm trying to google examples to figure this out but having trouble.
Corp769 if you can help me out for last part of code, then I'll be grateful, I'll even consider donating, sorry if I seem lazy to you, its not that I'm lazy in trying to figure this out its just that I have no resources in terms of figuring this out. I'm learning using the eBook provided on this site.
P.S. Code looks great so far, I tried using tail -f but that just repeats the whole log file in one email
I tried logwatch and other utilities like logcheck and swatch but had trouble configuring them, I tried to start them and gave me an error so I unistalled it... just looking for a quick and dirty way to send the last occurance of a line into an email from a log file.
#!/bin/sh
tail -F -q /var/log/vnc.log | \
grep --line-buffered 'acceptConnection():' | \
while read -r line ; do
# send each line by mail
echo "$line" | mail -s "subject" email@email.com
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.