]> git.proxmox.com Git - ceph.git/blame - ceph/src/mgr/ServiceMap.h
import 15.2.0 Octopus source
[ceph.git] / ceph / src / mgr / ServiceMap.h
CommitLineData
224ce89b
WB
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#pragma once
5
6#include <string>
7#include <map>
8#include <list>
9#include <sstream>
10
11#include "include/utime.h"
12#include "include/buffer.h"
13#include "msg/msg_types.h"
14
15namespace ceph {
16 class Formatter;
17}
18
19struct ServiceMap {
20 struct Daemon {
21 uint64_t gid = 0;
22 entity_addr_t addr;
23 epoch_t start_epoch = 0; ///< epoch first registered
24 utime_t start_stamp; ///< timestamp daemon started/registered
25 std::map<std::string,std::string> metadata; ///< static metadata
9f95a23c 26 std::map<std::string,std::string> task_status; ///< running task status
224ce89b 27
9f95a23c
TL
28 void encode(ceph::buffer::list& bl, uint64_t features) const;
29 void decode(ceph::buffer::list::const_iterator& p);
30 void dump(ceph::Formatter *f) const;
224ce89b
WB
31 static void generate_test_instances(std::list<Daemon*>& ls);
32 };
33
34 struct Service {
9f95a23c
TL
35 std::map<std::string,Daemon> daemons;
36 std::string summary; ///< summary status std::string for 'ceph -s'
224ce89b 37
9f95a23c
TL
38 void encode(ceph::buffer::list& bl, uint64_t features) const;
39 void decode(ceph::buffer::list::const_iterator& p);
40 void dump(ceph::Formatter *f) const;
224ce89b
WB
41 static void generate_test_instances(std::list<Service*>& ls);
42
43 std::string get_summary() const {
44 if (summary.size()) {
45 return summary;
46 }
47 if (daemons.empty()) {
48 return "no daemons active";
49 }
50 std::ostringstream ss;
c07f9fc5 51 ss << daemons.size() << (daemons.size() > 1 ? " daemons" : " daemon")
224ce89b 52 << " active";
11fdf7f2
TL
53
54 if (!daemons.empty()) {
55 ss << " (";
56 for (auto p = daemons.begin(); p != daemons.end(); ++p) {
57 if (p != daemons.begin()) {
58 ss << ", ";
59 }
60 ss << p->first;
61 }
62 ss << ")";
63 }
64
224ce89b
WB
65 return ss.str();
66 }
c07f9fc5 67
9f95a23c
TL
68 std::string get_task_summary(const std::string_view task_prefix) const {
69 // contruct a map similar to:
70 // {"service1 status" -> {"service1.0" -> "running"}}
71 // {"service2 status" -> {"service2.0" -> "idle"},
72 // {"service2.1" -> "running"}}
73 std::map<std::string, std::map<std::string, std::string>> by_task;
74 for (const auto &p : daemons) {
75 std::stringstream d;
76 d << task_prefix << "." << p.first;
77 for (const auto &q : p.second.task_status) {
78 auto p1 = by_task.emplace(q.first, std::map<std::string, std::string>{}).first;
79 auto p2 = p1->second.emplace(d.str(), std::string()).first;
80 p2->second = q.second;
81 }
82 }
83
84 std::stringstream ss;
85 for (const auto &p : by_task) {
86 ss << "\n " << p.first << ":";
87 for (auto q : p.second) {
88 ss << "\n " << q.first << ": " << q.second;
89 }
90 }
91
92 return ss.str();
93 }
94
95 void count_metadata(const std::string& field,
c07f9fc5
FG
96 std::map<std::string,int> *out) const {
97 for (auto& p : daemons) {
98 auto q = p.second.metadata.find(field);
99 if (q == p.second.metadata.end()) {
100 (*out)["unknown"]++;
101 } else {
102 (*out)[q->second]++;
103 }
104 }
105 }
106
224ce89b
WB
107 };
108
109 epoch_t epoch = 0;
110 utime_t modified;
9f95a23c 111 std::map<std::string,Service> services;
224ce89b 112
9f95a23c
TL
113 void encode(ceph::buffer::list& bl, uint64_t features) const;
114 void decode(ceph::buffer::list::const_iterator& p);
115 void dump(ceph::Formatter *f) const;
224ce89b
WB
116 static void generate_test_instances(std::list<ServiceMap*>& ls);
117
9f95a23c
TL
118 std::pair<Daemon*,bool> get_daemon(const std::string& service,
119 const std::string& daemon) {
120 auto& s = services[service];
121 auto [d, added] = s.daemons.try_emplace(daemon);
122 return {&d->second, added};
224ce89b
WB
123 }
124
125 bool rm_daemon(const std::string& service,
126 const std::string& daemon) {
127 auto p = services.find(service);
128 if (p == services.end()) {
129 return false;
130 }
131 auto q = p->second.daemons.find(daemon);
132 if (q == p->second.daemons.end()) {
133 return false;
134 }
135 p->second.daemons.erase(q);
136 if (p->second.daemons.empty()) {
137 services.erase(p);
138 }
139 return true;
140 }
9f95a23c
TL
141
142 static inline bool is_normal_ceph_entity(std::string_view type) {
143 if (type == "osd" ||
144 type == "client" ||
145 type == "mon" ||
146 type == "mds" ||
147 type == "mgr") {
148 return true;
149 }
150
151 return false;
152 }
224ce89b
WB
153};
154WRITE_CLASS_ENCODER_FEATURES(ServiceMap)
155WRITE_CLASS_ENCODER_FEATURES(ServiceMap::Service)
156WRITE_CLASS_ENCODER_FEATURES(ServiceMap::Daemon)