2014-10-17 15:13:17 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
|
|
|
* *
|
|
|
|
* 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-10-31 11:38:16 +01:00
|
|
|
#include "cli/nodeutility.hpp"
|
2014-10-18 21:06:28 +02:00
|
|
|
#include "cli/clicommand.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/tlsutility.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2014-10-27 15:12:19 +01:00
|
|
|
#include "base/utility.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2014-10-18 21:06:28 +02:00
|
|
|
#include "base/netstring.hpp"
|
|
|
|
#include "base/stdiostream.hpp"
|
|
|
|
#include "base/debug.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/console.hpp"
|
|
|
|
#include <boost/foreach.hpp>
|
2014-10-22 15:29:54 +02:00
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
2014-10-18 21:06:28 +02:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2014-10-21 14:23:47 +02:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2014-10-22 15:29:54 +02:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2014-10-18 21:06:28 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2014-10-17 15:13:17 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeUtility::GetRepositoryPath(void)
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-18 21:06:28 +02:00
|
|
|
return Application::GetLocalStateDir() + "/lib/icinga2/api/repository";
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeUtility::GetNodeRepositoryFile(const String& name)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
|
|
|
return GetRepositoryPath() + "/" + SHA256(name) + ".repo";
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeUtility::GetNodeSettingsFile(const String& name)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
|
|
|
return GetRepositoryPath() + "/" + SHA256(name) + ".settings";
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
std::vector<String> NodeUtility::GetNodeCompletionSuggestions(const String& word)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
|
|
|
std::vector<String> suggestions;
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& node, GetNodes()) {
|
|
|
|
String node_name = node->Get("endpoint");
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
if (node_name.Find(word) == 0)
|
|
|
|
suggestions.push_back(node_name);
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::PrintNodes(std::ostream& fp)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
2014-10-24 17:36:42 +02:00
|
|
|
bool first = true;
|
2014-10-24 12:04:14 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& node, GetNodes()) {
|
2014-10-24 12:04:14 +02:00
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
fp << "\n";
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
fp << "Node '"
|
|
|
|
<< ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << node->Get("endpoint") << ConsoleColorTag(Console_Normal)
|
2014-10-27 15:12:19 +01:00
|
|
|
<< "' (";
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
Dictionary::Ptr settings = node->Get("settings");
|
2014-10-27 15:12:19 +01:00
|
|
|
|
|
|
|
if (settings) {
|
|
|
|
String host = settings->Get("host");
|
|
|
|
String port = settings->Get("port");
|
|
|
|
double log_duration = settings->Get("log_duration");
|
|
|
|
|
|
|
|
if (!host.IsEmpty() && !port.IsEmpty())
|
|
|
|
fp << "host: " << host << ", port: " << port << ", ";
|
|
|
|
|
|
|
|
fp << "log duration: " << Utility::FormatDuration(log_duration) << ", ";
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
fp << "last seen: " << Utility::FormatDateTime("%c", node->Get("seen")) << ")\n";
|
2014-10-24 12:04:14 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
PrintNodeRepository(fp, node->Get("repository"));
|
2014-10-24 12:04:14 +02:00
|
|
|
}
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::PrintNodeRepository(std::ostream& fp, const Dictionary::Ptr& repository)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
2014-10-24 12:16:36 +02:00
|
|
|
if (!repository)
|
|
|
|
return;
|
|
|
|
|
2014-10-24 12:04:14 +02:00
|
|
|
ObjectLock olock(repository);
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, repository) {
|
|
|
|
fp << std::setw(4) << " "
|
|
|
|
<< "* Host '" << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << kv.first << ConsoleColorTag(Console_Normal) << "'\n";
|
|
|
|
|
|
|
|
Array::Ptr services = kv.second;
|
|
|
|
ObjectLock xlock(services);
|
|
|
|
BOOST_FOREACH(const String& service, services) {
|
|
|
|
fp << std::setw(8) << " " << "* Service '" << ConsoleColorTag(Console_ForegroundGreen | Console_Bold) << service << ConsoleColorTag(Console_Normal) << "'\n";
|
|
|
|
}
|
|
|
|
}
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::PrintNodesJson(std::ostream& fp)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr result = new Dictionary();
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& node, GetNodes()) {
|
|
|
|
result->Set(node->Get("endpoint"), node);
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|
2014-10-24 12:04:14 +02:00
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
fp << JsonEncode(result);
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::AddNode(const String& name)
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-31 11:38:16 +01:00
|
|
|
String path = GetNodeRepositoryFile(name);
|
2014-10-18 21:06:28 +02:00
|
|
|
|
|
|
|
if (Utility::PathExists(path) ) {
|
2014-10-27 15:12:19 +01:00
|
|
|
Log(LogInformation, "cli")
|
2014-10-31 11:38:16 +01:00
|
|
|
<< "Node '" << name << "' exists already.";
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr node = new Dictionary();
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
node->Set("seen", Utility::GetTime());
|
|
|
|
node->Set("endpoint", name);
|
|
|
|
node->Set("zone", name);
|
|
|
|
node->Set("repository", Empty);
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
Utility::SaveJsonFile(path, node);
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::AddNodeSettings(const String& name, const String& host,
|
2014-10-27 15:12:19 +01:00
|
|
|
const String& port, double log_duration)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr settings = new Dictionary();
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-27 15:12:19 +01:00
|
|
|
settings->Set("host", host);
|
|
|
|
settings->Set("port", port);
|
|
|
|
settings->Set("log_duration", log_duration);
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
Utility::SaveJsonFile(GetNodeSettingsFile(name), settings);
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::RemoveNode(const String& name)
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-31 11:38:16 +01:00
|
|
|
String repoPath = GetNodeRepositoryFile(name);
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-27 15:12:19 +01:00
|
|
|
if (!Utility::PathExists(repoPath))
|
|
|
|
return;
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-27 15:12:19 +01:00
|
|
|
if (unlink(repoPath.CStr()) < 0) {
|
2014-10-23 03:35:01 +02:00
|
|
|
Log(LogCritical, "cli")
|
2014-10-27 15:12:19 +01:00
|
|
|
<< "Cannot remove file '" << repoPath
|
2014-10-23 03:35:01 +02:00
|
|
|
<< "'. Failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) + "\".";
|
2014-10-27 15:12:19 +01:00
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("unlink")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(repoPath));
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String settingsPath = GetNodeSettingsFile(name);
|
2014-10-27 15:12:19 +01:00
|
|
|
|
|
|
|
if (Utility::PathExists(settingsPath)) {
|
|
|
|
if (unlink(settingsPath.CStr()) < 0) {
|
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Cannot remove file '" << settingsPath
|
|
|
|
<< "'. Failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) + "\".";
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("unlink")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(settingsPath));
|
|
|
|
}
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
std::vector<Dictionary::Ptr> NodeUtility::GetNodes(void)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
2014-10-31 11:38:16 +01:00
|
|
|
std::vector<Dictionary::Ptr> nodes;
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-27 15:12:19 +01:00
|
|
|
Utility::Glob(GetRepositoryPath() + "/*.repo",
|
2014-10-31 11:38:16 +01:00
|
|
|
boost::bind(&NodeUtility::CollectNodes, _1, boost::ref(nodes)), GlobFile);
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
return nodes;
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
Dictionary::Ptr NodeUtility::LoadNodeFile(const String& node_file)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
2014-10-31 11:38:16 +01:00
|
|
|
Dictionary::Ptr node = Utility::LoadJsonFile(node_file);
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
if (!node)
|
2014-10-18 21:06:28 +02:00
|
|
|
return Dictionary::Ptr();
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String settingsFile = GetNodeSettingsFile(node->Get("endpoint"));
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-27 15:12:19 +01:00
|
|
|
if (Utility::PathExists(settingsFile))
|
2014-10-31 11:38:16 +01:00
|
|
|
node->Set("settings", Utility::LoadJsonFile(settingsFile));
|
2014-10-27 15:12:19 +01:00
|
|
|
else
|
2014-10-31 11:38:16 +01:00
|
|
|
node->Remove("settings");
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
return node;
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
void NodeUtility::CollectNodes(const String& node_file, std::vector<Dictionary::Ptr>& nodes)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
2014-10-31 11:53:44 +01:00
|
|
|
Dictionary::Ptr node = LoadNodeFile(node_file);
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
if (!node)
|
2014-10-24 12:04:14 +02:00
|
|
|
return;
|
2014-10-23 03:35:01 +02:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
nodes.push_back(node);
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-22 15:29:54 +02:00
|
|
|
/*
|
2014-10-31 11:38:16 +01:00
|
|
|
* Node Setup helpers
|
2014-10-22 15:29:54 +02:00
|
|
|
*/
|
|
|
|
|
2014-10-31 22:21:36 +01:00
|
|
|
int NodeUtility::GenerateNodeIcingaConfig(const std::vector<std::string>& endpoints, const String& nodename, const String& zonename)
|
2014-10-22 15:29:54 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr my_config = new Array();
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr my_master_zone = new Dictionary();
|
|
|
|
Array::Ptr my_master_zone_members = new Array();
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-10-29 11:09:55 +01:00
|
|
|
String master_zone_name = "master"; //TODO: Find a better name.
|
|
|
|
|
2014-10-22 15:29:54 +02:00
|
|
|
BOOST_FOREACH(const std::string& endpoint, endpoints) {
|
|
|
|
|
|
|
|
/* extract all --endpoint arguments and store host,port info */
|
|
|
|
std::vector<String> tokens;
|
|
|
|
boost::algorithm::split(tokens, endpoint, boost::is_any_of(","));
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr my_master_endpoint = new Dictionary();
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-10-30 16:19:51 +01:00
|
|
|
if (tokens.size() > 1) {
|
|
|
|
String host = tokens[1];
|
|
|
|
host.Trim();
|
|
|
|
my_master_endpoint->Set("host", host);
|
|
|
|
}
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-10-30 16:19:51 +01:00
|
|
|
if (tokens.size() > 2) {
|
|
|
|
String port = tokens[2];
|
|
|
|
port.Trim();
|
|
|
|
my_master_endpoint->Set("port", port);
|
|
|
|
}
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-10-30 16:19:51 +01:00
|
|
|
String cn = tokens[0];
|
|
|
|
cn.Trim();
|
|
|
|
my_master_endpoint->Set("__name", cn);
|
2014-10-22 15:29:54 +02:00
|
|
|
my_master_endpoint->Set("__type", "Endpoint");
|
|
|
|
|
|
|
|
/* save endpoint in master zone */
|
2014-10-30 16:19:51 +01:00
|
|
|
my_master_zone_members->Add(cn);
|
2014-10-22 15:29:54 +02:00
|
|
|
|
|
|
|
my_config->Add(my_master_endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add the master zone to the config */
|
2014-10-29 11:09:55 +01:00
|
|
|
my_master_zone->Set("__name", master_zone_name);
|
2014-10-22 15:29:54 +02:00
|
|
|
my_master_zone->Set("__type", "Zone");
|
|
|
|
my_master_zone->Set("endpoints", my_master_zone_members);
|
|
|
|
|
|
|
|
my_config->Add(my_master_zone);
|
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
/* store the local generated node configuration */
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr my_endpoint = new Dictionary();
|
|
|
|
Dictionary::Ptr my_zone = new Dictionary();
|
2014-10-22 15:29:54 +02:00
|
|
|
|
|
|
|
my_endpoint->Set("__name", nodename);
|
|
|
|
my_endpoint->Set("__type", "Endpoint");
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr my_zone_members = new Array();
|
2014-10-22 15:29:54 +02:00
|
|
|
my_zone_members->Add(nodename);
|
|
|
|
|
|
|
|
my_zone->Set("__name", nodename);
|
|
|
|
my_zone->Set("__type", "Zone");
|
2014-10-29 11:09:55 +01:00
|
|
|
my_zone->Set("parent", master_zone_name); //set the master zone as parent
|
2014-10-31 11:53:44 +01:00
|
|
|
my_zone->Set("//this is the local node", nodename);
|
2014-10-22 15:29:54 +02:00
|
|
|
my_zone->Set("endpoints", my_zone_members);
|
|
|
|
|
|
|
|
/* store the local config */
|
|
|
|
my_config->Add(my_endpoint);
|
|
|
|
my_config->Add(my_zone);
|
|
|
|
|
|
|
|
/* write the newly generated configuration */
|
|
|
|
String zones_path = Application::GetSysconfDir() + "/icinga2/zones.conf";
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::WriteNodeConfigObjects(zones_path, my_config);
|
2014-10-22 15:29:54 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
int NodeUtility::GenerateNodeMasterIcingaConfig(const String& nodename)
|
2014-10-22 19:27:21 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr my_config = new Array();
|
2014-10-22 19:27:21 +02:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
/* store the local generated node master configuration */
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr my_master_endpoint = new Dictionary();
|
|
|
|
Dictionary::Ptr my_master_zone = new Dictionary();
|
|
|
|
Array::Ptr my_master_zone_members = new Array();
|
2014-10-22 19:27:21 +02:00
|
|
|
|
|
|
|
my_master_endpoint->Set("__name", nodename);
|
|
|
|
my_master_endpoint->Set("__type", "Endpoint");
|
|
|
|
|
|
|
|
my_master_zone_members->Add(nodename);
|
|
|
|
|
|
|
|
my_master_zone->Set("__name", "master");
|
|
|
|
my_master_zone->Set("__type", "Zone");
|
2014-10-31 11:53:44 +01:00
|
|
|
my_master_zone->Set("//this is the local node master named ", "master");
|
2014-10-22 19:27:21 +02:00
|
|
|
my_master_zone->Set("endpoints", my_master_zone_members);
|
|
|
|
|
|
|
|
/* store the local config */
|
|
|
|
my_config->Add(my_master_endpoint);
|
|
|
|
my_config->Add(my_master_zone);
|
|
|
|
|
|
|
|
/* write the newly generated configuration */
|
|
|
|
String zones_path = Application::GetSysconfDir() + "/icinga2/zones.conf";
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::WriteNodeConfigObjects(zones_path, my_config);
|
2014-10-22 19:27:21 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-21 21:33:21 +02:00
|
|
|
/*
|
|
|
|
* This is ugly and requires refactoring into a generic config writer class.
|
|
|
|
* TODO.
|
|
|
|
*/
|
2014-10-31 11:38:16 +01:00
|
|
|
bool NodeUtility::WriteNodeConfigObjects(const String& filename, const Array::Ptr& objects)
|
2014-10-21 21:33:21 +02:00
|
|
|
{
|
2014-10-23 03:35:01 +02:00
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Dumping config items to file '" << filename << "'.";
|
2014-10-22 15:29:54 +02:00
|
|
|
|
|
|
|
/* create a backup first */
|
|
|
|
CreateBackupFile(filename);
|
2014-10-21 21:33:21 +02:00
|
|
|
|
|
|
|
Utility::MkDirP(Utility::DirName(filename), 0755);
|
|
|
|
|
|
|
|
String tempPath = filename + ".tmp";
|
|
|
|
|
|
|
|
std::ofstream fp(tempPath.CStr(), std::ofstream::out | std::ostream::trunc);
|
|
|
|
|
2014-10-22 15:29:54 +02:00
|
|
|
fp << "/*\n";
|
2014-10-31 11:53:44 +01:00
|
|
|
fp << " * Generated by Icinga 2 node setup commands\n";
|
2014-10-22 15:29:54 +02:00
|
|
|
fp << " * on " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime()) << "\n";
|
|
|
|
fp << " */\n\n";
|
|
|
|
|
2014-10-21 21:33:21 +02:00
|
|
|
ObjectLock olock(objects);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& object, objects) {
|
|
|
|
String name = object->Get("__name");
|
|
|
|
String type = object->Get("__type");
|
|
|
|
|
|
|
|
SerializeObject(fp, name, type, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
fp << std::endl;
|
|
|
|
fp.close();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
_unlink(filename.CStr());
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
if (rename(tempPath.CStr(), filename.CStr()) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("rename")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(tempPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-22 15:29:54 +02:00
|
|
|
/*
|
2014-10-29 13:53:59 +01:00
|
|
|
* Black/Whitelist helpers
|
2014-10-22 15:29:54 +02:00
|
|
|
*/
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeUtility::GetBlackAndWhiteListPath(const String& type)
|
2014-10-30 00:29:09 +01:00
|
|
|
{
|
2014-10-31 11:38:16 +01:00
|
|
|
return NodeUtility::GetRepositoryPath() + "/" + type + ".list";
|
2014-10-30 00:29:09 +01:00
|
|
|
}
|
|
|
|
|
2014-10-31 16:21:07 +01:00
|
|
|
Array::Ptr NodeUtility::GetBlackAndWhiteList(const String& type)
|
2014-10-22 15:29:54 +02:00
|
|
|
{
|
2014-10-30 00:29:09 +01:00
|
|
|
String list_path = GetBlackAndWhiteListPath(type);
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr lists = new Array();
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-10-29 13:53:59 +01:00
|
|
|
if (Utility::PathExists(list_path)) {
|
|
|
|
lists = Utility::LoadJsonFile(list_path);
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:29:09 +01:00
|
|
|
return lists;
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:55:38 +01:00
|
|
|
int NodeUtility::UpdateBlackAndWhiteList(const String& type, const String& zone_filter, const String& host_filter, const String& service_filter)
|
2014-10-30 00:29:09 +01:00
|
|
|
{
|
2014-10-31 16:21:07 +01:00
|
|
|
Array::Ptr lists = GetBlackAndWhiteList(type);
|
|
|
|
|
|
|
|
{
|
|
|
|
ObjectLock olock(lists);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& filter, lists) {
|
|
|
|
|
|
|
|
if (filter->Get("zone") == zone_filter) {
|
|
|
|
if (filter->Get("host") == host_filter && service_filter.IsEmpty()) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Found zone filter '" << zone_filter << "' with host filter '" << host_filter << "'. Bailing out.";
|
|
|
|
return 1;
|
|
|
|
} else if (filter->Get("host") == host_filter && filter->Get("service") == service_filter) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Found zone filter '" << zone_filter << "' with host filter '" << host_filter << "' and service filter '"
|
|
|
|
<< service_filter << "'. Bailing out.";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 15:29:54 +02:00
|
|
|
}
|
2014-10-29 13:53:59 +01:00
|
|
|
}
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr new_filter = new Dictionary();
|
2014-10-31 16:21:07 +01:00
|
|
|
|
|
|
|
new_filter->Set("zone", zone_filter);
|
|
|
|
new_filter->Set("host", host_filter);
|
|
|
|
new_filter->Set("service", service_filter);
|
|
|
|
|
|
|
|
lists->Add(new_filter);
|
2014-10-29 13:53:59 +01:00
|
|
|
|
2014-10-30 00:29:09 +01:00
|
|
|
String list_path = GetBlackAndWhiteListPath(type);
|
2014-10-29 13:53:59 +01:00
|
|
|
Utility::SaveJsonFile(list_path, lists);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:55:38 +01:00
|
|
|
int NodeUtility::RemoveBlackAndWhiteList(const String& type, const String& zone_filter, const String& host_filter, const String& service_filter)
|
2014-10-29 13:53:59 +01:00
|
|
|
{
|
2014-10-31 16:21:07 +01:00
|
|
|
Array::Ptr lists = GetBlackAndWhiteList(type);
|
|
|
|
|
|
|
|
std::vector<int> remove_filters;
|
|
|
|
int remove_idx = 0;
|
|
|
|
{
|
|
|
|
ObjectLock olock(lists);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& filter, lists) {
|
|
|
|
|
|
|
|
if (filter->Get("zone") == zone_filter) {
|
|
|
|
if (filter->Get("host") == host_filter && service_filter.IsEmpty()) {
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Found zone filter '" << zone_filter << "' with host filter '" << host_filter << "'. Removing from " << type << ".";
|
|
|
|
remove_filters.push_back(remove_idx);
|
|
|
|
} else if (filter->Get("host") == host_filter && filter->Get("service") == service_filter) {
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Found zone filter '" << zone_filter << "' with host filter '" << host_filter << "' and service filter '"
|
|
|
|
<< service_filter << "'. Removing from " << type << ".";
|
|
|
|
remove_filters.push_back(remove_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_idx++;
|
2014-10-29 13:53:59 +01:00
|
|
|
}
|
2014-10-31 16:21:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if there are no matches for reomval, throw an error */
|
|
|
|
if (remove_filters.empty()) {
|
2014-10-29 13:53:59 +01:00
|
|
|
Log(LogCritical, "cli", "Cannot remove filter!");
|
|
|
|
return 1;
|
2014-10-22 15:29:54 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 16:21:07 +01:00
|
|
|
BOOST_FOREACH(int remove, remove_filters) {
|
|
|
|
lists->Remove(remove);
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:29:09 +01:00
|
|
|
String list_path = GetBlackAndWhiteListPath(type);
|
2014-10-29 13:53:59 +01:00
|
|
|
Utility::SaveJsonFile(list_path, lists);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
int NodeUtility::PrintBlackAndWhiteList(std::ostream& fp, const String& type)
|
2014-10-29 13:53:59 +01:00
|
|
|
{
|
2014-10-31 16:21:07 +01:00
|
|
|
Array::Ptr lists = GetBlackAndWhiteList(type);
|
|
|
|
|
|
|
|
if (lists->GetLength() == 0)
|
|
|
|
return 0;
|
2014-10-29 13:53:59 +01:00
|
|
|
|
|
|
|
fp << "Listing all " << type << " entries:\n";
|
|
|
|
|
|
|
|
ObjectLock olock(lists);
|
2014-10-31 16:21:07 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& filter, lists) {
|
|
|
|
fp << type << " filter for Node: '" << filter->Get("zone") << "' Host: '"
|
|
|
|
<< filter->Get("host") << "' Service: '" << filter->Get("service") << "'.\n";
|
2014-10-29 13:53:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:55:38 +01:00
|
|
|
bool NodeUtility::CheckAgainstBlackAndWhiteList(const String& type, const String& zone, const String& host, const String& service)
|
2014-10-30 00:29:09 +01:00
|
|
|
{
|
2014-10-31 16:21:07 +01:00
|
|
|
Array::Ptr lists = GetBlackAndWhiteList(type);
|
2014-10-30 00:29:09 +01:00
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Checking object against " << type << ".";
|
|
|
|
|
|
|
|
ObjectLock olock(lists);
|
2014-10-31 16:21:07 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& filter, lists) {
|
|
|
|
String zone_filter = filter->Get("zone");
|
|
|
|
String host_filter = filter->Get("host");
|
2014-10-30 00:29:09 +01:00
|
|
|
String service_filter;
|
|
|
|
|
2014-10-31 16:21:07 +01:00
|
|
|
if (filter->Contains("service"))
|
|
|
|
service_filter = filter->Get("service");
|
2014-10-30 00:29:09 +01:00
|
|
|
|
2014-10-31 16:21:07 +01:00
|
|
|
Log(LogNotice, "cli")
|
2014-10-31 11:55:38 +01:00
|
|
|
<< "Checking Node '" << zone << "' =~ '" << zone_filter << "', host '" << host << "' =~ '" << host_filter
|
2014-10-30 00:29:09 +01:00
|
|
|
<< "', service '" << service << "' =~ '" << service_filter << "'.";
|
|
|
|
|
2014-10-31 11:55:38 +01:00
|
|
|
if (Utility::Match(zone_filter, zone)) {
|
2014-10-30 00:29:09 +01:00
|
|
|
Log(LogNotice, "cli")
|
2014-10-31 11:55:38 +01:00
|
|
|
<< "Node '" << zone << "' matches filter '" << zone_filter << "'";
|
2014-10-30 00:29:09 +01:00
|
|
|
|
|
|
|
if (Utility::Match(host_filter, host)) {
|
|
|
|
Log(LogNotice, "cli")
|
|
|
|
<< "Host '" << host << "' matches filter '" << host_filter << "'";
|
|
|
|
|
|
|
|
/* no service filter means host match */
|
|
|
|
if (service_filter.IsEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Utility::Match(service_filter, service)) {
|
|
|
|
Log(LogNotice, "cli")
|
|
|
|
<< "Host '" << service << "' matches filter '" << service_filter << "'";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-29 13:53:59 +01:00
|
|
|
/*
|
|
|
|
* We generally don't overwrite files without backup before
|
|
|
|
*/
|
2014-10-31 11:38:16 +01:00
|
|
|
bool NodeUtility::CreateBackupFile(const String& target)
|
2014-10-29 13:53:59 +01:00
|
|
|
{
|
|
|
|
if (!Utility::PathExists(target))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
String backup = target + ".orig";
|
|
|
|
|
|
|
|
if (Utility::PathExists(backup)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Backup file '" << backup << "' already exists. Skipping backup.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Utility::CopyFile(target, backup);
|
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Created backup file '" << backup << "'.";
|
|
|
|
|
2014-10-22 15:29:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::SerializeObject(std::ostream& fp, const String& name, const String& type, const Dictionary::Ptr& object)
|
2014-10-21 21:33:21 +02:00
|
|
|
{
|
|
|
|
fp << "object " << type << " \"" << name << "\" {\n";
|
2014-10-22 21:49:41 +02:00
|
|
|
ObjectLock olock(object);
|
2014-10-21 21:33:21 +02:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, object) {
|
|
|
|
if (kv.first == "__type" || kv.first == "__name")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fp << "\t" << kv.first << " = ";
|
|
|
|
FormatValue(fp, kv.second);
|
|
|
|
fp << "\n";
|
|
|
|
}
|
2014-10-22 15:29:54 +02:00
|
|
|
fp << "}\n\n";
|
2014-10-21 21:33:21 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::FormatValue(std::ostream& fp, const Value& val)
|
2014-10-21 21:33:21 +02:00
|
|
|
{
|
|
|
|
if (val.IsObjectType<Array>()) {
|
|
|
|
FormatArray(fp, val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val.IsString()) {
|
|
|
|
fp << "\"" << Convert::ToString(val) << "\"";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp << Convert::ToString(val);
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::FormatArray(std::ostream& fp, const Array::Ptr& arr)
|
2014-10-21 21:33:21 +02:00
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
fp << "[ ";
|
|
|
|
|
|
|
|
if (arr) {
|
|
|
|
ObjectLock olock(arr);
|
|
|
|
BOOST_FOREACH(const Value& value, arr) {
|
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
fp << ", ";
|
|
|
|
|
|
|
|
FormatValue(fp, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
fp << " ";
|
|
|
|
|
|
|
|
fp << "]";
|
|
|
|
}
|
2014-10-23 15:05:12 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeUtility::UpdateConstant(const String& name, const String& value)
|
2014-10-23 15:05:12 +02:00
|
|
|
{
|
|
|
|
String constantsFile = Application::GetSysconfDir() + "/icinga2/constants.conf";
|
|
|
|
String tempFile = constantsFile + ".tmp";
|
|
|
|
|
|
|
|
std::ifstream ifp(constantsFile.CStr());
|
|
|
|
std::ofstream ofp(tempFile.CStr());
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
2014-10-29 13:53:59 +01:00
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Updating constants file '" << constantsFile << "'.";
|
|
|
|
|
2014-10-23 15:05:12 +02:00
|
|
|
std::string line;
|
|
|
|
while (std::getline(ifp, line)) {
|
|
|
|
if (line.find("const " + name + " = ") != std::string::npos) {
|
|
|
|
ofp << "const " + name + " = \"" + value + "\"\n";
|
|
|
|
found = true;
|
|
|
|
} else
|
|
|
|
ofp << line << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
ofp << "const " + name + " = \"" + value + "\"\n";
|
|
|
|
|
|
|
|
ifp.close();
|
|
|
|
ofp.close();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
_unlink(constantsFile.CStr());
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
if (rename(tempFile.CStr(), constantsFile.CStr()) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("rename")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(constantsFile));
|
|
|
|
}
|
|
|
|
}
|