Newer
Older
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# banner.py
#
# Copyright 2015 CESNET z. s. p. o.
# Author Jakub Cegan cegan@ics.muni.cz
#
#
def main(args):
SVGNS = "http://www.w3.org/2000/svg"
# We set up path and names
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
banner_name = "banner.svg"
template_name = "banner-template.svg"
# We have DB credentials
user = "warden"
password = "w4rd3n&u53r"
database = "warden3"
host = "localhost"
db = MySQLdb.connect(host = host, user = user, passwd = password, db = database)
cursor = db.cursor()
cursor.execute('SELECT count(*) AS reader_count FROM clients WHERE clients.read <> 0 AND clients.valid <> 0 AND clients.test = 0;')
row = cursor.fetchone()
receivers = str(row[0])
cursor.execute('SELECT count(*) AS writer_count FROM clients WHERE clients.write <> 0 AND clients.valid <> 0 AND clients.test = 0;')
row = cursor.fetchone()
senders = str(row[0])
cursor.execute('SELECT sum(round(((data_length + index_length) / 1024 / 1024 / 1024), 2)) AS db_size FROM information_schema.tables WHERE table_schema = "warden3" AND table_name="events"')
row = cursor.fetchone()
database_size = str(row[0]) + ' GB'
cursor.execute('SELECT max(id) - min(id) AS event_count FROM events;')
row = cursor.fetchone()
events = str(row[0])
cursor.execute('SELECT max(id) AS last_id FROM events;')
row = cursor.fetchone()
last_event = str(row[0])
time = datetime.datetime.today().strftime("%Y-%m-%dT%H:%M:%S%Z")
xml_data = etree.parse(template_name)
# We search for element 'text' with id='tile_text' in SVG namespace
find_text = etree.ETXPath("//{%s}text[@id='database']" % (SVGNS))
find_text(xml_data)[0].text = database_size
find_text = etree.ETXPath("//{%s}text[@id='events']" % (SVGNS))
find_text(xml_data)[0].text = events
find_text = etree.ETXPath("//{%s}text[@id='senders']" % (SVGNS))
find_text(xml_data)[0].text = senders
find_text = etree.ETXPath("//{%s}text[@id='receivers']" % (SVGNS))
find_text(xml_data)[0].text = receivers
find_text = etree.ETXPath("//{%s}text[@id='latest']" % (SVGNS))
find_text(xml_data)[0].text = time
new_svg = etree.tostring(xml_data)
xml_data.write(banner_path + banner_name)
# We will not use pygal graphs for now
#chart = pygal.StackedLine(fill=True, style=CleanStyle, x_label_rotation=40, tooltip_border_radius=10) # Setting style here is not necessary
#chart.title = 'Events in last 24 hours'
#chart.x_labels = map(lambda d: d.strftime('%H:%M:%S'), reversed([base - datetime.timedelta(hours=x) for x in range(0, 24)]))
#chart.add('Event type A', [random.randint(0,5000) for r in xrange(24)])
#chart.add('Event type B', [random.randint(0,5000) for r in xrange(24)])
#chart.add('Event type C', [random.randint(0,5000) for r in xrange(24)])
#chart.add('Other types', [random.randint(0,5000) for r in xrange(24)])
#chart.render_to_file('chart_hours.svg') # Save the svg to a file
#chart = pygal.StackedLine(fill=True, style=CleanStyle, x_label_rotation=40, tooltip_border_radius=10) # Setting style here is not necessary
#chart.title = 'Events in last month'
#chart.x_labels = map(lambda d: d.strftime('%d. %m. %Y'), reversed([base - datetime.timedelta(days=x) for x in range(0, 31)]))
#chart.add('Event type A', [random.randint(0,5000) for r in xrange(31)])
#chart.add('Event type B', [random.randint(0,5000) for r in xrange(31)])
#chart.add('Event type C', [random.randint(0,5000) for r in xrange(31)])
#chart.add('Other types', [random.randint(0,5000) for r in xrange(31)])
#chart.render_to_file('chart_month.svg') # Save the svg to a file
return 0
if __name__ == '__main__':
import sys
import random
import datetime
import MySQLdb
from lxml import etree
#import pygal
#from pygal.style import CleanStyle
sys.exit(main(sys.argv))