41 std::advance(it,
static_cast<uint32_t
>(param));
46 std::shared_ptr<networkLoad::networkParser> networkLoad::networkParser::inst =
nullptr;
48 networkLoad::networkLoad(std::string ethernetDataFileName, std::string ethName) : ethernetDataFile(
std::move(ethernetDataFileName)), ethDev(
std::move(ethName)) {
49 networkParser::getNetworkParser()->parse(ethernetDataFileName);
52 void networkLoad::networkParser::parse(
const std::string& netFile) {
54 if((this->currentTime + std::chrono::milliseconds(10)) > std::chrono::system_clock::now()) {
57 this->timeBefore = this->currentTime;
58 this->currentTime = std::chrono::system_clock::now();
61 std::ifstream ethFile;
64 ethFile.open(netFile);
65 }
catch (std::ifstream::failure &e) {
66 throw std::runtime_error(
"Exception: "+ netFile + std::string(e.what()));
69 this->timeBefore = std::chrono::system_clock::now();
70 this->ethObjOld = this->ethObj;
73 for(std::string line; std::getline(ethFile, line); lineCnt++) {
77 std::stringstream strStream(line);
79 std::string ifName =
"";
80 std::unordered_map<std::string, uint64_t> ifValues;
82 while(strStream >> strPart) {
84 strPart.erase(std::remove_if(strPart.begin(),strPart.end(),[](
auto c) {
85 return !std::isalnum(c);
90 ifValues[it->data()] = std::stoull(strPart);
95 this->ethObj[ifName] = ifValues;
97 if(this->ethObjOld.empty()) {
98 this->ethObjOld = this->ethObj;
102 const std::unordered_map<std::string, uint64_t>
103 &networkLoad::networkParser::getEthObj(
const std::string& ethDevice)
const {
104 return this->ethObj.at(ethDevice);
107 const std::unordered_map<std::string, uint64_t> &
108 networkLoad::networkParser::getEthObjOld(
const std::string& ethDevice)
const {
109 return this->ethObjOld.at(ethDevice);
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);
122 networkLoad::networkParser::networkParser() {
123 this->currentTime = std::chrono::system_clock::now() - std::chrono::milliseconds (100);
124 this->timeBefore = std::chrono::system_clock::now();
127 std::shared_ptr<networkLoad::networkParser> networkLoad::networkParser::getNetworkParser() {
128 if(networkLoad::networkParser::inst ==
nullptr) {
129 networkLoad::networkParser::inst = std::make_unique<networkLoad::networkParser>();
131 return networkLoad::networkParser::inst;
134 const std::chrono::system_clock::time_point networkLoad::networkParser::getTimeStamp()
const {
135 return this->currentTime;
140 const std::chrono::system_clock::time_point networkLoad::networkParser::getTimeBefore()
const {
141 return this->timeBefore;
146 return this->isDeviceAvailable;
156 auto parser = networkParser::getNetworkParser();
157 return parser->getNetworkDevices(ethernetDataFile);
169 uint64_t Bytes = totalBytes;
174 kByte = Bytes / 1024;
178 mByte = kByte / 1024;
182 gByte = mByte / 1024;
221 uint64_t Bytes = totalBytes * 8;
226 kByte = Bytes / 1024;
230 mByte = kByte / 1024;
234 gByte = mByte / 1024;
274 networkParser::getNetworkParser()->parse(this->ethernetDataFile);
276 return elem == designator;
278 throw std::runtime_error(
"invalid designator: " + designator);
280 auto before = networkParser::getNetworkParser()->getTimeBefore();
281 auto current = networkParser::getNetworkParser()->getTimeStamp();
283 auto msec = std::chrono::duration_cast<std::chrono::milliseconds> (
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) {
291 Bytes /=
static_cast<unsigned long>(msec.count());
297 networkParser::getNetworkParser()->parse(this->ethernetDataFile);
298 auto ifObj = networkParser::getNetworkParser()->getEthObj(this->ethDev);
300 return elem == designator;
302 throw std::runtime_error(
"invalid designator: " + designator);
304 return ifObj[designator];