AnyCollect  1.1.2
Config.cc
Go to the documentation of this file.
1 //
2 // Config.cc
3 //
4 // Created on October 26th 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 #include "Config.h"
22 #include "Source.h"
23 
24 
25 namespace AnyCollect {
26  Config::Config(const std::string& path) noexcept {
27  try {
28  Source configFile = Source{path};
29  configFile.update();
30  if (configFile.contents().empty())
31  return;
32 
33  nlohmann::json configJson = nlohmann::json::parse(configFile.contents());
34  from_json(configJson, *this);
35  }
36  catch(const std::exception& e) {
37  std::cerr << "Error while parsing configuration file: invalid JSON contents." <<std::endl;
38  std::cerr << "Internal error: " << e.what() << std::endl;
39  abort();
40  }
41  }
42 
43 
44  void from_json(const nlohmann::json& je, Config::expression& e) noexcept {
45  e.regex = getValue<Config::expression::regexType>(je, Config::expression::regexKey);
46  for (const auto& jem : getValue<Config::expression::metricsType>(je, Config::expression::metricsKey)) {
48  m.name = getValue<Config::expression::metric::nameType>(jem, Config::expression::metric::nameKey);
49  m.value = getValue<Config::expression::metric::valueType>(jem, Config::expression::metric::valueKey);
50  m.unit = getValue<Config::expression::metric::unitType>(jem, Config::expression::metric::unitKey);
51  m.tags = getValue<Config::expression::metric::tagsType>(jem, Config::expression::metric::tagsKey);
52  m.computeRate = getValue<Config::expression::metric::computeRateType>(jem, Config::expression::metric::computeRateKey);
53  m.convertToUnitsPerSecond = getValue<Config::expression::metric::convertToUnitsPerSecondType>(jem, Config::expression::metric::convertToUnitsPerSecondKey);
54  e.metrics.push_back(std::move(m));
55  }
56  }
57 
58  void from_json(const nlohmann::json& j, Config& c) noexcept {
59  if (j.count(std::string(Config::filesKey)) > 0) {
60  for (const auto& jf : getValue<Config::filesType>(j, Config::filesKey)) {
61  Config::file f;
62  f.paths = getValue<Config::file::pathsType>(jf, Config::file::pathsKey);
63  for (const auto& jfe : getValue<Config::file::expressionsType>(jf, Config::file::expressionsKey)) {
65  from_json(jfe, e);
66  f.expressions.push_back(std::move(e));
67  }
68  c.files.push_back(std::move(f));
69  }
70  }
71  if (j.count(std::string(Config::commandsKey)) > 0) {
72  for (const auto& jp : getValue<Config::commandsType>(j, Config::commandsKey)) {
74  p.program = getValue<Config::command::programType>(jp, Config::command::programKey);
75  p.arguments = getValue<Config::command::argumentsType>(jp, Config::command::argumentsKey);
76  for (const auto& jpe : getValue<Config::command::expressionsType>(jp, Config::command::expressionsKey)) {
78  from_json(jpe, e);
79  p.expressions.push_back(std::move(e));
80  }
81  c.commands.push_back(std::move(p));
82  }
83  }
84  }
85 }
static constexpr std::string_view expressionsKey
Definition: Config.h:70
static constexpr std::string_view programKey
Definition: Config.h:78
static constexpr std::string_view argumentsKey
Definition: Config.h:80
Config(const std::string &path) noexcept
Parses the specified config file into a Config object.
Definition: Config.cc:26
argumentsType arguments
Definition: Config.h:86
static constexpr std::string_view filesKey
Definition: Config.h:90
static constexpr std::string_view unitKey
Definition: Config.h:42
static constexpr std::string_view metricsKey
Definition: Config.h:60
bool update() noexcept
Updates the source (read the file or execute command) and store the results.
Definition: Source.cc:161
static constexpr std::string_view convertToUnitsPerSecondKey
Definition: Config.h:48
Class used to represent a source (file or command output) from which metrics can be matched...
Definition: Source.h:39
Struct used to represent the JSON configuration file.
Definition: Config.h:35
static constexpr std::string_view nameKey
Definition: Config.h:38
static constexpr std::string_view tagsKey
Definition: Config.h:44
static constexpr std::string_view expressionsKey
Definition: Config.h:82
static constexpr std::string_view computeRateKey
Definition: Config.h:46
const std::string_view & contents() const noexcept
Returns the stored contents (file contents or command output)
Definition: Source.cc:80
static constexpr std::string_view pathsKey
Definition: Config.h:68
static constexpr std::string_view commandsKey
Definition: Config.h:92
std::vector< Config::expression > expressions
Definition: Config.h:74
void from_json(const nlohmann::json &je, Config::expression &e) noexcept
Definition: Config.cc:44
static constexpr std::string_view valueKey
Definition: Config.h:40
std::vector< Config::expression > expressions
Definition: Config.h:87
static constexpr std::string_view regexKey
Definition: Config.h:58
convertToUnitsPerSecondType convertToUnitsPerSecond
Definition: Config.h:56