]> granicus.if.org Git - icinga2/blob - lib/redis/rediswriter-config.cpp
Merge branch 'support/2.6'
[icinga2] / lib / redis / rediswriter-config.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
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 "redis/rediswriter.hpp"
21 #include "icinga/customvarobject.hpp"
22 #include "base/json.hpp"
23 #include "base/logger.hpp"
24 #include "base/serializer.hpp"
25 #include "base/initialize.hpp"
26
27 using namespace icinga;
28
29 /*
30 - icinga:config:<type> as hash
31 key: sha1 checksum(name)
32 value: JsonEncode(Serialize(object, FAConfig)) + config_checksum
33
34 Diff between calculated config_checksum and Redis json config_checksum
35 Alternative: Replace into.
36
37
38 - icinga:status:<type> as hash
39 key: sha1 checksum(name)
40 value: JsonEncode(Serialize(object, FAState))
41 */
42
43 INITIALIZE_ONCE(&RedisWriter::ConfigStaticInitialize);
44
45 void RedisWriter::ConfigStaticInitialize(void)
46 {
47         /* triggered in ProcessCheckResult(), requires UpdateNextCheck() to be called before */
48         ConfigObject::OnStateChanged.connect(boost::bind(&RedisWriter::StateChangedHandler, _1));
49         CustomVarObject::OnVarsChanged.connect(boost::bind(&RedisWriter::VarsChangedHandler, _1));
50
51         /* triggered on create, update and delete objects */
52         ConfigObject::OnVersionChanged.connect(boost::bind(&RedisWriter::VersionChangedHandler, _1));
53 }
54
55 //TODO: OnActiveChanged handling.
56 void RedisWriter::UpdateAllConfigObjects(void)
57 {
58         AssertOnWorkQueue();
59
60         //TODO: Just use config types
61         for (const Type::Ptr& type : Type::GetAllTypes()) {
62                 if (!ConfigObject::TypeInstance->IsAssignableFrom(type))
63                         continue;
64
65                 String typeName = type->GetName();
66
67                 /* replace into aka delete insert is faster than a full diff */
68                 redisReply *reply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "DEL icinga:config:%s icinga:status:%s", typeName.CStr(), typeName.CStr()));
69
70                 if (!reply) {
71                         redisFree(m_Context);
72                         m_Context = NULL;
73                         return;
74                 }
75
76                 if (reply->type == REDIS_REPLY_STATUS || reply->type == REDIS_REPLY_ERROR) {
77                         Log(LogInformation, "RedisWriter")
78                             << "DEL icinga:config:" << typeName << ": " << reply->str;
79                 }
80
81                 if (reply->type == REDIS_REPLY_ERROR) {
82                         freeReplyObject(reply);
83                         return;
84                 }
85
86                 freeReplyObject(reply);
87
88                 /* fetch all objects and dump them */
89                 ConfigType *ctype = dynamic_cast<ConfigType *>(type.get());
90                 VERIFY(ctype);
91
92                 for (const ConfigObject::Ptr& object : ctype->GetObjects()) {
93                         SendConfigUpdate(object, typeName);
94                         SendStatusUpdate(object, typeName);
95                 }
96         }
97 }
98
99 void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, const String& typeName)
100 {
101         AssertOnWorkQueue();
102
103         /* Serialize config object attributes */
104         Dictionary::Ptr objectAttrs = SerializeObjectAttrs(object, FAConfig);
105
106         String jsonBody = JsonEncode(objectAttrs);
107
108         //TODO: checksum
109         String objectName = object->GetName();
110
111         redisReply *reply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "HSET icinga:config:%s %s %s", typeName.CStr(), objectName.CStr(), jsonBody.CStr()));
112
113         if (!reply) {
114                 redisFree(m_Context);
115                 m_Context = NULL;
116                 return;
117         }
118
119         if (reply->type == REDIS_REPLY_STATUS || reply->type == REDIS_REPLY_ERROR) {
120                 Log(LogInformation, "RedisWriter")
121                     << "HSET icinga:config:" << typeName << " " << objectName << " " << jsonBody << ": " << reply->str;
122         }
123
124         if (reply->type == REDIS_REPLY_ERROR) {
125                 freeReplyObject(reply);
126                 return;
127         }
128
129         freeReplyObject(reply);
130 }
131
132 void RedisWriter::SendStatusUpdate(const ConfigObject::Ptr& object, const String& typeName)
133 {
134         AssertOnWorkQueue();
135
136         /* Serialize config object attributes */
137         Dictionary::Ptr objectAttrs = SerializeObjectAttrs(object, FAState);
138
139         String jsonBody = JsonEncode(objectAttrs);
140
141         //TODO: checksum
142         String objectName = object->GetName();
143
144         redisReply *reply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "HSET icinga:status:%s %s %s", typeName.CStr(), objectName.CStr(), jsonBody.CStr()));
145
146         if (!reply) {
147                 redisFree(m_Context);
148                 m_Context = NULL;
149                 return;
150         }
151
152         if (reply->type == REDIS_REPLY_STATUS || reply->type == REDIS_REPLY_ERROR) {
153                 Log(LogInformation, "RedisWriter")
154                     << "HSET icinga:status:" << typeName << " " << objectName << " " << jsonBody << ": " << reply->str;
155         }
156
157         if (reply->type == REDIS_REPLY_ERROR) {
158                 freeReplyObject(reply);
159                 return;
160         }
161
162         freeReplyObject(reply);
163 }
164
165 Dictionary::Ptr RedisWriter::SerializeObjectAttrs(const Object::Ptr& object, int fieldType)
166 {
167         Type::Ptr type = object->GetReflectionType();
168
169         std::vector<int> fids;
170
171         for (int fid = 0; fid < type->GetFieldCount(); fid++) {
172                 fids.push_back(fid);
173         }
174
175         Dictionary::Ptr resultAttrs = new Dictionary();
176
177         for (int& fid : fids) {
178                 Field field = type->GetFieldInfo(fid);
179
180                 if ((field.Attributes & fieldType) == 0)
181                         continue;
182
183                 Value val = object->GetField(fid);
184
185                 /* hide attributes which shouldn't be user-visible */
186                 if (field.Attributes & FANoUserView)
187                         continue;
188
189                 /* hide internal navigation fields */
190                 if (field.Attributes & FANavigation && !(field.Attributes & (FAConfig | FAState)))
191                         continue;
192
193                 Value sval = Serialize(val);
194                 resultAttrs->Set(field.Name, sval);
195         }
196
197         return resultAttrs;
198 }
199
200 void RedisWriter::StateChangedHandler(const ConfigObject::Ptr& object)
201 {
202         Type::Ptr type = object->GetReflectionType();
203
204         for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
205                 rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendStatusUpdate, rw, object, type->GetName()));
206         }
207 }
208
209 void RedisWriter::VarsChangedHandler(const ConfigObject::Ptr& object)
210 {
211         Type::Ptr type = object->GetReflectionType();
212
213         for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
214                 rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendConfigUpdate, rw, object, type->GetName()));
215         }
216 }
217
218 void RedisWriter::VersionChangedHandler(const ConfigObject::Ptr& object)
219 {
220         Type::Ptr type = object->GetReflectionType();
221
222         for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
223                 rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendConfigUpdate, rw.get(), object, type->GetName()));
224         }
225 }