Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

So I have two files, and if a new line appears on either of these files, I'd like to receive an IM (preferably jabber or gTalk) containing the contents of that line. Do you guys have any suggestions for a Linux daemon or something that could do that?

share|improve this question
add comment

2 Answers

up vote 11 down vote accepted

If you are logging trough syslog, Metalog has support to execute a command whenever a message matching some criterion gets logged. Otherwise, you can use tailf to watch for new lines in a log file.

sendxmpp is a small perl script to send XMPP messages (possibly already available as a package for your favorite distribution)

You could stitch those two together with a shell script without too much difficulty. For the metalog case, create a script like this one:

#!/bin/sh
echo $* |sendxmpp your-xmpp-id@gmail.com

And add command = /path/to/script.sh to the relevant section of metalog.conf

For the tailf case, you could try something like this, run in a persistent way:

tailf /var/log/file-to-watch.log |(while true; do read M; echo $M | sendxmpp recipient@gmail.com; done)

sendxmpp needs a valid XMPP account, see the man page for how to configure the account to be used.

(from my experience, XMPP-delivered error messages tend to become quite annoying if they're too frequent...)

share|improve this answer
    
Hilarious. I just found sendxmpp and set something like this up. Hadn't heard of tailf though, thanks. –  icco Jul 30 '10 at 20:42
    
Side note: In case of problems, check the buffering behavior of your pipe! (If you're greping, you probably have to add --line-buffered to the incantation for messages to show up.) I just spent a good hour trying to find out why messages appeared haphazardly or not at all. –  nobody Jul 13 '12 at 4:07
add comment

I made that little python script. You can use it as a starting point

import xmpp, os, time

login     = 'Your.Login' # @gmail.com
pwd       = 'YourPassword'
recipient = 'YourFriend@gmail.com'
logfile   = "/home/myself/test.log"

def sendmsg(text):
  global login, pwd, recipient
  cnx = xmpp.Client('gmail.com')
  cnx.connect( server=('talk.google.com',5223) )
  cnx.auth(login,pwd, 'botty')
  cnx.send( xmpp.Message( recipient , text ) )

oldsize = newsize = os.path.getsize(logfile)
while True:
  newsize = os.path.getsize(logfile)
  if newsize != oldsize:
    f = open(logfile)
    f.seek(oldsize, os.SEEK_SET)
    s = f.read()
    if s[-1] == '\n':
      sendmsg(s)
      oldsize = f.tell()
    f.close()
  time.sleep(10)

I used information on that page for connecting xmpppy to Google Talk.

share|improve this answer
    
Does this require any Python dependancies or is XMPP included with Base? –  Tom Jul 30 '10 at 20:38
    
You have to install it from xmpppy.sourceforge.net –  Alexandre Jasmin Jul 30 '10 at 20:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.