version up eggs.
[slapos.git] / stack / monitor / status2rss.py
1 import datetime
2 import PyRSS2Gen
3 import sys
4 import sqlite3
5 import time
6 import base64
7
8 # Based on http://thehelpfulhacker.net/2011/03/27/a-rss-feed-for-your-crontabs/
9
10 # ### Defaults
11 TITLE = sys.argv[1]
12 LINK = sys.argv[2]
13 db_path = sys.argv[3]
14 DESCRIPTION = TITLE
15 SUCCESS = "SUCCESS"
16 FAILURE = "FAILURE"
17
18 items = []
19 status = ""
20
21 current_timestamp = int(time.time())
22 # We only build the RSS for the last ten days
23 period = 3600 * 24 * 10
24 db = sqlite3.connect(db_path)
25 rows = db.execute("select timestamp, status from status where timestamp>? order by timestamp", (current_timestamp - period,))
26 for row in rows:
27 line_timestamp, line_status = row
28 line_status = line_status.encode()
29
30 if line_status == status:
31 continue
32
33 status = line_status
34
35 event_time = datetime.datetime.fromtimestamp(line_timestamp).strftime('%Y-%m-%d %H:%M:%S')
36
37 rss_item = PyRSS2Gen.RSSItem(
38 title = status,
39 description = "%s: %s" % (event_time, status),
40 link = LINK,
41 pubDate = event_time,
42 guid = PyRSS2Gen.Guid(base64.b64encode("%s, %s" % (event_time, status)))
43 )
44 items.append(rss_item)
45
46 ### Build the rss feed
47 items.reverse()
48 rss_feed = PyRSS2Gen.RSS2 (
49 title = TITLE,
50 link = LINK,
51 description = DESCRIPTION,
52 lastBuildDate = datetime.datetime.utcnow(),
53 items = items
54 )
55
56 print rss_feed.to_xml()