linux_networkload.cpp
Go to the documentation of this file.
1 /**
2  * @author: Daniel Fuchs
3  * @contact: fuxeysolutions@gmail.com
4  *
5  * distributed under the MIT License (MIT).
6  * Copyright (c) Daniel Fuchs
7  *
8  */
9 #include "linux_networkload.hpp"
10 #include <string>
11 #include <exception>
12 #include <fstream>
13 #include <iostream>
14 #include <list>
15 #include <sstream>
16 #include <stdexcept>
17 #include <algorithm>
18 #include <cstring>
19 #include <utility>
20 
21 const std::list<std::string> identifiers{"RXbytes",
22  "RXpackets",
23  "RXerrs",
24  "RXdrop",
25  "RXfifo",
26  "RXframe",
27  "RXcompressed",
28  "RXmulticast",
29  "TXbytes",
30  "TXpackets",
31  "TXerrs",
32  "TXdrop",
33  "TXfifo",
34  "TXcolls",
35  "TXcarrier",
36  "TXcompressed"
37 };
38 
40  auto it = identifiers.begin();
41  std::advance(it,static_cast<uint32_t>(param));
42  return it->data();
43 }
44 
45 
46 std::shared_ptr<networkLoad::networkParser> networkLoad::networkParser::inst = nullptr;
47 
48 networkLoad::networkLoad(std::string ethernetDataFileName, std::string ethName) : ethernetDataFile(std::move(ethernetDataFileName)), ethDev(std::move(ethName)) {
49  networkParser::getNetworkParser()->parse(ethernetDataFileName);
50 }
51 
52 void networkLoad::networkParser::parse(const std::string& netFile) {
53 
54  if((this->currentTime + std::chrono::milliseconds(10)) > std::chrono::system_clock::now()) {
55  return;
56  } else {
57  this->timeBefore = this->currentTime;
58  this->currentTime = std::chrono::system_clock::now();
59  }
60 
61  std::ifstream ethFile;
62 
63  try {
64  ethFile.open(netFile);
65  } catch (std::ifstream::failure &e) {
66  throw std::runtime_error("Exception: "+ netFile + std::string(e.what()));
67  }
68 
69  this->timeBefore = std::chrono::system_clock::now();
70  this->ethObjOld = this->ethObj;
71 
72  uint32_t lineCnt = 0;
73  for(std::string line; std::getline(ethFile, line); lineCnt++) {
74  if(lineCnt < 2) {
75  continue;
76  }
77  std::stringstream strStream(line);
78  std::string strPart;
79  std::string ifName = "";
80  std::unordered_map<std::string, uint64_t> ifValues;
81  auto it = identifiers.begin();
82  while(strStream >> strPart) {
83  if(ifName.empty()) {
84  strPart.erase(std::remove_if(strPart.begin(),strPart.end(),[](auto c) {
85  return !std::isalnum(c);
86  }), strPart.end());
87  ifName = strPart;
88  } else {
89  if(it != identifiers.end()) {
90  ifValues[it->data()] = std::stoull(strPart);
91  }
92  it++;
93  }
94  }
95  this->ethObj[ifName] = ifValues;
96  }
97  if(this->ethObjOld.empty()) {
98  this->ethObjOld = this->ethObj;
99  }
100 }
101 
102 const std::unordered_map<std::string, uint64_t>
103 &networkLoad::networkParser::getEthObj(const std::string& ethDevice) const {
104  return this->ethObj.at(ethDevice);
105 }
106 
107 const std::unordered_map<std::string, uint64_t> &
108 networkLoad::networkParser::getEthObjOld(const std::string& ethDevice) const {
109  return this->ethObjOld.at(ethDevice);
110 }
111 
112 
113 std::list<std::string> networkLoad::networkParser::getNetworkDevices(std::string netFile) {
114  std::list<std::string> ifList;
115  this->parse(netFile);
116  for(const auto& elem: this->ethObj) {
117  ifList.push_back(elem.first);
118  }
119  return ifList;
120 }
121 
122 networkLoad::networkParser::networkParser() {
123  this->currentTime = std::chrono::system_clock::now() - std::chrono::milliseconds (100);
124  this->timeBefore = std::chrono::system_clock::now();
125 }
126 
127 std::shared_ptr<networkLoad::networkParser> networkLoad::networkParser::getNetworkParser() {
128  if(networkLoad::networkParser::inst == nullptr) {
129  networkLoad::networkParser::inst = std::make_unique<networkLoad::networkParser>();
130  }
131  return networkLoad::networkParser::inst;
132 }
133 
134 const std::chrono::system_clock::time_point networkLoad::networkParser::getTimeStamp() const {
135  return this->currentTime;
136 }
137 
138 
139 
140 const std::chrono::system_clock::time_point networkLoad::networkParser::getTimeBefore() const {
141  return this->timeBefore;
142 }
143 
144 
146  return this->isDeviceAvailable;
147 }
148 
149 
151  return this->ethDev;
152 }
153 
154 std::list<std::string> networkLoad::scanNetworkDevices(const std::string& ethernetDataFile) {
155 
156  auto parser = networkParser::getNetworkParser();
157  return parser->getNetworkDevices(ethernetDataFile);
158 }
159 
160 std::string networkLoad::getBytesPerSeceondString(uint64_t bytesPerSecond) {
161  return getBytesString(bytesPerSecond) + "/s";
162 }
163 
164 std::string networkLoad::getBitsPerSeceondString(uint64_t bytesPerSecond) {
165  return getBitsString(bytesPerSecond) + "/s";
166 }
167 
168 std::string networkLoad::getBytesString(uint64_t totalBytes) {
169  uint64_t Bytes = totalBytes;
170  uint64_t kByte = 0;
171  uint64_t mByte = 0;
172  uint64_t gByte = 0;
173  if (Bytes > 1024) {
174  kByte = Bytes / 1024;
175  Bytes %= 1024;
176  }
177  if (kByte > 1024) {
178  mByte = kByte / 1024;
179  kByte %= 1024;
180  }
181  if(mByte > 1024) {
182  gByte = mByte / 1024;
183  mByte %=1024;
184  }
185 
186  if (gByte > 0) {
187  std::string net;
188  net += std::to_string(gByte);
189  net += ".";
190  net += std::to_string(mByte / 100);
191  net += "gByte";
192  return net;
193  }
194 
195 
196  if (mByte > 0) {
197  std::string net;
198  net += std::to_string(mByte);
199  net += ".";
200  net += std::to_string(kByte / 100);
201  net += "mByte";
202  return net;
203  }
204 
205  if (kByte > 0) {
206  std::string net;
207  net += std::to_string(kByte);
208  net += ".";
209  net += std::to_string(Bytes / 100);
210  net += "kByte";
211  return net;
212  }
213 
214  std::string net;
215  net += std::to_string(Bytes);
216  net += "Byte";
217  return net;
218 }
219 
220 std::string networkLoad::getBitsString(uint64_t totalBytes) {
221  uint64_t Bytes = totalBytes * 8;
222  uint64_t kByte = 0;
223  uint64_t mByte = 0;
224  uint64_t gByte = 0;
225  if (Bytes > 1024) {
226  kByte = Bytes / 1024;
227  Bytes %= 1024;
228  }
229  if (kByte > 1024) {
230  mByte = kByte / 1024;
231  kByte %= 1024;
232  }
233  if(mByte > 1024) {
234  gByte = mByte / 1024;
235  mByte %=1024;
236  }
237 
238  if (gByte > 0) {
239  std::string net;
240  net += std::to_string(gByte);
241  net += ".";
242  net += std::to_string(mByte / 100);
243  net += "gBit";
244  return net;
245  }
246 
247 
248  if (mByte > 0) {
249  std::string net;
250  net += std::to_string(mByte);
251  net += ".";
252  net += std::to_string(kByte / 100);
253  net += "mBit";
254  return net;
255  }
256 
257  if (kByte > 0) {
258  std::string net;
259  net += std::to_string(kByte);
260  net += ".";
261  net += std::to_string(Bytes / 100);
262  net += "kBit";
263  return net;
264  }
265 
266 
267  std::string net;
268  net += std::to_string(Bytes);
269  net += "Bit";
270  return net;
271 }
272 
273 uint64_t networkLoad::getParamPerSecond(std::string designator) {
274  networkParser::getNetworkParser()->parse(this->ethernetDataFile);
275  if(!std::count_if(identifiers.begin(),identifiers.end(),[designator](auto elem) {
276  return elem == designator;
277  })) {
278  throw std::runtime_error("invalid designator: " + designator);
279  }
280  auto before = networkParser::getNetworkParser()->getTimeBefore();
281  auto current = networkParser::getNetworkParser()->getTimeStamp();
282 
283  auto msec = std::chrono::duration_cast<std::chrono::milliseconds> (
284  before - current);
285 
286  uint64_t Bytes = (networkParser::getNetworkParser()->getEthObj(this->ethDev).at(designator) -
287  networkParser::getNetworkParser()->getEthObjOld(this->ethDev).at(designator));
288  if (static_cast<unsigned long>(msec.count()) <= 0) {
289  Bytes /= 1;
290  } else {
291  Bytes /= static_cast<unsigned long>(msec.count());
292  }
293  return Bytes;
294 }
295 
296 uint64_t networkLoad::getParamSinceStartup(std::string designator) {
297  networkParser::getNetworkParser()->parse(this->ethernetDataFile);
298  auto ifObj = networkParser::getNetworkParser()->getEthObj(this->ethDev);
299  if(!std::count_if(identifiers.begin(),identifiers.end(),[designator](auto elem) {
300  return elem == designator;
301  })) {
302  throw std::runtime_error("invalid designator: " + designator);
303  }
304  return ifObj[designator];
305 }
306 
networkLoad::networkLoad
networkLoad(std::string ethernetDataFileName="/proc/net/dev", std::string ethName="eth0")
Definition: linux_networkload.cpp:48
networkLoad::getParamSinceStartup
uint64_t getParamSinceStartup(std::string designator)
Definition: linux_networkload.cpp:296
networkLoad::mapEnumToString
static std::string mapEnumToString(networkLoad::networkParam param)
Definition: linux_networkload.cpp:39
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
networkLoad::getBitsString
static std::string getBitsString(uint64_t totalBytes)
Definition: linux_networkload.cpp:220
networkLoad::getBytesPerSeceondString
static std::string getBytesPerSeceondString(uint64_t bytesPerSecond)
Definition: linux_networkload.cpp:160
linux_networkload.hpp
networkLoad::scanNetworkDevices
static std::list< std::string > scanNetworkDevices(const std::string &ethernetDataFile="/proc/net/dev")
Definition: linux_networkload.cpp:154
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
networkLoad::networkParam
networkParam
Definition: linux_networkload.hpp:47
networkLoad::getBytesString
static std::string getBytesString(uint64_t totalBytes)
Definition: linux_networkload.cpp:168
IceStorm::parser
Parser * parser
Definition: Parser.cpp:33
std
Definition: Application.h:66
networkLoad::getDeviceName
std::string getDeviceName()
Definition: linux_networkload.cpp:150
networkLoad::getParamPerSecond
uint64_t getParamPerSecond(std::string designator)
Definition: linux_networkload.cpp:273
networkLoad::getBitsPerSeceondString
static std::string getBitsPerSeceondString(uint64_t bytesPerSecond)
Definition: linux_networkload.cpp:164
networkLoad::isDeviceUp
bool isDeviceUp() const
Definition: linux_networkload.cpp:145
identifiers
const std::list< std::string > identifiers
Definition: linux_networkload.cpp:21