]> git.proxmox.com Git - ceph.git/blame - ceph/src/mgr/ServiceMap.h
update sources to ceph Nautilus 14.2.1
[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
26
27 void encode(bufferlist& bl, uint64_t features) const;
11fdf7f2 28 void decode(bufferlist::const_iterator& p);
224ce89b
WB
29 void dump(Formatter *f) const;
30 static void generate_test_instances(std::list<Daemon*>& ls);
31 };
32
33 struct Service {
34 map<std::string,Daemon> daemons;
35 std::string summary; ///< summary status string for 'ceph -s'
36
37 void encode(bufferlist& bl, uint64_t features) const;
11fdf7f2 38 void decode(bufferlist::const_iterator& p);
224ce89b
WB
39 void dump(Formatter *f) const;
40 static void generate_test_instances(std::list<Service*>& ls);
41
42 std::string get_summary() const {
43 if (summary.size()) {
44 return summary;
45 }
46 if (daemons.empty()) {
47 return "no daemons active";
48 }
49 std::ostringstream ss;
c07f9fc5 50 ss << daemons.size() << (daemons.size() > 1 ? " daemons" : " daemon")
224ce89b 51 << " active";
11fdf7f2
TL
52
53 if (!daemons.empty()) {
54 ss << " (";
55 for (auto p = daemons.begin(); p != daemons.end(); ++p) {
56 if (p != daemons.begin()) {
57 ss << ", ";
58 }
59 ss << p->first;
60 }
61 ss << ")";
62 }
63
224ce89b
WB
64 return ss.str();
65 }
c07f9fc5
FG
66
67 void count_metadata(const string& field,
68 std::map<std::string,int> *out) const {
69 for (auto& p : daemons) {
70 auto q = p.second.metadata.find(field);
71 if (q == p.second.metadata.end()) {
72 (*out)["unknown"]++;
73 } else {
74 (*out)[q->second]++;
75 }
76 }
77 }
78
224ce89b
WB
79 };
80
81 epoch_t epoch = 0;
82 utime_t modified;
83 map<std::string,Service> services;
84
85 void encode(bufferlist& bl, uint64_t features) const;
11fdf7f2 86 void decode(bufferlist::const_iterator& p);
224ce89b
WB
87 void dump(Formatter *f) const;
88 static void generate_test_instances(std::list<ServiceMap*>& ls);
89
90 Daemon* get_daemon(const std::string& service,
91 const std::string& daemon) {
92 return &services[service].daemons[daemon];
93 }
94
95 bool rm_daemon(const std::string& service,
96 const std::string& daemon) {
97 auto p = services.find(service);
98 if (p == services.end()) {
99 return false;
100 }
101 auto q = p->second.daemons.find(daemon);
102 if (q == p->second.daemons.end()) {
103 return false;
104 }
105 p->second.daemons.erase(q);
106 if (p->second.daemons.empty()) {
107 services.erase(p);
108 }
109 return true;
110 }
111};
112WRITE_CLASS_ENCODER_FEATURES(ServiceMap)
113WRITE_CLASS_ENCODER_FEATURES(ServiceMap::Service)
114WRITE_CLASS_ENCODER_FEATURES(ServiceMap::Daemon)