Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created February 23, 2011 16:22
Show Gist options
  • Save revolunet/840649 to your computer and use it in GitHub Desktop.
Save revolunet/840649 to your computer and use it in GitHub Desktop.
output some graphs from rsnapreport.pl output (google charts)
#!/usr/bin/python
#
#
# use pygooglecharts from http://pygooglechart.slowchop.com/
#
# just pipe rsnapreport.pl to this python script to get beautiful graphs with rsnapshot statistics
# send the email report as HTML to get your graphs embedded
#
# example crontab :
# 0 23 * * * root /usr/bin/rsnapshot daily 2>&1 | /home/juju/scripts/rsnapreport.pl | python /home/juju/scripts/rsnapshot-graphs.py | sendmail -t
#
#
import sys
data=sys.stdin.read()
import re
rawstr = r"^([^\s]+)\s+(\d+)\s+(\d+)\s+([\d\.]+)\s+([\d\.]+)\s+([\d\.]+) sec?o?n?d?s?\s+([\d\.]+) seco?n?d?s?\s*$"
reg = re.compile(rawstr, re.MULTILINE)
match_obj = reg.findall(data)
datas = {}
for m in match_obj:
path, files, transfered, size, transfered_size, time1, time2 = m
if path.find(':')>-1:
root, path = path.split(':')
else:
root = 'localhost'
#path = path
if not datas.has_key(root):
datas[ root ] = {
'files':0
,'transfered':0
,'size':0
,'transfered_size':0
,'time1':0
,'paths':{}
}
datas[ root ]['paths'][ path ] = {
'files':files
,'transfered':transfered
,'size':size
,'transfered_size':transfered_size
,'time1':time1
}
datas[ root ]['files'] += float(files)
datas[ root ]['transfered'] += float(transfered)
datas[ root ]['size'] += float(size)
datas[ root ]['transfered_size'] += float(transfered_size)
datas[ root ]['time1'] += float(time1)
def filterDict(inDict, key):
labels = inDict.keys()
values = [int(datas[k][ key ]) for k in labels]
labels = ['%s (%s) '% (labels[i], values[i]) for i,v in enumerate(values)]
return labels, values
def getChart(key):
labs, vals = filterDict(datas, key)
# pie total sizes/files/transfered/time per host and path
from pygooglechart import PieChart3D
chart = PieChart3D(600, 150)
chart.add_data( vals )
chart.set_pie_labels( labs )
chart.set_title( '%s by HOST' % key )
return chart.get_url()
html = ""
for i in ['files', 'transfered', 'size', 'transfered_size', 'time1']:
html += "<img src='%s'><br><br>\n\n" % getChart( i )
print """From: admin@revolunet.com
To: admin@revolunet.com
Subject: RSNAPSHOT REVOLUNET DAILY
Mime-Version: 1.0
Content-Type: text/html
"""
print "<html><pre>"
print data
print "</pre>"
print ''
print html
print "</html>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment