1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "cli/variableutility.hpp"
21 #include "base/logger.hpp"
22 #include "base/application.hpp"
23 #include "base/utility.hpp"
24 #include "base/stdiostream.hpp"
25 #include "base/netstring.hpp"
26 #include "base/json.hpp"
27 #include "remote/jsonrpc.hpp"
30 using namespace icinga;
32 Value VariableUtility::GetVariable(const String& name)
34 String varsfile = Configuration::VarsPath;
37 fp.open(varsfile.CStr(), std::ios_base::in);
39 StdioStream::Ptr sfp = new StdioStream(&fp, false);
42 StreamReadContext src;
44 StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
49 if (srs != StatusNewItem)
52 Dictionary::Ptr variable = JsonDecode(message);
54 if (variable->Get("name") == name) {
55 return variable->Get("value");
62 void VariableUtility::PrintVariables(std::ostream& outfp)
64 String varsfile = Configuration::VarsPath;
67 fp.open(varsfile.CStr(), std::ios_base::in);
69 StdioStream::Ptr sfp = new StdioStream(&fp, false);
70 unsigned long variables_count = 0;
73 StreamReadContext src;
75 StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
80 if (srs != StatusNewItem)
83 Dictionary::Ptr variable = JsonDecode(message);
84 outfp << variable->Get("name") << " = " << variable->Get("value") << "\n";
92 << "Parsed " << variables_count << " variables.";