Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(system_monitor): add on/off config for network traffic monitor #9069

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions system/system_monitor/config/net_monitor.param.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -291,6 +296,7 @@ class NetMonitor : public rclcpp::Node
int getifaddrs_error_code_; //!< @brief Error code set by getifaddrs()
std::vector<NetworkInfomation> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ enum Request {
NONE = 0,
START_NETHOGS,
GET_RESULT,
SKIP_NETHOGS,
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <boost/process.hpp>

#include <fmt/format.h>
#include <signal.h>
#include <syslog.h>

namespace process = boost::process;
Expand Down Expand Up @@ -120,6 +121,9 @@
case Request::GET_RESULT:
get_result();
break;
case Request::SKIP_NETHOGS:
skip_nethogs();

Check warning on line 125 in system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp#L124-L125

Added lines #L124 - L125 were not covered by tests
break;
default:
syslog(LOG_WARNING, "Unknown message. %d\n", request_id);
break;
Expand Down Expand Up @@ -153,6 +157,19 @@
thread_ = std::thread(&TrafficReaderService::execute_nethogs, this);
}

void TrafficReaderService::skip_nethogs()

Check warning on line 160 in system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp#L160

Added line #L160 was not covered by tests
{
syslog(LOG_INFO, "Skipping nethogs...\n");

if (thread_.joinable()) {
{
std::lock_guard<std::mutex> lock(mutex_);
stop_ = true;

Check warning on line 167 in system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp#L166-L167

Added lines #L166 - L167 were not covered by tests
}
thread_.join();

Check warning on line 169 in system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp#L169

Added line #L169 was not covered by tests
}
}

Check warning on line 171 in system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp#L171

Added line #L171 was not covered by tests

void TrafficReaderService::get_result()
{
// Inform ros node
Expand Down Expand Up @@ -234,7 +251,9 @@
}
}

c.terminate();
// Send SIGKILL instead of using c.terminate() to avoid a zombie process
kill(c.id(), SIGKILL);
c.wait();

Check warning on line 256 in system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/traffic_reader/traffic_reader_service.cpp#L255-L256

Added lines #L255 - L256 were not covered by tests

std::ostringstream out_stream;
is_error >> out_stream.rdbuf();
Expand Down
81 changes: 53 additions & 28 deletions system/system_monitor/src/net_monitor/net_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
device_params_(
declare_parameter<std::vector<std::string>>("devices", std::vector<std::string>())),
getifaddrs_error_code_(0),
enable_traffic_monitor_(declare_parameter<bool>("enable_traffic_monitor", true)),

Check warning on line 44 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L44

Added line #L44 was not covered by tests
monitor_program_(declare_parameter<std::string>("monitor_program", "greengrass")),
socket_path_(declare_parameter("socket_path", traffic_reader_service::socket_path)),
crc_error_check_duration_(declare_parameter<int>("crc_error_check_duration", 1)),
Expand Down Expand Up @@ -81,7 +82,11 @@
get_reassembles_failed_column_index();

// Send request to start nethogs
send_start_nethogs_request();
if (enable_traffic_monitor_) {
send_start_nethogs_request();

Check warning on line 86 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L86

Added line #L86 was not covered by tests
} else {
send_skip_nethogs_request();

Check warning on line 88 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L88

Added line #L88 was not covered by tests
}
}

NetMonitor::~NetMonitor()
Expand Down Expand Up @@ -234,38 +239,43 @@
// 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);

Check warning on line 245 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L245

Added line #L245 was not covered by tests

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);

Check warning on line 250 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L249-L250

Added lines #L249 - L250 were not covered by tests
} else {
std::stringstream lines{result.output};
std::string line;
std::vector<std::string> 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");

Check warning on line 252 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L252

Added line #L252 was not covered by tests

if (result.output.empty()) {
status.add("nethogs: result", fmt::format("No data monitored: {}", monitor_program_));

Check warning on line 255 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L255

Added line #L255 was not covered by tests
} else {
std::stringstream lines{result.output};

Check warning on line 257 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L257

Added line #L257 was not covered by tests
std::string line;
std::vector<std::string> 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);

Check warning on line 264 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L264

Added line #L264 was not covered by tests
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());

Check warning on line 268 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L266-L268

Added lines #L266 - L268 were not covered by tests
} else {
status.add(fmt::format("nethogs {}: result", index), line);

Check warning on line 270 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L270

Added line #L270 was not covered by tests
}
++index;

Check warning on line 272 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L272

Added line #L272 was not covered by tests
}
++index;
}
}
} else {
status.summary(DiagStatus::OK, "OK");
status.add("nethogs: result", "traffic monitor is NOT activated");

Check notice on line 278 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Bumpy Road Ahead

NetMonitor::monitor_traffic increases from 2 to 3 logical blocks with deeply nested code, threshold is one single block per function. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.

Check notice on line 278 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Deep, Nested Complexity

NetMonitor::monitor_traffic increases in nested complexity depth from 4 to 5, threshold = 4. This function contains deeply nested logic such as if statements and/or loops. The deeper the nesting, the lower the code health.

Check warning on line 278 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L277-L278

Added lines #L277 - L278 were not covered by tests
}

// Measure elapsed time since start time and report
Expand Down Expand Up @@ -624,6 +634,21 @@
close_connection();
}

void NetMonitor::send_skip_nethogs_request()

Check warning on line 637 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L637

Added line #L637 was not covered by tests
{
// Connect to boot/shutdown service
if (!connect_service()) {
close_connection();
return;

Check warning on line 642 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L641-L642

Added lines #L641 - L642 were not covered by tests
}

// Send data to traffic-reader service
send_data(traffic_reader_service::SKIP_NETHOGS);

Check warning on line 646 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L646

Added line #L646 was not covered by tests

// Close connection with traffic-reader service
close_connection();

Check warning on line 649 in system/system_monitor/src/net_monitor/net_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/net_monitor/net_monitor.cpp#L649

Added line #L649 was not covered by tests
}

void NetMonitor::get_nethogs_result(traffic_reader_service::Result & result)
{
// Connect to traffic-reader service
Expand Down
Loading