diff --git a/src/ProcessMonitor.cxx b/src/ProcessMonitor.cxx index 9b65c73d4..f64042868 100644 --- a/src/ProcessMonitor.cxx +++ b/src/ProcessMonitor.cxx @@ -10,6 +10,8 @@ #include #include #include +#include +#include namespace o2 { @@ -24,30 +26,6 @@ ProcessMonitor::ProcessMonitor() mTimeLastRun = std::chrono::high_resolution_clock::now(); } -std::vector ProcessMonitor::getNetworkUsage() -{ - std::vector metrics; - std::stringstream ss; - // get bytes received and transmitted per interface - ss << "cat /proc/" << mPid << "/net/dev | tail -n +3 | grep -v -e 'lo' -e 'virbr0' | awk ' {print $1 $2 \":\" $10}'"; - std::string output = exec(ss.str().c_str()); - // for each line (each network interfrace) - std::istringstream iss(output); - for (std::string line; std::getline(iss, line); ) { - auto position = line.find(":"); - auto secondPosition = line.find(":", position + 1); - metrics.emplace_back(Metric{ - static_cast(std::stoull(line.substr(position + 1, secondPosition - position - 1))), - "bytesReceived"}.addTags({{"if", line.substr(0, position)}}) - ); - metrics.emplace_back(Metric{ - static_cast(std::stoull(line.substr(secondPosition + 1, line.size()))), - "bytesTransmitted"}.addTags({{"if", line.substr(0, position)}}) - ); - } - return metrics; -} - Metric ProcessMonitor::getMemoryUsage() { std::string command = "ps --no-headers -o pmem --pid " + std::to_string(mPid); @@ -82,6 +60,31 @@ std::vector ProcessMonitor::getCpuAndContexts() { return metrics; } +std::vector ProcessMonitor::getNetworkUsage() +{ + std::vector metrics; + std::ifstream infile("/proc/" + std::to_string(mPid) + "/net/dev"); + std::string line; + std::getline(infile, line); + std::getline(infile, line); + while (std::getline(infile, line)) { + if (line.find("virbr") != std::string::npos) continue; + if (line.find("lo") != std::string::npos) continue; + std::istringstream iss(line); + std::vector tokens{std::istream_iterator{iss}, + std::istream_iterator{}}; + metrics.emplace_back(Metric{ + static_cast(std::stoull(tokens[1])), + "bytesReceived"}.addTags({{"if", tokens[0]}}) + ); + metrics.emplace_back(Metric{ + static_cast(std::stoull(tokens[9])), + "bytesTransmitted"}.addTags({{"if", tokens[0]}}) + ); + } +return metrics; +} + std::string ProcessMonitor::exec(const char* cmd) { char buffer[128];