27#include <boost/filesystem.hpp>
49 (
"resource-monitor-interval-seconds", bpo::value<uint32_t>()->default_value(def_interval_in_secs),
50 "Time in seconds between two consecutive checks of resource usage. Should be between 1 and 300" )
51 (
"resource-monitor-space-threshold", bpo::value<uint32_t>()->default_value(def_space_threshold),
52 "Threshold in terms of percentage of used space vs total space. If used space is above (threshold - 5%), a warning is generated. Unless resource-monitor-not-shutdown-on-threshold-exceeded is enabled, a graceful shutdown is initiated if used space is above the threshold. The value should be between 6 and 99" )
53 (
"resource-monitor-not-shutdown-on-threshold-exceeded",
54 "Used to indicate nodeop will not shutdown when threshold is exceeded." )
55 (
"resource-monitor-warning-interval", bpo::value<uint32_t>()->default_value(def_monitor_warning_interval),
56 "Number of resource monitor intervals between two consecutive warnings when the threshold is hit. Should be between 1 and 450" )
61 dlog(
"plugin_initialize");
63 auto interval = options.at(
"resource-monitor-interval-seconds").as<
uint32_t>();
64 SYS_ASSERT(interval >= monitor_interval_min && interval <= monitor_interval_max, chain::plugin_config_exception,
65 "\"resource-monitor-interval-seconds\" must be between ${monitor_interval_min} and ${monitor_interval_max}", (
"monitor_interval_min", monitor_interval_min) (
"monitor_interval_max", monitor_interval_max));
67 ilog(
"Monitoring interval set to ${interval}", (
"interval", interval));
69 auto threshold = options.at(
"resource-monitor-space-threshold").as<
uint32_t>();
70 SYS_ASSERT(threshold >= space_threshold_min && threshold <= space_threshold_max, chain::plugin_config_exception,
71 "\"resource-monitor-space-threshold\" must be between ${space_threshold_min} and ${space_threshold_max}", (
"space_threshold_min", space_threshold_min) (
"space_threshold_max", space_threshold_max));
72 space_handler.
set_threshold(threshold, threshold - space_threshold_warning_diff);
73 ilog(
"Space usage threshold set to ${threshold}", (
"threshold", threshold));
75 if (options.count(
"resource-monitor-not-shutdown-on-threshold-exceeded")) {
78 ilog(
"Shutdown flag when threshold exceeded set to false");
82 ilog(
"Shutdown flag when threshold exceeded set to true");
85 auto warning_interval = options.at(
"resource-monitor-warning-interval").as<
uint32_t>();
86 SYS_ASSERT(warning_interval >= warning_interval_min && warning_interval <= warning_interval_max, chain::plugin_config_exception,
87 "\"resource-monitor-warning-interval\" must be between ${warning_interval_min} and ${warning_interval_max}", (
"warning_interval_min", warning_interval_min) (
"warning_interval_max", warning_interval_max));
89 ilog(
"Warning interval set to ${warning_interval}", (
"warning_interval", warning_interval));
94 ilog(
"Creating and starting monitor thread");
99 for (
auto& dir: directories_registered ) {
105 for (bfs::directory_iterator itr(dir); itr != bfs::directory_iterator(); ++itr) {
112 monitor_thread = std::thread( [
this] {
127 monitor_thread.join();
129 ilog(
"exit shutdown");
133 dlog(
"${path} registered to be monitored", (
"path",
path.
string()));
134 directories_registered.push_back(
path);
138 std::thread monitor_thread;
139 std::vector<bfs::path> directories_registered;
141 static constexpr uint32_t def_interval_in_secs = 2;
142 static constexpr uint32_t monitor_interval_min = 1;
143 static constexpr uint32_t monitor_interval_max = 300;
145 static constexpr uint32_t def_space_threshold = 90;
146 static constexpr uint32_t space_threshold_min = 6;
147 static constexpr uint32_t space_threshold_max = 99;
148 static constexpr uint32_t space_threshold_warning_diff = 5;
150 static constexpr uint32_t def_monitor_warning_interval = 30;
151 static constexpr uint32_t warning_interval_min = 1;
152 static constexpr uint32_t warning_interval_max = 450;
154 boost::asio::io_context ctx;
157 file_space_handler_t space_handler;
165 my->set_program_options(
cli, cfg);
169 my->plugin_initialize(options);
173 my->plugin_startup();
177 my->plugin_shutdown();
181 my->monitor_directory(
path );
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
wraps boost::filesystem::path to provide platform independent path manipulation.
std::string string() const
void set_threshold(uint32_t new_threshold, uint32_t new_warning_threshold)
void set_warning_interval(uint32_t new_warning_interval)
void add_file_system(const bfs::path &path_name)
void space_monitor_loop()
void set_shutdown_on_exceeded(bool new_shutdown_on_exceeded)
void set_sleep_time(uint32_t sleep_time)
void plugin_initialize(const appbase::variables_map &options)
void set_program_options(options_description &, options_description &cfg)
void monitor_directory(const bfs::path &path)
resource_monitor_plugin_impl()
virtual void set_program_options(options_description &, options_description &cfg) override
void monitor_directory(const bfs::path &path)
void plugin_initialize(const variables_map &options)
virtual ~resource_monitor_plugin()
resource_monitor_plugin()
Defines exception's used by fc.
bool is_directory(const path &p)
void set_os_thread_name(const string &name)