-
Notifications
You must be signed in to change notification settings - Fork 570
/
dbobject.cpp
200 lines (157 loc) · 5.41 KB
/
dbobject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "db_ido/dbobject.h"
#include "db_ido/dbtype.h"
#include "db_ido/dbvalue.h"
#include "icinga/service.h"
#include "cluster/endpoint.h"
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/utility.h"
#include "base/initialize.h"
#include "base/logger_fwd.h"
#include <boost/foreach.hpp>
using namespace icinga;
boost::signals2::signal<void (const DbQuery&)> DbObject::OnQuery;
INITIALIZE_ONCE(&DbObject::StaticInitialize);
DbObject::DbObject(const shared_ptr<DbType>& type, const String& name1, const String& name2)
: m_Name1(name1), m_Name2(name2), m_Type(type), m_LastConfigUpdate(0), m_LastStatusUpdate(0)
{ }
void DbObject::StaticInitialize(void)
{
DynamicObject::OnStateChanged.connect(boost::bind(&DbObject::StateChangedHandler, _1, _2));
}
void DbObject::SetObject(const DynamicObject::Ptr& object)
{
m_Object = object;
}
DynamicObject::Ptr DbObject::GetObject(void) const
{
return m_Object;
}
String DbObject::GetName1(void) const
{
return m_Name1;
}
String DbObject::GetName2(void) const
{
return m_Name2;
}
DbType::Ptr DbObject::GetType(void) const
{
return m_Type;
}
void DbObject::SendConfigUpdate(void)
{
Dictionary::Ptr fields = GetConfigFields();
if (!fields)
return;
DbQuery query;
query.Table = GetType()->GetTable() + "s";
query.Type = DbQueryInsert | DbQueryUpdate;
query.Category = DbCatConfig;
query.Fields = fields;
query.Fields->Set(GetType()->GetIDColumn(), GetObject());
query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
query.Fields->Set("config_type", 1);
query.WhereCriteria = make_shared<Dictionary>();
query.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject());
query.Object = GetSelf();
query.ConfigUpdate = true;
OnQuery(query);
m_LastConfigUpdate = Utility::GetTime();
OnConfigUpdate();
}
void DbObject::SendStatusUpdate(const String& authority)
{
Dictionary::Ptr fields = GetStatusFields();
Endpoint::Ptr endpoint = Endpoint::GetByName(authority);
if (!fields)
return;
DbQuery query;
query.Table = GetType()->GetTable() + "status";
query.Type = DbQueryInsert | DbQueryUpdate;
query.Category = DbCatState;
query.Fields = fields;
query.Fields->Set(GetType()->GetIDColumn(), GetObject());
query.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
if (endpoint)
query.Fields->Set("endpoint_object_id", endpoint);
query.Fields->Set("status_update_time", DbValue::FromTimestamp(Utility::GetTime()));
query.WhereCriteria = make_shared<Dictionary>();
query.WhereCriteria->Set(GetType()->GetIDColumn(), GetObject());
query.Object = GetSelf();
query.StatusUpdate = true;
OnQuery(query);
m_LastStatusUpdate = Utility::GetTime();
OnStatusUpdate();
}
double DbObject::GetLastConfigUpdate(void) const
{
return m_LastConfigUpdate;
}
double DbObject::GetLastStatusUpdate(void) const
{
return m_LastStatusUpdate;
}
bool DbObject::IsStatusAttribute(const String&) const
{
return false;
}
void DbObject::OnConfigUpdate(void)
{
/* Default handler does nothing. */
}
void DbObject::OnStatusUpdate(void)
{
/* Default handler does nothing. */
}
DbObject::Ptr DbObject::GetOrCreateByObject(const DynamicObject::Ptr& object)
{
DbObject::Ptr dbobj = static_pointer_cast<DbObject>(object->GetExtension("DbObject"));
if (dbobj)
return dbobj;
DbType::Ptr dbtype = DbType::GetByName(object->GetType()->GetName());
if (!dbtype)
return DbObject::Ptr();
Service::Ptr service;
String name1, name2;
service = dynamic_pointer_cast<Service>(object);
if (service) {
Host::Ptr host = service->GetHost();
name1 = service->GetHost()->GetName();
name2 = service->GetShortName();
} else {
name1 = object->GetName();
}
dbobj = dbtype->GetOrCreateObjectByName(name1, name2);
{
ObjectLock olock(object);
dbobj->SetObject(object);
object->SetExtension("DbObject", dbobj);
}
return dbobj;
}
void DbObject::StateChangedHandler(const DynamicObject::Ptr& object, const String& authority)
{
DbObject::Ptr dbobj = GetOrCreateByObject(object);
if (!dbobj)
return;
dbobj->SendStatusUpdate(authority);
}