]> granicus.if.org Git - icinga2/commitdiff
compat: Add more host attributes, refactor contacts getter.
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 2 Oct 2013 19:09:13 +0000 (21:09 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 2 Oct 2013 19:09:13 +0000 (21:09 +0200)
components/compat/statusdatawriter.cpp
components/compat/statusdatawriter.h
components/livestatus/hoststable.cpp
components/livestatus/servicestable.cpp
lib/db_ido/hostdbobject.cpp
lib/db_ido/servicedbobject.cpp
lib/icinga/compatutility.cpp
lib/icinga/compatutility.h

index 56a98506c036aa5d4801f3f0cc832c39e42e9cd9..8f3f6803e59f7144bbb46dd8fd6aca4953d0cf9f 100644 (file)
@@ -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<Host::Ptr> 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);
index 2f63b12eb557c291bce0671f0c25c1c77fa1ccf1..697c4f88c86ee61736ff53690a5fdd9787e29f81 100644 (file)
@@ -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);
 
index cefd6892355ea6c82200d523a6a24b2a7d880781..85b3c56af4c766bb6db3ac0d40089d390d7bc047 100644 (file)
@@ -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<Array>();
 
-       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<Array>();
 
-       ObjectLock olock(contactgroups);
-       BOOST_FOREACH(const UserGroup::Ptr& usergroup, contactgroups) {
+       BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(hc)) {
                contactgroup_names->Add(usergroup->GetName());
        }
 
index 396b4b1e7cb76aadfbf9382a6388bbc092e77423..ec1dba88c761cd86a93b6e84f2f09814b313f98e 100644 (file)
@@ -751,11 +751,9 @@ Value ServicesTable::InNotificationPeriodAccessor(const Value& row)
 
 Value ServicesTable::ContactsAccessor(const Value& row)
 {
-       Array::Ptr contacts = CompatUtility::GetServiceNotificationUsers(static_cast<Service::Ptr>(row));
        Array::Ptr contact_names = boost::make_shared<Array>();
 
-       ObjectLock olock(contacts);
-       BOOST_FOREACH(const User::Ptr& user, contacts) {
+       BOOST_FOREACH(const User::Ptr& user, CompatUtility::GetServiceNotificationUsers(static_cast<Service::Ptr>(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<Service::Ptr>(row));
-
        Array::Ptr contactgroup_names = boost::make_shared<Array>();
 
-       ObjectLock olock(contactgroups);
-       BOOST_FOREACH(const UserGroup::Ptr& usergroup, contactgroups) {
+       BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetServiceNotificationUserGroups(static_cast<Service::Ptr>(row))) {
                contactgroup_names->Add(usergroup->GetName());
        }
 
index 8ad6fa99323184bdac9088c1b287a86e29dfdfa9..d788ac4cdff29734e8f7baf751002bfd1415e30d 100644 (file)
@@ -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<Dictionary>();
-                               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<Dictionary>();
+                       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<Dictionary>();
-                               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<Dictionary>();
+                       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);
                }
        }
 
index c997df1b659710fd76487f2610e279552e53e836..e1f52392699d08293d095d17c9b059a4345c2a90 100644 (file)
@@ -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<Dictionary>();
-                       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<Dictionary>();
+               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<Dictionary>();
-                       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<Dictionary>();
+               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 */
index 9fa6b96d88e2dca081c3264504a70b4830176ac7..5cd77c2f7ee609a29439b067f753f8d477330af9 100644 (file)
@@ -483,12 +483,11 @@ Dictionary::Ptr CompatUtility::GetCustomVariableConfig(const DynamicObject::Ptr&
        return customvars;
 }
 
-Value CompatUtility::GetServiceNotificationUsers(const Service::Ptr& service)
+std::set<User::Ptr> CompatUtility::GetServiceNotificationUsers(const Service::Ptr& service)
 {
        /* Service -> Notifications -> (Users + UserGroups -> Users) */
        std::set<User::Ptr> allUsers;
        std::set<User::Ptr> users;
-       Array::Ptr contacts = boost::make_shared<Array>();
 
        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<UserGroup::Ptr> CompatUtility::GetServiceNotificationUserGroups(const Service::Ptr& service)
 {
+       std::set<UserGroup::Ptr> usergroups;
        /* Service -> Notifications -> UserGroups */
-       Array::Ptr contactgroups = boost::make_shared<Array>();
-
        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)
index 0635ceff13a52f7429921869df75f52d0411980d..1875dfcc504843a938823e1c2a6fa00e469b53e9 100644 (file)
@@ -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<User::Ptr> GetServiceNotificationUsers(const Service::Ptr& service);
+       static std::set<UserGroup::Ptr> GetServiceNotificationUserGroups(const Service::Ptr& service);
 
        static Dictionary::Ptr GetCheckResultOutput(const Dictionary::Ptr& cr);
        static String GetCheckResultPerfdata(const Dictionary::Ptr& cr);