1010#include < chrono>
1111#include < sstream>
1212#include < cmath>
13+ #include < fstream>
14+ #include < utility>
1315
1416namespace o2
1517{
@@ -24,30 +26,6 @@ ProcessMonitor::ProcessMonitor()
2426 mTimeLastRun = std::chrono::high_resolution_clock::now ();
2527}
2628
27- std::vector<Metric> ProcessMonitor::getNetworkUsage ()
28- {
29- std::vector<Metric> metrics;
30- std::stringstream ss;
31- // get bytes received and transmitted per interface
32- ss << " cat /proc/" << mPid << " /net/dev | tail -n +3 | grep -v -e 'lo' -e 'virbr0' | awk ' {print $1 $2 \" :\" $10}'" ;
33- std::string output = exec (ss.str ().c_str ());
34- // for each line (each network interfrace)
35- std::istringstream iss (output);
36- for (std::string line; std::getline (iss, line); ) {
37- auto position = line.find (" :" );
38- auto secondPosition = line.find (" :" , position + 1 );
39- metrics.emplace_back (Metric{
40- static_cast <uint64_t >(std::stoull (line.substr (position + 1 , secondPosition - position - 1 ))),
41- " bytesReceived" }.addTags ({{" if" , line.substr (0 , position)}})
42- );
43- metrics.emplace_back (Metric{
44- static_cast <uint64_t >(std::stoull (line.substr (secondPosition + 1 , line.size ()))),
45- " bytesTransmitted" }.addTags ({{" if" , line.substr (0 , position)}})
46- );
47- }
48- return metrics;
49- }
50-
5129Metric ProcessMonitor::getMemoryUsage ()
5230{
5331 std::string command = " ps --no-headers -o pmem --pid " + std::to_string (mPid );
@@ -82,6 +60,31 @@ std::vector<Metric> ProcessMonitor::getCpuAndContexts() {
8260 return metrics;
8361}
8462
63+ std::vector<Metric> ProcessMonitor::getNetworkUsage ()
64+ {
65+ std::vector<Metric> metrics;
66+ std::ifstream infile (" /proc/" + std::to_string (mPid ) + " /net/dev" );
67+ std::string line;
68+ std::getline (infile, line);
69+ std::getline (infile, line);
70+ while (std::getline (infile, line)) {
71+ if (line.find (" virbr" ) != std::string::npos) continue ;
72+ if (line.find (" lo" ) != std::string::npos) continue ;
73+ std::istringstream iss (line);
74+ std::vector<std::string> tokens{std::istream_iterator<std::string>{iss},
75+ std::istream_iterator<std::string>{}};
76+ metrics.emplace_back (Metric{
77+ static_cast <uint64_t >(std::stoull (tokens[1 ])),
78+ " bytesReceived" }.addTags ({{" if" , tokens[0 ]}})
79+ );
80+ metrics.emplace_back (Metric{
81+ static_cast <uint64_t >(std::stoull (tokens[9 ])),
82+ " bytesTransmitted" }.addTags ({{" if" , tokens[0 ]}})
83+ );
84+ }
85+ return metrics;
86+ }
87+
8588std::string ProcessMonitor::exec (const char * cmd)
8689{
8790 char buffer[128 ];
0 commit comments