2014-05-13 13:18:27 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2014-05-13 13:18:27 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
|
|
|
#include "remote/apifunction.hpp"
|
2015-07-21 09:32:17 +02:00
|
|
|
#include "config/configcompiler.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/convert.hpp"
|
2014-12-15 10:16:06 +01:00
|
|
|
#include "base/exception.hpp"
|
2014-05-13 13:18:27 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <fstream>
|
2015-02-09 15:22:55 +01:00
|
|
|
#include <iomanip>
|
2014-05-13 13:18:27 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-05-13 15:57:02 +02:00
|
|
|
REGISTER_APIFUNCTION(Update, config, &ApiListener::ConfigUpdateHandler);
|
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
void ApiListener::ConfigGlobHandler(ConfigDirInformation& config, const String& path, const String& file)
|
2014-05-13 13:18:27 +02:00
|
|
|
{
|
|
|
|
CONTEXT("Creating config update for file '" + file + "'");
|
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogNotice, "ApiListener")
|
|
|
|
<< "Creating config update for file '" << file << "'";
|
2014-06-12 22:51:48 +02:00
|
|
|
|
2015-02-09 07:44:45 +01:00
|
|
|
std::ifstream fp(file.CStr(), std::ifstream::binary);
|
2014-05-13 13:18:27 +02:00
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
String content((std::istreambuf_iterator<char>(fp)), std::istreambuf_iterator<char>());
|
2016-01-26 10:46:27 +01:00
|
|
|
|
|
|
|
Dictionary::Ptr update;
|
|
|
|
|
|
|
|
if (Utility::Match("*.conf", file))
|
|
|
|
update = config.UpdateV1;
|
|
|
|
else
|
|
|
|
update = config.UpdateV2;
|
|
|
|
|
|
|
|
update->Set(file.SubStr(path.GetLength()), content);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary::Ptr ApiListener::MergeConfigUpdate(const ConfigDirInformation& config)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr result = new Dictionary();
|
|
|
|
|
|
|
|
if (config.UpdateV1)
|
|
|
|
config.UpdateV1->CopyTo(result);
|
|
|
|
|
|
|
|
if (config.UpdateV2)
|
|
|
|
config.UpdateV2->CopyTo(result);
|
|
|
|
|
|
|
|
return result;
|
2014-05-13 13:18:27 +02:00
|
|
|
}
|
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
ConfigDirInformation ApiListener::LoadConfigDir(const String& dir)
|
2014-05-13 13:18:27 +02:00
|
|
|
{
|
2016-01-26 10:46:27 +01:00
|
|
|
ConfigDirInformation config;
|
|
|
|
config.UpdateV1 = new Dictionary();
|
|
|
|
config.UpdateV2 = new Dictionary();
|
2016-01-26 09:04:26 +01:00
|
|
|
Utility::GlobRecursive(dir, "*", boost::bind(&ApiListener::ConfigGlobHandler, boost::ref(config), dir, _1), GlobFile);
|
2014-05-13 15:57:02 +02:00
|
|
|
return config;
|
|
|
|
}
|
2014-05-13 13:18:27 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
bool ApiListener::UpdateConfigDir(const ConfigDirInformation& oldConfigInfo, const ConfigDirInformation& newConfigInfo, const String& configDir, bool authoritative)
|
2014-05-13 15:57:02 +02:00
|
|
|
{
|
|
|
|
bool configChange = false;
|
2014-05-13 13:18:27 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
Dictionary::Ptr oldConfig = MergeConfigUpdate(oldConfigInfo);
|
|
|
|
Dictionary::Ptr newConfig = MergeConfigUpdate(newConfigInfo);
|
|
|
|
|
2016-01-25 15:21:25 +01:00
|
|
|
double oldTimestamp;
|
|
|
|
|
|
|
|
if (!oldConfig->Contains(".timestamp"))
|
|
|
|
oldTimestamp = 0;
|
|
|
|
else
|
|
|
|
oldTimestamp = oldConfig->Get(".timestamp");
|
|
|
|
|
|
|
|
double newTimestamp;
|
2014-05-15 14:27:50 +02:00
|
|
|
|
2016-01-25 15:21:25 +01:00
|
|
|
if (!newConfig->Contains(".timestamp"))
|
|
|
|
newTimestamp = Utility::GetTime();
|
|
|
|
else
|
|
|
|
newTimestamp = newConfig->Get(".timestamp");
|
2016-01-25 14:25:37 +01:00
|
|
|
|
|
|
|
/* skip update if our config is newer */
|
2016-01-25 15:21:25 +01:00
|
|
|
if (oldTimestamp >= newTimestamp)
|
2016-01-25 14:25:37 +01:00
|
|
|
return false;
|
2014-05-13 13:18:27 +02:00
|
|
|
|
2015-02-24 16:03:32 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(newConfig);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, newConfig) {
|
|
|
|
if (oldConfig->Get(kv.first) != kv.second) {
|
2016-01-26 15:42:54 +01:00
|
|
|
if (!Utility::Match("*/.timestamp", kv.first))
|
|
|
|
configChange = true;
|
2015-02-24 16:03:32 +01:00
|
|
|
|
|
|
|
String path = configDir + "/" + kv.first;
|
|
|
|
Log(LogInformation, "ApiListener")
|
|
|
|
<< "Updating configuration file: " << path;
|
|
|
|
|
|
|
|
//pass the directory and generate a dir tree, if not existing already
|
|
|
|
Utility::MkDirP(Utility::DirName(path), 0755);
|
|
|
|
std::ofstream fp(path.CStr(), std::ofstream::out | std::ostream::binary | std::ostream::trunc);
|
|
|
|
fp << kv.second;
|
|
|
|
fp.close();
|
|
|
|
}
|
2014-05-13 13:18:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-24 16:03:32 +01:00
|
|
|
ObjectLock xlock(oldConfig);
|
2014-05-13 15:57:02 +02:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, oldConfig) {
|
|
|
|
if (!newConfig->Contains(kv.first)) {
|
|
|
|
configChange = true;
|
|
|
|
|
|
|
|
String path = configDir + "/" + kv.first;
|
2014-05-13 13:18:27 +02:00
|
|
|
(void) unlink(path.CStr());
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 15:57:02 +02:00
|
|
|
|
2014-05-15 14:27:50 +02:00
|
|
|
String tsPath = configDir + "/.timestamp";
|
|
|
|
if (!Utility::PathExists(tsPath)) {
|
|
|
|
std::ofstream fp(tsPath.CStr(), std::ofstream::out | std::ostream::trunc);
|
2016-01-25 14:25:37 +01:00
|
|
|
fp << std::fixed << newTimestamp;
|
2014-05-15 14:27:50 +02:00
|
|
|
fp.close();
|
|
|
|
}
|
|
|
|
|
2015-01-20 13:18:40 +01:00
|
|
|
if (authoritative) {
|
|
|
|
String authPath = configDir + "/.authoritative";
|
2015-02-07 23:41:43 +01:00
|
|
|
if (!Utility::PathExists(authPath)) {
|
|
|
|
std::ofstream fp(authPath.CStr(), std::ofstream::out | std::ostream::trunc);
|
2015-01-20 13:18:40 +01:00
|
|
|
fp.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 15:57:02 +02:00
|
|
|
return configChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApiListener::SyncZoneDir(const Zone::Ptr& zone) const
|
|
|
|
{
|
2016-01-26 10:46:27 +01:00
|
|
|
ConfigDirInformation newConfigInfo;
|
|
|
|
newConfigInfo.UpdateV1 = new Dictionary();
|
|
|
|
newConfigInfo.UpdateV2 = new Dictionary();
|
|
|
|
|
2015-07-21 09:32:17 +02:00
|
|
|
BOOST_FOREACH(const ZoneFragment& zf, ConfigCompiler::GetZoneDirs(zone->GetName())) {
|
2016-01-26 10:46:27 +01:00
|
|
|
ConfigDirInformation newConfigPart = LoadConfigDir(zf.Path);
|
2015-07-21 09:32:17 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(newConfigPart.UpdateV1);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, newConfigPart.UpdateV1) {
|
|
|
|
newConfigInfo.UpdateV1->Set("/" + zf.Tag + kv.first, kv.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(newConfigPart.UpdateV2);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, newConfigPart.UpdateV2) {
|
|
|
|
newConfigInfo.UpdateV2->Set("/" + zf.Tag + kv.first, kv.second);
|
|
|
|
}
|
2015-07-21 09:32:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
int sumUpdates = newConfigInfo.UpdateV1->GetLength() + newConfigInfo.UpdateV2->GetLength();
|
|
|
|
|
|
|
|
if (sumUpdates == 0)
|
2015-12-11 19:54:17 +01:00
|
|
|
return;
|
|
|
|
|
2014-05-13 15:57:02 +02:00
|
|
|
String oldDir = Application::GetLocalStateDir() + "/lib/icinga2/api/zones/" + zone->GetName();
|
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogInformation, "ApiListener")
|
2016-01-26 10:46:27 +01:00
|
|
|
<< "Copying " << sumUpdates << " zone configuration files for zone '" << zone->GetName() << "' to '" << oldDir << "'.";
|
2014-08-20 14:07:23 +02:00
|
|
|
|
2016-01-26 10:45:03 +01:00
|
|
|
Utility::MkDirP(oldDir, 0700);
|
2014-05-13 15:57:02 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
ConfigDirInformation oldConfigInfo = LoadConfigDir(oldDir);
|
2014-05-13 15:57:02 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
UpdateConfigDir(oldConfigInfo, newConfigInfo, oldDir, true);
|
2014-05-13 13:18:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApiListener::SyncZoneDirs(void) const
|
|
|
|
{
|
2015-08-15 20:28:05 +02:00
|
|
|
BOOST_FOREACH(const Zone::Ptr& zone, ConfigType::GetObjectsByType<Zone>()) {
|
2014-06-13 09:23:05 +02:00
|
|
|
try {
|
|
|
|
SyncZoneDir(zone);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {
|
2014-06-13 09:23:05 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-05-13 13:18:27 +02:00
|
|
|
}
|
2014-05-13 15:57:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
void ApiListener::SendConfigUpdate(const JsonRpcConnection::Ptr& aclient)
|
2014-05-13 15:57:02 +02:00
|
|
|
{
|
|
|
|
Endpoint::Ptr endpoint = aclient->GetEndpoint();
|
|
|
|
ASSERT(endpoint);
|
|
|
|
|
|
|
|
Zone::Ptr azone = endpoint->GetZone();
|
|
|
|
Zone::Ptr lzone = Zone::GetLocalZone();
|
|
|
|
|
|
|
|
/* don't try to send config updates to our master */
|
2014-05-23 18:35:43 +02:00
|
|
|
if (!azone->IsChildOf(lzone))
|
2014-05-13 15:57:02 +02:00
|
|
|
return;
|
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
Dictionary::Ptr configUpdateV1 = new Dictionary();
|
|
|
|
Dictionary::Ptr configUpdateV2 = new Dictionary();
|
2014-05-13 15:57:02 +02:00
|
|
|
|
|
|
|
String zonesDir = Application::GetLocalStateDir() + "/lib/icinga2/api/zones";
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
BOOST_FOREACH(const Zone::Ptr& zone, ConfigType::GetObjectsByType<Zone>()) {
|
2014-05-13 15:57:02 +02:00
|
|
|
String zoneDir = zonesDir + "/" + zone->GetName();
|
|
|
|
|
2016-01-21 13:02:53 +01:00
|
|
|
if (!zone->IsChildOf(azone) && !zone->IsGlobal())
|
2014-05-13 15:57:02 +02:00
|
|
|
continue;
|
2016-01-21 13:02:53 +01:00
|
|
|
|
|
|
|
if (!Utility::PathExists(zoneDir))
|
2014-06-12 14:31:07 +02:00
|
|
|
continue;
|
|
|
|
|
2015-09-15 16:09:56 +02:00
|
|
|
Log(LogInformation, "ApiListener")
|
|
|
|
<< "Syncing " << (zone->IsGlobal() ? "global " : "")
|
|
|
|
<< "zone '" << zone->GetName() << "' to endpoint '" << endpoint->GetName() << "'.";
|
2014-05-13 15:57:02 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
ConfigDirInformation config = LoadConfigDir(zonesDir + "/" + zone->GetName());
|
|
|
|
configUpdateV1->Set(zone->GetName(), config.UpdateV1);
|
|
|
|
configUpdateV2->Set(zone->GetName(), config.UpdateV2);
|
2014-05-13 15:57:02 +02:00
|
|
|
}
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr params = new Dictionary();
|
2016-01-26 10:46:27 +01:00
|
|
|
params->Set("update", configUpdateV1);
|
|
|
|
params->Set("update_v2", configUpdateV2);
|
2014-05-13 15:57:02 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr message = new Dictionary();
|
2014-05-13 15:57:02 +02:00
|
|
|
message->Set("jsonrpc", "2.0");
|
|
|
|
message->Set("method", "config::Update");
|
|
|
|
message->Set("params", params);
|
|
|
|
|
|
|
|
aclient->SendMessage(message);
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
Value ApiListener::ConfigUpdateHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
|
2014-05-13 15:57:02 +02:00
|
|
|
{
|
2015-08-04 14:47:44 +02:00
|
|
|
if (!origin->FromClient->GetEndpoint() || (origin->FromZone && !Zone::GetLocalZone()->IsChildOf(origin->FromZone)))
|
2014-05-13 15:57:02 +02:00
|
|
|
return Empty;
|
|
|
|
|
2014-05-15 10:13:32 +02:00
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
|
|
|
|
2014-06-12 14:31:07 +02:00
|
|
|
if (!listener) {
|
|
|
|
Log(LogCritical, "ApiListener", "No instance available.");
|
2014-05-15 10:13:32 +02:00
|
|
|
return Empty;
|
2014-06-12 14:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!listener->GetAcceptConfig()) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogWarning, "ApiListener")
|
|
|
|
<< "Ignoring config update. '" << listener->GetName() << "' does not accept config.";
|
2014-06-12 14:31:07 +02:00
|
|
|
return Empty;
|
|
|
|
}
|
2014-05-15 10:13:32 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
Dictionary::Ptr updateV1 = params->Get("update");
|
|
|
|
Dictionary::Ptr updateV2 = params->Get("update_v2");
|
2014-05-13 13:18:27 +02:00
|
|
|
|
|
|
|
bool configChange = false;
|
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
ObjectLock olock(updateV1);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, updateV1) {
|
2014-05-13 15:57:02 +02:00
|
|
|
Zone::Ptr zone = Zone::GetByName(kv.first);
|
|
|
|
|
|
|
|
if (!zone) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogWarning, "ApiListener")
|
2015-03-09 08:43:46 +01:00
|
|
|
<< "Ignoring config update for unknown zone '" << kv.first << "'.";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-11 19:54:17 +01:00
|
|
|
if (ConfigCompiler::HasZoneConfigAuthority(kv.first)) {
|
2015-03-09 08:43:46 +01:00
|
|
|
Log(LogWarning, "ApiListener")
|
|
|
|
<< "Ignoring config update for zone '" << kv.first << "' because we have an authoritative version of the zone's config.";
|
2014-05-13 15:57:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String oldDir = Application::GetLocalStateDir() + "/lib/icinga2/api/zones/" + zone->GetName();
|
|
|
|
|
2016-01-26 10:45:03 +01:00
|
|
|
Utility::MkDirP(oldDir, 0700);
|
2014-05-13 15:57:02 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
ConfigDirInformation newConfigInfo;
|
|
|
|
newConfigInfo.UpdateV1 = kv.second;
|
|
|
|
|
|
|
|
if (updateV2)
|
|
|
|
newConfigInfo.UpdateV2 = updateV2->Get(kv.first);
|
|
|
|
|
2014-05-13 15:57:02 +02:00
|
|
|
Dictionary::Ptr newConfig = kv.second;
|
2016-01-26 10:46:27 +01:00
|
|
|
ConfigDirInformation oldConfigInfo = LoadConfigDir(oldDir);
|
2014-05-13 15:57:02 +02:00
|
|
|
|
2016-01-26 10:46:27 +01:00
|
|
|
if (UpdateConfigDir(oldConfigInfo, newConfigInfo, oldDir, false))
|
2014-05-13 15:57:02 +02:00
|
|
|
configChange = true;
|
|
|
|
}
|
2014-05-13 13:18:27 +02:00
|
|
|
|
|
|
|
if (configChange) {
|
2014-05-28 13:58:56 +02:00
|
|
|
Log(LogInformation, "ApiListener", "Restarting after configuration change.");
|
2014-05-13 13:18:27 +02:00
|
|
|
Application::RequestRestart();
|
|
|
|
}
|
2014-05-13 15:57:02 +02:00
|
|
|
|
|
|
|
return Empty;
|
2014-05-13 13:18:27 +02:00
|
|
|
}
|