summaryrefslogtreecommitdiffstats
path: root/src/mon/health_check.h
blob: 4e74637f9e53d99622e8516cad9ff54653490ed9 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#pragma once

#include <string>
#include <map>

#include "include/health.h"
#include "include/utime.h"
#include "common/Formatter.h"

struct health_check_t {
  health_status_t severity;
  std::string summary;
  std::list<std::string> detail;
  int64_t count = 0;

  DENC(health_check_t, v, p) {
    DENC_START(2, 1, p);
    denc(v.severity, p);
    denc(v.summary, p);
    denc(v.detail, p);
    if (struct_v >= 2) {
      denc(v.count, p);
    }
    DENC_FINISH(p);
  }

  friend bool operator==(const health_check_t& l,
			 const health_check_t& r) {
    return l.severity == r.severity &&
      l.summary == r.summary &&
      l.detail == r.detail &&
      l.count == r.count;
  }
  friend bool operator!=(const health_check_t& l,
			 const health_check_t& r) {
    return !(l == r);
  }

  void dump(ceph::Formatter *f, bool want_detail=true) const {
    f->dump_stream("severity") << severity;

    f->open_object_section("summary");
    f->dump_string("message", summary);
    f->dump_int("count", count);
    f->close_section();

    if (want_detail) {
      f->open_array_section("detail");
      for (auto& p : detail) {
	f->open_object_section("detail_item");
	f->dump_string("message", p);
	f->close_section();
      }
      f->close_section();
    }
  }

  static void generate_test_instances(std::list<health_check_t*>& ls) {
    ls.push_back(new health_check_t);
    ls.push_back(new health_check_t);
    ls.back()->severity = HEALTH_ERR;
    ls.back()->summary = "summarization";
    ls.back()->detail = {"one", "two", "three"};
    ls.back()->count = 42;
  }
};
WRITE_CLASS_DENC(health_check_t)


struct health_mute_t {
  std::string code;
  utime_t ttl;
  bool sticky = false;
  std::string summary;
  int64_t count;

  DENC(health_mute_t, v, p) {
    DENC_START(1, 1, p);
    denc(v.code, p);
    denc(v.ttl, p);
    denc(v.sticky, p);
    denc(v.summary, p);
    denc(v.count, p);
    DENC_FINISH(p);
  }

  void dump(ceph::Formatter *f) const {
    f->dump_string("code", code);
    if (ttl != utime_t()) {
      f->dump_stream("ttl") << ttl;
    }
    f->dump_bool("sticky", sticky);
    f->dump_string("summary", summary);
    f->dump_int("count", count);
  }

  static void generate_test_instances(std::list<health_mute_t*>& ls) {
    ls.push_back(new health_mute_t);
    ls.push_back(new health_mute_t);
    ls.back()->code = "OSD_DOWN";
    ls.back()->ttl = utime_t(1, 2);
    ls.back()->sticky = true;
    ls.back()->summary = "foo bar";
    ls.back()->count = 2;
  }
};
WRITE_CLASS_DENC(health_mute_t)

struct health_check_map_t {
  std::map<std::string,health_check_t> checks;

  DENC(health_check_map_t, v, p) {
    DENC_START(1, 1, p);
    denc(v.checks, p);
    DENC_FINISH(p);
  }

  void dump(ceph::Formatter *f) const {
    for (auto& [code, check] : checks) {
      f->dump_object(code, check);
    }
  }

  static void generate_test_instances(std::list<health_check_map_t*>& ls) {
    ls.push_back(new health_check_map_t);
    ls.push_back(new health_check_map_t);
    {
      auto& d = ls.back()->add("FOO", HEALTH_WARN, "foo", 2);
      d.detail.push_back("a");
      d.detail.push_back("b");
    }
    {
      auto& d = ls.back()->add("BAR", HEALTH_ERR, "bar!", 3);
      d.detail.push_back("c");
      d.detail.push_back("d");
      d.detail.push_back("e");
    }
  }

  void clear() {
    checks.clear();
  }
  bool empty() const {
    return checks.empty();
  }
  void swap(health_check_map_t& other) {
    checks.swap(other.checks);
  }

  health_check_t& add(const std::string& code,
		      health_status_t severity,
		      const std::string& summary,
		      int64_t count) {
    ceph_assert(checks.count(code) == 0);
    health_check_t& r = checks[code];
    r.severity = severity;
    r.summary = summary;
    r.count = count;
    return r;
  }
  health_check_t& get_or_add(const std::string& code,
			     health_status_t severity,
			     const std::string& summary,
			     int64_t count) {
    health_check_t& r = checks[code];
    r.severity = severity;
    r.summary = summary;
    r.count += count;
    return r;
  }

  void merge(const health_check_map_t& o) {
    for (auto& [code, check] : o.checks) {
      auto [it, new_check] = checks.try_emplace(code, check);
      if (!new_check) {
        // merge details, and hope the summary matches!
        it->second.detail.insert(
          it->second.detail.end(),
          check.detail.begin(),
          check.detail.end());
        it->second.count += check.count;
      }
    }
  }

  friend bool operator==(const health_check_map_t& l,
			 const health_check_map_t& r) {
    return l.checks == r.checks;
  }
  friend bool operator!=(const health_check_map_t& l,
			 const health_check_map_t& r) {
    return !(l == r);
  }
};
WRITE_CLASS_DENC(health_check_map_t)