Oh, yes. Look what I did today:
#!/usr/bin/env python
import sys, os, re, time, pxssh, wx
APACHE_HOST_USER = "root"
APACHE_HOST_PASSWD = "password" # root password of server on which Apache runs
APACHE_HOST_NAME = "localhost" # host name of server on which Apache runs
APACHE_ACCESS_LOG = "/var/log/apache2/access.log"
APACHE_ERROR_LOG = "/var/log/apache2/error.log"
POLL_INTERVAL = 60 * 1
LOG_REGEX = re.compile("([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) - - \[([^\]]*)\] \"([^\"]*)\" ([0-9]{3})")
class popup(wx.Frame):
def __init__(self, ip_addr, date, uri, status):
wx.Frame.__init__(self, None, wx.ID_ANY, title="Apache Log", size=(500,80))
self.info = wx.StaticText(self, -1, "IP address: %s; date: %s; uri: \"%s\"; status: %s" %\
(ip_addr, date, uri, status), wx.Point(10,10))
self.dismiss = wx.Button(self, 10, "Close", wx.Point(10,30))
wx.EVT_BUTTON(self, 10, self.OnClick)
self.Show(True)
def OnClick(self, event):
self.Close(True)
def main():
"""
The daemon. Examines last line of apache access. Displays window if IP address
is different to last stored one.
"""
LAST_IP_ADDR = ""
while 1:
s = pxssh.pxssh()
if not s.login(APACHE_HOST_NAME, APACHE_HOST_USER, APACHE_HOST_PASSWD):
pass
else:
s.sendline("tail -1 %s" % APACHE_ACCESS_LOG)
s.prompt()
for line in s.before.split("\n"):
m = LOG_REGEX.match(line)
if m is not None:
ip_addr = m.group(1)
date = m.group(2)
uri = m.group(3)
status = m.group(4)
if ip_addr != LAST_IP_ADDR:
app = wx.App()
entry_info_window = popup(ip_addr, date, uri, status)
app.MainLoop()
LAST_IP_ADDR = ip_addr
break
s.logout()
time.sleep(POLL_INTERVAL)
if __name__ == "__main__":
forking code from Python Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
do the UNIX double-fork magic, see Stevens' "Advanced
Programming in the UNIX Environment" for details (ISBN 0201563177)
try:
pid = os.fork()
if pid > 0:
exit first parent
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)
do second fork
try:
pid = os.fork()
if pid > 0:
exit from second parent, print eventual PID before
print "Daemon PID %d" % pid
sys.exit(0)
except OSError, e:
print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
sys.exit(1)
start the daemon main loop
main()
Someone on a Python forum asked if it would be possible to write a program which monitored your Apache access log and displayed a message on your desktop whenever someone was viewing your site. It seems that the answer is "yes".