]> granicus.if.org Git - icinga2/commitdiff
ido: Add notifications.
authorMichael Friedrich <michael.friedrich@netways.de>
Tue, 24 Sep 2013 16:50:42 +0000 (18:50 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Tue, 24 Sep 2013 16:50:42 +0000 (18:50 +0200)
refs #4379

lib/ido/servicedbobject.cpp
lib/ido/servicedbobject.h

index 6adc1dc68a109ba2d56bbf59d8e8f994e8748eac..0f59745fa638437a0c3b631645ed23dd9be95109 100644 (file)
@@ -49,7 +49,7 @@ void ServiceDbObject::StaticInitialize(void)
 
        /* History */
        Service::OnAcknowledgementSet.connect(boost::bind(&ServiceDbObject::AddAcknowledgement, _1, _2, _3, _4, _5, _6));
-
+       Service::OnNotificationSentToAllUsers.connect(bind(&ServiceDbObject::AddNotification, _1, _2, _3, _4, _5, _6, _7));
 }
 
 ServiceDbObject::ServiceDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
@@ -261,7 +261,7 @@ void ServiceDbObject::OnConfigUpdate(void)
        RemoveDowntimes(service);
        AddComments(service);
        AddDowntimes(service);
+
        /* service host config update */
        Host::Ptr host = service->GetHost();
 
@@ -785,4 +785,81 @@ void ServiceDbObject::AddAcknowledgement(const Service::Ptr& service, const Stri
        }
 }
 
+void ServiceDbObject::AddNotification(const Service::Ptr& service, const std::set<User::Ptr>& users, NotificationType type,
+                                     const Dictionary::Ptr& cr, const String& author, const String& text, unsigned long notified_users)
+{
+       Host::Ptr host = service->GetHost();
+
+       if (!host)
+               return;
+
+       Log(LogDebug, "ido", "add notification for '" + service->GetName() + "'");
+
+       /* start and end happen at the same time */
+       double now = Utility::GetTime();
+       unsigned long start_time = static_cast<long>(now);
+       unsigned long end_time = start_time;
+       unsigned long start_time_usec = (now - start_time) * 1000 * 1000;
+       unsigned long end_time_usec = start_time_usec;
+
+       DbQuery query1;
+       query1.Table = "notifications";
+       query1.Type = DbQueryInsert;
+
+       Dictionary::Ptr fields1 = boost::make_shared<Dictionary>();
+       fields1->Set("notification_type", 1); /* service */
+       fields1->Set("notification_reason", CompatUtility::MapNotificationReasonType(type));
+       fields1->Set("object_id", service);
+       fields1->Set("start_time", DbValue::FromTimestamp(start_time));
+       fields1->Set("start_time_usec", start_time_usec);
+       fields1->Set("end_time", DbValue::FromTimestamp(end_time));
+       fields1->Set("end_time_usec", end_time_usec);
+       fields1->Set("state", service->GetState());
+
+       if (cr) {
+               Dictionary::Ptr output_bag = CompatUtility::GetCheckResultOutput(cr);
+               fields1->Set("output", output_bag->Get("output"));
+               fields1->Set("long_output", output_bag->Get("long_output"));
+       }
+
+       fields1->Set("escalated", 0);
+       fields1->Set("contacts_notified", notified_users);
+       fields1->Set("instance_id", 0); /* DbConnection class fills in real ID */
+
+       query1.Fields = fields1;
+       OnQuery(query1);
+
+       if (host->GetHostCheckService() == service) {
+       DbQuery query1;
+       query1.Table = "notifications";
+       query1.Type = DbQueryInsert;
 
+       Dictionary::Ptr fields1 = boost::make_shared<Dictionary>();
+               DbQuery query2;
+               query2.Table = "notifications";
+               query2.Type = DbQueryInsert;
+
+               Dictionary::Ptr fields2 = boost::make_shared<Dictionary>();
+               fields2->Set("notification_type", 2); /* host */
+               fields2->Set("notification_reason", CompatUtility::MapNotificationReasonType(type));
+               fields2->Set("object_id", host);
+               fields2->Set("start_time", DbValue::FromTimestamp(start_time));
+               fields2->Set("start_time_usec", start_time_usec);
+               fields2->Set("end_time", DbValue::FromTimestamp(end_time));
+               fields2->Set("end_time_usec", end_time_usec);
+               fields2->Set("state", service->GetState());
+
+               if (cr) {
+                       Dictionary::Ptr output_bag = CompatUtility::GetCheckResultOutput(cr);
+                       fields2->Set("output", output_bag->Get("output"));
+                       fields2->Set("long_output", output_bag->Get("long_output"));
+               }
+
+               fields2->Set("escalated", 0);
+               fields2->Set("contacts_notified", notified_users);
+               fields2->Set("instance_id", 0); /* DbConnection class fills in real ID */
+
+               query2.Fields = fields2;
+               OnQuery(query2);
+       }
+}
index d3f63bd382fd46b2355b30eb62845234302a7293..8e91f93cbb13f56555402a687d7756e4a25a9e80 100644 (file)
@@ -67,6 +67,7 @@ private:
 
         /* History */
         static void AddAcknowledgement(const Service::Ptr& service, const String& author, const String& comment, AcknowledgementType type, double expiry, const String& authority);
+        static void AddNotification(const Service::Ptr& service, const std::set<User::Ptr>& users, NotificationType type, const Dictionary::Ptr& cr, const String& author, const String& text, unsigned long notified_users);
 
 };