代码拉取完成,页面将自动刷新
From 1197ffd72a00ff90e9c4d74cf028946cc32f79d2 Mon Sep 17 00:00:00 2001
From: Robert Altnoeder <robert.altnoeder@linbit.com>
Date: Tue, 19 Mar 2024 16:14:05 +0100
Subject: [PATCH 083/100] DRBDmon: Enable loading saved DRBD events, adjust
node name related code
---
user/drbdmon/Configurable.h | 4 +-
user/drbdmon/DrbdMon.cpp | 43 +++++++++++--------
user/drbdmon/DrbdMon.h | 6 ++-
user/drbdmon/MonitorEnvironment.h | 3 +-
user/drbdmon/drbdmon_main.cpp | 37 ++++++++--------
.../subprocess/EventsSourceSpawner.cpp | 29 ++++++++++---
user/drbdmon/subprocess/EventsSourceSpawner.h | 8 ++--
user/drbdmon/terminal/ComponentsHub.cpp | 1 +
user/drbdmon/terminal/ComponentsHub.h | 1 +
user/drbdmon/terminal/DisplayCommonImpl.cpp | 11 +++++
user/drbdmon/terminal/DisplayController.cpp | 11 +++--
user/drbdmon/terminal/DisplayController.h | 2 +
12 files changed, 100 insertions(+), 56 deletions(-)
diff --git a/user/drbdmon/Configurable.h b/user/drbdmon/Configurable.h
index 53387f1e..1d6bfd93 100644
--- a/user/drbdmon/Configurable.h
+++ b/user/drbdmon/Configurable.h
@@ -19,10 +19,10 @@ class Configurable
virtual void options_help() noexcept = 0;
// @throws std::bad_alloc, ConfigurationException
- virtual void set_flag(std::string& key) = 0;
+ virtual void set_flag(const std::string& key) = 0;
// @throws std::bad_alloc, ConfigurationException
- virtual void set_option(std::string& key, std::string& value) = 0;
+ virtual void set_option(const std::string& key, const std::string& value) = 0;
};
#endif /* CONFIGURABLE_H */
diff --git a/user/drbdmon/DrbdMon.cpp b/user/drbdmon/DrbdMon.cpp
index b9ac34d8..2c217416 100644
--- a/user/drbdmon/DrbdMon.cpp
+++ b/user/drbdmon/DrbdMon.cpp
@@ -34,9 +34,11 @@ extern "C"
const std::string DrbdMon::OPT_HELP_KEY = "help";
const std::string DrbdMon::OPT_VERSION_KEY = "version";
const std::string DrbdMon::OPT_FREQ_LMT_KEY = "freqlmt";
+const std::string DrbdMon::OPT_EVENTS_LOG_KEY = "events-log";
const ConfigOption DrbdMon::OPT_HELP(true, OPT_HELP_KEY);
const ConfigOption DrbdMon::OPT_VERSION(true, OPT_VERSION_KEY);
const ConfigOption DrbdMon::OPT_FREQ_LMT(false, OPT_FREQ_LMT_KEY);
+const ConfigOption DrbdMon::OPT_EVENTS_LOG(false, OPT_EVENTS_LOG_KEY);
const char* DrbdMon::ENV_COLOR_MODE = "DRBDMON_COLORS";
const char* DrbdMon::COLOR_MODE_EXTENDED = "extended";
@@ -108,11 +110,24 @@ void DrbdMon::run()
{
try
{
+ // Interval timer initialization
+ // Must be constructed before configuring the Configurable instances
+ interval_timer_mgr = std::unique_ptr<IntervalTimer>(
+ new IntervalTimer(log, bounds<uint16_t>(0, config.dsp_interval, MAX_INTERVAL))
+ );
+ timer_available = config.dsp_interval != 0;
+ timer_armed = false;
+
+ configurables = std::unique_ptr<Configurable*[]>(new Configurable*[2]);
+ configurables[0] = dynamic_cast<Configurable*> (this);
+ configurables[1] = nullptr;
+ configure_options();
+
event_props = std::unique_ptr<PropsMap>(new PropsMap(&comparators::compare_string));
events_source = std::unique_ptr<EventsSourceSpawner>(new EventsSourceSpawner(log));
- events_source->spawn_source();
+ events_source->spawn_source(&(mon_env.events_file_path));
events_io = std::unique_ptr<EventsIo>(
new EventsIo(events_source->get_events_out_fd(), events_source->get_events_err_fd())
);
@@ -125,21 +140,6 @@ void DrbdMon::run()
new SubProcessNotification(dynamic_cast<EventsIoWakeup*> (events_io.get()))
);
- // Interval timer initialization
- // Must be constructed before configuring the Configurable instances
- interval_timer_mgr = std::unique_ptr<IntervalTimer>(
- new IntervalTimer(log, bounds<uint16_t>(0, config.dsp_interval, MAX_INTERVAL))
- );
- timer_available = config.dsp_interval != 0;
- timer_armed = false;
-
- configurables = std::unique_ptr<Configurable*[]>(new Configurable*[3]);
- configurables[0] = dynamic_cast<Configurable*> (this);
- // configurables[1] = dynamic_cast<Configurable*> (display_impl);
- configurables[1] = nullptr;
- configurables[2] = nullptr;
- configure_options();
-
// Cleanup any zombies that might not have been collected,
// because SIGCHLD is blocked during reinitialization
events_source->cleanup_child_processes();
@@ -886,6 +886,7 @@ void DrbdMon::announce_options(Configurator& collector)
collector.add_config_option(owner, OPT_HELP);
collector.add_config_option(owner, OPT_VERSION);
collector.add_config_option(owner, OPT_FREQ_LMT);
+ collector.add_config_option(owner, OPT_EVENTS_LOG);
}
void DrbdMon::options_help() noexcept
@@ -894,6 +895,7 @@ void DrbdMon::options_help() noexcept
std::cerr << DrbdMonConsts::PROGRAM_NAME << " configuration options:\n";
std::cerr << " --version Display version information\n";
std::cerr << " --help Display help\n";
+ std::cerr << " --events-log <file> Display the DRBD state saved in the specified file\n";
std::cerr << " --freqlmt <interval> Set a frequency limit for display updates\n";
std::cerr << " <interval> Minimum delay between display updates [integer]\n";
std::cerr << " Supported unit suffixes: s (seconds), ms (milliseconds)\n";
@@ -907,7 +909,7 @@ void DrbdMon::options_help() noexcept
}
// @throws std::bad_alloc, ConfigurationException
-void DrbdMon::set_flag(std::string& key)
+void DrbdMon::set_flag(const std::string& key)
{
if (key == OPT_HELP.key)
{
@@ -929,7 +931,7 @@ void DrbdMon::set_flag(std::string& key)
}
// @throws std::bad_alloc, ConfigurationException
-void DrbdMon::set_option(std::string& key, std::string& value)
+void DrbdMon::set_option(const std::string& key, const std::string& value)
{
if (key == OPT_FREQ_LMT.key)
{
@@ -984,6 +986,11 @@ void DrbdMon::set_option(std::string& key, std::string& value)
throw ConfigurationException();
}
}
+ else
+ if (key == OPT_EVENTS_LOG.key)
+ {
+ mon_env.events_file_path = value;
+ }
}
uint32_t DrbdMon::get_problem_count() const noexcept
diff --git a/user/drbdmon/DrbdMon.h b/user/drbdmon/DrbdMon.h
index 3515446c..330e4445 100644
--- a/user/drbdmon/DrbdMon.h
+++ b/user/drbdmon/DrbdMon.h
@@ -42,9 +42,11 @@ class DrbdMon : public DrbdMonCore, public Configurable, public Configurator
static const std::string OPT_HELP_KEY;
static const std::string OPT_VERSION_KEY;
static const std::string OPT_FREQ_LMT_KEY;
+ static const std::string OPT_EVENTS_LOG_KEY;
static const ConfigOption OPT_HELP;
static const ConfigOption OPT_VERSION;
static const ConfigOption OPT_FREQ_LMT;
+ static const ConfigOption OPT_EVENTS_LOG;
// Environment variable for color mode selection
static const char* ENV_COLOR_MODE;
@@ -120,10 +122,10 @@ class DrbdMon : public DrbdMonCore, public Configurable, public Configurator
virtual void options_help() noexcept override;
// @throws std::bad_alloc
- virtual void set_flag(std::string& key) override;
+ virtual void set_flag(const std::string& key) override;
// @throws std::bad_alloc
- virtual void set_option(std::string& key, std::string& value) override;
+ virtual void set_option(const std::string& key, const std::string& value) override;
virtual uint32_t get_problem_count() const noexcept override;
diff --git a/user/drbdmon/MonitorEnvironment.h b/user/drbdmon/MonitorEnvironment.h
index 758b2d3e..35e1cd9b 100644
--- a/user/drbdmon/MonitorEnvironment.h
+++ b/user/drbdmon/MonitorEnvironment.h
@@ -18,8 +18,9 @@ class MonitorEnvironment
std::unique_ptr<MessageLog> log;
std::unique_ptr<MessageLog> debug_log;
std::unique_ptr<Configuration> config;
- std::unique_ptr<std::string> node_name_mgr;
+ std::string node_name;
std::string config_file_path;
+ std::string events_file_path;
};
#endif /* MONITORENVIRONMENT_H */
diff --git a/user/drbdmon/drbdmon_main.cpp b/user/drbdmon/drbdmon_main.cpp
index be29b5ac..36c1b518 100644
--- a/user/drbdmon/drbdmon_main.cpp
+++ b/user/drbdmon/drbdmon_main.cpp
@@ -65,9 +65,9 @@ namespace drbdmon
static void clear_screen() noexcept;
static void cond_print_error_header(
bool& error_header_printed,
- const std::unique_ptr<std::string>& node_name
+ const std::string& node_name
) noexcept;
- static void set_window_title(const std::string* const node_name);
+ static void set_window_title(const std::string& node_name);
static void clear_window_title();
}
@@ -164,7 +164,7 @@ int main(int argc, char* argv[])
}
else
{
- drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name_mgr);
+ drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name);
std::cout << "Application start failed, cannot adjust security settings" << std::endl;
exit_code = drbdmon::EXIT_ERR_SECURITY;
}
@@ -206,15 +206,13 @@ static void drbdmon::monitor_loop(
mon_env.error_header_printed = false;
try
{
- if (mon_env.node_name_mgr == nullptr)
+ if (mon_env.node_name.empty())
{
- system_api::init_node_name(mon_env.node_name_mgr);
+ system_api::init_node_name(mon_env.node_name);
}
- drbdmon::set_window_title(mon_env.node_name_mgr.get());
+ drbdmon::set_window_title(mon_env.node_name);
- const std::unique_ptr<DrbdMon> dm_instance(
- new DrbdMon(argc, argv, mon_env)
- );
+ const std::unique_ptr<DrbdMon> dm_instance(new DrbdMon(argc, argv, mon_env));
dm_instance->run();
if (mon_env.fin_action != DrbdMon::finish_action::TERMINATE_NO_CLEAR)
{
@@ -237,7 +235,7 @@ static void drbdmon::monitor_loop(
{
if (mon_env.log->has_entries())
{
- drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name_mgr);
+ drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name);
std::cout << "** " << DrbdMonConsts::PROGRAM_NAME << " messages log\n\n";
mon_env.log->display_messages(std::cout);
std::cout << std::endl;
@@ -246,7 +244,7 @@ static void drbdmon::monitor_loop(
if (mon_env.fail_data == DrbdMon::fail_info::OUT_OF_MEMORY)
{
- drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name_mgr);
+ drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name);
std::cout << "** " << DrbdMonConsts::PROGRAM_NAME <<
": Out of memory, trying to restart" << std::endl;
}
@@ -258,7 +256,7 @@ static void drbdmon::monitor_loop(
else
if (mon_env.fin_action == DrbdMon::finish_action::RESTART_DELAYED)
{
- drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name_mgr);
+ drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name);
std::cout << "** " << DrbdMonConsts::PROGRAM_NAME << ": Reinitializing in " <<
(static_cast<unsigned long> (drbdmon::DELAY_MSECS) / 1000) << " seconds" << std::endl;
@@ -282,7 +280,7 @@ static void drbdmon::monitor_loop(
else
if (mon_env.fin_action == DrbdMon::finish_action::RESTART_IMMED)
{
- drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name_mgr);
+ drbdmon::cond_print_error_header(mon_env.error_header_printed, mon_env.node_name);
std::cout << "** " << DrbdMonConsts::PROGRAM_NAME << ": Reinitializing immediately" << std::endl;
}
}
@@ -295,26 +293,25 @@ static void drbdmon::clear_screen() noexcept
static void drbdmon::cond_print_error_header(
bool& error_header_printed,
- const std::unique_ptr<std::string>& node_name
+ const std::string& node_name
) noexcept
{
if (!error_header_printed)
{
std::cout << "** " << DrbdMonConsts::PROGRAM_NAME << " v" << DrbdMonConsts::UTILS_VERSION << '\n';
- if (node_name != nullptr)
+ if (!node_name.empty())
{
- std::string* node_name_ptr = node_name.get();
- std::cout << " Node " << *node_name_ptr << '\n';
+ std::cout << " Node " << node_name << '\n';
}
error_header_printed = true;
}
}
-static void drbdmon::set_window_title(const std::string* const node_name)
+static void drbdmon::set_window_title(const std::string& node_name)
{
- if (node_name != nullptr)
+ if (!node_name.empty())
{
- std::cout << "\x1B]2;" << WINDOW_TITLE_APP << "(Node " << *node_name << ")\x07";
+ std::cout << "\x1B]2;" << WINDOW_TITLE_APP << "(Node " << node_name << ")\x07";
}
else
{
diff --git a/user/drbdmon/subprocess/EventsSourceSpawner.cpp b/user/drbdmon/subprocess/EventsSourceSpawner.cpp
index 4f372565..20fa13be 100644
--- a/user/drbdmon/subprocess/EventsSourceSpawner.cpp
+++ b/user/drbdmon/subprocess/EventsSourceSpawner.cpp
@@ -13,14 +13,15 @@ extern "C"
#include <errno.h>
}
-const char* EventsSourceSpawner::EVENTS_PROGRAM = "drbdsetup";
-const char* EventsSourceSpawner::EVENTS_PROGRAM_ARGS[] =
+const char* const EventsSourceSpawner::EVENTS_PROGRAM = "drbdsetup";
+const char* const EventsSourceSpawner::EVENTS_PROGRAM_ARGS[] =
{
"drbdsetup",
"events2",
"all",
nullptr
};
+const char* const EventsSourceSpawner::SAVED_EVENTS_PROGRAM = "eventsfeeder";
EventsSourceSpawner::EventsSourceSpawner(MessageLog& logRef):
log(logRef)
@@ -73,7 +74,7 @@ int EventsSourceSpawner::get_events_err_fd()
}
// @throws std::bad_alloc, EventSourceException
-void EventsSourceSpawner::spawn_source()
+void EventsSourceSpawner::spawn_source(const std::string* const save_file_path_ptr)
{
// Initialize the pipes
posix::Pipe out_pipe_mgr(&out_pipe_fd);
@@ -89,7 +90,23 @@ void EventsSourceSpawner::spawn_source()
// Initialize the datastructures for posix_spawn())
posix::SpawnFileActions pipe_init;
posix::SpawnAttr spawn_attr;
- posix::SpawnArgs spawn_args(EVENTS_PROGRAM_ARGS);
+ std::unique_ptr<posix::SpawnArgs> spawn_args;
+
+ const char* provider_pgm = nullptr;
+ if (save_file_path_ptr == nullptr || save_file_path_ptr->empty())
+ {
+ provider_pgm = EVENTS_PROGRAM;
+ spawn_args = std::unique_ptr<posix::SpawnArgs>(new posix::SpawnArgs(EVENTS_PROGRAM_ARGS));
+ }
+ else
+ {
+ provider_pgm = SAVED_EVENTS_PROGRAM;
+ const char* args[3];
+ args[0] = SAVED_EVENTS_PROGRAM;
+ args[1] = save_file_path_ptr->c_str();
+ args[2] = nullptr;
+ spawn_args = std::unique_ptr<posix::SpawnArgs>(new posix::SpawnArgs(args));
+ }
// Redirect stdout to the pipes write side
checked_int_rc(
@@ -132,8 +149,8 @@ void EventsSourceSpawner::spawn_source()
checked_int_rc(posix_spawnattr_setflags(spawn_attr.attr, POSIX_SPAWN_SETSIGDEF));
// Attempt to spawn the events source program
- int spawn_rc = posix_spawnp(&spawned_pid, EVENTS_PROGRAM, pipe_init.actions, spawn_attr.attr,
- spawn_args.args, environ);
+ int spawn_rc = posix_spawnp(&spawned_pid, provider_pgm, pipe_init.actions, spawn_attr.attr,
+ spawn_args->args, environ);
if (spawn_rc != 0)
{
spawned_pid = -1;
diff --git a/user/drbdmon/subprocess/EventsSourceSpawner.h b/user/drbdmon/subprocess/EventsSourceSpawner.h
index 0c4a1fa3..51902039 100644
--- a/user/drbdmon/subprocess/EventsSourceSpawner.h
+++ b/user/drbdmon/subprocess/EventsSourceSpawner.h
@@ -3,6 +3,7 @@
#include <default_types.h>
#include <new>
+#include <string>
#include <stdexcept>
#include <MessageLog.h>
@@ -16,8 +17,9 @@ extern "C"
class EventsSourceSpawner
{
public:
- static const char* EVENTS_PROGRAM;
- static const char* EVENTS_PROGRAM_ARGS[];
+ static const char* const EVENTS_PROGRAM;
+ static const char* const EVENTS_PROGRAM_ARGS[];
+ static const char* const SAVED_EVENTS_PROGRAM;
EventsSourceSpawner(MessageLog& logRef);
virtual ~EventsSourceSpawner();
@@ -32,7 +34,7 @@ class EventsSourceSpawner
virtual int get_events_err_fd();
// @throws std::bad_alloc, EventSourceException
- virtual void spawn_source();
+ virtual void spawn_source(const std::string* const save_file_path_ptr);
// @throws EventsSourceException
virtual void cleanup_child_processes();
diff --git a/user/drbdmon/terminal/ComponentsHub.cpp b/user/drbdmon/terminal/ComponentsHub.cpp
index 94443ccd..5c1a3fd5 100644
--- a/user/drbdmon/terminal/ComponentsHub.cpp
+++ b/user/drbdmon/terminal/ComponentsHub.cpp
@@ -48,6 +48,7 @@ void ComponentsHub::verify() const
log == nullptr ||
debug_log == nullptr ||
node_name == nullptr ||
+ events_file == nullptr ||
ansi_ctl == nullptr ||
style_coll == nullptr ||
active_color_table == nullptr ||
diff --git a/user/drbdmon/terminal/ComponentsHub.h b/user/drbdmon/terminal/ComponentsHub.h
index 28f36a2b..2266214f 100644
--- a/user/drbdmon/terminal/ComponentsHub.h
+++ b/user/drbdmon/terminal/ComponentsHub.h
@@ -42,6 +42,7 @@ class ComponentsHub
DrbdCommands* drbd_cmd_exec {nullptr};
const Configuration* config {nullptr};
const std::string* node_name {nullptr};
+ const std::string* events_file {nullptr};
AnsiControl* ansi_ctl {nullptr};
DisplayStyleCollection* style_coll {nullptr};
diff --git a/user/drbdmon/terminal/DisplayCommonImpl.cpp b/user/drbdmon/terminal/DisplayCommonImpl.cpp
index 15babde9..8427b2fc 100644
--- a/user/drbdmon/terminal/DisplayCommonImpl.cpp
+++ b/user/drbdmon/terminal/DisplayCommonImpl.cpp
@@ -38,6 +38,17 @@ void DisplayCommonImpl::display_header() const
"%s%s%s", clr.title_bar.c_str(), AnsiControl::ANSI_CLEAR_LINE.c_str(),
program_version.c_str()
);
+ if (dsp_comp_hub.events_file != nullptr && !dsp_comp_hub.events_file->empty())
+ {
+ dsp_io.write_text(" | ");
+ dsp_io.write_text(clr.alert_label.c_str());
+ dsp_io.write_text("File ");
+ dsp_io.write_string_field(
+ *(dsp_comp_hub.events_file),
+ dsp_comp_hub.term_cols - program_version.length() - 8, false
+ );
+ }
+ else
if (dsp_comp_hub.node_name != nullptr)
{
dsp_io.write_text(" | Node ");
diff --git a/user/drbdmon/terminal/DisplayController.cpp b/user/drbdmon/terminal/DisplayController.cpp
index 2eff7be6..ccf3e7bc 100644
--- a/user/drbdmon/terminal/DisplayController.cpp
+++ b/user/drbdmon/terminal/DisplayController.cpp
@@ -49,6 +49,8 @@ DisplayController::DisplayController(
core_instance(core_instance_ref),
mon_env(mon_env_ref)
{
+ events_file = mon_env.sys_api->file_name_for_path(mon_env.events_file_path);
+
dsp_map = std::unique_ptr<DisplayMap>(new DisplayMap(&comparators::compare_string));
term_ctl_mgr = mon_env.sys_api->create_terminal_control();
@@ -62,16 +64,17 @@ DisplayController::DisplayController(
sub_proc_queue_mgr = std::unique_ptr<SubProcessQueue>(new SubProcessQueue());
dsp_comp_hub_mgr->core_instance = &core_instance;
- dsp_comp_hub_mgr->sys_api = &(mon_env.sys_api);
+ dsp_comp_hub_mgr->sys_api = mon_env.sys_api.get();
dsp_comp_hub_mgr->dsp_selector = dynamic_cast<DisplaySelector*> (this);
dsp_comp_hub_mgr->dsp_io = dsp_io_mgr.get();
dsp_comp_hub_mgr->dsp_shared = dsp_shared_mgr.get();
dsp_comp_hub_mgr->term_size = dynamic_cast<TermSize*> (term_size_mgr.get());
dsp_comp_hub_mgr->rsc_map = &rsc_map_ref;
dsp_comp_hub_mgr->prb_rsc_map = &prb_rsc_map_ref;
- dsp_comp_hub_mgr->log = &(mon_env.log);
- dsp_comp_hub_mgr->debug_log = &(mon_env.debug_log);
- dsp_comp_hub_mgr->node_name = mon_env.node_name_mgr.get();
+ dsp_comp_hub_mgr->log = mon_env.log.get();
+ dsp_comp_hub_mgr->debug_log = mon_env.debug_log.get();
+ dsp_comp_hub_mgr->node_name = &(mon_env.node_name);
+ dsp_comp_hub_mgr->events_file = &events_file;
dsp_comp_hub_mgr->style_coll = dsp_styles_mgr.get();
dsp_comp_hub_mgr->ansi_ctl = ansi_ctl_mgr.get();
dsp_comp_hub_mgr->sub_proc_queue = sub_proc_queue_mgr.get();
diff --git a/user/drbdmon/terminal/DisplayController.h b/user/drbdmon/terminal/DisplayController.h
index 4dee5300..8389c2b5 100644
--- a/user/drbdmon/terminal/DisplayController.h
+++ b/user/drbdmon/terminal/DisplayController.h
@@ -86,6 +86,8 @@ class DisplayController : public GenericDisplay, public DisplaySelector
DrbdMonCore& core_instance;
MonitorEnvironment& mon_env;
+ std::string events_file;
+
std::unique_ptr<DisplayMap> dsp_map;
std::unique_ptr<TerminalControl> term_ctl_mgr;
--
2.33.1.windows.1
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。