diff --git a/system/system_monitor/config/net_monitor.param.yaml b/system/system_monitor/config/net_monitor.param.yaml index f2c68adda2efe..7a1e5aeff2db1 100644 --- a/system/system_monitor/config/net_monitor.param.yaml +++ b/system/system_monitor/config/net_monitor.param.yaml @@ -2,6 +2,7 @@ ros__parameters: devices: ["*"] monitor_program: "greengrass" + enable_traffic_monitor: true crc_error_check_duration: 1 crc_error_count_threshold: 1 reassembles_failed_check_duration: 1 diff --git a/system/system_monitor/include/system_monitor/net_monitor/net_monitor.hpp b/system/system_monitor/include/system_monitor/net_monitor/net_monitor.hpp index b1acac7c6ea78..4714d980fd882 100644 --- a/system/system_monitor/include/system_monitor/net_monitor/net_monitor.hpp +++ b/system/system_monitor/include/system_monitor/net_monitor/net_monitor.hpp @@ -227,6 +227,11 @@ class NetMonitor : public rclcpp::Node */ void send_start_nethogs_request(); + /** + * @brief Send request to skip nethogs + */ + void send_skip_nethogs_request(); + /** * @brief Get result of nethogs * @param [out] result result of nethogs @@ -291,6 +296,7 @@ class NetMonitor : public rclcpp::Node int getifaddrs_error_code_; //!< @brief Error code set by getifaddrs() std::vector network_list_; //!< @brief List of Network information + bool enable_traffic_monitor_; //!< @brief enable nethogs std::string monitor_program_; //!< @brief nethogs monitor program name std::string socket_path_; //!< @brief Path of UNIX domain socket boost::asio::io_service io_service_; //!< @brief Core I/O functionality diff --git a/system/system_monitor/include/system_monitor/traffic_reader/traffic_reader_common.hpp b/system/system_monitor/include/system_monitor/traffic_reader/traffic_reader_common.hpp index 187df18735575..386facc3e0355 100644 --- a/system/system_monitor/include/system_monitor/traffic_reader/traffic_reader_common.hpp +++ b/system/system_monitor/include/system_monitor/traffic_reader/traffic_reader_common.hpp @@ -35,6 +35,7 @@ enum Request { NONE = 0, START_NETHOGS, GET_RESULT, + SKIP_NETHOGS, }; /** diff --git a/system/system_monitor/include/system_monitor/traffic_reader/traffic_reader_service.hpp b/system/system_monitor/include/system_monitor/traffic_reader/traffic_reader_service.hpp index 0da67d95705ae..2b2f0de4a1ae6 100644 --- a/system/system_monitor/include/system_monitor/traffic_reader/traffic_reader_service.hpp +++ b/system/system_monitor/include/system_monitor/traffic_reader/traffic_reader_service.hpp @@ -69,6 +69,11 @@ class TrafficReaderService */ void start_nethogs(boost::archive::text_iarchive & archive); + /** + * @brief Skip nethogs + */ + void skip_nethogs(); + /** * @brief Get command line of process from nethogs output * @param[in] line nethogs output diff --git a/system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp b/system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp index 836c4b509ffa7..927f1b175b46b 100644 --- a/system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp +++ b/system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp @@ -23,6 +23,7 @@ #include #include +#include #include namespace process = boost::process; @@ -120,6 +121,9 @@ void TrafficReaderService::handle_message(const char * buffer) case Request::GET_RESULT: get_result(); break; + case Request::SKIP_NETHOGS: + skip_nethogs(); + break; default: syslog(LOG_WARNING, "Unknown message. %d\n", request_id); break; @@ -153,6 +157,19 @@ void TrafficReaderService::start_nethogs(boost::archive::text_iarchive & archive thread_ = std::thread(&TrafficReaderService::execute_nethogs, this); } +void TrafficReaderService::skip_nethogs() +{ + syslog(LOG_INFO, "Skipping nethogs...\n"); + + if (thread_.joinable()) { + { + std::lock_guard lock(mutex_); + stop_ = true; + } + thread_.join(); + } +} + void TrafficReaderService::get_result() { // Inform ros node @@ -234,7 +251,9 @@ void TrafficReaderService::execute_nethogs() } } - c.terminate(); + // Send SIGKILL instead of using c.terminate() to avoid a zombie process + kill(c.id(), SIGKILL); + c.wait(); std::ostringstream out_stream; is_error >> out_stream.rdbuf(); diff --git a/system/system_monitor/src/net_monitor/net_monitor.cpp b/system/system_monitor/src/net_monitor/net_monitor.cpp index db2e23a44215a..9c13c5ea10347 100644 --- a/system/system_monitor/src/net_monitor/net_monitor.cpp +++ b/system/system_monitor/src/net_monitor/net_monitor.cpp @@ -41,6 +41,7 @@ NetMonitor::NetMonitor(const rclcpp::NodeOptions & options) device_params_( declare_parameter>("devices", std::vector())), getifaddrs_error_code_(0), + enable_traffic_monitor_(declare_parameter("enable_traffic_monitor", true)), monitor_program_(declare_parameter("monitor_program", "greengrass")), socket_path_(declare_parameter("socket_path", traffic_reader_service::socket_path)), crc_error_check_duration_(declare_parameter("crc_error_check_duration", 1)), @@ -81,7 +82,11 @@ NetMonitor::NetMonitor(const rclcpp::NodeOptions & options) get_reassembles_failed_column_index(); // Send request to start nethogs - send_start_nethogs_request(); + if (enable_traffic_monitor_) { + send_start_nethogs_request(); + } else { + send_skip_nethogs_request(); + } } NetMonitor::~NetMonitor() @@ -234,38 +239,43 @@ void NetMonitor::monitor_traffic(diagnostic_updater::DiagnosticStatusWrapper & s // Remember start time to measure elapsed time const auto t_start = SystemMonitorUtility::startMeasurement(); - // Get result of nethogs - traffic_reader_service::Result result; - get_nethogs_result(result); - - // traffic_reader result to output - if (result.error_code != EXIT_SUCCESS) { - status.summary(DiagStatus::ERROR, "traffic_reader error"); - status.add("error", result.output); - } else { - status.summary(DiagStatus::OK, "OK"); + if (enable_traffic_monitor_) { + // Get result of nethogs + traffic_reader_service::Result result; + get_nethogs_result(result); - if (result.output.empty()) { - status.add("nethogs: result", fmt::format("No data monitored: {}", monitor_program_)); + // traffic_reader result to output + if (result.error_code != EXIT_SUCCESS) { + status.summary(DiagStatus::ERROR, "traffic_reader error"); + status.add("error", result.output); } else { - std::stringstream lines{result.output}; - std::string line; - std::vector list; - int index = 0; - while (std::getline(lines, line)) { - if (line.empty()) continue; - - boost::split(list, line, boost::is_any_of("\t"), boost::token_compress_on); - if (list.size() > 3) { - status.add(fmt::format("nethogs {}: program", index), list[3].c_str()); - status.add(fmt::format("nethogs {}: sent (KB/s)", index), list[1].c_str()); - status.add(fmt::format("nethogs {}: received (KB/sec)", index), list[2].c_str()); - } else { - status.add(fmt::format("nethogs {}: result", index), line); + status.summary(DiagStatus::OK, "OK"); + + if (result.output.empty()) { + status.add("nethogs: result", fmt::format("No data monitored: {}", monitor_program_)); + } else { + std::stringstream lines{result.output}; + std::string line; + std::vector list; + int index = 0; + while (std::getline(lines, line)) { + if (line.empty()) continue; + + boost::split(list, line, boost::is_any_of("\t"), boost::token_compress_on); + if (list.size() > 3) { + status.add(fmt::format("nethogs {}: program", index), list[3].c_str()); + status.add(fmt::format("nethogs {}: sent (KB/s)", index), list[1].c_str()); + status.add(fmt::format("nethogs {}: received (KB/sec)", index), list[2].c_str()); + } else { + status.add(fmt::format("nethogs {}: result", index), line); + } + ++index; } - ++index; } } + } else { + status.summary(DiagStatus::OK, "OK"); + status.add("nethogs: result", "traffic monitor is NOT activated"); } // Measure elapsed time since start time and report @@ -624,6 +634,21 @@ void NetMonitor::send_start_nethogs_request() close_connection(); } +void NetMonitor::send_skip_nethogs_request() +{ + // Connect to boot/shutdown service + if (!connect_service()) { + close_connection(); + return; + } + + // Send data to traffic-reader service + send_data(traffic_reader_service::SKIP_NETHOGS); + + // Close connection with traffic-reader service + close_connection(); +} + void NetMonitor::get_nethogs_result(traffic_reader_service::Result & result) { // Connect to traffic-reader service