def generate_report(): logging.info('reading dynamodb table') try: region = boto.sts.regions() sts = boto.sts.connect_to_region(region[0].name) logging.info('assuming role: {} ({})'.format(role[dynamo_role], dynamo_role)) sts.assume_role(role_arn=role[dynamo_role], role_session_name=dynamo_role) except BotoServerError as e: logging.warning('exception: {}'.format(e.error_message)) logging.critical('could not assume role') raise table = Table(table_name) res = table.scan() print 'Report:\n' total=0 for row in res: total=total+1 acc, reg, serv, id, tag = row['id'].split(kdelim) since_n = row['tsfirst'] since_s = time.ctime(since_n) days = round((tsnow - since_n) / 86400, 2) print ('Acount: {}\n' 'Region: {}\n' 'Service: {}\n' 'Id: {}\n' 'Missing tag: {}\n' 'First seen: {}\n' 'Days missing: {}\n').format(acc, reg, serv, id, tag, since_s, days) print '{} missing tags found.'.format(total)
def clean_db(): global tsnow logging.info('cleaning db') table = Table(table_name) res = table.scan() for row in res: id = row['id'] logging.debug('{} {} {} ({})'.format(id, row['tsfirst'], row['tslast'], tsnow)) if row['tslast'] < tsnow: logging.info('tag {} now exists, deleting from db'.format(id)) table.delete_item(id=id)
def do_rmall(self, line): "remove [tablename...] yes" args = self.getargs(line) if args and args[-1] == "yes": args.pop() if not args: args = [self.table.table_name] while args: table = boto.dynamodb2.table.Table(args.pop(0), connection=self.conn) print "from table " + table.table_name for item in table.scan(attributes_to_get=[], request_limit=10): print " removing %s" % item item.delete() else: print "ok, never mind..."
def generate_report(): logging.info('reading dynamodb table') try: region = boto.sts.regions() sts = boto.sts.connect_to_region(region[0].name) logging.info('assuming role: {} ({})'.format(role[dynamo_role], dynamo_role)) sts.assume_role(role_arn=role[dynamo_role], role_session_name=dynamo_role) except BotoServerError as e: logging.warning('exception: {}'.format(e.error_message)) logging.critical('could not assume role') raise table = Table(table_name) res = table.scan() print 'Report:\n' total = 0 for row in res: total = total + 1 acc, reg, serv, id, tag = row['id'].split(kdelim) since_n = row['tsfirst'] since_s = time.ctime(since_n) days = round((tsnow - since_n) / 86400, 2) print('Acount: {}\n' 'Region: {}\n' 'Service: {}\n' 'Id: {}\n' 'Missing tag: {}\n' 'First seen: {}\n' 'Days missing: {}\n').format(acc, reg, serv, id, tag, since_s, days) print '{} missing tags found.'.format(total)
def do_scan(self, line): """ scan [:tablename] [--batch=#] [-{max}] [+filter_attribute:filter_value] [attributes,...] filter_attribute is either the field name to filter on or a field name with a conditional, as specified in boto's documentation, in the form of {name}__{conditional} where conditional is: eq (equal value) ne {value} (not equal value) lte (less or equal then value) lt (less then value) gte (greater or equal then value) gt (greater then value) nnull (value not null / exists) null (value is null / does not exists) contains (contains value) ncontains (does not contains value) beginswith (attribute begins with value) in (value in range) between (between value1 and value2 - use: between=value1,value2) """ table, line = self.get_table_params(line) args = self.getargs(line) scan_filter = {} #count = False as_array = False max_size = None batch_size = None start = None cond = None while args: if args[0].startswith('+'): arg = args.pop(0) filter_name, filter_value = arg[1:].split(':', 1) if "__" not in filter_name: filter_name += "__eq" scan_filter[filter_name] = self.get_typed_value(filter_name, filter_value) elif args[0].startswith('--batch='): arg = args.pop(0) batch_size = int(arg[8:]) elif args[0].startswith('--max='): arg = args.pop(0) max_size = int(arg[6:]) elif args[0].startswith('--start='): arg = args.pop(0) start = (arg[8:], ) elif args[0] == "--and": args.pop(0) cond = "AND" elif args[0] == "--or": args.pop(0) cond = "OR" elif args[0] == '--next': arg = args.pop(0) if self.next_key: start = self.next_key else: print "no next" return elif args[0] == '-a' or args[0] == '--array': as_array = True args.pop(0) elif args[0].startswith('-'): arg = args.pop(0) #if arg == '-c' or arg == '--count': # count = True if arg[0] == '-' and arg[1:].isdigit(): max_size = int(arg[1:]) elif arg == '--': break else: print "invalid argument: %s" % arg break else: break attr_keys = args[0].split(",") if args else None attrs = list(set(attr_keys)) if attr_keys else None result = table.scan(limit=max_size, max_page_size=batch_size, attributes=attrs, conditional_operator=cond, exclusive_start_key=start, **scan_filter) if False: # count: print "count: %s/%s" % (result.scanned_count, result.count) self.next_key = None else: if as_array and attr_keys: self.print_iterator_array(result, attr_keys) else: self.print_iterator(result) self.next_key = result._last_key_seen if self.consumed: print "consumed units:", result.consumed_units
table = boto.dynamodb2.table.Table('dsltn_battery', connection=boto.dynamodb2.connect_to_region('us-west-2')) batteryd = Batteryd('/tmp/batteryd.pid', table) if len(sys.argv) == 2: if 'start' == sys.argv[1]: batteryd.start() elif 'stop' == sys.argv[1]: batteryd.stop() elif 'restart' == sys.argv[1]: batteryd.restart() elif 'analyze' == sys.argv[1]: if raw_input('[G]raphics, [C]ount: ') == 'C': table = boto.dynamodb2.table.Table('dsltn_battery', connection=boto.dynamodb2.connect_to_region('us-west-2')) start_time = raw_input('Input start time: ') table = boto.dynamodb2.table.Table('dsltn_battery', connection=boto.dynamodb2.connect_to_region('us-west-2')) events = table.scan(_timeStamp__gte=start_time) for event in events: print event['Event ID'] elif 'reset' == sys.argv[1]: confirm = raw_input('Are you sure you want to reset? [Y/n]') if confirm != 'Y': print 'No reset performed.' sys.exit(1) table = boto.dynamodb2.table.Table('dsltn_battery', connection=boto.dynamodb2.connect_to_region('us-west-2')) assert(table.delete()) table = boto.dynamodb2.table.Table.create('dsltn_battery', schema=[ boto.dynamodb2.fields.HashKey('Event ID', data_type=boto.dynamodb2.types.NUMBER) ], throughput={ 'read': 3, 'write': 5, }, global_indexes={