July archive
Logwatch and Gentoo
Today I have upgraded logwatch package on my VPS server to the latest version (7.1).
Most noticeable change in this version comparing to previous (6.0.2) installed on my system – whole new directory structure (see Details on how to create your own filter, create local customizations, etc for more details).
Logwatch is very useful tool if you want to know what your server is doing the whole day. It provides nice summary of log of every service, like list of emerged packages, number of mails sent by your mail server, number of failed authentication attempts through SSH, summary of served by apache files, free disk space, information about possible attacks on your server by using known hacks and a lot more.
Unfortunately default configuration of just installed package is not compliant with default logfiles used by gentoo and some services are missing in result report. logwatch provides easy way to override default configuration in /etc/logwatch/conf/override.conf file. Below is file that I am using on my gentoo server:
logwatch: Detail = High
logfiles/cron: LogFile = messages
logfiles/cron: Archive = messages.*.gz
logfiles/cron: *OnlyService = cron
logfiles/http: LogFile = apache2/*access_log
logfiles/http: Archive = apache2/*access_log.*.gz
logfiles/maillog: LogFile = messages
logfiles/maillog: Archive = messages.*.gz
Posted by ksh on July 22, 2006 | 4 comments | gentoo, linux
Django Blog: Notifying about new comments by email
Every blogger is interested in comments for his/her blog posts. So do I. Unfortunately there are no so much comments on my blog (only two), but I hope there will be more in future :)
But how could the author of the blog be notified about new comments? One possibility is to create feed for new comments and subscribe to it using your favourite feed reader (f.e. Google Reader). In fact I had been using this approach until recently. But I think that more convenient approach is to be notified by email. Now I am going to describe how I implemented this functionality in my blog.
The problem seems to be simple – just send email after comment object is saved to database. But I am using django.contrib.comments application and can not modify its code.
Fortunately django provides way to hook into process of the object saving. Internally Django (or more strictly speaking post magic-removal version of Django) uses PyDispatch framework – multiple-producer-multiple-consumer signal-dispatching system :) Despite this complex name idea of this framework is really simple: you can produce signals using following method
dispatcher.send(signal=Any, sender=Anonymous,
*args, **kwargs)
and register listeners for the signals using:
dispatcher.connect(receiver, signal=Any, sender=Any,
weak=True)
Check sources of django.dispatch package for more details.
Django ORM framework supports following signals defined in django.db.models.signals package:
| signal | when given signal is sent | args | kwargs |
| pre_init | before model object initialization (from constructor) | positional arguments to init | keyword arguments to init |
| post_init | after object is initialized | None | instance = self |
| pre_save | just before saving object to database | None | instance = self |
| post_save | after saving object to database | None | instance = self |
| pre_delete | just before deleting object from database | None | instance = self |
| post_delete | after deleting object from database | None | instance = self |
For all these signals model class serves as sender.
Now our task becomes very easy – write and register post_save signal listener for FreeComment class:
from django.db import models
from django.db.models import signals
from django.dispatch import dispatcher
from django.core.mail import send_mail
from django.template import Context, loader, Template,
TemplateDoesNotExist
from django.contrib.comments.models import FreeComment
# send mail on comment
def send_comment_by_mail(instance):
comment = instance
if comment.content_type.model_class() != Entry:
return
entry = Entry.objects.get(id__exact = comment.object_id)
# templates for mail subject and body
try:
subject_tmp = loader.get_template("free_comment_subject")
except TemplateDoesNotExist:
subject_tmp = Template('New comment for entry \
"{{ entry.title }}" by "{{ comment.person_name }}"')
try:
body_tmp = loader.get_template("free_comment_body")
except TemplateDoesNotExist:
body_tmp = Template('{{ comment.comment }}')
# send email to the user
ctx = Context({'entry': entry, 'comment': comment})
subject = subject_tmp.render(ctx).decode('utf-8').strip()
body = body_tmp.render(ctx).decode('utf-8')
entry.author.email_user(subject, body)
# connect signal
dispatcher.connect(send_comment_by_mail,
sender = FreeComment,
signal = signals.post_save)
Posted by ksh on July 1, 2006 | 10 comments | development, django