From: Michael Friedrich Date: Wed, 2 Oct 2013 19:09:13 +0000 (+0200) Subject: compat: Add more host attributes, refactor contacts getter. X-Git-Tag: v0.0.3~283 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bfa8cf789b9345c2e0dcb2741f4e03ef71b24896;p=icinga2 compat: Add more host attributes, refactor contacts getter. --- diff --git a/components/compat/statusdatawriter.cpp b/components/compat/statusdatawriter.cpp index 56a98506c..8f3f6803e 100644 --- a/components/compat/statusdatawriter.cpp +++ b/components/compat/statusdatawriter.cpp @@ -270,8 +270,16 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) { fp << "define host {" << "\n" << "\t" << "host_name" << "\t" << host->GetName() << "\n" + << "\t" << "display_name" << "\t" << host->GetDisplayName() << "\n" << "\t" << "alias" << "\t" << host->GetDisplayName() << "\n"; + Dictionary::Ptr macros = host->GetMacros(); + + if (macros) { + fp << "\t" << "address" << "\t" << macros->Get("address") << "\n" + << "\t" << "address6" << "\t" << macros->Get("address6") << "\n"; + } + std::set parents = host->GetParentHosts(); if (!parents.empty()) { @@ -298,8 +306,31 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) fp << "\t" << "check_command" << "\t" << "check_" << checkcommand->GetName() << "\n"; EventCommand::Ptr eventcommand = hc->GetEventCommand(); - if (eventcommand) - fp << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n"; + if (eventcommand) { + fp << "\t" << "event_handler_enabled" << "\t" << 1 << "\n" + << "\t" << "event_handler" << "\t" << "event_" << eventcommand->GetName() << "\n"; + } else { + fp << "\t" << "event_handler_enabled" << "\t" << 0 << "\n"; + } + + TimePeriod::Ptr check_period = hc->GetCheckPeriod(); + if (check_period) + fp << "\t" << "check_period" << "\t" << check_period->GetName() << "\n"; + + fp << "\t" << "contacts" << "\t"; + DumpNameList(fp, CompatUtility::GetServiceNotificationUsers(hc)); + fp << "\n"; + + fp << "\t" << "contact_groups" << "\t"; + DumpNameList(fp, CompatUtility::GetServiceNotificationUserGroups(hc)); + fp << "\n"; + + fp << "\t" << "initial_state" << "\t" << "o" << "\n" + << "\t" << "low_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n" + << "\t" << "high_flap_threshold" << "\t" << hc->GetFlappingThreshold() << "\n" + << "\t" << "process_perf_data" << "\t" << 1 << "\n" + << "\t" << "check_freshness" << "\t" << 1 << "\n"; + } else { fp << "\t" << "check_interval" << "\t" << 60 << "\n" << "\t" << "retry_interval" << "\t" << 60 << "\n" @@ -310,6 +341,15 @@ void StatusDataWriter::DumpHostObject(std::ostream& fp, const Host::Ptr& host) } + /* TODO FIXME + Array::Ptr groups = host->GetGroups(); + if (groups) { + fp << "\t" << "hostgroups" << "\t"; + DumpNameArray(fp, host->GetGroups()); + fp << "\n"; + } + */ + DumpCustomAttributes(fp, host); fp << "\t" << "}" << "\n" @@ -685,6 +725,22 @@ void StatusDataWriter::StatusTimerHandler(void) } } +/* +void StatusDataWriter::DumpNameArray(std::ostream& fp, const Array::Ptr& array) +{ + bool first = true; + ObjectLock olock(array); + BOOST_FOREACH(const Array::Ptr& obj, array) { + if (!first) + fp << ","; + else + first = false; + + fp << obj->GetName(); + } +} +*/ + void StatusDataWriter::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const { DynamicObject::InternalSerialize(bag, attributeTypes); diff --git a/components/compat/statusdatawriter.h b/components/compat/statusdatawriter.h index 2f63b12eb..697c4f88c 100644 --- a/components/compat/statusdatawriter.h +++ b/components/compat/statusdatawriter.h @@ -97,6 +97,8 @@ private: } } + void DumpNameArray(std::ostream& fp, const Array::Ptr& array); + void DumpServiceStatus(std::ostream& fp, const Service::Ptr& service); void DumpServiceObject(std::ostream& fp, const Service::Ptr& service); diff --git a/components/livestatus/hoststable.cpp b/components/livestatus/hoststable.cpp index cefd68923..85b3c56af 100644 --- a/components/livestatus/hoststable.cpp +++ b/components/livestatus/hoststable.cpp @@ -1088,11 +1088,9 @@ Value HostsTable::ContactsAccessor(const Value& row) if (!hc) return Empty; - Array::Ptr contacts = CompatUtility::GetServiceNotificationUsers(hc); Array::Ptr contact_names = boost::make_shared(); - ObjectLock olock(contacts); - BOOST_FOREACH(const User::Ptr& user, contacts) { + BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(hc)) { contact_names->Add(user->GetName()); } @@ -1589,12 +1587,9 @@ Value HostsTable::ContactGroupsAccessor(const Value& row) if (!hc) return Empty; - Array::Ptr contactgroups = CompatUtility::GetServiceNotificationUserGroups(hc); - Array::Ptr contactgroup_names = boost::make_shared(); - ObjectLock olock(contactgroups); - BOOST_FOREACH(const UserGroup::Ptr& usergroup, contactgroups) { + BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(hc)) { contactgroup_names->Add(usergroup->GetName()); } diff --git a/components/livestatus/servicestable.cpp b/components/livestatus/servicestable.cpp index 396b4b1e7..ec1dba88c 100644 --- a/components/livestatus/servicestable.cpp +++ b/components/livestatus/servicestable.cpp @@ -751,11 +751,9 @@ Value ServicesTable::InNotificationPeriodAccessor(const Value& row) Value ServicesTable::ContactsAccessor(const Value& row) { - Array::Ptr contacts = CompatUtility::GetServiceNotificationUsers(static_cast(row)); Array::Ptr contact_names = boost::make_shared(); - ObjectLock olock(contacts); - BOOST_FOREACH(const User::Ptr& user, contacts) { + BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(static_cast(row))) { contact_names->Add(user->GetName()); } @@ -1010,12 +1008,9 @@ Value ServicesTable::GroupsAccessor(const Value& row) Value ServicesTable::ContactGroupsAccessor(const Value& row) { - Array::Ptr contactgroups = CompatUtility::GetServiceNotificationUserGroups(static_cast(row)); - Array::Ptr contactgroup_names = boost::make_shared(); - ObjectLock olock(contactgroups); - BOOST_FOREACH(const UserGroup::Ptr& usergroup, contactgroups) { + BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(static_cast(row))) { contactgroup_names->Add(usergroup->GetName()); } diff --git a/lib/db_ido/hostdbobject.cpp b/lib/db_ido/hostdbobject.cpp index 8ad6fa993..d788ac4cd 100644 --- a/lib/db_ido/hostdbobject.cpp +++ b/lib/db_ido/hostdbobject.cpp @@ -240,45 +240,38 @@ void HostDbObject::OnConfigUpdate(void) Service::Ptr service = host->GetCheckService(); if (service) { - Array::Ptr contacts = CompatUtility::GetServiceNotificationUsers(service); - Log(LogDebug, "db_ido", "host contacts: " + host->GetName() + " length: " + Convert::ToString(contacts->GetLength())); - - { - ObjectLock olock(contacts); - BOOST_FOREACH(const User::Ptr& user, contacts) { - Log(LogDebug, "db_ido", "host contacts: " + user->GetName()); - - Dictionary::Ptr fields_contact = boost::make_shared(); - fields_contact->Set("host_id", DbValue::FromObjectInsertID(host)); - fields_contact->Set("contact_object_id", user); - fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ - - DbQuery query_contact; - query_contact.Table = GetType()->GetTable() + "_contacts"; - query_contact.Type = DbQueryInsert; - query_contact.Fields = fields_contact; - OnQuery(query_contact); - } + Log(LogDebug, "db_ido", "host contacts: " + host->GetName()); + + BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) { + Log(LogDebug, "db_ido", "host contacts: " + user->GetName()); + + Dictionary::Ptr fields_contact = boost::make_shared(); + fields_contact->Set("host_id", DbValue::FromObjectInsertID(host)); + fields_contact->Set("contact_object_id", user); + fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ + + DbQuery query_contact; + query_contact.Table = GetType()->GetTable() + "_contacts"; + query_contact.Type = DbQueryInsert; + query_contact.Fields = fields_contact; + OnQuery(query_contact); } - Array::Ptr contactgroups = CompatUtility::GetServiceNotificationUserGroups(service); - Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName() + " length: " + Convert::ToString(contactgroups->GetLength())); - { - ObjectLock olock(contactgroups); - BOOST_FOREACH(const UserGroup::Ptr& usergroup, contactgroups) { - Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName()); - - Dictionary::Ptr fields_contact = boost::make_shared(); - fields_contact->Set("host_id", DbValue::FromObjectInsertID(host)); - fields_contact->Set("contactgroup_object_id", usergroup); - fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ - - DbQuery query_contact; - query_contact.Table = GetType()->GetTable() + "_contactgroups"; - query_contact.Type = DbQueryInsert; - query_contact.Fields = fields_contact; - OnQuery(query_contact); - } + Log(LogDebug, "db_ido", "host contactgroups: " + host->GetName()); + + BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) { + Log(LogDebug, "db_ido", "host contactgroups: " + usergroup->GetName()); + + Dictionary::Ptr fields_contact = boost::make_shared(); + fields_contact->Set("host_id", DbValue::FromObjectInsertID(host)); + fields_contact->Set("contactgroup_object_id", usergroup); + fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ + + DbQuery query_contact; + query_contact.Table = GetType()->GetTable() + "_contactgroups"; + query_contact.Type = DbQueryInsert; + query_contact.Fields = fields_contact; + OnQuery(query_contact); } } diff --git a/lib/db_ido/servicedbobject.cpp b/lib/db_ido/servicedbobject.cpp index c997df1b6..e1f523926 100644 --- a/lib/db_ido/servicedbobject.cpp +++ b/lib/db_ido/servicedbobject.cpp @@ -237,45 +237,38 @@ void ServiceDbObject::OnConfigUpdate(void) } /* service contacts, contactgroups */ - Array::Ptr contacts = CompatUtility::GetServiceNotificationUsers(service); - Log(LogDebug, "db_ido", "service contacts: " + service->GetName() + " length: " + Convert::ToString(contacts->GetLength())); + Log(LogDebug, "db_ido", "service contacts: " + service->GetName()); - { - ObjectLock olock(contacts); - BOOST_FOREACH(const User::Ptr& user, contacts) { - Log(LogDebug, "db_ido", "service contacts: " + user->GetName()); - - Dictionary::Ptr fields_contact = boost::make_shared(); - fields_contact->Set("service_id", DbValue::FromObjectInsertID(service)); - fields_contact->Set("contact_object_id", user); - fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ - - DbQuery query_contact; - query_contact.Table = GetType()->GetTable() + "_contacts"; - query_contact.Type = DbQueryInsert; - query_contact.Fields = fields_contact; - OnQuery(query_contact); - } + BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(service)) { + Log(LogDebug, "db_ido", "service contacts: " + user->GetName()); + + Dictionary::Ptr fields_contact = boost::make_shared(); + fields_contact->Set("service_id", DbValue::FromObjectInsertID(service)); + fields_contact->Set("contact_object_id", user); + fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ + + DbQuery query_contact; + query_contact.Table = GetType()->GetTable() + "_contacts"; + query_contact.Type = DbQueryInsert; + query_contact.Fields = fields_contact; + OnQuery(query_contact); } - Array::Ptr contactgroups = CompatUtility::GetServiceNotificationUserGroups(service); - Log(LogDebug, "db_ido", "service contactgroups: " + service->GetName() + " length: " + Convert::ToString(contactgroups->GetLength())); - { - ObjectLock olock(contactgroups); - BOOST_FOREACH(const UserGroup::Ptr& usergroup, contactgroups) { - Log(LogDebug, "db_ido", "service contactgroups: " + usergroup->GetName()); - - Dictionary::Ptr fields_contact = boost::make_shared(); - fields_contact->Set("service_id", DbValue::FromObjectInsertID(service)); - fields_contact->Set("contactgroup_object_id", usergroup); - fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ - - DbQuery query_contact; - query_contact.Table = GetType()->GetTable() + "_contactgroups"; - query_contact.Type = DbQueryInsert; - query_contact.Fields = fields_contact; - OnQuery(query_contact); - } + Log(LogDebug, "db_ido", "service contactgroups: " + service->GetName()); + + BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(service)) { + Log(LogDebug, "db_ido", "service contactgroups: " + usergroup->GetName()); + + Dictionary::Ptr fields_contact = boost::make_shared(); + fields_contact->Set("service_id", DbValue::FromObjectInsertID(service)); + fields_contact->Set("contactgroup_object_id", usergroup); + fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ + + DbQuery query_contact; + query_contact.Table = GetType()->GetTable() + "_contactgroups"; + query_contact.Type = DbQueryInsert; + query_contact.Fields = fields_contact; + OnQuery(query_contact); } /* custom variables */ diff --git a/lib/icinga/compatutility.cpp b/lib/icinga/compatutility.cpp index 9fa6b96d8..5cd77c2f7 100644 --- a/lib/icinga/compatutility.cpp +++ b/lib/icinga/compatutility.cpp @@ -483,12 +483,11 @@ Dictionary::Ptr CompatUtility::GetCustomVariableConfig(const DynamicObject::Ptr& return customvars; } -Value CompatUtility::GetServiceNotificationUsers(const Service::Ptr& service) +std::set CompatUtility::GetServiceNotificationUsers(const Service::Ptr& service) { /* Service -> Notifications -> (Users + UserGroups -> Users) */ std::set allUsers; std::set users; - Array::Ptr contacts = boost::make_shared(); BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { ObjectLock olock(notification); @@ -503,27 +502,22 @@ Value CompatUtility::GetServiceNotificationUsers(const Service::Ptr& service) } } - BOOST_FOREACH(const User::Ptr& user, allUsers) { - contacts->Add(user); - } - - return contacts; + return allUsers; } -Value CompatUtility::GetServiceNotificationUserGroups(const Service::Ptr& service) +std::set CompatUtility::GetServiceNotificationUserGroups(const Service::Ptr& service) { + std::set usergroups; /* Service -> Notifications -> UserGroups */ - Array::Ptr contactgroups = boost::make_shared(); - BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) { ObjectLock olock(notification); BOOST_FOREACH(const UserGroup::Ptr& ug, notification->GetUserGroups()) { - contactgroups->Add(ug); + usergroups.insert(ug); } } - return contactgroups; + return usergroups; } Dictionary::Ptr CompatUtility::GetCheckResultOutput(const Dictionary::Ptr& cr) diff --git a/lib/icinga/compatutility.h b/lib/icinga/compatutility.h index 0635ceff1..1875dfcc5 100644 --- a/lib/icinga/compatutility.h +++ b/lib/icinga/compatutility.h @@ -56,8 +56,8 @@ public: static Dictionary::Ptr GetCustomVariableConfig(const DynamicObject::Ptr& object); - static Value GetServiceNotificationUsers(const Service::Ptr& service); - static Value GetServiceNotificationUserGroups(const Service::Ptr& service); + static std::set GetServiceNotificationUsers(const Service::Ptr& service); + static std::set GetServiceNotificationUserGroups(const Service::Ptr& service); static Dictionary::Ptr GetCheckResultOutput(const Dictionary::Ptr& cr); static String GetCheckResultPerfdata(const Dictionary::Ptr& cr);