mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-23 21:55:03 +02:00
parent
57de273483
commit
f5a209421d
@ -179,7 +179,8 @@ int RepositoryObjectCommand::Run(const boost::program_options::variables_map& vm
|
|||||||
RepositoryUtility::AddObject(name, m_Type, attrs);
|
RepositoryUtility::AddObject(name, m_Type, attrs);
|
||||||
}
|
}
|
||||||
else if (m_Command == RepositoryCommandRemove) {
|
else if (m_Command == RepositoryCommandRemove) {
|
||||||
RepositoryUtility::RemoveObject(name, m_Type);
|
/* pass attrs for service->host_name requirement */
|
||||||
|
RepositoryUtility::RemoveObject(name, m_Type, attrs);
|
||||||
}
|
}
|
||||||
else if (m_Command == RepositoryCommandSet) {
|
else if (m_Command == RepositoryCommandSet) {
|
||||||
Log(LogWarning, "cli")
|
Log(LogWarning, "cli")
|
||||||
|
@ -56,38 +56,48 @@ Dictionary::Ptr RepositoryUtility::GetArgumentAttributes(const std::vector<std::
|
|||||||
return attr;
|
return attr;
|
||||||
}
|
}
|
||||||
|
|
||||||
String RepositoryUtility::GetRepositoryDPath(void)
|
String RepositoryUtility::GetRepositoryConfigPath(void)
|
||||||
{
|
{
|
||||||
return Application::GetSysconfDir() + "/icinga2/repository.d";
|
return Application::GetSysconfDir() + "/icinga2/repository.d";
|
||||||
}
|
}
|
||||||
|
|
||||||
String RepositoryUtility::GetRepositoryDObjectsPath(const String& type, const String& hostname)
|
String RepositoryUtility::GetRepositoryObjectConfigPath(const String& type, const Dictionary::Ptr& object)
|
||||||
{
|
{
|
||||||
//TODO find a better way to retrieve the objects path
|
String path = GetRepositoryConfigPath() + "/";
|
||||||
if (type == "Host")
|
|
||||||
return GetRepositoryDPath() + "/hosts";
|
if (type == "Host") {
|
||||||
|
path += "hosts";
|
||||||
|
}
|
||||||
else if (type == "Service")
|
else if (type == "Service")
|
||||||
return GetRepositoryDPath() + "/hosts/" + hostname;
|
path += "hosts/" + object->Get("host_name");
|
||||||
else if (type == "Zone")
|
else if (type == "Zone")
|
||||||
return GetRepositoryDPath() + "/zones";
|
path += "zones";
|
||||||
else if (type == "Endpoints")
|
else if (type == "Endpoints")
|
||||||
return GetRepositoryDPath() + "/endpoints";
|
path += "endpoints";
|
||||||
else
|
|
||||||
return GetRepositoryDPath();
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
String RepositoryUtility::GetRepositoryObjectConfigFilePath(const String& type, const Dictionary::Ptr& object)
|
||||||
|
{
|
||||||
|
String path = GetRepositoryObjectConfigPath(type, object);
|
||||||
|
|
||||||
|
path += "/" + object->Get("name") + ".conf";
|
||||||
|
|
||||||
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
String RepositoryUtility::GetRepositoryChangeLogPath(void)
|
String RepositoryUtility::GetRepositoryChangeLogPath(void)
|
||||||
{
|
{
|
||||||
return Application::GetLocalStateDir() + "/lib/icinga2/repository";
|
return Application::GetLocalStateDir() + "/lib/icinga2/repository/changes";
|
||||||
}
|
}
|
||||||
|
|
||||||
void RepositoryUtility::PrintObjects(std::ostream& fp, const String& type)
|
void RepositoryUtility::PrintObjects(std::ostream& fp, const String& type)
|
||||||
{
|
{
|
||||||
std::vector<String> objects;
|
std::vector<String> objects = GetObjects(); //full path
|
||||||
GetObjects(type, objects);
|
|
||||||
|
|
||||||
BOOST_FOREACH(const String& object, objects) {
|
BOOST_FOREACH(const String& object, objects) {
|
||||||
Dictionary::Ptr obj = GetObjectFromRepository(GetRepositoryDObjectsPath(type) + "/" + object + ".conf");
|
Dictionary::Ptr obj = GetObjectFromRepository(object);
|
||||||
|
|
||||||
if (obj) {
|
if (obj) {
|
||||||
fp << "Object Name: " << object << "\n";
|
fp << "Object Name: " << object << "\n";
|
||||||
@ -113,7 +123,7 @@ bool RepositoryUtility::AddObject(const String& name, const String& type, const
|
|||||||
return WriteObjectToRepositoryChangeLog(path, change);
|
return WriteObjectToRepositoryChangeLog(path, change);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RepositoryUtility::RemoveObject(const String& name, const String& type)
|
bool RepositoryUtility::RemoveObject(const String& name, const String& type, const Dictionary::Ptr& attr)
|
||||||
{
|
{
|
||||||
/* add a new changelog entry by timestamp */
|
/* add a new changelog entry by timestamp */
|
||||||
String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(static_cast<long>(Utility::GetTime())) + ".change";
|
String path = GetRepositoryChangeLogPath() + "/" + Convert::ToString(static_cast<long>(Utility::GetTime())) + ".change";
|
||||||
@ -124,6 +134,7 @@ bool RepositoryUtility::RemoveObject(const String& name, const String& type)
|
|||||||
change->Set("name", name);
|
change->Set("name", name);
|
||||||
change->Set("type", type);
|
change->Set("type", type);
|
||||||
change->Set("command", "remove");
|
change->Set("command", "remove");
|
||||||
|
change->Set("attr", attr); //required for service->host_name
|
||||||
|
|
||||||
return WriteObjectToRepositoryChangeLog(path, change);
|
return WriteObjectToRepositoryChangeLog(path, change);
|
||||||
}
|
}
|
||||||
@ -159,14 +170,14 @@ bool RepositoryUtility::SetObjectAttribute(const String& name, const String& typ
|
|||||||
/* internal implementation when changes are committed */
|
/* internal implementation when changes are committed */
|
||||||
bool RepositoryUtility::AddObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr)
|
bool RepositoryUtility::AddObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr)
|
||||||
{
|
{
|
||||||
String path = GetRepositoryDObjectsPath(type, name) + "/" + name + ".conf";
|
String path = GetRepositoryObjectConfigPath(type, attr) + "/" + name + ".conf";
|
||||||
|
|
||||||
return WriteObjectToRepository(path, name, type, attr);
|
return WriteObjectToRepository(path, name, type, attr);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RepositoryUtility::RemoveObjectInternal(const String& name, const String& type)
|
bool RepositoryUtility::RemoveObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr)
|
||||||
{
|
{
|
||||||
String path = GetRepositoryDObjectsPath(type, name) + "/" + name + ".conf";
|
String path = GetRepositoryObjectConfigPath(type, attr) + "/" + name + ".conf";
|
||||||
|
|
||||||
return RemoveObjectFileInternal(path);
|
return RemoveObjectFileInternal(path);
|
||||||
}
|
}
|
||||||
@ -187,10 +198,10 @@ bool RepositoryUtility::RemoveObjectFileInternal(const String& path)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RepositoryUtility::SetObjectAttributeInternal(const String& name, const String& type, const String& attr, const Value& val)
|
bool RepositoryUtility::SetObjectAttributeInternal(const String& name, const String& type, const String& key, const Value& val, const Dictionary::Ptr& attr)
|
||||||
{
|
{
|
||||||
//Fixme
|
//Fixme
|
||||||
String path = GetRepositoryDObjectsPath(type, name) + "/" + name + ".conf";
|
String path = GetRepositoryObjectConfigPath(type, attr) + "/" + name + ".conf";
|
||||||
|
|
||||||
Dictionary::Ptr obj = GetObjectFromRepository(path);
|
Dictionary::Ptr obj = GetObjectFromRepository(path);
|
||||||
|
|
||||||
@ -200,7 +211,7 @@ bool RepositoryUtility::SetObjectAttributeInternal(const String& name, const Str
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
obj->Set(attr, val);
|
obj->Set(key, val);
|
||||||
|
|
||||||
std::cout << "Writing object '" << name << "' to path '" << path << "'.\n";
|
std::cout << "Writing object '" << name << "' to path '" << path << "'.\n";
|
||||||
|
|
||||||
@ -251,7 +262,7 @@ bool RepositoryUtility::WriteObjectToRepositoryChangeLog(const String& path, con
|
|||||||
{
|
{
|
||||||
Log(LogInformation, "cli", "Dumping changelog items to file '" + path + "'");
|
Log(LogInformation, "cli", "Dumping changelog items to file '" + path + "'");
|
||||||
|
|
||||||
Utility::MkDirP(Utility::DirName(path), 0755);
|
Utility::MkDirP(Utility::DirName(path), 0750);
|
||||||
|
|
||||||
String tempPath = path + ".tmp";
|
String tempPath = path + ".tmp";
|
||||||
|
|
||||||
@ -288,35 +299,26 @@ Dictionary::Ptr RepositoryUtility::GetObjectFromRepositoryChangeLog(const String
|
|||||||
return JsonDeserialize(content);
|
return JsonDeserialize(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* collect functions
|
* collect functions
|
||||||
*/
|
*/
|
||||||
bool RepositoryUtility::GetObjects(const String& type, std::vector<String>& objects)
|
std::vector<String> RepositoryUtility::GetObjects(void)
|
||||||
{
|
{
|
||||||
String path = GetRepositoryDPath() + "/";
|
std::vector<String> objects;
|
||||||
|
String path = GetRepositoryConfigPath() + "/";
|
||||||
|
|
||||||
if (type == "service")
|
Utility::Glob(path + "/*.conf",
|
||||||
path = "hosts/*";
|
boost::bind(&RepositoryUtility::CollectObjects, _1, boost::ref(objects)), GlobFile);
|
||||||
else
|
|
||||||
path = type;
|
|
||||||
|
|
||||||
if (!Utility::Glob(path + "/*.conf",
|
return objects;
|
||||||
boost::bind(&RepositoryUtility::CollectObjects, _1, boost::ref(objects)), GlobFile)) {
|
|
||||||
Log(LogCritical, "cli", "Cannot access path '" + path + "'.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RepositoryUtility::CollectObjects(const String& object_file, std::vector<String>& objects)
|
void RepositoryUtility::CollectObjects(const String& object_file, std::vector<String>& objects)
|
||||||
{
|
{
|
||||||
String object = Utility::BaseName(object_file);
|
Log(LogDebug, "cli")
|
||||||
boost::algorithm::replace_all(object, ".conf", "");
|
<< "Adding object: '" << object_file << "'.";
|
||||||
|
|
||||||
Log(LogDebug, "cli", "Adding object: " + object);
|
objects.push_back(object_file);
|
||||||
objects.push_back(object);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -325,11 +327,8 @@ bool RepositoryUtility::GetChangeLog(const boost::function<void (const Dictionar
|
|||||||
std::vector<String> changelog;
|
std::vector<String> changelog;
|
||||||
String path = GetRepositoryChangeLogPath() + "/";
|
String path = GetRepositoryChangeLogPath() + "/";
|
||||||
|
|
||||||
if (!Utility::Glob(path + "/*.change",
|
Utility::Glob(path + "/*.change",
|
||||||
boost::bind(&RepositoryUtility::CollectChangeLog, _1, boost::ref(changelog)), GlobFile)) {
|
boost::bind(&RepositoryUtility::CollectChangeLog, _1, boost::ref(changelog)), GlobFile);
|
||||||
Log(LogCritical, "cli", "Cannot access path '" + path + "'.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* sort by timestamp ascending */
|
/* sort by timestamp ascending */
|
||||||
std::sort(changelog.begin(), changelog.end());
|
std::sort(changelog.begin(), changelog.end());
|
||||||
@ -374,7 +373,7 @@ void RepositoryUtility::CommitChange(const Dictionary::Ptr& change)
|
|||||||
AddObjectInternal(name, type, attr);
|
AddObjectInternal(name, type, attr);
|
||||||
}
|
}
|
||||||
else if (command == "remove") {
|
else if (command == "remove") {
|
||||||
RemoveObjectInternal(name, type);
|
RemoveObjectInternal(name, type, attr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -407,7 +406,7 @@ void RepositoryUtility::SerializeObject(std::ostream& fp, const String& name, co
|
|||||||
}
|
}
|
||||||
|
|
||||||
BOOST_FOREACH(const Dictionary::Pair& kv, object) {
|
BOOST_FOREACH(const Dictionary::Pair& kv, object) {
|
||||||
if (kv.first == "import") {
|
if (kv.first == "import" || kv.first == "name") {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
fp << "\t" << kv.first << " = ";
|
fp << "\t" << kv.first << " = ";
|
||||||
|
@ -37,8 +37,11 @@ class RepositoryUtility
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static Dictionary::Ptr GetArgumentAttributes(const std::vector<std::string>& arguments);
|
static Dictionary::Ptr GetArgumentAttributes(const std::vector<std::string>& arguments);
|
||||||
static String GetRepositoryDPath(void);
|
|
||||||
static String GetRepositoryDObjectsPath(const String& type, const String& hostname = Empty);
|
static String GetRepositoryConfigPath(void);
|
||||||
|
static String GetRepositoryObjectConfigPath(const String& type, const Dictionary::Ptr& object);
|
||||||
|
static String GetRepositoryObjectConfigFilePath(const String& type, const Dictionary::Ptr& object);
|
||||||
|
|
||||||
static String GetRepositoryChangeLogPath(void);
|
static String GetRepositoryChangeLogPath(void);
|
||||||
|
|
||||||
static void PrintObjects(std::ostream& fp, const String& type);
|
static void PrintObjects(std::ostream& fp, const String& type);
|
||||||
@ -46,21 +49,22 @@ public:
|
|||||||
static void PrintChangeLog(std::ostream& fp);
|
static void PrintChangeLog(std::ostream& fp);
|
||||||
|
|
||||||
static bool AddObject(const String& name, const String& type, const Dictionary::Ptr& attr);
|
static bool AddObject(const String& name, const String& type, const Dictionary::Ptr& attr);
|
||||||
static bool RemoveObject(const String& name, const String& type);
|
static bool RemoveObject(const String& name, const String& type, const Dictionary::Ptr& attr);
|
||||||
|
|
||||||
static bool SetObjectAttribute(const String& name, const String& type, const String& attr, const Value& val);
|
static bool SetObjectAttribute(const String& name, const String& type, const String& attr, const Value& val);
|
||||||
|
|
||||||
static bool CommitChangeLog(void);
|
static bool CommitChangeLog(void);
|
||||||
|
|
||||||
static bool GetObjects(const String& type, std::vector<String>& objects);
|
static std::vector<String> GetObjects(void);
|
||||||
private:
|
private:
|
||||||
RepositoryUtility(void);
|
RepositoryUtility(void);
|
||||||
|
|
||||||
static bool RemoveObjectFileInternal(const String& path);
|
static bool RemoveObjectFileInternal(const String& path);
|
||||||
|
|
||||||
static bool AddObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr);
|
static bool AddObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr);
|
||||||
static bool RemoveObjectInternal(const String& name, const String& type);
|
static bool RemoveObjectInternal(const String& name, const String& type, const Dictionary::Ptr& attr);
|
||||||
static bool SetObjectAttributeInternal(const String& name, const String& type, const String& attr, const Value& val);
|
static bool SetObjectAttributeInternal(const String& name, const String& type, const String& key,
|
||||||
|
const Value& val, const Dictionary::Ptr& attr);
|
||||||
|
|
||||||
/* repository.d */
|
/* repository.d */
|
||||||
static void CollectObjects(const String& object_file, std::vector<String>& objects);
|
static void CollectObjects(const String& object_file, std::vector<String>& objects);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user