LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2011, 03:00 AM   #1
fixnode
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Rep: Reputation: 0
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)
 
Old 03-23-2011, 07:13 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
You could use cron to check every X minutes, and sendmail or ssmtp to send it via email.

Edit - Oops, didn't see the mail part of your script.
 
Old 03-23-2011, 03:30 PM   #3
fixnode
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by corp769 View Post
You could use cron to check every X minutes, and sendmail or ssmtp to send it via email.

Edit - Oops, didn't see the mail part of your script.
can anybody help me code this?, I'm new to programming and new to Linux!

greatly appreciated,

Thanks,
Phillip K
 
Old 03-24-2011, 12:34 PM   #4
fixnode
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Ok so I got this line:
Code:
grep -e "acceptConnection():" /var/log/vnc.log | tail -1 | mail -s "subject" email@email.com
can somebody help put this in a script that runs in the backround and constantly checks the log file?

PLEASE HELP


Thanks,
Phillip K
 
Old 03-24-2011, 01:47 PM   #5
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
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?
 
1 members found this post helpful.
Old 03-24-2011, 10:11 PM   #6
fixnode
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
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!

Thanks,
Phillip K
 
Old 03-25-2011, 02:48 AM   #7
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
To come up with something quick and dirty:
Code:
#!/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.
 
Old 03-25-2011, 02:15 PM   #8
fixnode
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by corp769 View Post
To come up with something quick and dirty:
Code:
#!/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

Thanks again,
Phillip K
 
Old 03-25-2011, 02:21 PM   #9
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Perhaps logwatch can do what you want.
http://www.debianhelp.co.uk/logwatch1.htm

Kind regards
 
Old 03-25-2011, 09:37 PM   #10
fixnode
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by repo View Post
Perhaps logwatch can do what you want.
http://www.debianhelp.co.uk/logwatch1.htm

Kind regards
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.

Thanks though

Phillip K
 
Old 03-27-2011, 02:12 PM   #11
fixnode
LQ Newbie
 
Registered: Mar 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Nevermind I figured it out!

Code:
#!/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
hope that helps anyone out! thanks again!

Pk
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Kernel Log: Coming in 2.6.38 (Part 2) – File systems LXer Syndicated Linux News 0 02-18-2011 04:30 PM
LXer: Kernel Log: Coming in 2.6.37 (Part 2) - File systems LXer Syndicated Linux News 0 12-09-2010 03:10 PM
[SOLVED] Need some help reading part of a log file agangsto Linux - General 5 07-09-2010 02:14 AM
LXer: Kernel Log: Coming in 2.6.34 (Part 2) - File Systems LXer Syndicated Linux News 0 04-23-2010 04:21 PM
script to get a part of line from a file mystical dervish Programming 7 05-11-2007 02:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration