]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
osdmap, mon: optional dump 'osd tree' in json
authorSage Weil <sage@inktank.com>
Tue, 2 Oct 2012 16:43:13 +0000 (09:43 -0700)
committerSage Weil <sage@inktank.com>
Tue, 2 Oct 2012 17:42:50 +0000 (10:42 -0700)
Signed-off-by: Sage Weil <sage@inktank.com>
src/mon/OSDMonitor.cc
src/osd/OSDMap.cc
src/osd/OSDMap.h
src/osdmaptool.cc

index 223c5aa04c917db8277f8a36a4edb957a72969e1..51b8997b69c15581bc8c28909ca326d533720ffd 100644 (file)
@@ -1677,10 +1677,23 @@ bool OSDMonitor::preprocess_command(MMonCommand *m)
          }
        } else if (cmd == "tree") {
          stringstream ds;
-         p->print_tree(ds);
-         rdata.append(ds);
-         ss << "dumped osdmap tree epoch " << p->get_epoch();
-         r = 0;
+         if (format == "json") {
+           JSONFormatter jf(true);
+           jf.open_object_section("tree");
+           p->print_tree(NULL, &jf);
+           jf.close_section();
+           jf.flush(ds);
+           r = 0;
+         } else if (format == "plain") {
+           p->print_tree(&ds, NULL);
+           r = 0;
+         } else {
+           ss << "unrecognized format '" << format << "'";
+           r = -EINVAL;
+         }
+         if (r == 0) {
+           rdata.append(ds);
+         }
        } else if (cmd == "getmap") {
          p->encode(rdata);
          ss << "got osdmap epoch " << p->get_epoch();
index 6ec5f118aaf771497c7b582070543c0772062282..6ad50c25e2db4e53ec001da1b14399eacecd720b 100644 (file)
@@ -1428,27 +1428,50 @@ void OSDMap::print(ostream& out) const
   // ignore pg_swap_primary
 }
 
-void OSDMap::print_osd_line(int cur, ostream& out) const
+void OSDMap::print_osd_line(int cur, ostream *out, Formatter *f) const
 {
-  out << "osd." << cur << "\t";
-  if (!exists(cur))
-    out << "DNE\t\t";
-  else {
-    if (is_up(cur))
-      out << "up\t";
-    else
-      out << "down\t";
-    std::streamsize p = out.precision();
-    out << std::setprecision(4) 
-       << (exists(cur) ? get_weightf(cur) : 0)
-       << std::setprecision(p)
-       << "\t";
+  if (f) {
+    f->dump_unsigned("id", cur);
+    f->dump_stream("name") << "osd." << cur;
+    f->dump_unsigned("exists", (int)exists(cur));
+    f->dump_string("type", crush->get_type_name(0));
+    f->dump_int("type_id", 0);
+  }
+  if (out)
+    *out << "osd." << cur << "\t";
+  if (!exists(cur)) {
+    *out << "DNE\t\t";
+  } else {
+    if (is_up(cur)) {
+      if (out)
+       *out << "up\t";
+      if (f)
+       f->dump_string("status", "up");
+    } else {
+      if (out)
+       *out << "down\t";
+      if (f)
+       f->dump_string("status", "down");
+    }
+    if (out) {
+      std::streamsize p = out->precision();
+      *out << std::setprecision(4)
+          << (exists(cur) ? get_weightf(cur) : 0)
+          << std::setprecision(p)
+          << "\t";
+    }
+    if (f) {
+      f->dump_float("reweight", get_weightf(cur));
+    }
   }
 }
 
-void OSDMap::print_tree(ostream& out) const
+void OSDMap::print_tree(ostream *out, Formatter *f) const
 {
-  out << "# id\tweight\ttype name\tup/down\treweight\n";
+  if (out)
+    *out << "# id\tweight\ttype name\tup/down\treweight\n";
+  if (f)
+    f->open_array_section("nodes");
   set<int> touched;
   set<int> roots;
   crush->find_roots(roots);
@@ -1461,30 +1484,64 @@ void OSDMap::print_tree(ostream& out) const
       float weight = q.front().weight;
       q.pop_front();
 
-      out << cur << "\t";
-      int oldprecision = out.precision();
-      out << std::setprecision(4) << weight << std::setprecision(oldprecision) << "\t";
-
-      for (int k=0; k<depth; k++)
-       out << "\t";
+      if (out) {
+       *out << cur << "\t";
+       int oldprecision = out->precision();
+       *out << std::setprecision(4) << weight << std::setprecision(oldprecision) << "\t";
 
+       for (int k=0; k<depth; k++)
+         *out << "\t";
+      }
+      if (f) {
+       f->open_object_section("item");
+      }
       if (cur >= 0) {
-       print_osd_line(cur, out);
-       out << "\n";
+       print_osd_line(cur, out, f);
+       if (out)
+         *out << "\n";
+       if (f) {
+         f->close_section();
+       }
        touched.insert(cur);
+      }
+      if (f) {
+       f->dump_float("crush_weight", weight);
+       f->dump_unsigned("depth", depth);
+      }
+      if (cur >= 0) {
        continue;
       }
 
-      int type = crush->get_bucket_type(cur);
-      out << crush->get_type_name(type) << " " << crush->get_item_name(cur) << "\n";
-
       // queue bucket contents...
+      int type = crush->get_bucket_type(cur);
       int s = crush->get_bucket_size(cur);
-      for (int k=s-1; k>=0; k--)
-       q.push_front(qi(crush->get_bucket_item(cur, k), depth+1,
-                       (float)crush->get_bucket_item_weight(cur, k) / (float)0x10000));
+      if (f) {
+       f->dump_int("id", cur);
+       f->dump_string("name", crush->get_item_name(cur));
+       f->dump_string("type", crush->get_type_name(type));
+       f->dump_int("type_id", type);
+       f->open_array_section("children");
+      }
+      for (int k=s-1; k>=0; k--) {
+       int item = crush->get_bucket_item(cur, k);
+       q.push_front(qi(item, depth+1, (float)crush->get_bucket_item_weight(cur, k) / (float)0x10000));
+       f->dump_int("child", item);
+      }
+      if (f)
+       f->close_section();
+
+      if (out)
+       *out << crush->get_type_name(type) << " " << crush->get_item_name(cur) << "\n";
+      if (f) {
+       f->close_section();
+      }
+
     }
   }
+  if (f) {
+    f->close_section();
+    f->open_array_section("stray");
+  }
 
   set<int> stray;
   for (int i=0; i<max_osd; i++)
@@ -1492,14 +1549,22 @@ void OSDMap::print_tree(ostream& out) const
       stray.insert(i);
 
   if (!stray.empty()) {
-    out << "\n";
+    if (out)
+      *out << "\n";
+    if (f)
+      f->open_object_section("osd");
     for (set<int>::iterator p = stray.begin(); p != stray.end(); ++p) {
-      out << *p << "\t0\t";
-      print_osd_line(*p, out);
-      out << "\n";
+      if (out)
+       *out << *p << "\t0\t";
+      print_osd_line(*p, out, f);
+      if (out)
+       *out << "\n";
     }
+    if (f)
+      f->close_section();
   }
-
+  if (f)
+    f->close_section();
 }
 
 void OSDMap::print_summary(ostream& out) const
index 153172b36635a3efc0007113117c4699ab9eaf5b..632b2c2527b10af2201df4e343a5c510cd9c60c1 100644 (file)
@@ -545,11 +545,11 @@ public:
 
 
 private:
-  void print_osd_line(int cur, ostream& out) const;
+  void print_osd_line(int cur, ostream *out, Formatter *f) const;
 public:
   void print(ostream& out) const;
   void print_summary(ostream& out) const;
-  void print_tree(ostream& out) const;
+  void print_tree(ostream *out, Formatter *f) const;
 
   string get_flag_string() const;
   static string get_flag_string(unsigned flags);
index ef8af8f90e015da867e18e058e6e8990bf13fe41..c28926bff7183a8a98d0f7a50e44e4c7d38329dc 100644 (file)
@@ -315,7 +315,7 @@ int main(int argc, const char **argv)
   if (print_json)
     osdmap.dump_json(cout);
   if (tree) 
-    osdmap.print_tree(cout);
+    osdmap.print_tree(&cout, NULL);
 
   if (modified) {
     bl.clear();