]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/ActivePyModules.cc
import 15.2.5
[ceph.git] / ceph / src / mgr / ActivePyModules.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2014 John Spray <john.spray@inktank.com>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 */
13
14 // Include this first to get python headers earlier
15 #include "Gil.h"
16
17 #include "common/errno.h"
18 #include "include/stringify.h"
19
20 #include "PyFormatter.h"
21
22 #include "osd/OSDMap.h"
23 #include "mon/MonMap.h"
24
25 #include "mgr/MgrContext.h"
26
27 // For ::config_prefix
28 #include "PyModule.h"
29 #include "PyModuleRegistry.h"
30
31 #include "ActivePyModules.h"
32 #include "DaemonKey.h"
33 #include "DaemonServer.h"
34
35 #define dout_context g_ceph_context
36 #define dout_subsys ceph_subsys_mgr
37 #undef dout_prefix
38 #define dout_prefix *_dout << "mgr " << __func__ << " "
39
40 ActivePyModules::ActivePyModules(PyModuleConfig &module_config_,
41 std::map<std::string, std::string> store_data,
42 DaemonStateIndex &ds, ClusterState &cs,
43 MonClient &mc, LogChannelRef clog_,
44 LogChannelRef audit_clog_, Objecter &objecter_,
45 Client &client_, Finisher &f, DaemonServer &server,
46 PyModuleRegistry &pmr)
47 : module_config(module_config_), daemon_state(ds), cluster_state(cs),
48 monc(mc), clog(clog_), audit_clog(audit_clog_), objecter(objecter_),
49 client(client_), finisher(f),
50 cmd_finisher(g_ceph_context, "cmd_finisher", "cmdfin"),
51 server(server), py_module_registry(pmr)
52 {
53 store_cache = std::move(store_data);
54 cmd_finisher.start();
55 }
56
57 ActivePyModules::~ActivePyModules() = default;
58
59 void ActivePyModules::dump_server(const std::string &hostname,
60 const DaemonStateCollection &dmc,
61 Formatter *f)
62 {
63 f->dump_string("hostname", hostname);
64 f->open_array_section("services");
65 std::string ceph_version;
66
67 for (const auto &[key, state] : dmc) {
68 std::lock_guard l(state->lock);
69 // TODO: pick the highest version, and make sure that
70 // somewhere else (during health reporting?) we are
71 // indicating to the user if we see mixed versions
72 auto ver_iter = state->metadata.find("ceph_version");
73 if (ver_iter != state->metadata.end()) {
74 ceph_version = state->metadata.at("ceph_version");
75 }
76
77 f->open_object_section("service");
78 f->dump_string("type", key.type);
79 f->dump_string("id", key.name);
80 f->close_section();
81 }
82 f->close_section();
83
84 f->dump_string("ceph_version", ceph_version);
85 }
86
87
88
89 PyObject *ActivePyModules::get_server_python(const std::string &hostname)
90 {
91 PyThreadState *tstate = PyEval_SaveThread();
92 std::lock_guard l(lock);
93 PyEval_RestoreThread(tstate);
94 dout(10) << " (" << hostname << ")" << dendl;
95
96 auto dmc = daemon_state.get_by_server(hostname);
97
98 PyFormatter f;
99 dump_server(hostname, dmc, &f);
100 return f.get();
101 }
102
103
104 PyObject *ActivePyModules::list_servers_python()
105 {
106 PyFormatter f(false, true);
107 PyThreadState *tstate = PyEval_SaveThread();
108 dout(10) << " >" << dendl;
109
110 daemon_state.with_daemons_by_server([this, &f, &tstate]
111 (const std::map<std::string, DaemonStateCollection> &all) {
112 PyEval_RestoreThread(tstate);
113
114 for (const auto &i : all) {
115 const auto &hostname = i.first;
116
117 f.open_object_section("server");
118 dump_server(hostname, i.second, &f);
119 f.close_section();
120 }
121 });
122
123 return f.get();
124 }
125
126 PyObject *ActivePyModules::get_metadata_python(
127 const std::string &svc_type,
128 const std::string &svc_id)
129 {
130 auto metadata = daemon_state.get(DaemonKey{svc_type, svc_id});
131 if (metadata == nullptr) {
132 derr << "Requested missing service " << svc_type << "." << svc_id << dendl;
133 Py_RETURN_NONE;
134 }
135
136 std::lock_guard l(metadata->lock);
137 PyFormatter f;
138 f.dump_string("hostname", metadata->hostname);
139 for (const auto &i : metadata->metadata) {
140 f.dump_string(i.first.c_str(), i.second);
141 }
142
143 return f.get();
144 }
145
146 PyObject *ActivePyModules::get_daemon_status_python(
147 const std::string &svc_type,
148 const std::string &svc_id)
149 {
150 auto metadata = daemon_state.get(DaemonKey{svc_type, svc_id});
151 if (metadata == nullptr) {
152 derr << "Requested missing service " << svc_type << "." << svc_id << dendl;
153 Py_RETURN_NONE;
154 }
155
156 std::lock_guard l(metadata->lock);
157 PyFormatter f;
158 for (const auto &i : metadata->service_status) {
159 f.dump_string(i.first.c_str(), i.second);
160 }
161 return f.get();
162 }
163
164 PyObject *ActivePyModules::get_python(const std::string &what)
165 {
166 PyFormatter f;
167
168 // Drop the GIL, as most of the following blocks will block on
169 // a mutex -- they are all responsible for re-taking the GIL before
170 // touching the PyFormatter instance or returning from the function.
171 PyThreadState *tstate = PyEval_SaveThread();
172
173 if (what == "fs_map") {
174 cluster_state.with_fsmap([&f, &tstate](const FSMap &fsmap) {
175 PyEval_RestoreThread(tstate);
176 fsmap.dump(&f);
177 });
178 return f.get();
179 } else if (what == "osdmap_crush_map_text") {
180 bufferlist rdata;
181 cluster_state.with_osdmap([&rdata, &tstate](const OSDMap &osd_map){
182 PyEval_RestoreThread(tstate);
183 osd_map.crush->encode(rdata, CEPH_FEATURES_SUPPORTED_DEFAULT);
184 });
185 std::string crush_text = rdata.to_str();
186 return PyUnicode_FromString(crush_text.c_str());
187 } else if (what.substr(0, 7) == "osd_map") {
188 cluster_state.with_osdmap([&f, &what, &tstate](const OSDMap &osd_map){
189 PyEval_RestoreThread(tstate);
190 if (what == "osd_map") {
191 osd_map.dump(&f);
192 } else if (what == "osd_map_tree") {
193 osd_map.print_tree(&f, nullptr);
194 } else if (what == "osd_map_crush") {
195 osd_map.crush->dump(&f);
196 }
197 });
198 return f.get();
199 } else if (what == "modified_config_options") {
200 PyEval_RestoreThread(tstate);
201 auto all_daemons = daemon_state.get_all();
202 set<string> names;
203 for (auto& [key, daemon] : all_daemons) {
204 std::lock_guard l(daemon->lock);
205 for (auto& [name, valmap] : daemon->config) {
206 names.insert(name);
207 }
208 }
209 f.open_array_section("options");
210 for (auto& name : names) {
211 f.dump_string("name", name);
212 }
213 f.close_section();
214 return f.get();
215 } else if (what.substr(0, 6) == "config") {
216 PyEval_RestoreThread(tstate);
217 if (what == "config_options") {
218 g_conf().config_options(&f);
219 } else if (what == "config") {
220 g_conf().show_config(&f);
221 }
222 return f.get();
223 } else if (what == "mon_map") {
224 cluster_state.with_monmap(
225 [&f, &tstate](const MonMap &monmap) {
226 PyEval_RestoreThread(tstate);
227 monmap.dump(&f);
228 }
229 );
230 return f.get();
231 } else if (what == "service_map") {
232 cluster_state.with_servicemap(
233 [&f, &tstate](const ServiceMap &service_map) {
234 PyEval_RestoreThread(tstate);
235 service_map.dump(&f);
236 }
237 );
238 return f.get();
239 } else if (what == "osd_metadata") {
240 auto dmc = daemon_state.get_by_service("osd");
241 PyEval_RestoreThread(tstate);
242
243 for (const auto &[key, state] : dmc) {
244 std::lock_guard l(state->lock);
245 f.open_object_section(key.name.c_str());
246 f.dump_string("hostname", state->hostname);
247 for (const auto &[name, val] : state->metadata) {
248 f.dump_string(name.c_str(), val);
249 }
250 f.close_section();
251 }
252 return f.get();
253 } else if (what == "mds_metadata") {
254 auto dmc = daemon_state.get_by_service("mds");
255 PyEval_RestoreThread(tstate);
256
257 for (const auto &[key, state] : dmc) {
258 std::lock_guard l(state->lock);
259 f.open_object_section(key.name.c_str());
260 f.dump_string("hostname", state->hostname);
261 for (const auto &[name, val] : state->metadata) {
262 f.dump_string(name.c_str(), val);
263 }
264 f.close_section();
265 }
266 return f.get();
267 } else if (what == "pg_summary") {
268 cluster_state.with_pgmap(
269 [&f, &tstate](const PGMap &pg_map) {
270 PyEval_RestoreThread(tstate);
271
272 std::map<std::string, std::map<std::string, uint32_t> > osds;
273 std::map<std::string, std::map<std::string, uint32_t> > pools;
274 std::map<std::string, uint32_t> all;
275 for (const auto &i : pg_map.pg_stat) {
276 const auto pool = i.first.m_pool;
277 const std::string state = pg_state_string(i.second.state);
278 // Insert to per-pool map
279 pools[stringify(pool)][state]++;
280 for (const auto &osd_id : i.second.acting) {
281 osds[stringify(osd_id)][state]++;
282 }
283 all[state]++;
284 }
285 f.open_object_section("by_osd");
286 for (const auto &i : osds) {
287 f.open_object_section(i.first.c_str());
288 for (const auto &j : i.second) {
289 f.dump_int(j.first.c_str(), j.second);
290 }
291 f.close_section();
292 }
293 f.close_section();
294 f.open_object_section("by_pool");
295 for (const auto &i : pools) {
296 f.open_object_section(i.first.c_str());
297 for (const auto &j : i.second) {
298 f.dump_int(j.first.c_str(), j.second);
299 }
300 f.close_section();
301 }
302 f.close_section();
303 f.open_object_section("all");
304 for (const auto &i : all) {
305 f.dump_int(i.first.c_str(), i.second);
306 }
307 f.close_section();
308 f.open_object_section("pg_stats_sum");
309 pg_map.pg_sum.dump(&f);
310 f.close_section();
311 }
312 );
313 return f.get();
314 } else if (what == "pg_status") {
315 cluster_state.with_pgmap(
316 [&f, &tstate](const PGMap &pg_map) {
317 PyEval_RestoreThread(tstate);
318 pg_map.print_summary(&f, nullptr);
319 }
320 );
321 return f.get();
322 } else if (what == "pg_dump") {
323 cluster_state.with_pgmap(
324 [&f, &tstate](const PGMap &pg_map) {
325 PyEval_RestoreThread(tstate);
326 pg_map.dump(&f, false);
327 }
328 );
329 return f.get();
330 } else if (what == "devices") {
331 daemon_state.with_devices2(
332 [&tstate, &f]() {
333 PyEval_RestoreThread(tstate);
334 f.open_array_section("devices");
335 },
336 [&f] (const DeviceState& dev) {
337 f.dump_object("device", dev);
338 });
339 f.close_section();
340 return f.get();
341 } else if (what.size() > 7 &&
342 what.substr(0, 7) == "device ") {
343 string devid = what.substr(7);
344 if (!daemon_state.with_device(
345 devid,
346 [&f, &tstate] (const DeviceState& dev) {
347 PyEval_RestoreThread(tstate);
348 f.dump_object("device", dev);
349 })) {
350 // device not found
351 PyEval_RestoreThread(tstate);
352 }
353 return f.get();
354 } else if (what == "io_rate") {
355 cluster_state.with_pgmap(
356 [&f, &tstate](const PGMap &pg_map) {
357 PyEval_RestoreThread(tstate);
358 pg_map.dump_delta(&f);
359 }
360 );
361 return f.get();
362 } else if (what == "df") {
363 cluster_state.with_osdmap_and_pgmap(
364 [&f, &tstate](
365 const OSDMap& osd_map,
366 const PGMap &pg_map) {
367 PyEval_RestoreThread(tstate);
368 pg_map.dump_cluster_stats(nullptr, &f, true);
369 pg_map.dump_pool_stats_full(osd_map, nullptr, &f, true);
370 });
371 return f.get();
372 } else if (what == "pg_stats") {
373 cluster_state.with_pgmap(
374 [&f, &tstate](const PGMap &pg_map) {
375 PyEval_RestoreThread(tstate);
376 pg_map.dump_pg_stats(&f, false);
377 });
378 return f.get();
379 } else if (what == "pool_stats") {
380 cluster_state.with_pgmap(
381 [&f, &tstate](const PGMap &pg_map) {
382 PyEval_RestoreThread(tstate);
383 pg_map.dump_pool_stats(&f);
384 });
385 return f.get();
386 } else if (what == "pg_ready") {
387 PyEval_RestoreThread(tstate);
388 server.dump_pg_ready(&f);
389 return f.get();
390 } else if (what == "osd_stats") {
391 cluster_state.with_pgmap(
392 [&f, &tstate](const PGMap &pg_map) {
393 PyEval_RestoreThread(tstate);
394 pg_map.dump_osd_stats(&f, false);
395 });
396 return f.get();
397 } else if (what == "osd_ping_times") {
398 cluster_state.with_pgmap(
399 [&f, &tstate](const PGMap &pg_map) {
400 PyEval_RestoreThread(tstate);
401 pg_map.dump_osd_ping_times(&f);
402 });
403 return f.get();
404 } else if (what == "osd_pool_stats") {
405 int64_t poolid = -ENOENT;
406 cluster_state.with_osdmap_and_pgmap([&](const OSDMap& osdmap,
407 const PGMap& pg_map) {
408 PyEval_RestoreThread(tstate);
409 f.open_array_section("pool_stats");
410 for (auto &p : osdmap.get_pools()) {
411 poolid = p.first;
412 pg_map.dump_pool_stats_and_io_rate(poolid, osdmap, &f, nullptr);
413 }
414 f.close_section();
415 });
416 return f.get();
417 } else if (what == "health") {
418 cluster_state.with_health(
419 [&f, &tstate](const ceph::bufferlist &health_json) {
420 PyEval_RestoreThread(tstate);
421 f.dump_string("json", health_json.to_str());
422 });
423 return f.get();
424 } else if (what == "mon_status") {
425 cluster_state.with_mon_status(
426 [&f, &tstate](const ceph::bufferlist &mon_status_json) {
427 PyEval_RestoreThread(tstate);
428 f.dump_string("json", mon_status_json.to_str());
429 });
430 return f.get();
431 } else if (what == "mgr_map") {
432 cluster_state.with_mgrmap([&f, &tstate](const MgrMap &mgr_map) {
433 PyEval_RestoreThread(tstate);
434 mgr_map.dump(&f);
435 });
436 return f.get();
437 } else {
438 derr << "Python module requested unknown data '" << what << "'" << dendl;
439 PyEval_RestoreThread(tstate);
440 Py_RETURN_NONE;
441 }
442 }
443
444 void ActivePyModules::start_one(PyModuleRef py_module)
445 {
446 std::lock_guard l(lock);
447
448 const auto name = py_module->get_name();
449 auto active_module = std::make_shared<ActivePyModule>(py_module, clog);
450
451 pending_modules.insert(name);
452 // Send all python calls down a Finisher to avoid blocking
453 // C++ code, and avoid any potential lock cycles.
454 finisher.queue(new LambdaContext([this, active_module, name](int) {
455 int r = active_module->load(this);
456 std::lock_guard l(lock);
457 pending_modules.erase(name);
458 if (r != 0) {
459 derr << "Failed to run module in active mode ('" << name << "')"
460 << dendl;
461 } else {
462 auto em = modules.emplace(name, active_module);
463 ceph_assert(em.second); // actually inserted
464
465 dout(4) << "Starting thread for " << name << dendl;
466 active_module->thread.create(active_module->get_thread_name());
467 }
468 }));
469 }
470
471 void ActivePyModules::shutdown()
472 {
473 std::lock_guard locker(lock);
474
475 // Signal modules to drop out of serve() and/or tear down resources
476 for (auto& [name, module] : modules) {
477 lock.unlock();
478 dout(10) << "calling module " << name << " shutdown()" << dendl;
479 module->shutdown();
480 dout(10) << "module " << name << " shutdown() returned" << dendl;
481 lock.lock();
482 }
483
484 // For modules implementing serve(), finish the threads where we
485 // were running that.
486 for (auto& [name, module] : modules) {
487 lock.unlock();
488 dout(10) << "joining module " << name << dendl;
489 module->thread.join();
490 dout(10) << "joined module " << name << dendl;
491 lock.lock();
492 }
493
494 cmd_finisher.wait_for_empty();
495 cmd_finisher.stop();
496
497 modules.clear();
498 }
499
500 void ActivePyModules::notify_all(const std::string &notify_type,
501 const std::string &notify_id)
502 {
503 std::lock_guard l(lock);
504
505 dout(10) << __func__ << ": notify_all " << notify_type << dendl;
506 for (auto& [name, module] : modules) {
507 // Send all python calls down a Finisher to avoid blocking
508 // C++ code, and avoid any potential lock cycles.
509 dout(15) << "queuing notify to " << name << dendl;
510 // workaround for https://bugs.llvm.org/show_bug.cgi?id=35984
511 finisher.queue(new LambdaContext([module=module, notify_type, notify_id]
512 (int r){
513 module->notify(notify_type, notify_id);
514 }));
515 }
516 }
517
518 void ActivePyModules::notify_all(const LogEntry &log_entry)
519 {
520 std::lock_guard l(lock);
521
522 dout(10) << __func__ << ": notify_all (clog)" << dendl;
523 for (auto& [name, module] : modules) {
524 // Send all python calls down a Finisher to avoid blocking
525 // C++ code, and avoid any potential lock cycles.
526 //
527 // Note intentional use of non-reference lambda binding on
528 // log_entry: we take a copy because caller's instance is
529 // probably ephemeral.
530 dout(15) << "queuing notify (clog) to " << name << dendl;
531 // workaround for https://bugs.llvm.org/show_bug.cgi?id=35984
532 finisher.queue(new LambdaContext([module=module, log_entry](int r){
533 module->notify_clog(log_entry);
534 }));
535 }
536 }
537
538 bool ActivePyModules::get_store(const std::string &module_name,
539 const std::string &key, std::string *val) const
540 {
541 PyThreadState *tstate = PyEval_SaveThread();
542 std::lock_guard l(lock);
543 PyEval_RestoreThread(tstate);
544
545 const std::string global_key = PyModule::config_prefix
546 + module_name + "/" + key;
547
548 dout(4) << __func__ << " key: " << global_key << dendl;
549
550 auto i = store_cache.find(global_key);
551 if (i != store_cache.end()) {
552 *val = i->second;
553 return true;
554 } else {
555 return false;
556 }
557 }
558
559 PyObject *ActivePyModules::dispatch_remote(
560 const std::string &other_module,
561 const std::string &method,
562 PyObject *args,
563 PyObject *kwargs,
564 std::string *err)
565 {
566 auto mod_iter = modules.find(other_module);
567 ceph_assert(mod_iter != modules.end());
568
569 return mod_iter->second->dispatch_remote(method, args, kwargs, err);
570 }
571
572 bool ActivePyModules::get_config(const std::string &module_name,
573 const std::string &key, std::string *val) const
574 {
575 const std::string global_key = PyModule::config_prefix
576 + module_name + "/" + key;
577
578 dout(20) << " key: " << global_key << dendl;
579
580 std::lock_guard lock(module_config.lock);
581
582 auto i = module_config.config.find(global_key);
583 if (i != module_config.config.end()) {
584 *val = i->second;
585 return true;
586 } else {
587 return false;
588 }
589 }
590
591 PyObject *ActivePyModules::get_typed_config(
592 const std::string &module_name,
593 const std::string &key,
594 const std::string &prefix) const
595 {
596 PyThreadState *tstate = PyEval_SaveThread();
597 std::string value;
598 std::string final_key;
599 bool found = false;
600 if (prefix.size()) {
601 final_key = prefix + "/" + key;
602 found = get_config(module_name, final_key, &value);
603 }
604 if (!found) {
605 final_key = key;
606 found = get_config(module_name, final_key, &value);
607 }
608 if (found) {
609 PyModuleRef module = py_module_registry.get_module(module_name);
610 PyEval_RestoreThread(tstate);
611 if (!module) {
612 derr << "Module '" << module_name << "' is not available" << dendl;
613 Py_RETURN_NONE;
614 }
615 dout(10) << __func__ << " " << final_key << " found: " << value << dendl;
616 return module->get_typed_option_value(key, value);
617 }
618 PyEval_RestoreThread(tstate);
619 if (prefix.size()) {
620 dout(10) << " [" << prefix << "/]" << key << " not found "
621 << dendl;
622 } else {
623 dout(10) << " " << key << " not found " << dendl;
624 }
625 Py_RETURN_NONE;
626 }
627
628 PyObject *ActivePyModules::get_store_prefix(const std::string &module_name,
629 const std::string &prefix) const
630 {
631 PyThreadState *tstate = PyEval_SaveThread();
632 std::lock_guard l(lock);
633 std::lock_guard lock(module_config.lock);
634 PyEval_RestoreThread(tstate);
635
636 const std::string base_prefix = PyModule::config_prefix
637 + module_name + "/";
638 const std::string global_prefix = base_prefix + prefix;
639 dout(4) << __func__ << " prefix: " << global_prefix << dendl;
640
641 PyFormatter f;
642
643 for (auto p = store_cache.lower_bound(global_prefix);
644 p != store_cache.end() && p->first.find(global_prefix) == 0;
645 ++p) {
646 f.dump_string(p->first.c_str() + base_prefix.size(), p->second);
647 }
648 return f.get();
649 }
650
651 void ActivePyModules::set_store(const std::string &module_name,
652 const std::string &key, const boost::optional<std::string>& val)
653 {
654 const std::string global_key = PyModule::config_prefix
655 + module_name + "/" + key;
656
657 Command set_cmd;
658 {
659 std::lock_guard l(lock);
660 if (val) {
661 store_cache[global_key] = *val;
662 } else {
663 store_cache.erase(global_key);
664 }
665
666 std::ostringstream cmd_json;
667 JSONFormatter jf;
668 jf.open_object_section("cmd");
669 if (val) {
670 jf.dump_string("prefix", "config-key set");
671 jf.dump_string("key", global_key);
672 jf.dump_string("val", *val);
673 } else {
674 jf.dump_string("prefix", "config-key del");
675 jf.dump_string("key", global_key);
676 }
677 jf.close_section();
678 jf.flush(cmd_json);
679 set_cmd.run(&monc, cmd_json.str());
680 }
681 set_cmd.wait();
682
683 if (set_cmd.r != 0) {
684 // config-key set will fail if mgr's auth key has insufficient
685 // permission to set config keys
686 // FIXME: should this somehow raise an exception back into Python land?
687 dout(0) << "`config-key set " << global_key << " " << val << "` failed: "
688 << cpp_strerror(set_cmd.r) << dendl;
689 dout(0) << "mon returned " << set_cmd.r << ": " << set_cmd.outs << dendl;
690 }
691 }
692
693 void ActivePyModules::set_config(const std::string &module_name,
694 const std::string &key, const boost::optional<std::string>& val)
695 {
696 module_config.set_config(&monc, module_name, key, val);
697 }
698
699 std::map<std::string, std::string> ActivePyModules::get_services() const
700 {
701 std::map<std::string, std::string> result;
702 std::lock_guard l(lock);
703 for (const auto& [name, module] : modules) {
704 std::string svc_str = module->get_uri();
705 if (!svc_str.empty()) {
706 result[name] = svc_str;
707 }
708 }
709
710 return result;
711 }
712
713 PyObject* ActivePyModules::with_perf_counters(
714 std::function<void(PerfCounterInstance& counter_instance, PerfCounterType& counter_type, PyFormatter& f)> fct,
715 const std::string &svc_name,
716 const std::string &svc_id,
717 const std::string &path) const
718 {
719 PyThreadState *tstate = PyEval_SaveThread();
720 std::lock_guard l(lock);
721 PyEval_RestoreThread(tstate);
722
723 PyFormatter f;
724 f.open_array_section(path.c_str());
725
726 auto metadata = daemon_state.get(DaemonKey{svc_name, svc_id});
727 if (metadata) {
728 std::lock_guard l2(metadata->lock);
729 if (metadata->perf_counters.instances.count(path)) {
730 auto counter_instance = metadata->perf_counters.instances.at(path);
731 auto counter_type = metadata->perf_counters.types.at(path);
732 fct(counter_instance, counter_type, f);
733 } else {
734 dout(4) << "Missing counter: '" << path << "' ("
735 << svc_name << "." << svc_id << ")" << dendl;
736 dout(20) << "Paths are:" << dendl;
737 for (const auto &i : metadata->perf_counters.instances) {
738 dout(20) << i.first << dendl;
739 }
740 }
741 } else {
742 dout(4) << "No daemon state for "
743 << svc_name << "." << svc_id << ")" << dendl;
744 }
745 f.close_section();
746 return f.get();
747 }
748
749 PyObject* ActivePyModules::get_counter_python(
750 const std::string &svc_name,
751 const std::string &svc_id,
752 const std::string &path)
753 {
754 auto extract_counters = [](
755 PerfCounterInstance& counter_instance,
756 PerfCounterType& counter_type,
757 PyFormatter& f)
758 {
759 if (counter_type.type & PERFCOUNTER_LONGRUNAVG) {
760 const auto &avg_data = counter_instance.get_data_avg();
761 for (const auto &datapoint : avg_data) {
762 f.open_array_section("datapoint");
763 f.dump_float("t", datapoint.t);
764 f.dump_unsigned("s", datapoint.s);
765 f.dump_unsigned("c", datapoint.c);
766 f.close_section();
767 }
768 } else {
769 const auto &data = counter_instance.get_data();
770 for (const auto &datapoint : data) {
771 f.open_array_section("datapoint");
772 f.dump_float("t", datapoint.t);
773 f.dump_unsigned("v", datapoint.v);
774 f.close_section();
775 }
776 }
777 };
778 return with_perf_counters(extract_counters, svc_name, svc_id, path);
779 }
780
781 PyObject* ActivePyModules::get_latest_counter_python(
782 const std::string &svc_name,
783 const std::string &svc_id,
784 const std::string &path)
785 {
786 auto extract_latest_counters = [](
787 PerfCounterInstance& counter_instance,
788 PerfCounterType& counter_type,
789 PyFormatter& f)
790 {
791 if (counter_type.type & PERFCOUNTER_LONGRUNAVG) {
792 const auto &datapoint = counter_instance.get_latest_data_avg();
793 f.dump_float("t", datapoint.t);
794 f.dump_unsigned("s", datapoint.s);
795 f.dump_unsigned("c", datapoint.c);
796 } else {
797 const auto &datapoint = counter_instance.get_latest_data();
798 f.dump_float("t", datapoint.t);
799 f.dump_unsigned("v", datapoint.v);
800 }
801 };
802 return with_perf_counters(extract_latest_counters, svc_name, svc_id, path);
803 }
804
805 PyObject* ActivePyModules::get_perf_schema_python(
806 const std::string &svc_type,
807 const std::string &svc_id)
808 {
809 PyThreadState *tstate = PyEval_SaveThread();
810 std::lock_guard l(lock);
811 PyEval_RestoreThread(tstate);
812
813 DaemonStateCollection daemons;
814
815 if (svc_type == "") {
816 daemons = daemon_state.get_all();
817 } else if (svc_id.empty()) {
818 daemons = daemon_state.get_by_service(svc_type);
819 } else {
820 auto key = DaemonKey{svc_type, svc_id};
821 // so that the below can be a loop in all cases
822 auto got = daemon_state.get(key);
823 if (got != nullptr) {
824 daemons[key] = got;
825 }
826 }
827
828 PyFormatter f;
829 if (!daemons.empty()) {
830 for (auto& [key, state] : daemons) {
831 f.open_object_section(ceph::to_string(key).c_str());
832
833 std::lock_guard l(state->lock);
834 for (auto ctr_inst_iter : state->perf_counters.instances) {
835 const auto &counter_name = ctr_inst_iter.first;
836 f.open_object_section(counter_name.c_str());
837 auto type = state->perf_counters.types[counter_name];
838 f.dump_string("description", type.description);
839 if (!type.nick.empty()) {
840 f.dump_string("nick", type.nick);
841 }
842 f.dump_unsigned("type", type.type);
843 f.dump_unsigned("priority", type.priority);
844 f.dump_unsigned("units", type.unit);
845 f.close_section();
846 }
847 f.close_section();
848 }
849 } else {
850 dout(4) << __func__ << ": No daemon state found for "
851 << svc_type << "." << svc_id << ")" << dendl;
852 }
853 return f.get();
854 }
855
856 PyObject *ActivePyModules::get_context()
857 {
858 PyThreadState *tstate = PyEval_SaveThread();
859 std::lock_guard l(lock);
860 PyEval_RestoreThread(tstate);
861
862 // Construct a capsule containing ceph context.
863 // Not incrementing/decrementing ref count on the context because
864 // it's the global one and it has process lifetime.
865 auto capsule = PyCapsule_New(g_ceph_context, nullptr, nullptr);
866 return capsule;
867 }
868
869 /**
870 * Helper for our wrapped types that take a capsule in their constructor.
871 */
872 PyObject *construct_with_capsule(
873 const std::string &module_name,
874 const std::string &clsname,
875 void *wrapped)
876 {
877 // Look up the OSDMap type which we will construct
878 PyObject *module = PyImport_ImportModule(module_name.c_str());
879 if (!module) {
880 derr << "Failed to import python module:" << dendl;
881 derr << handle_pyerror() << dendl;
882 }
883 ceph_assert(module);
884
885 PyObject *wrapper_type = PyObject_GetAttrString(
886 module, (const char*)clsname.c_str());
887 if (!wrapper_type) {
888 derr << "Failed to get python type:" << dendl;
889 derr << handle_pyerror() << dendl;
890 }
891 ceph_assert(wrapper_type);
892
893 // Construct a capsule containing an OSDMap.
894 auto wrapped_capsule = PyCapsule_New(wrapped, nullptr, nullptr);
895 ceph_assert(wrapped_capsule);
896
897 // Construct the python OSDMap
898 auto pArgs = PyTuple_Pack(1, wrapped_capsule);
899 auto wrapper_instance = PyObject_CallObject(wrapper_type, pArgs);
900 if (wrapper_instance == nullptr) {
901 derr << "Failed to construct python OSDMap:" << dendl;
902 derr << handle_pyerror() << dendl;
903 }
904 ceph_assert(wrapper_instance != nullptr);
905 Py_DECREF(pArgs);
906 Py_DECREF(wrapped_capsule);
907
908 Py_DECREF(wrapper_type);
909 Py_DECREF(module);
910
911 return wrapper_instance;
912 }
913
914 PyObject *ActivePyModules::get_osdmap()
915 {
916 OSDMap *newmap = new OSDMap;
917
918 PyThreadState *tstate = PyEval_SaveThread();
919 {
920 std::lock_guard l(lock);
921 cluster_state.with_osdmap([&](const OSDMap& o) {
922 newmap->deepish_copy_from(o);
923 });
924 }
925 PyEval_RestoreThread(tstate);
926
927 return construct_with_capsule("mgr_module", "OSDMap", (void*)newmap);
928 }
929
930 void ActivePyModules::set_health_checks(const std::string& module_name,
931 health_check_map_t&& checks)
932 {
933 bool changed = false;
934
935 lock.lock();
936 auto p = modules.find(module_name);
937 if (p != modules.end()) {
938 changed = p->second->set_health_checks(std::move(checks));
939 }
940 lock.unlock();
941
942 // immediately schedule a report to be sent to the monitors with the new
943 // health checks that have changed. This is done asynchronusly to avoid
944 // blocking python land. ActivePyModules::lock needs to be dropped to make
945 // lockdep happy:
946 //
947 // send_report callers: DaemonServer::lock -> PyModuleRegistery::lock
948 // active_start: PyModuleRegistry::lock -> ActivePyModules::lock
949 //
950 // if we don't release this->lock before calling schedule_tick a cycle is
951 // formed with the addition of ActivePyModules::lock -> DaemonServer::lock.
952 // This is still correct as send_report is run asynchronously under
953 // DaemonServer::lock.
954 if (changed)
955 server.schedule_tick(0);
956 }
957
958 int ActivePyModules::handle_command(
959 const ModuleCommand& module_command,
960 const MgrSession& session,
961 const cmdmap_t &cmdmap,
962 const bufferlist &inbuf,
963 std::stringstream *ds,
964 std::stringstream *ss)
965 {
966 lock.lock();
967 auto mod_iter = modules.find(module_command.module_name);
968 if (mod_iter == modules.end()) {
969 *ss << "Module '" << module_command.module_name << "' is not available";
970 lock.unlock();
971 return -ENOENT;
972 }
973
974 lock.unlock();
975 return mod_iter->second->handle_command(module_command, session, cmdmap,
976 inbuf, ds, ss);
977 }
978
979 void ActivePyModules::get_health_checks(health_check_map_t *checks)
980 {
981 std::lock_guard l(lock);
982 for (auto& [name, module] : modules) {
983 dout(15) << "getting health checks for " << name << dendl;
984 module->get_health_checks(checks);
985 }
986 }
987
988 void ActivePyModules::update_progress_event(
989 const std::string& evid,
990 const std::string& desc,
991 float progress)
992 {
993 std::lock_guard l(lock);
994 auto& pe = progress_events[evid];
995 pe.message = desc;
996 pe.progress = progress;
997 }
998
999 void ActivePyModules::complete_progress_event(const std::string& evid)
1000 {
1001 std::lock_guard l(lock);
1002 progress_events.erase(evid);
1003 }
1004
1005 void ActivePyModules::clear_all_progress_events()
1006 {
1007 std::lock_guard l(lock);
1008 progress_events.clear();
1009 }
1010
1011 void ActivePyModules::get_progress_events(std::map<std::string,ProgressEvent> *events)
1012 {
1013 std::lock_guard l(lock);
1014 *events = progress_events;
1015 }
1016
1017 void ActivePyModules::config_notify()
1018 {
1019 std::lock_guard l(lock);
1020 for (auto& [name, module] : modules) {
1021 // Send all python calls down a Finisher to avoid blocking
1022 // C++ code, and avoid any potential lock cycles.
1023 dout(15) << "notify (config) " << name << dendl;
1024 // workaround for https://bugs.llvm.org/show_bug.cgi?id=35984
1025 finisher.queue(new LambdaContext([module=module](int r){
1026 module->config_notify();
1027 }));
1028 }
1029 }
1030
1031 void ActivePyModules::set_uri(const std::string& module_name,
1032 const std::string &uri)
1033 {
1034 std::lock_guard l(lock);
1035
1036 dout(4) << " module " << module_name << " set URI '" << uri << "'" << dendl;
1037
1038 modules.at(module_name)->set_uri(uri);
1039 }
1040
1041 MetricQueryID ActivePyModules::add_osd_perf_query(
1042 const OSDPerfMetricQuery &query,
1043 const std::optional<OSDPerfMetricLimit> &limit)
1044 {
1045 return server.add_osd_perf_query(query, limit);
1046 }
1047
1048 void ActivePyModules::remove_osd_perf_query(MetricQueryID query_id)
1049 {
1050 int r = server.remove_osd_perf_query(query_id);
1051 if (r < 0) {
1052 dout(0) << "remove_osd_perf_query for query_id=" << query_id << " failed: "
1053 << cpp_strerror(r) << dendl;
1054 }
1055 }
1056
1057 PyObject *ActivePyModules::get_osd_perf_counters(MetricQueryID query_id)
1058 {
1059 std::map<OSDPerfMetricKey, PerformanceCounters> counters;
1060
1061 int r = server.get_osd_perf_counters(query_id, &counters);
1062 if (r < 0) {
1063 dout(0) << "get_osd_perf_counters for query_id=" << query_id << " failed: "
1064 << cpp_strerror(r) << dendl;
1065 Py_RETURN_NONE;
1066 }
1067
1068 PyFormatter f;
1069
1070 f.open_array_section("counters");
1071 for (auto &it : counters) {
1072 auto &key = it.first;
1073 auto &instance_counters = it.second;
1074 f.open_object_section("i");
1075 f.open_array_section("k");
1076 for (auto &sub_key : key) {
1077 f.open_array_section("s");
1078 for (size_t i = 0; i < sub_key.size(); i++) {
1079 f.dump_string(stringify(i).c_str(), sub_key[i]);
1080 }
1081 f.close_section(); // s
1082 }
1083 f.close_section(); // k
1084 f.open_array_section("c");
1085 for (auto &c : instance_counters) {
1086 f.open_array_section("p");
1087 f.dump_unsigned("0", c.first);
1088 f.dump_unsigned("1", c.second);
1089 f.close_section(); // p
1090 }
1091 f.close_section(); // c
1092 f.close_section(); // i
1093 }
1094 f.close_section(); // counters
1095
1096 return f.get();
1097 }
1098
1099 void ActivePyModules::cluster_log(const std::string &channel, clog_type prio,
1100 const std::string &message)
1101 {
1102 std::lock_guard l(lock);
1103
1104 auto cl = monc.get_log_client()->create_channel(channel);
1105 map<string,string> log_to_monitors;
1106 map<string,string> log_to_syslog;
1107 map<string,string> log_channel;
1108 map<string,string> log_prio;
1109 map<string,string> log_to_graylog;
1110 map<string,string> log_to_graylog_host;
1111 map<string,string> log_to_graylog_port;
1112 uuid_d fsid;
1113 string host;
1114 if (parse_log_client_options(g_ceph_context, log_to_monitors, log_to_syslog,
1115 log_channel, log_prio, log_to_graylog,
1116 log_to_graylog_host, log_to_graylog_port,
1117 fsid, host) == 0)
1118 cl->update_config(log_to_monitors, log_to_syslog,
1119 log_channel, log_prio, log_to_graylog,
1120 log_to_graylog_host, log_to_graylog_port,
1121 fsid, host);
1122 cl->do_log(prio, message);
1123 }
1124
1125 void ActivePyModules::register_client(std::string_view name, std::string addrs)
1126 {
1127 std::lock_guard l(lock);
1128
1129 entity_addrvec_t addrv;
1130 addrv.parse(addrs.data());
1131
1132 dout(7) << "registering msgr client handle " << addrv << dendl;
1133 py_module_registry.register_client(name, std::move(addrv));
1134 }
1135
1136 void ActivePyModules::unregister_client(std::string_view name, std::string addrs)
1137 {
1138 std::lock_guard l(lock);
1139
1140 entity_addrvec_t addrv;
1141 addrv.parse(addrs.data());
1142
1143 dout(7) << "unregistering msgr client handle " << addrv << dendl;
1144 py_module_registry.unregister_client(name, addrv);
1145 }