summaryrefslogtreecommitdiff
path: root/planet/logmailer.py
blob: ca20c86466cd446c413a9d06f56807460f18c525 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# vim: ai ts=4 sts=4 sw=4
"""PostgreSQL Planet Aggregator

This file contains the functions to email a report of failed fetches
by reading the aggregator log table in the database.

Current version just sends a single summary report. A future enhancement
could be to send reports directly to individual blog owners.

Copyright (C) 2009 PostgreSQL Global Development Group
"""

import psycopg2
import smtplib
import email.Message
import ConfigParser

class LogChecker(object):
	def __init__(self, cfg, db):
		self.cfg = cfg
		self.db = db
		
	def Check(self):
		c = self.db.cursor()
		c.execute("""
	SELECT name,info,count(*) as num FROM planet.aggregatorlog
	INNER JOIN planet.feeds ON feed=feeds.id 
	WHERE success='f' AND ts > CURRENT_TIMESTAMP-'24 hours'::interval
	GROUP BY name,info
	HAVING count(*) > %s
	ORDER BY num,name;
""" % self.cfg.get('notify','minerrors'))

		if c.rowcount > 0:
			s = """
One or more of the blogs fetched in the past 24 hours caused an error
as listed below.

"""
			last = ""
			for name,info,num in c:
				s += "(%3s times) %s (%s)\n" % (num, name, info)
			
			s += "\n\n"

			toAddr = self.cfg.get('notify','mailto')
			fromAddr = self.cfg.get('notify','mailfrom')
			
			msg = email.Message.Message()
			msg['To'] = toAddr
			msg['From'] = fromAddr
			msg['Subject'] = 'Planet PostgreSQL error summary'
			msg.set_payload(s)
			
			
			smtp = smtplib.SMTP('127.0.0.1')
			smtp.sendmail(fromAddr, toAddr, msg.as_string())
			smtp.quit()
		
			
if __name__=="__main__":
	c = ConfigParser.ConfigParser()
	c.read('planet.ini')
	LogChecker(c, psycopg2.connect(c.get('planet','db'))).Check()