1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.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 "base/scriptglobal.hpp"
21 #include "base/singleton.hpp"
22 #include "base/logger.hpp"
23 #include "base/stdiostream.hpp"
24 #include "base/netstring.hpp"
25 #include "base/json.hpp"
26 #include "base/convert.hpp"
27 #include "base/objectlock.hpp"
28 #include "base/exception.hpp"
29 #include <boost/algorithm/string/split.hpp>
32 using namespace icinga;
34 Dictionary::Ptr ScriptGlobal::m_Globals = new Dictionary();
36 Value ScriptGlobal::Get(const String& name, const Value *defaultValue)
40 if (!m_Globals->Get(name, &result)) {
44 BOOST_THROW_EXCEPTION(std::invalid_argument("Tried to access undefined script variable '" + name + "'"));
50 void ScriptGlobal::Set(const String& name, const Value& value)
52 std::vector<String> tokens = name.Split(".");
55 BOOST_THROW_EXCEPTION(std::invalid_argument("Name must not be empty"));
58 ObjectLock olock(m_Globals);
60 Dictionary::Ptr parent = m_Globals;
62 for (std::vector<String>::size_type i = 0; i < tokens.size(); i++) {
63 const String& token = tokens[i];
65 if (i + 1 != tokens.size()) {
68 if (!parent->Get(token, &vparent)) {
69 Dictionary::Ptr dict = new Dictionary();
70 parent->Set(token, dict);
78 parent->Set(tokens[tokens.size() - 1], value);
82 bool ScriptGlobal::Exists(const String& name)
84 return m_Globals->Contains(name);
87 Dictionary::Ptr ScriptGlobal::GetGlobals()
92 void ScriptGlobal::WriteToFile(const String& filename)
94 Log(LogInformation, "ScriptGlobal")
95 << "Dumping variables to file '" << filename << "'";
98 String tempFilename = Utility::CreateTempFile(filename + ".XXXXXX", 0600, fp);
101 BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + tempFilename + "' file"));
103 StdioStream::Ptr sfp = new StdioStream(&fp, false);
105 ObjectLock olock(m_Globals);
106 for (const Dictionary::Pair& kv : m_Globals) {
107 Value value = kv.second;
109 if (value.IsObject())
110 value = Convert::ToString(value);
112 Dictionary::Ptr persistentVariable = new Dictionary({
113 { "name", kv.first },
117 String json = JsonEncode(persistentVariable);
119 NetString::WriteStringToStream(sfp, json);
127 _unlink(filename.CStr());
130 if (rename(tempFilename.CStr(), filename.CStr()) < 0) {
131 BOOST_THROW_EXCEPTION(posix_error()
132 << boost::errinfo_api_function("rename")
133 << boost::errinfo_errno(errno)
134 << boost::errinfo_file_name(tempFilename));