1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "cli/variableutility.hpp"
4 #include "base/logger.hpp"
5 #include "base/application.hpp"
6 #include "base/utility.hpp"
7 #include "base/stdiostream.hpp"
8 #include "base/netstring.hpp"
9 #include "base/json.hpp"
10 #include "remote/jsonrpc.hpp"
13 using namespace icinga;
15 Value VariableUtility::GetVariable(const String& name)
17 String varsfile = Configuration::VarsPath;
20 fp.open(varsfile.CStr(), std::ios_base::in);
22 StdioStream::Ptr sfp = new StdioStream(&fp, false);
25 StreamReadContext src;
27 StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
32 if (srs != StatusNewItem)
35 Dictionary::Ptr variable = JsonDecode(message);
37 if (variable->Get("name") == name) {
38 return variable->Get("value");
45 void VariableUtility::PrintVariables(std::ostream& outfp)
47 String varsfile = Configuration::VarsPath;
50 fp.open(varsfile.CStr(), std::ios_base::in);
52 StdioStream::Ptr sfp = new StdioStream(&fp, false);
53 unsigned long variables_count = 0;
56 StreamReadContext src;
58 StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
63 if (srs != StatusNewItem)
66 Dictionary::Ptr variable = JsonDecode(message);
67 outfp << variable->Get("name") << " = " << variable->Get("value") << "\n";
75 << "Parsed " << variables_count << " variables.";