]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd/action/Status.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / tools / rbd / action / Status.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "tools/rbd/ArgumentTypes.h"
5 #include "tools/rbd/Shell.h"
6 #include "tools/rbd/Utils.h"
7 #include "include/rbd_types.h"
8 #include "common/errno.h"
9 #include "common/Formatter.h"
10 #include <iostream>
11 #include <boost/program_options.hpp>
12
13 namespace rbd {
14 namespace action {
15 namespace status {
16
17 namespace at = argument_types;
18 namespace po = boost::program_options;
19
20 static int do_show_status(librados::IoCtx &io_ctx, librbd::Image &image,
21 const char *imgname, Formatter *f)
22 {
23 uint8_t old_format;
24 int r;
25 std::string header_oid;
26 std::list<obj_watch_t> watchers;
27
28 r = image.old_format(&old_format);
29 if (r < 0)
30 return r;
31
32 if (old_format) {
33 header_oid = imgname;
34 header_oid += RBD_SUFFIX;
35 } else {
36 std::string id;
37 r = image.get_id(&id);
38 if (r < 0) {
39 return r;
40 }
41
42 header_oid = RBD_HEADER_PREFIX + id;
43 }
44
45 r = io_ctx.list_watchers(header_oid, &watchers);
46 if (r < 0)
47 return r;
48
49 if (f)
50 f->open_object_section("status");
51
52 if (f) {
53 f->open_array_section("watchers");
54 for (std::list<obj_watch_t>::iterator i = watchers.begin(); i != watchers.end(); ++i) {
55 f->open_object_section("watcher");
56 f->dump_string("address", i->addr);
57 f->dump_unsigned("client", i->watcher_id);
58 f->dump_unsigned("cookie", i->cookie);
59 f->close_section();
60 }
61 f->close_section();
62 } else {
63 if (watchers.size()) {
64 std::cout << "Watchers:" << std::endl;
65 for (std::list<obj_watch_t>::iterator i = watchers.begin();
66 i != watchers.end(); ++i) {
67 std::cout << "\twatcher=" << i->addr << " client." << i->watcher_id
68 << " cookie=" << i->cookie << std::endl;
69 }
70 } else {
71 std::cout << "Watchers: none" << std::endl;
72 }
73 }
74 if (f) {
75 f->close_section();
76 f->flush(std::cout);
77 }
78
79 return 0;
80 }
81
82 void get_arguments(po::options_description *positional,
83 po::options_description *options) {
84 at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
85 at::add_format_options(options);
86 }
87
88 int execute(const po::variables_map &vm) {
89 size_t arg_index = 0;
90 std::string pool_name;
91 std::string image_name;
92 std::string snap_name;
93 int r = utils::get_pool_image_snapshot_names(
94 vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name,
95 &snap_name, utils::SNAPSHOT_PRESENCE_NONE, utils::SPEC_VALIDATION_NONE);
96 if (r < 0) {
97 return r;
98 }
99
100 at::Format::Formatter formatter;
101 r = utils::get_formatter(vm, &formatter);
102 if (r < 0) {
103 return r;
104 }
105
106 librados::Rados rados;
107 librados::IoCtx io_ctx;
108 librbd::Image image;
109 r = utils::init_and_open_image(pool_name, image_name, "", "", true, &rados,
110 &io_ctx, &image);
111 if (r < 0) {
112 return r;
113 }
114
115 r = do_show_status(io_ctx, image, image_name.c_str(), formatter.get());
116 if (r < 0) {
117 std::cerr << "rbd: show status failed: " << cpp_strerror(r) << std::endl;
118 return r;
119 }
120 return 0;
121 }
122
123 Shell::Action action(
124 {"status"}, {}, "Show the status of this image.", "", &get_arguments,
125 &execute);
126
127 } // namespace status
128 } // namespace action
129 } // namespace rbd