summaryrefslogtreecommitdiff
path: root/bin/swift-container-info
blob: 39852e9778eb361e45daa8aaa310dbf6f98fa5c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/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
from swift.common.request_helpers import (
    is_user_meta, strip_user_meta_prefix, is_sys_meta, strip_sys_meta_prefix)


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)
    broker = ContainerBroker(db_file)
    info = broker.get_info()
    account = info['account']
    container = info['container']
    print 'Path: /%s/%s' % (account, container)
    print '  Account: %s' % account
    print '  Container: %s' % container
    container_hash = hash_path(account, container)
    print '  Container Hash: %s' % container_hash
    print 'Metadata:'
    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 '  UUID: %s' % info['id']

    for key, value in info.iteritems():
        if key.lower().startswith('x_container_'):
            title = key.replace('_', '-').title()
            print '  %s: %s' % (title, value)
    user_metadata = {}
    sys_metadata = {}
    for key, (value, timestamp) in broker.metadata.iteritems():
        if is_user_meta('container', key):
            user_metadata[strip_user_meta_prefix('container', key)] = value
        elif is_sys_meta('container', key):
            sys_metadata[strip_sys_meta_prefix('container', key)] = value
        else:
            title = key.replace('_', '-').title()
            print '  %s: %s' % (title, value)
    if sys_metadata:
        print '  System Metadata: %s' % sys_metadata
    else:
        print 'No system metadata found in db file'

    if user_metadata:
        print '  User Metadata: %s' % user_metadata
    else:
        print 'No user metadata found in db file'

    print

    try:
        ring = Ring(swift_dir, ring_name='container')
    except Exception:
        ring = None
    else:
        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('%prog [options] CONTAINER_DB_FILE')
    parser.add_option(
        '-d', '--swift-dir', default='/etc/swift',
        help="Pass location of swift directory")

    options, args = parser.parse_args()

    if len(args) != 1:
        sys.exit(parser.print_help())
    print_container_info(*args, **vars(options))