43 std::advance(it,
static_cast<uint32_t
>(param));
47std::shared_ptr<networkLoad::networkParser> networkLoad::networkParser::inst =
nullptr;
50 ethernetDataFile(
std::move(ethernetDataFileName)), ethDev(
std::move(ethName))
52 networkParser::getNetworkParser()->parse(ethernetDataFileName);
56networkLoad::networkParser::parse(
const std::string& netFile)
59 if ((this->currentTime + std::chrono::milliseconds(10)) > std::chrono::system_clock::now())
65 this->timeBefore = this->currentTime;
66 this->currentTime = std::chrono::system_clock::now();
69 std::ifstream ethFile;
73 ethFile.open(netFile);
75 catch (std::ifstream::failure& e)
77 throw std::runtime_error(
"Exception: " + netFile + std::string(e.what()));
80 this->timeBefore = std::chrono::system_clock::now();
81 this->ethObjOld = this->ethObj;
84 for (std::string line; std::getline(ethFile, line); lineCnt++)
90 std::stringstream strStream(line);
92 std::string ifName =
"";
93 std::unordered_map<std::string, uint64_t> ifValues;
95 while (strStream >> strPart)
99 strPart.erase(std::remove_if(strPart.begin(),
101 [](
auto c) { return !std::isalnum(c); }),
109 ifValues[it->data()] = std::stoull(strPart);
114 this->ethObj[ifName] = ifValues;
116 if (this->ethObjOld.empty())
118 this->ethObjOld = this->ethObj;
122const std::unordered_map<std::string, uint64_t>&
123networkLoad::networkParser::getEthObj(
const std::string& ethDevice)
const
125 return this->ethObj.at(ethDevice);
128const std::unordered_map<std::string, uint64_t>&
129networkLoad::networkParser::getEthObjOld(
const std::string& ethDevice)
const
131 return this->ethObjOld.at(ethDevice);
134std::list<std::string>
135networkLoad::networkParser::getNetworkDevices(std::string netFile)
137 std::list<std::string> ifList;
138 this->parse(netFile);
139 for (
const auto& elem : this->ethObj)
141 ifList.push_back(elem.first);
146networkLoad::networkParser::networkParser()
148 this->currentTime = std::chrono::system_clock::now() - std::chrono::milliseconds(100);
149 this->timeBefore = std::chrono::system_clock::now();
152std::shared_ptr<networkLoad::networkParser>
153networkLoad::networkParser::getNetworkParser()
155 if (networkLoad::networkParser::inst ==
nullptr)
157 networkLoad::networkParser::inst = std::make_unique<networkLoad::networkParser>();
159 return networkLoad::networkParser::inst;
162const std::chrono::system_clock::time_point
163networkLoad::networkParser::getTimeStamp()
const
165 return this->currentTime;
168const std::chrono::system_clock::time_point
169networkLoad::networkParser::getTimeBefore()
const
171 return this->timeBefore;
177 return this->isDeviceAvailable;
186std::list<std::string>
190 auto parser = networkParser::getNetworkParser();
191 return parser->getNetworkDevices(ethernetDataFile);
209 uint64_t Bytes = totalBytes;
215 kByte = Bytes / 1024;
220 mByte = kByte / 1024;
225 gByte = mByte / 1024;
232 net += std::to_string(gByte);
234 net += std::to_string(mByte / 100);
243 net += std::to_string(mByte);
245 net += std::to_string(kByte / 100);
253 net += std::to_string(kByte);
255 net += std::to_string(Bytes / 100);
261 net += std::to_string(Bytes);
269 uint64_t Bytes = totalBytes * 8;
275 kByte = Bytes / 1024;
280 mByte = kByte / 1024;
285 gByte = mByte / 1024;
292 net += std::to_string(gByte);
294 net += std::to_string(mByte / 100);
303 net += std::to_string(mByte);
305 net += std::to_string(kByte / 100);
313 net += std::to_string(kByte);
315 net += std::to_string(Bytes / 100);
322 net += std::to_string(Bytes);
330 networkParser::getNetworkParser()->parse(this->ethernetDataFile);
333 [designator](
auto elem) { return elem == designator; }))
335 throw std::runtime_error(
"invalid designator: " + designator);
337 auto before = networkParser::getNetworkParser()->getTimeBefore();
338 auto current = networkParser::getNetworkParser()->getTimeStamp();
340 auto msec = std::chrono::duration_cast<std::chrono::milliseconds>(before - current);
342 uint64_t Bytes = (networkParser::getNetworkParser()->getEthObj(this->ethDev).at(designator) -
343 networkParser::getNetworkParser()->getEthObjOld(this->ethDev).at(designator));
344 if (
static_cast<unsigned long>(msec.count()) <= 0)
350 Bytes /=
static_cast<unsigned long>(msec.count());
358 networkParser::getNetworkParser()->parse(this->ethernetDataFile);
359 auto ifObj = networkParser::getNetworkParser()->getEthObj(this->ethDev);
362 [designator](
auto elem) { return elem == designator; }))
364 throw std::runtime_error(
"invalid designator: " + designator);
366 return ifObj[designator];
uint64_t getParamPerSecond(std::string designator)
static std::string getBitsPerSeceondString(uint64_t bytesPerSecond)
static std::string getBitsString(uint64_t totalBytes)
uint64_t getParamSinceStartup(std::string designator)
static std::list< std::string > scanNetworkDevices(const std::string ðernetDataFile="/proc/net/dev")
std::string getDeviceName()
static std::string getBytesString(uint64_t totalBytes)
static std::string mapEnumToString(networkLoad::networkParam param)
networkLoad(std::string ethernetDataFileName="/proc/net/dev", std::string ethName="eth0")
static std::string getBytesPerSeceondString(uint64_t bytesPerSecond)
const std::list< std::string > identifiers