]> granicus.if.org Git - icinga2/blob - lib/icinga/timeperiod.cpp
b46caef37ee98870f36b9a18d28900546d92ff9f
[icinga2] / lib / icinga / timeperiod.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "i2-icinga.h"
21
22 using namespace icinga;
23
24 static AttributeDescription timePeriodAttributes[] = {
25         { "cached_state", Attribute_Transient },
26         { "cached_next_change", Attribute_Transient }
27 };
28
29 REGISTER_TYPE(TimePeriod, timePeriodAttributes);
30
31 String TimePeriod::GetAlias(void) const
32 {
33         String value = Get("alias");
34
35         if (!value.IsEmpty())
36                 return value;
37         else
38                 return GetName();
39 }
40
41 bool TimePeriod::Exists(const String& name)
42 {
43         return (DynamicObject::GetObject("TimePeriod", name));
44 }
45
46 TimePeriod::Ptr TimePeriod::GetByName(const String& name)
47 {
48         DynamicObject::Ptr configObject = DynamicObject::GetObject("TimePeriod", name);
49
50         if (!configObject)
51                 throw_exception(invalid_argument("TimePeriod '" + name + "' does not exist."));
52
53         return dynamic_pointer_cast<TimePeriod>(configObject);
54 }
55
56 bool TimePeriod::IsActive(void) {
57         if (GetNextChange() > Utility::GetTime()) {
58                 vector<Value> args;
59                 args.push_back(static_cast<TimePeriod::Ptr>(GetSelf()));
60                 return InvokeMethodSync<bool>("is_active", args);
61         } else {
62                 return Get("cached_state");
63         }
64 }
65
66 double TimePeriod::GetNextChange(void) {
67         double next_change = Get("cached_next_change");
68
69         if (next_change < Utility::GetTime()) {
70                 vector<Value> args;
71                 args.push_back(static_cast<TimePeriod::Ptr>(GetSelf()));
72                 next_change = InvokeMethodSync<bool>("is_active", args);
73                 // TODO: figure out next_change by calling method
74
75                 Set("cached_next_change", next_change);
76         }
77
78         return next_change;
79 }