]> granicus.if.org Git - icinga2/blob - lib/cli/variableutility.cpp
ApiListener#ApiTimerHandler(): delete all replayed logs
[icinga2] / lib / cli / variableutility.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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"
28 #include <fstream>
29
30 using namespace icinga;
31
32 Value VariableUtility::GetVariable(const String& name)
33 {
34         String varsfile = Configuration::VarsPath;
35
36         std::fstream fp;
37         fp.open(varsfile.CStr(), std::ios_base::in);
38
39         StdioStream::Ptr sfp = new StdioStream(&fp, false);
40
41         String message;
42         StreamReadContext src;
43         for (;;) {
44                 StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
45
46                 if (srs == StatusEof)
47                         break;
48
49                 if (srs != StatusNewItem)
50                         continue;
51
52                 Dictionary::Ptr variable = JsonDecode(message);
53
54                 if (variable->Get("name") == name) {
55                         return variable->Get("value");
56                 }
57         }
58
59         return Empty;
60 }
61
62 void VariableUtility::PrintVariables(std::ostream& outfp)
63 {
64         String varsfile = Configuration::VarsPath;
65
66         std::fstream fp;
67         fp.open(varsfile.CStr(), std::ios_base::in);
68
69         StdioStream::Ptr sfp = new StdioStream(&fp, false);
70         unsigned long variables_count = 0;
71
72         String message;
73         StreamReadContext src;
74         for (;;) {
75                 StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src);
76
77                 if (srs == StatusEof)
78                         break;
79
80                 if (srs != StatusNewItem)
81                         continue;
82
83                 Dictionary::Ptr variable = JsonDecode(message);
84                 outfp << variable->Get("name") << " = " << variable->Get("value") << "\n";
85                 variables_count++;
86         }
87
88         sfp->Close();
89         fp.close();
90
91         Log(LogNotice, "cli")
92                 << "Parsed " << variables_count << " variables.";
93 }