]> granicus.if.org Git - icinga2/blob - lib/cli/variableutility.cpp
Added ca restore command+docs to undo effects of ca remove
[icinga2] / lib / cli / variableutility.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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"
11 #include <fstream>
12
13 using namespace icinga;
14
15 Value VariableUtility::GetVariable(const String& name)
16 {
17         String varsfile = Configuration::VarsPath;
18
19         std::fstream fp;
20         fp.open(varsfile.CStr(), std::ios_base::in);
21
22         StdioStream::Ptr sfp = new StdioStream(&fp, false);
23
24         String message;
25         StreamReadContext src;
26         for (;;) {
27                 StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
28
29                 if (srs == StatusEof)
30                         break;
31
32                 if (srs != StatusNewItem)
33                         continue;
34
35                 Dictionary::Ptr variable = JsonDecode(message);
36
37                 if (variable->Get("name") == name) {
38                         return variable->Get("value");
39                 }
40         }
41
42         return Empty;
43 }
44
45 void VariableUtility::PrintVariables(std::ostream& outfp)
46 {
47         String varsfile = Configuration::VarsPath;
48
49         std::fstream fp;
50         fp.open(varsfile.CStr(), std::ios_base::in);
51
52         StdioStream::Ptr sfp = new StdioStream(&fp, false);
53         unsigned long variables_count = 0;
54
55         String message;
56         StreamReadContext src;
57         for (;;) {
58                 StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
59
60                 if (srs == StatusEof)
61                         break;
62
63                 if (srs != StatusNewItem)
64                         continue;
65
66                 Dictionary::Ptr variable = JsonDecode(message);
67                 outfp << variable->Get("name") << " = " << variable->Get("value") << "\n";
68                 variables_count++;
69         }
70
71         sfp->Close();
72         fp.close();
73
74         Log(LogNotice, "cli")
75                 << "Parsed " << variables_count << " variables.";
76 }