MetroCollect  2.3.4
SourceProcNetDev.h
Go to the documentation of this file.
1 //
2 // SourceProcNetDev.h
3 //
4 // Created on August 27th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #pragma once
22 
23 #include <array>
24 #include <fstream>
25 #include <string>
26 #include <string_view>
27 #include <vector>
28 
29 #include "Source.h"
30 
31 using namespace std::literals;
32 
33 
38  class SourceProcNetDev : public SourceBase {
39  protected:
40  static constexpr std::string_view filePath = "/proc/1/net/dev"sv;
41 
42  static constexpr std::string_view sourcePrefix = "network"sv;
43  static constexpr std::string_view fieldNameInterfaceDescription = "interface"sv;
44  static constexpr std::string_view fieldRecieve = "rx"sv;
45  static constexpr std::string_view fieldTransmit = "tx"sv;
46  static constexpr std::string_view fieldNameAll = "*"sv;
47  static constexpr std::array fieldUnitsAssociation = { KeyUnit{"bytes"sv, "bytes"sv}, KeyUnit{"packets"sv, "packets"sv}, KeyUnit{"compressed"sv, "packets"sv}, KeyUnit{"multicast"sv, "packets"sv} };
48  static constexpr std::string_view defaultUnit = "errors"sv;
49 
50  std::ifstream file_;
51  std::vector<char> buffer_;
52  std::vector<std::string> interfaces_;
53  std::vector<std::string> rxFieldNames_;
54  std::vector<std::string> txFieldNames_;
55 
57  void parseFields();
58 
59  public:
63  static SourceProcNetDev& get() {
64  static SourceProcNetDev instance;
65  return instance;
66  }
67 
68  size_t fieldCount() const noexcept override final;
69  const std::vector<size_t> indexesOfFieldName(const FieldName& fieldName, Interests* interests = nullptr) const noexcept override final;
70  const std::string fieldNameSourcePrefix() const noexcept override final;
71  const FieldInfo fieldInfoAtIndex(size_t index) const noexcept override final;
72  const std::vector<FieldInfo> allFieldsInfo() const noexcept override final;
73 
74  void fetchData(const Interests& interests, DataArray::Iterator current) override final;
75  void computeDiff(const Interests& interests, DiffArray::Iterator diff, DataArray::ConstIterator current, DataArray::ConstIterator previous, double factor = 1) noexcept override final;
76  };
77 }
Object used to describe a field (or metric)
Definition: SourceField.h:37
std::vector< std::string > rxFieldNames_
Reception metric names.
Sources of network metrics extracted from the file /proc/net/dev.
Stuct to associate a field name containing a certain key with a unit.
Definition: SourceTools.h:78
std::vector< std::string > txFieldNames_
Transmission metric names.
std::vector< std::string > FieldName
Type used for field names (an array of strings)
Definition: SourceField.h:31
Namespace for sources of metrics objects and operations.
std::vector< DataValueType >::const_iterator ConstIterator
Const iterator type of MetricsDataArray.
Definition: MetricTypes.h:37
STL namespace.
std::vector< char > buffer_
Buffer to put file contents into.
Boolean array to keep track of which fields a source has to fetch metrics for.
Singleton abstract class, base class of all metric sources classes.
Definition: Source.h:42
std::vector< DataValueType >::iterator Iterator
Iterator type of MetricsDataArray.
Definition: MetricTypes.h:36
std::vector< std::string > interfaces_
Network interfaces names.