1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
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. *
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. *
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 ******************************************************************************/
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"
27 using namespace icinga;
30 - icinga:config:<type> as hash
31 key: sha1 checksum(name)
32 value: JsonEncode(Serialize(object, FAConfig)) + config_checksum
34 Diff between calculated config_checksum and Redis json config_checksum
35 Alternative: Replace into.
38 - icinga:status:<type> as hash
39 key: sha1 checksum(name)
40 value: JsonEncode(Serialize(object, FAState))
43 INITIALIZE_ONCE(&RedisWriter::ConfigStaticInitialize);
45 void RedisWriter::ConfigStaticInitialize(void)
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));
51 /* triggered on create, update and delete objects */
52 ConfigObject::OnVersionChanged.connect(boost::bind(&RedisWriter::VersionChangedHandler, _1));
55 //TODO: OnActiveChanged handling.
56 void RedisWriter::UpdateAllConfigObjects(void)
60 //TODO: Just use config types
61 for (const Type::Ptr& type : Type::GetAllTypes()) {
62 if (!ConfigObject::TypeInstance->IsAssignableFrom(type))
65 String typeName = type->GetName();
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()));
76 if (reply->type == REDIS_REPLY_STATUS || reply->type == REDIS_REPLY_ERROR) {
77 Log(LogInformation, "RedisWriter")
78 << "DEL icinga:config:" << typeName << ": " << reply->str;
81 if (reply->type == REDIS_REPLY_ERROR) {
82 freeReplyObject(reply);
86 freeReplyObject(reply);
88 /* fetch all objects and dump them */
89 ConfigType *ctype = dynamic_cast<ConfigType *>(type.get());
92 for (const ConfigObject::Ptr& object : ctype->GetObjects()) {
93 SendConfigUpdate(object, typeName);
94 SendStatusUpdate(object, typeName);
99 void RedisWriter::SendConfigUpdate(const ConfigObject::Ptr& object, const String& typeName)
103 /* Serialize config object attributes */
104 Dictionary::Ptr objectAttrs = SerializeObjectAttrs(object, FAConfig);
106 String jsonBody = JsonEncode(objectAttrs);
109 String objectName = object->GetName();
111 redisReply *reply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "HSET icinga:config:%s %s %s", typeName.CStr(), objectName.CStr(), jsonBody.CStr()));
114 redisFree(m_Context);
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;
124 if (reply->type == REDIS_REPLY_ERROR) {
125 freeReplyObject(reply);
129 freeReplyObject(reply);
132 void RedisWriter::SendStatusUpdate(const ConfigObject::Ptr& object, const String& typeName)
136 /* Serialize config object attributes */
137 Dictionary::Ptr objectAttrs = SerializeObjectAttrs(object, FAState);
139 String jsonBody = JsonEncode(objectAttrs);
142 String objectName = object->GetName();
144 redisReply *reply = reinterpret_cast<redisReply *>(redisCommand(m_Context, "HSET icinga:status:%s %s %s", typeName.CStr(), objectName.CStr(), jsonBody.CStr()));
147 redisFree(m_Context);
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;
157 if (reply->type == REDIS_REPLY_ERROR) {
158 freeReplyObject(reply);
162 freeReplyObject(reply);
165 Dictionary::Ptr RedisWriter::SerializeObjectAttrs(const Object::Ptr& object, int fieldType)
167 Type::Ptr type = object->GetReflectionType();
169 std::vector<int> fids;
171 for (int fid = 0; fid < type->GetFieldCount(); fid++) {
175 Dictionary::Ptr resultAttrs = new Dictionary();
177 for (int& fid : fids) {
178 Field field = type->GetFieldInfo(fid);
180 if ((field.Attributes & fieldType) == 0)
183 Value val = object->GetField(fid);
185 /* hide attributes which shouldn't be user-visible */
186 if (field.Attributes & FANoUserView)
189 /* hide internal navigation fields */
190 if (field.Attributes & FANavigation && !(field.Attributes & (FAConfig | FAState)))
193 Value sval = Serialize(val);
194 resultAttrs->Set(field.Name, sval);
200 void RedisWriter::StateChangedHandler(const ConfigObject::Ptr& object)
202 Type::Ptr type = object->GetReflectionType();
204 for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
205 rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendStatusUpdate, rw, object, type->GetName()));
209 void RedisWriter::VarsChangedHandler(const ConfigObject::Ptr& object)
211 Type::Ptr type = object->GetReflectionType();
213 for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
214 rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendConfigUpdate, rw, object, type->GetName()));
218 void RedisWriter::VersionChangedHandler(const ConfigObject::Ptr& object)
220 Type::Ptr type = object->GetReflectionType();
222 for (const RedisWriter::Ptr& rw : ConfigType::GetObjectsByType<RedisWriter>()) {
223 rw->m_WorkQueue.Enqueue(boost::bind(&RedisWriter::SendConfigUpdate, rw.get(), object, type->GetName()));