swift/bin/swift-container-info

102 lines
3.8 KiB
Plaintext
Raw Normal View History

#!/usr/bin/python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
from datetime import datetime
from optparse import OptionParser
from swift.common.ring import Ring
from swift.common.utils import hash_path, storage_directory
from swift.container.backend import ContainerBroker
def print_container_info(db_file, swift_dir='/etc/swift'):
if not os.path.exists(db_file) or not db_file.endswith('.db'):
print "DB file doesn't exist"
sys.exit(1)
try:
ring = Ring(swift_dir, ring_name='container')
except Exception:
ring = None
metadata = {}
broker = ContainerBroker(db_file)
for key, (value, timestamp) in broker.metadata.iteritems():
metadata[key] = value
info = broker.get_info()
account = info['account']
container = info['container']
container_hash = hash_path(account, container)
print ' Account: %s' % info['account']
print ' Container: %s' % info['container']
print ' Container_hash: %s' % container_hash
print (' Created at: %s (%s)' %
(datetime.fromtimestamp(float(info['created_at'])),
info['created_at']))
print (' Put Timestamp: %s (%s)' %
(datetime.fromtimestamp(float(info['put_timestamp'])),
info['put_timestamp']))
print (' Delete Timestamp: %s (%s)' %
(datetime.fromtimestamp(float(info['delete_timestamp'])),
info['delete_timestamp']))
print ' Object Count: %s' % info['object_count']
print ' Bytes Used: %s' % info['bytes_used']
print (' Reported Put Timestamp: %s (%s)' %
(datetime.fromtimestamp(float(info['reported_put_timestamp'])),
info['reported_put_timestamp']))
print (' Reported Delete Timestamp: %s (%s)' %
(datetime.fromtimestamp(float(info['reported_delete_timestamp'])),
info['reported_delete_timestamp']))
print ' Reported Object Count: %s' % info['reported_object_count']
print ' Reported Bytes Used: %s' % info['reported_bytes_used']
print ' Chexor: %s' % info['hash']
print ' ID: %s' % info['id']
for key, value in info.iteritems():
if key.lower().startswith('x_container_'):
key = "-".join(key.split('_')).title()
print ' %s: %s' % (key, value)
if metadata:
print ' User Metadata: %s' % metadata
print
else:
print 'No user metadata found in db file'
print
if ring is not None:
print 'Ring locations:'
part, nodes = ring.get_nodes(account, container)
for node in nodes:
print (' %s:%s - /srv/node/%s/%s/%s.db' %
(node['ip'], node['port'], node['device'],
storage_directory('containers', part, container_hash),
container_hash))
print
print 'note: /srv/node is used as default value of `devices`, the real '\
'value is set in container-server.conf on each storage node.'
if __name__ == '__main__':
parser = OptionParser()
parser.set_defaults(swift_dir='/etc/swift')
parser.add_option(
'-d', '--swift-dir',
help="Pass location of swift directory")
options, args = parser.parse_args()
if len(sys.argv) <= 1:
print "Usage: %s [--swift-dir] CONTAINER_DB_FILE" % sys.argv[0]
sys.exit(1)
print_container_info(args[0], swift_dir=options.swift_dir)