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);
|
|
|
|
|
2014-06-12 22:51:48 +02:00
|
|
|
void ApiListener::ConfigGlobHandler(Dictionary::Ptr& 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>());
|
|
|
|
config->Set(file.SubStr(path.GetLength()), content);
|
|
|
|
}
|
|
|
|
|
2014-05-13 15:57:02 +02:00
|
|
|
Dictionary::Ptr ApiListener::LoadConfigDir(const String& dir)
|
2014-05-13 13:18:27 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr config = new Dictionary();
|
2014-06-12 22:51:48 +02:00
|
|
|
Utility::GlobRecursive(dir, "*.conf", 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
|
|
|
|
2015-01-20 13:18:40 +01:00
|
|
|
bool ApiListener::UpdateConfigDir(const Dictionary::Ptr& oldConfig, const Dictionary::Ptr& newConfig, const String& configDir, bool authoritative)
|
2014-05-13 15:57:02 +02:00
|
|
|
{
|
|
|
|
bool configChange = false;
|
2014-05-13 13:18:27 +02:00
|
|
|
|
2014-05-15 14:27:50 +02:00
|
|
|
if (oldConfig->Contains(".timestamp") && newConfig->Contains(".timestamp")) {
|
|
|
|
double oldTS = Convert::ToDouble(oldConfig->Get(".timestamp"));
|
|
|
|
double newTS = Convert::ToDouble(newConfig->Get(".timestamp"));
|
|
|
|
|
|
|
|
/* skip update if our config is newer */
|
|
|
|
if (oldTS <= newTS)
|
|
|
|
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) {
|
|
|
|
configChange = true;
|
|
|
|
|
|
|
|
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);
|
2015-02-09 15:22:55 +01:00
|
|
|
fp << std::fixed << Utility::GetTime();
|
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
|
|
|
|
{
|
2015-07-21 09:32:17 +02:00
|
|
|
Dictionary::Ptr newConfig = new Dictionary();
|
|
|
|
BOOST_FOREACH(const ZoneFragment& zf, ConfigCompiler::GetZoneDirs(zone->GetName())) {
|
|
|
|
Dictionary::Ptr newConfigPart = LoadConfigDir(zf.Path);
|
|
|
|
|
|
|
|
ObjectLock olock(newConfigPart);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, newConfigPart) {
|
2015-07-21 11:45:17 +02:00
|
|
|
newConfig->Set("/" + zf.Tag + kv.first, kv.second);
|
2015-07-21 09:32:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 19:54:17 +01:00
|
|
|
if (newConfig->GetLength() == 0)
|
|
|
|
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")
|
2015-12-11 19:54:17 +01:00
|
|
|
<< "Copying " << newConfig->GetLength() << " zone configuration files for zone '" << zone->GetName() << "' to '" << oldDir << "'.";
|
2014-08-20 14:07:23 +02:00
|
|
|
|
2015-08-24 15:11:49 +02:00
|
|
|
Utility::MkDir(oldDir, 0700);
|
2014-05-13 15:57:02 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr oldConfig = LoadConfigDir(oldDir);
|
|
|
|
|
2015-01-20 13:18:40 +01:00
|
|
|
UpdateConfigDir(oldConfig, newConfig, 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;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr configUpdate = 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();
|
|
|
|
|
2014-06-12 14:31:07 +02:00
|
|
|
if (!zone->IsChildOf(azone) && !zone->IsGlobal()) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogNotice, "ApiListener")
|
|
|
|
<< "Skipping sync for '" << zone->GetName() << "'. Not a child of zone '" << azone->GetName() << "'.";
|
2014-05-13 15:57:02 +02:00
|
|
|
continue;
|
2014-06-12 14:31:07 +02:00
|
|
|
}
|
|
|
|
if (!Utility::PathExists(zoneDir)) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogNotice, "ApiListener")
|
|
|
|
<< "Ignoring sync for '" << zone->GetName() << "'. Zone directory '" << zoneDir << "' does not exist.";
|
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
|
|
|
|
|
|
|
configUpdate->Set(zone->GetName(), LoadConfigDir(zonesDir + "/" + zone->GetName()));
|
|
|
|
}
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr params = new Dictionary();
|
2014-05-13 15:57:02 +02:00
|
|
|
params->Set("update", configUpdate);
|
|
|
|
|
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
|
|
|
|
2014-05-13 15:57:02 +02:00
|
|
|
Dictionary::Ptr update = params->Get("update");
|
2014-05-13 13:18:27 +02:00
|
|
|
|
|
|
|
bool configChange = false;
|
|
|
|
|
2014-12-01 13:18:46 +01:00
|
|
|
ObjectLock olock(update);
|
2014-05-13 15:57:02 +02:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, update) {
|
|
|
|
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();
|
|
|
|
|
2015-08-24 15:11:49 +02:00
|
|
|
Utility::MkDir(oldDir, 0700);
|
2014-05-13 15:57:02 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr newConfig = kv.second;
|
|
|
|
Dictionary::Ptr oldConfig = LoadConfigDir(oldDir);
|
|
|
|
|
2015-01-20 13:18:40 +01:00
|
|
|
if (UpdateConfigDir(oldConfig, newConfig, 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
|
|
|
}
|