summaryrefslogtreecommitdiffstats
path: root/src/tools/cephfs/EventOutput.cc
blob: 8cb235a82f987a94238a4cd0d58ea5903f95d7a4 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// -*- mode:c++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * ceph - scalable distributed file system
 *
 * copyright (c) 2014 john spray <john.spray@inktank.com>
 *
 * this is free software; you can redistribute it and/or
 * modify it under the terms of the gnu lesser general public
 * license version 2.1, as published by the free software
 * foundation.  see file copying.
 */


#include <iostream>
#include <fstream>

#include "common/errno.h"
#include "mds/mdstypes.h"
#include "mds/events/EUpdate.h"
#include "mds/LogEvent.h"
#include "JournalScanner.h"

#include "EventOutput.h"


int EventOutput::binary() const
{
  // Binary output, files
  int r = ::mkdir(path.c_str(), 0755);
  if (r != 0) {
    r = -errno;
    if (r != -EEXIST) {
      std::cerr << "Error creating output directory: " << cpp_strerror(r) << std::endl;
      return r;
    }
  }

  for (JournalScanner::EventMap::const_iterator i = scan.events.begin(); i != scan.events.end(); ++i) {
    bufferlist bin;
    std::stringstream filename;
    if (auto& le = i->second.log_event; le) {
      le->encode(bin, CEPH_FEATURES_SUPPORTED_DEFAULT);
      filename << "0x" << std::hex << i->first << std::dec << "_" << le->get_type_str() << ".bin";
    } else if (auto& pi = i->second.pi; pi) {
      pi->encode(bin);
      filename << "0x" << std::hex << i->first << std::dec << "_" << pi->get_type_str() << ".bin";
    }

    std::string const file_path = path + std::string("/") + filename.str();
    std::ofstream bin_file(file_path.c_str(), std::ofstream::out | std::ofstream::binary);
    bin.write_stream(bin_file);
    bin_file.close();
    if (bin_file.fail()) {
      return -EIO;
    }
  }
  std::cerr << "Wrote output to binary files in directory '" << path << "'" << std::endl;

  return 0;
}

int EventOutput::json() const
{
  JSONFormatter jf(true);
  std::ofstream out_file(path.c_str(), std::ofstream::out);
  jf.open_array_section("journal");
  {
    for (JournalScanner::EventMap::const_iterator i = scan.events.begin(); i != scan.events.end(); ++i) {
      if (auto& le = i->second.log_event; le) {
	jf.open_object_section("log_event");
	le->dump(&jf);
	jf.close_section();  // log_event
      } else if (auto& pi = i->second.pi; pi) {
	jf.open_object_section("purge_action");
	pi->dump(&jf);
	jf.close_section();
      }
    }
  }
  jf.close_section();  // journal
  jf.flush(out_file);
  out_file.close();

  if (out_file.fail()) {
    return -EIO;
  } else {
    std::cerr << "Wrote output to JSON file '" << path << "'" << std::endl;
    return 0;
  }
}

void EventOutput::list() const
{
  for (JournalScanner::EventMap::const_iterator i = scan.events.begin(); i != scan.events.end(); ++i) {
    if (auto& le = i->second.log_event; le) {
      std::vector<std::string> ev_paths;
      EMetaBlob const *emb = le->get_metablob();
      if (emb) {
	emb->get_paths(ev_paths);
      }

      std::string detail;
      if (le->get_type() == EVENT_UPDATE) {
	auto& eu = reinterpret_cast<EUpdate&>(*le);
	detail = eu.type;
      }

      std::cout << le->get_stamp() << " 0x"
	<< std::hex << i->first << std::dec << " "
	<< le->get_type_str() << ": "
	<< " (" << detail << ")" << std::endl;
      for (std::vector<std::string>::iterator i = ev_paths.begin(); i != ev_paths.end(); ++i) {
	std::cout << "  " << *i << std::endl;
      }
    } else if (auto& pi = i->second.pi; pi) {
      std::cout << pi->stamp << " 0x"
	<< std::hex << i->first << std::dec << " "
	<< pi->get_type_str() << std::endl;
    }
  }
}

void EventOutput::summary() const
{
  std::map<std::string, int> type_count;
  for (JournalScanner::EventMap::const_iterator i = scan.events.begin(); i != scan.events.end(); ++i) {
    std::string type;
    if (auto& le = i->second.log_event; le)
      type = le->get_type_str();
    else if (auto& pi = i->second.pi; pi)
      type = pi->get_type_str();
    if (type_count.count(type) == 0) {
      type_count[type] = 0;
    }
    type_count[type] += 1;
  }

  std::cout << "Events by type:" << std::endl;
  for (std::map<std::string, int>::iterator i = type_count.begin(); i != type_count.end(); ++i) {
      std::cout << "  " << i->first << ": " << i->second << std::endl;
  }

  std::cout << "Errors: " << scan.errors.size() << std::endl;
  if (!scan.errors.empty()) {
    for (JournalScanner::ErrorMap::const_iterator i = scan.errors.begin();
         i != scan.errors.end(); ++i) {
      std::cout << "  0x" << std::hex << i->first << std::dec
                << ": " << i->second.r << " "
                << i->second.description << std::endl;
    }
  }
}