Skip to content

Instantly share code, notes, and snippets.

@tgerla
Created August 3, 2012 15:36
Show Gist options
  • Select an option

  • Save tgerla/3248759 to your computer and use it in GitHub Desktop.

Select an option

Save tgerla/3248759 to your computer and use it in GitHub Desktop.
Fetch reports from Eucalyptus via command-line
#!/usr/bin/python
#
# generate_report.py
# Command-line utility to generate reports
#
import sys
import time
import subprocess
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-t", "--type", dest = "report_type",
help = "type of report: instance, storage, or s3")
parser.add_option("-p", "--password", dest = "admin_pwd",
help = "cloud eucalyptus/admin password")
parser.add_option("-b", "--begin", dest = "begin_date",
help = "begin date (mm/dd/yyyy)")
parser.add_option("-e", "--end", dest = "end_date",
help = "end date (mm/dd/yyyy)")
parser.add_option("-g", "--groupBy", dest = "group_by", default = "none",
help = "group by: zone, cluster, account, user, or none (default: none)")
parser.add_option("-c", "--criterion", dest = "criterion", default = "account",
help = "report criterion: zone, cluster, account, user (default: account)")
parser.add_option("-f", "--format", dest = "format", default = "csv",
help = "report format: html, csv, or pdf (default: csv)")
parser.add_option("-o", "--output", dest = "output_file",
help = "output filename")
(options, args) = parser.parse_args()
if options.report_type not in ('instance', 'storage', 's3'):
print "%s is not a valid report type. Valid report types: instance, storage, s3" % options.report_type
parser.print_help()
sys.exit(1)
if options.format not in ('html', 'csv', 'pdf'):
print "%s is not a valid output format. Valid output formats: html, csv, pdf (default: csv)" % options.format
parser.print_help()
sys.exit(1)
if options.criterion not in ('zone', 'cluster', 'account', 'user'):
print "%s is not a valid report critieron. Valid report criteria: zone, cluster, account, user (default: account)" % options.criterion
parser.print_help()
sys.exit(1)
if options.group_by not in ('zone', 'cluster', 'account', 'user', 'none'):
print "%s is not a valid report grouping. Valid report grouping: zone, cluster, account, user, none (default: none)" % options.group_by
parser.print_help()
sys.exit(1)
if not options.output_file:
print "You must specify an output file."
parser.print_help()
sys.exit(1)
if options.begin_date is None or options.end_date is None:
print "You must provide a begin and end date."
parser.print_help()
sys.exit(1)
beginDate = "%d" % (time.mktime(time.strptime(options.begin_date, "%m/%d/%Y")) * 1000)
endDate = "%d" % (time.mktime(time.strptime(options.end_date, "%m/%d/%Y")) * 1000)
url = "https://localhost:8443/loginservlet?adminPw=%s" % options.admin_pwd
p = subprocess.Popen(['curl', '-sk', url], stdout = subprocess.PIPE)
session_id, err = p.communicate()
if "Incorrect admin password" in session_id:
print "Server reported incorrect admin password, please verify"
sys.exit(1)
print "Using session id:", session_id
url = "https://localhost:8443/reportservlet?session=" + session_id + \
"&type=" + options.report_type + \
"&page=0&format=" + options.format + \
"&flush=false" + \
"&start=" + beginDate + \
"&end=" + endDate + \
"&criterion=" + options.criterion + \
"&groupByCriterion=" + options.group_by
print "Using request URL", url
subprocess.call(['curl', '-sko', options.output_file, url])
print "Report generated and saved to %s" % options.output_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment