parser.add_argument('--version', action='version', version='%(prog)s ' + str(meta.version)) subparsers = parser.add_subparsers( help='sub-command help', dest="subcommand" ) # ################## Report sub command ######################## parser_report = subparsers.add_parser( 'report', help='One time report between two dates.' ) # Collect default dates default_start, default_end = utils.get_date_interval() parser_report.add_argument( '--tenant_id', type=str, default=None, help="Isolate report to one tenant if provided." ) # Report start date parser_report.add_argument( '--start', type=parse_date, help='Start date in YYYY-MM-DD. Defaults to today', default=default_start.date().isoformat() ) # Report end date parser_report.add_argument( '--end', type=parse_date,
def parse_cmd(argv, flags, params, optdir): get_opt(argv, flags, params, optdir) if optdir['list']: if not optdir['dbname']: raise getopt.GetoptError("--dbname required") return 0 filename = optdir.get('filename', None) date = optdir.get('date', None) ftype = optdir.get('ftype', None) # check required options if not filename and not ftype: raise getopt.GetoptError("--file or --type required") if filename and ftype: raise getopt.GetoptError("--file and --type are mutually exclusive") if ftype and not date: if ftype.startswith('ST_Z'): date_list = [yesterday()] else: date_list = [last_day_of_month()] elif ftype and date and ':' in date: if ftype.startswith('ST_Z'): date_list = get_date_interval(date) else: raise getopt.GetoptError("Date interval is valid only for '--type ST_ZXXX'") else: date_list = [date] if optdir['overwrite'] and optdir.get('append', False): raise getopt.GetoptError("--append and --overwrite are mutually exclusive") if optdir['layer']: optdir['layer'] = optdir['layer'].split(',') if filename: # is file a valid VFR file filename = check_file(filename) else: # --date & --type flist = [] base_url = "http://vdp.cuzk.cz/vymenny_format/" if ftype != 'ST_UVOH': base_url += "soucasna/" else: base_url += "specialni/" for d in date_list: fname = "%s_%s.xml.gz" % (d, ftype) url = base_url + fname if os.path.exists(fname): # use existing file flist.append(fname) else: # download file flist.append(download_vfr(url)) if not flist: raise getopt.GetoptError("Empty date range") filename = os.linesep.join(flist) if not filename: raise getopt.GetoptError("Invalid input file") return filename
def agent(args): """Run a long running polling agent. Periodically updates the database with timeseries active stat data. :param args: argparse args """ conf = config.load(args.config_file) nova = get_nova(conf) database.get_engine(conf.get('db', {})) detailed = True # Special all tenant. all_tenant_id = '0' * 32 # Init last poll time to 0 to trigger first poll last_polled = 0 while True: if time.time() - last_polled > conf.get('polling_interval', 120): with database.session_scope() as session: start, end = utils.get_date_interval() # Grab usage results usages = nova.usage.list(start, end, detailed=detailed) r = UsageReport(usages) # Get datetime for time value now = utils.get_now() # Iterate over all tenants for tenant_usage in usages: # Ensure tenant is in tenant table tenant_id = tenant_usage.tenant_id tenant = Tenant.get_or_create(session, tenant_id) session.commit() # Get tenant stats and add to session v, m, s = r.active_stats(tenant_id=tenant_id) session.add(ActiveVCPUS( value=v, time=now, tenant_id=tenant.id )) session.add(ActiveMemoryMB( value=m, time=now, tenant_id=tenant.id )) session.add(ActiveLocalStorageGB( value=s, time=now, tenant_id=tenant.id )) # Save all tenant stats v, m, s = r.active_stats() print ("Active vcpus", v) print ("Active memory MB", m) print ("Active storage GB", s) all_tenant = Tenant.get_or_create(session, all_tenant_id) session.commit() session.add(ActiveVCPUS( value=v, time=now, tenant_id=all_tenant.id )) session.add(ActiveMemoryMB( value=m, time=now, tenant_id=all_tenant.id )) session.add(ActiveLocalStorageGB( value=s, time=now, tenant_id=all_tenant.id )) last_polled = time.time() print ("Updating polling interval") time.sleep(1) exit()