linux_networkload.hpp
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 #pragma once
10 
11 
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <list>
16 #include <vector>
17 #include <chrono>
18 #include <unordered_map>
19 
20 
21 class networkLoad {
22 
23 public:
24  static std::list<std::string> scanNetworkDevices(const std::string& ethernetDataFile= "/proc/net/dev");
25  static std::vector<std::shared_ptr<networkLoad>> createLinuxEthernetScanList(const std::string& ethernetDataFileName = "/proc/net/dev") {
26  std::vector<std::shared_ptr<networkLoad>> v;
27  for (const auto& elem: networkLoad::scanNetworkDevices(ethernetDataFileName)) {
28  v.push_back(std::make_shared<networkLoad>(ethernetDataFileName,elem));
29  }
30  return v;
31  }
32 
33  explicit networkLoad(std::string ethernetDataFileName = "/proc/net/dev", std::string ethName = "eth0");
34  uint64_t getParamPerSecond(std::string designator);
35  uint64_t getParamSinceStartup(std::string designator);
36 
37  static std::string getBytesPerSeceondString(uint64_t bytesPerSecond);
38  static std::string getBitsPerSeceondString(uint64_t bytesPerSecond);
39  static std::string getBytesString(uint64_t totalBytes);
40  static std::string getBitsString(uint64_t totalBytes);
41 
42  bool isDeviceUp() const;
43  std::string getDeviceName();
44 
45 
46 
47  enum networkParam {
48  RXbytes = 0,
64  };
65  static std::string mapEnumToString(networkLoad::networkParam param);
66 private:
67 
68  std::string ethernetDataFile;
69  std::string ethDev;
70  bool isDeviceAvailable = false;
71 
72 
73  class networkParser {
74  private:
75  std::chrono::system_clock::time_point currentTime;
76  std::chrono::system_clock::time_point timeBefore;
77 
78  std::map<std::string, std::unordered_map<std::string, uint64_t>> ethObj;
79  std::map<std::string, std::unordered_map<std::string, uint64_t>> ethObjOld;
80 
81  public:
82  static std::shared_ptr<networkParser> getNetworkParser();
83  networkParser();
84  void parse(const std::string& netFile = "/proc/net/dev");
85  const std::unordered_map<std::string, uint64_t> &getEthObj(const std::string& ethDevice) const;
86  std::list<std::string> getNetworkDevices(std::string netFile = "/proc/net/dev");
87  const std::chrono::system_clock::time_point getTimeStamp() const;
88  const std::unordered_map<std::string, uint64_t> &getEthObjOld(const std::string& ethDevice) const;
89  const std::chrono::system_clock::time_point getTimeBefore() const;
90 
91  static std::shared_ptr<networkParser> inst;
92  };
93 
94 
95 };
96 
networkLoad::TXdrop
@ TXdrop
Definition: linux_networkload.hpp:59
networkLoad::TXbytes
@ TXbytes
Definition: linux_networkload.hpp:56
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::TXerrs
@ TXerrs
Definition: linux_networkload.hpp:58
networkLoad::createLinuxEthernetScanList
static std::vector< std::shared_ptr< networkLoad > > createLinuxEthernetScanList(const std::string &ethernetDataFileName="/proc/net/dev")
Definition: linux_networkload.hpp:25
networkLoad::TXcolls
@ TXcolls
Definition: linux_networkload.hpp:61
networkLoad::RXframe
@ RXframe
Definition: linux_networkload.hpp:53
networkLoad::RXdrop
@ RXdrop
Definition: linux_networkload.hpp:51
networkLoad::TXcompressed
@ TXcompressed
Definition: linux_networkload.hpp:63
networkLoad::mapEnumToString
static std::string mapEnumToString(networkLoad::networkParam param)
Definition: linux_networkload.cpp:39
networkLoad::RXmulticast
@ RXmulticast
Definition: linux_networkload.hpp:55
networkLoad::RXfifo
@ RXfifo
Definition: linux_networkload.hpp:52
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
networkLoad::TXcarrier
@ TXcarrier
Definition: linux_networkload.hpp:62
networkLoad::scanNetworkDevices
static std::list< std::string > scanNetworkDevices(const std::string &ethernetDataFile="/proc/net/dev")
Definition: linux_networkload.cpp:154
networkLoad::networkParam
networkParam
Definition: linux_networkload.hpp:47
networkLoad::RXcompressed
@ RXcompressed
Definition: linux_networkload.hpp:54
networkLoad::getBytesString
static std::string getBytesString(uint64_t totalBytes)
Definition: linux_networkload.cpp:168
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
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::RXpackets
@ RXpackets
Definition: linux_networkload.hpp:49
networkLoad::TXpackets
@ TXpackets
Definition: linux_networkload.hpp:57
networkLoad::isDeviceUp
bool isDeviceUp() const
Definition: linux_networkload.cpp:145
networkLoad
Definition: linux_networkload.hpp:21
networkLoad::RXbytes
@ RXbytes
Definition: linux_networkload.hpp:48
networkLoad::RXerrs
@ RXerrs
Definition: linux_networkload.hpp:50
networkLoad::TXfifo
@ TXfifo
Definition: linux_networkload.hpp:60