]> granicus.if.org Git - icinga2/blob - lib/cli/variablegetcommand.cpp
add some object locking to the Dump method (which could theoreticylly suffer from...
[icinga2] / lib / cli / variablegetcommand.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "cli/variablegetcommand.hpp"
4 #include "cli/variableutility.hpp"
5 #include "base/logger.hpp"
6 #include "base/application.hpp"
7 #include "base/convert.hpp"
8 #include "base/configobject.hpp"
9 #include "base/configtype.hpp"
10 #include "base/json.hpp"
11 #include "base/netstring.hpp"
12 #include "base/stdiostream.hpp"
13 #include "base/debug.hpp"
14 #include "base/objectlock.hpp"
15 #include "base/console.hpp"
16 #include "base/scriptglobal.hpp"
17 #include <boost/algorithm/string/join.hpp>
18 #include <boost/algorithm/string/replace.hpp>
19 #include <fstream>
20 #include <iostream>
21
22 using namespace icinga;
23 namespace po = boost::program_options;
24
25 REGISTER_CLICOMMAND("variable/get", VariableGetCommand);
26
27 String VariableGetCommand::GetDescription() const
28 {
29         return "Prints the value of an Icinga 2 variable.";
30 }
31
32 String VariableGetCommand::GetShortDescription() const
33 {
34         return "gets a variable";
35 }
36
37 void VariableGetCommand::InitParameters(boost::program_options::options_description& visibleDesc,
38         boost::program_options::options_description& hiddenDesc) const
39 {
40         visibleDesc.add_options()
41                 ("current", "Uses the current value (i.e. from the running process, rather than from the vars file)");
42 }
43
44 int VariableGetCommand::GetMinArguments() const
45 {
46         return 1;
47 }
48
49 /**
50  * The entry point for the "variable get" CLI command.
51  *
52  * @returns An exit status.
53  */
54 int VariableGetCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
55 {
56         if (vm.count("current")) {
57                 std::cout << ScriptGlobal::Get(ap[0], &Empty) << "\n";
58                 return 0;
59         }
60
61         String varsfile = Configuration::VarsPath;
62
63         if (!Utility::PathExists(varsfile)) {
64                 Log(LogCritical, "cli")
65                         << "Cannot open variables file '" << varsfile << "'.";
66                 Log(LogCritical, "cli", "Run 'icinga2 daemon -C' to validate config and generate the cache file.");
67                 return 1;
68         }
69
70         Value value = VariableUtility::GetVariable(ap[0]);
71
72         std::cout << value << "\n";
73
74         return 0;
75 }